libStatGen Software 1
PedigreeGlobals.h
1/*
2 * Copyright (C) 2010 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __PEDGLOBALS_H__
19#define __PEDGLOBALS_H__
20
21#include "Constant.h"
22#include "StringArray.h"
23#include "StringHash.h"
24#include "IntArray.h"
25#include "MathVector.h"
26
27#include <iostream>
28
30{
31public:
32 // Chromosome number
33 int chromosome;
34
35 // Position along chromosome in morgans
36 double position;
37 double positionMale;
38 double positionFemale;
39
40 Vector freq;
41 String name;
42 StringArray alleleLabels;
43 StringIntHash alleleNumbers;
44
45 MarkerInfo(String & string)
46 {
47 serial = count++;
48 name = string;
49 chromosome = -1;
50 position = 0.0;
51 positionMale = 0.0;
52 positionFemale = 0.0;
53 }
54
55 bool AdjustFrequencies();
56
57 static int ComparePosition(MarkerInfo ** left, MarkerInfo ** right);
58
59 String GetAlleleLabel(int allele);
60 int GetAlleleNumber(char label) const
61 {
62 String labelString;
63 labelString = label;
64 return(GetAlleleNumber(labelString));
65 }
66 int GetAlleleNumber(const String & label) const
67 {
68 return label == "0" ? 0 : alleleNumbers.Integer(label);
69 }
70
71 int NewAllele(char label)
72 {
73 String labelString;
74 labelString = label;
75 return(NewAllele(labelString));
76 }
77
78 int NewAllele(const String & label);
79
80 // Calling update serial for a series of markers ensures they are
81 // clustered in a particular order
82 void UpdateSerial()
83 {
84 serial = count++;
85 }
86
87 void IndexAlleles();
88
89 int CountAlleles()
90 {
91 return alleleLabels.Length() ? alleleLabels.Length() - 1 : 0;
92 }
93
94private:
95 // How many marker info structures have we created?
96 static int count;
97 static String label;
98
99 // When sorting markers, use serial_no to break ties, so
100 // markers we saw first in the map file / datafile come
101 // first
102 int serial;
103};
104
105std::ostream &operator << (std::ostream &stream, MarkerInfo &m);
106
108{
109public:
110 static int traitCount;
111 static int markerCount;
112 static int affectionCount;
113 static int covariateCount;
114 static int stringCount;
115
116 // Should be set to true if handling X-linked data
117 static bool chromosomeX;
118 // Set to true when map file includes position info
119 // based on sex-specific recombination fractions
120 static bool sexSpecificMap;
121
122 static StringArray traitNames;
123 static StringArray covariateNames;
124 static StringArray affectionNames;
125 static StringArray markerNames;
126 static StringArray stringNames;
127 static StringIntHash markerLookup;
128 static StringIntHash traitLookup;
129 static StringIntHash affectionLookup;
130 static StringIntHash covariateLookup;
131 static StringIntHash stringLookup;
132
133 // These functions are guaranteed to return a valid ID
134 // If no matching attribute exists, one is created
135 //
136
137 static int GetTraitID(const char * name);
138 static int GetMarkerID(const char * name);
139 static int GetCovariateID(const char * name);
140 static int GetAffectionID(const char * name);
141 static int GetStringID(const char * name);
142
143 // These functions return a matching ID or -1 if none is found
144 //
145
146 static int LookupTrait(const char * name)
147 {
148 return traitLookup.Integer(name);
149 }
150 static int LookupMarker(const char * name)
151 {
152 return markerLookup.Integer(name);
153 }
154 static int LookupCovariate(const char * name)
155 {
156 return covariateLookup.Integer(name);
157 }
158 static int LookupAffection(const char * name)
159 {
160 return affectionLookup.Integer(name);
161 }
162 static int LookupString(const char * name)
163 {
164 return stringLookup.Integer(name);
165 }
166
167 static int markerInfoCount;
168 static int markerInfoSize;
169 static MarkerInfo ** markerInfo;
170 static StringHash markerInfoByName;
171 static MarkerInfo ** markerInfoByInteger;
172
173 static void GrowMarkerInfo();
174 static MarkerInfo * GetMarkerInfo(String & name);
175 static MarkerInfo * GetMarkerInfo(int marker);
176
177 static int SortMarkersInMapOrder(IntArray & markers, int chromosome = -1);
178 static void GetOrderedMarkers(IntArray & markers);
179 static void FlagMissingMarkers(IntArray & missingMarkers);
180
181 static bool MarkerPositionsAvailable();
182 static bool AlleleFrequenciesAvailable();
183
184 static void VerifySexSpecificOrder();
185
186 static void LoadAlleleFrequencies(const char * filename, bool required = false);
187 static void LoadAlleleFrequencies(IFILE & file);
188
189 static void LoadMarkerMap(const char * filename, bool filter = false);
190 static void LoadMarkerMap(IFILE & file, bool filter = false);
191
192 static void LoadBasepairMap(const char * filename);
193 static void LoadBasepairMap(IFILE & file);
194
195 static void WriteMapFile(const char * filename);
196 static void WriteMapFile(FILE * file);
197
198 static void WriteFreqFile(const char * filename, bool old_format = false);
199 static void WriteFreqFile(FILE * file, bool old_format = false);
200
201 static int LoadAllele(int marker, String & label); // Read an allele
202 static int LoadAllele(MarkerInfo * info, String & label);
203
205 {
206 instanceCount++;
207 }
209
210private:
211 static int instanceCount;
212
213};
214
215#endif
216
InputFile & operator<<(InputFile &stream, const std::string &str)
Write to a file using streaming.
Definition: InputFile.h:736
Class for easily reading/writing files without having to worry about file type (uncompressed,...
Definition: InputFile.h:37