libStatGen Software  1
PedigreeGlobals.h
00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef __PEDGLOBALS_H__
00019 #define __PEDGLOBALS_H__
00020 
00021 #include "Constant.h"
00022 #include "StringArray.h"
00023 #include "StringHash.h"
00024 #include "IntArray.h"
00025 #include "MathVector.h"
00026 
00027 #include <iostream>
00028 
00029 class MarkerInfo
00030 {
00031 public:
00032     // Chromosome number
00033     int    chromosome;
00034 
00035     // Position along chromosome in morgans
00036     double position;
00037     double positionMale;
00038     double positionFemale;
00039 
00040     Vector         freq;
00041     String         name;
00042     StringArray    alleleLabels;
00043     StringIntHash  alleleNumbers;
00044 
00045     MarkerInfo(String & string)
00046     {
00047         serial = count++;
00048         name = string;
00049         chromosome = -1;
00050         position = 0.0;
00051         positionMale = 0.0;
00052         positionFemale = 0.0;
00053     }
00054 
00055     bool AdjustFrequencies();
00056 
00057     static int ComparePosition(MarkerInfo ** left, MarkerInfo ** right);
00058 
00059     String GetAlleleLabel(int allele);
00060     int    GetAlleleNumber(char label) const
00061     {
00062         String labelString;
00063         labelString = label;
00064         return(GetAlleleNumber(labelString));
00065     }
00066     int    GetAlleleNumber(const String & label) const
00067     {
00068         return label == "0" ? 0 : alleleNumbers.Integer(label);
00069     }
00070 
00071     int  NewAllele(char label)
00072     {
00073         String labelString;
00074         labelString = label;
00075         return(NewAllele(labelString));
00076     }
00077 
00078     int  NewAllele(const String & label);
00079 
00080     // Calling update serial for a series of markers ensures they are
00081     // clustered in a particular order
00082     void UpdateSerial()
00083     {
00084         serial = count++;
00085     }
00086 
00087     void IndexAlleles();
00088 
00089     int CountAlleles()
00090     {
00091         return alleleLabels.Length() ? alleleLabels.Length() - 1 : 0;
00092     }
00093 
00094 private:
00095     // How many marker info structures have we created?
00096     static int count;
00097     static String label;
00098 
00099     // When sorting markers, use serial_no to break ties, so
00100     // markers we saw first in the map file / datafile come
00101     // first
00102     int serial;
00103 };
00104 
00105 std::ostream &operator << (std::ostream &stream, MarkerInfo &m);
00106 
00107 class PedigreeGlobals
00108 {
00109 public:
00110     static int traitCount;
00111     static int markerCount;
00112     static int affectionCount;
00113     static int covariateCount;
00114     static int stringCount;
00115 
00116     // Should be set to true if handling X-linked data
00117     static bool chromosomeX;
00118     // Set to true when map file includes position info
00119     // based on sex-specific recombination fractions
00120     static bool sexSpecificMap;
00121 
00122     static StringArray   traitNames;
00123     static StringArray   covariateNames;
00124     static StringArray   affectionNames;
00125     static StringArray   markerNames;
00126     static StringArray   stringNames;
00127     static StringIntHash markerLookup;
00128     static StringIntHash traitLookup;
00129     static StringIntHash affectionLookup;
00130     static StringIntHash covariateLookup;
00131     static StringIntHash stringLookup;
00132 
00133     // These functions are guaranteed to return a valid ID
00134     // If no matching attribute exists, one is created
00135     //
00136 
00137     static int GetTraitID(const char * name);
00138     static int GetMarkerID(const char * name);
00139     static int GetCovariateID(const char * name);
00140     static int GetAffectionID(const char * name);
00141     static int GetStringID(const char * name);
00142 
00143     // These functions return a matching ID or -1 if none is found
00144     //
00145 
00146     static int LookupTrait(const char * name)
00147     {
00148         return traitLookup.Integer(name);
00149     }
00150     static int LookupMarker(const char * name)
00151     {
00152         return markerLookup.Integer(name);
00153     }
00154     static int LookupCovariate(const char * name)
00155     {
00156         return covariateLookup.Integer(name);
00157     }
00158     static int LookupAffection(const char * name)
00159     {
00160         return affectionLookup.Integer(name);
00161     }
00162     static int LookupString(const char * name)
00163     {
00164         return stringLookup.Integer(name);
00165     }
00166 
00167     static int markerInfoCount;
00168     static int markerInfoSize;
00169     static MarkerInfo ** markerInfo;
00170     static StringHash    markerInfoByName;
00171     static MarkerInfo ** markerInfoByInteger;
00172 
00173     static void GrowMarkerInfo();
00174     static MarkerInfo * GetMarkerInfo(String & name);
00175     static MarkerInfo * GetMarkerInfo(int marker);
00176 
00177     static int  SortMarkersInMapOrder(IntArray & markers, int chromosome = -1);
00178     static void GetOrderedMarkers(IntArray & markers);
00179     static void FlagMissingMarkers(IntArray & missingMarkers);
00180 
00181     static bool MarkerPositionsAvailable();
00182     static bool AlleleFrequenciesAvailable();
00183 
00184     static void VerifySexSpecificOrder();
00185 
00186     static void LoadAlleleFrequencies(const char * filename, bool required = false);
00187     static void LoadAlleleFrequencies(IFILE & file);
00188 
00189     static void LoadMarkerMap(const char * filename, bool filter = false);
00190     static void LoadMarkerMap(IFILE & file, bool filter = false);
00191 
00192     static void LoadBasepairMap(const char * filename);
00193     static void LoadBasepairMap(IFILE & file);
00194 
00195     static void WriteMapFile(const char * filename);
00196     static void WriteMapFile(FILE * file);
00197 
00198     static void WriteFreqFile(const char * filename, bool old_format = false);
00199     static void WriteFreqFile(FILE * file, bool old_format = false);
00200 
00201     static int  LoadAllele(int marker, String & label);          // Read an allele
00202     static int  LoadAllele(MarkerInfo * info, String & label);
00203 
00204     PedigreeGlobals()
00205     {
00206         instanceCount++;
00207     }
00208     ~PedigreeGlobals();
00209 
00210 private:
00211     static int  instanceCount;
00212 
00213 };
00214 
00215 #endif
00216 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends