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(const String & label) const
00061     {
00062         return label == "0" ? 0 : alleleNumbers.Integer(label);
00063     }
00064 
00065     int  NewAllele(const String & label);
00066 
00067     // Calling update serial for a series of markers ensures they are
00068     // clustered in a particular order
00069     void UpdateSerial()
00070     {
00071         serial = count++;
00072     }
00073 
00074     void IndexAlleles();
00075 
00076     int CountAlleles()
00077     {
00078         return alleleLabels.Length() ? alleleLabels.Length() - 1 : 0;
00079     }
00080 
00081 private:
00082     // How many marker info structures have we created?
00083     static int count;
00084     static String label;
00085 
00086     // When sorting markers, use serial_no to break ties, so
00087     // markers we saw first in the map file / datafile come
00088     // first
00089     int serial;
00090 };
00091 
00092 std::ostream &operator << (std::ostream &stream, MarkerInfo &m);
00093 
00094 class PedigreeGlobals
00095 {
00096 public:
00097     static int traitCount;
00098     static int markerCount;
00099     static int affectionCount;
00100     static int covariateCount;
00101 
00102     // Should be set to true if handling X-linked data
00103     static bool chromosomeX;
00104     // Set to true when map file includes position info
00105     // based on sex-specific recombination fractions
00106     static bool sexSpecificMap;
00107 
00108     static StringArray   traitNames;
00109     static StringArray   covariateNames;
00110     static StringArray   affectionNames;
00111     static StringArray   markerNames;
00112     static StringIntHash markerLookup;
00113     static StringIntHash traitLookup;
00114     static StringIntHash affectionLookup;
00115     static StringIntHash covariateLookup;
00116 
00117     // These functions are guaranteed to return a valid ID
00118     // If no matching attribute exists, one is created
00119     //
00120 
00121     static int GetTraitID(const char * name);
00122     static int GetMarkerID(const char * name);
00123     static int GetCovariateID(const char * name);
00124     static int GetAffectionID(const char * name);
00125 
00126     // These functions return a matching ID or -1 if none is found
00127     //
00128 
00129     static int LookupTrait(const char * name)
00130     {
00131         return traitLookup.Integer(name);
00132     }
00133     static int LookupMarker(const char * name)
00134     {
00135         return markerLookup.Integer(name);
00136     }
00137     static int LookupCovariate(const char * name)
00138     {
00139         return covariateLookup.Integer(name);
00140     }
00141     static int LookupAffection(const char * name)
00142     {
00143         return affectionLookup.Integer(name);
00144     }
00145 
00146     static int markerInfoCount;
00147     static int markerInfoSize;
00148     static MarkerInfo ** markerInfo;
00149     static StringHash    markerInfoByName;
00150     static MarkerInfo ** markerInfoByInteger;
00151 
00152     static void GrowMarkerInfo();
00153     static MarkerInfo * GetMarkerInfo(String & name);
00154     static MarkerInfo * GetMarkerInfo(int marker);
00155 
00156     static int  SortMarkersInMapOrder(IntArray & markers, int chromosome = -1);
00157     static void GetOrderedMarkers(IntArray & markers);
00158     static void FlagMissingMarkers(IntArray & missingMarkers);
00159 
00160     static bool MarkerPositionsAvailable();
00161     static bool AlleleFrequenciesAvailable();
00162 
00163     static void VerifySexSpecificOrder();
00164 
00165     static void LoadAlleleFrequencies(const char * filename, bool required = false);
00166     static void LoadAlleleFrequencies(IFILE & file);
00167 
00168     static void LoadMarkerMap(const char * filename, bool filter = false);
00169     static void LoadMarkerMap(IFILE & file, bool filter = false);
00170 
00171     static void LoadBasepairMap(const char * filename);
00172     static void LoadBasepairMap(IFILE & file);
00173 
00174     static void WriteMapFile(const char * filename);
00175     static void WriteMapFile(FILE * file);
00176 
00177     static void WriteFreqFile(const char * filename, bool old_format = false);
00178     static void WriteFreqFile(FILE * file, bool old_format = false);
00179 
00180     static int  LoadAllele(int marker, String & label);          // Read an allele
00181     static int  LoadAllele(MarkerInfo * info, String & label);
00182 
00183     PedigreeGlobals()
00184     {
00185         instanceCount++;
00186     }
00187     ~PedigreeGlobals();
00188 
00189 private:
00190     static int  instanceCount;
00191 
00192 };
00193 
00194 #endif
00195 
Generated on Wed Nov 17 15:38:29 2010 for StatGen Software by  doxygen 1.6.3