File: VcfRecordGenotype.cpp

package info (click to toggle)
libstatgen 1.0.15-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,588 kB
  • sloc: cpp: 49,624; ansic: 1,408; makefile: 320; sh: 60
file content (303 lines) | stat: -rw-r--r-- 7,304 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 *  Copyright (C) 2011-2012  Regents of the University of Michigan,
 *                           Hyun Min Kang, Matthew Flickenger, Matthew Snyder,
 *                           and Goncalo Abecasis
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "VcfRecordGenotype.h"
#include <stdlib.h>

std::set <std::string> VcfRecordGenotype::ourStoreFields;


void VcfRecordGenotype::storeAllFields()
{
    ourStoreFields.clear();
}


void VcfRecordGenotype::addStoreField(const char* field)
{
    ourStoreFields.insert(field);
}

bool VcfRecordGenotype::storeField(std::string& field)
{
    if(ourStoreFields.size() == 0)
    {
        // No fields were set so read all fields.
        return(true);
    }
    return(ourStoreFields.find(field) != ourStoreFields.end());
}

VcfRecordGenotype::VcfRecordGenotype()
{
    reset();
}


VcfRecordGenotype::~VcfRecordGenotype()
{
}


bool VcfRecordGenotype::read(IFILE filePtr)
{
    return(read(filePtr, NULL));
}


bool VcfRecordGenotype::read(IFILE filePtr, VcfSubsetSamples* subsetInfo)
{
    // Needed for skipping samples.
    static const std::string fieldEndChars = "\n\t";
    static const int tabPos = 1;

    // Clear out any previously set values.
    reset();
    
    if(ifeof(filePtr))
    {
        // End of file, just return false.
        return(false);
    }
    
    // Read the format.
    if(!myFormat.read(filePtr))
    {
        // No more fields
        return(false);
    }

   // Read all the samples until the end of the line.
    VcfGenotypeSample* nextSample = NULL;
    bool moreSamples = true;
    int sampleIndex = 0;
    while(moreSamples)
    {
        // Done reading the format field, so read the samples.
        // Check if this sample should be kept.
        if(subsetInfo != NULL)
        {
            // Check if this sample should be kept.
            if(!subsetInfo->keep(sampleIndex))
            {
                // this sample should not be kept.
                if(filePtr->readTilChar(fieldEndChars) != tabPos)
                {
                    // Stopped on new line or end of file instead of
                    // a tab, so no more samples to read.
                    moreSamples = false;
                }
                ++sampleIndex;
                continue;
            }
        }

        // Read this sample.
        nextSample = &(mySamples.getNextEmpty());
        if(nextSample == NULL)
        {
            throw(std::runtime_error("VCF failed to get another sample."));
        }
        if(!nextSample->read(filePtr, myFormat))
        {
            // No more fields.
            moreSamples = false;
        }
       ++sampleIndex;
    }
    
    // Return whether or not a tab was found at the end of the field.
    return(false);
}


bool VcfRecordGenotype::write(IFILE filePtr)
{
    bool status = true;

    // Check if there are any fields to write.
    if(myFormat.getNumFields() == 0)
    {
        // Nothing to write.
        return(true);
    }

    // Write the format.
    status &= myFormat.write(filePtr);

    // Loop through and write each sample.
    for(int i = 0; i < mySamples.size(); i++)
    {
        status &= mySamples.get(i).write(filePtr);
    }
    return(status);
}


void VcfRecordGenotype::reset()
{
    myFormat.reset();
    mySamples.reset();
}


const std::string* VcfRecordGenotype::getString(const std::string& key, 
                                                int sampleNum)
{
    if(sampleNum >= mySamples.size())
    {
        // Out of range sample index.
        return(NULL);
    }
    // Get the field from the sample.
    return(mySamples.get(sampleNum).getString(key));
}


bool VcfRecordGenotype::setString(const std::string& key, 
                                  int sampleNum, 
                                  const std::string& value)
{
    if(sampleNum >= mySamples.size())
    {
        // Out of range sample index.
        return(NULL);
    }
    // Set the field in the sample.
    return(mySamples.get(sampleNum).setString(key, value));
}


int VcfRecordGenotype::getGT(int sampleNum, unsigned int gtIndex)
{
    if(sampleNum >= mySamples.size())
    {
        // Out of range sample index.
        return(VcfGenotypeSample::INVALID_GT);
    }
    // Get the field from the sample.
    return(mySamples.get(sampleNum).getGT(gtIndex));

}


void VcfRecordGenotype::setGT(int sampleNum, unsigned int gtIndex, int newGt)
{
    if(sampleNum >= mySamples.size())
    {
        // Out of range sample index.
        throw(std::runtime_error("setGT called with out of range sample."));
    }
    // Set the field for the sample.
    mySamples.get(sampleNum).setGT(gtIndex, newGt);

}


int VcfRecordGenotype::getNumGTs(int sampleNum)
{
    if(sampleNum >= mySamples.size())
    {
        // Out of range sample index, no GTs.
        return(0);
    }
    // Get the field from the sample.
    return(mySamples.get(sampleNum).getNumGTs());

}


bool VcfRecordGenotype::allPhased()
{
    for(int i = 0; i < mySamples.size(); i++)
    {
        if(!mySamples.get(i).isPhased() || mySamples.get(i).isUnphased())
        {
            // found a sample that is not phased or is unphased, so
            // return false.
            return(false);
        }
    }
    // All phased.
    return(true);
}


bool VcfRecordGenotype::allUnphased()
{
    for(int i = 0; i < mySamples.size(); i++)
    {
        if(!mySamples.get(i).isUnphased() || mySamples.get(i).isPhased())
        {
            // found a sample that is not unphased or is phased, so
            // return false.
            return(false);
        }
    }
    // All unphased.
    return(true);
}


bool VcfRecordGenotype::hasAllGenotypeAlleles()
{
    for(int i = 0; i < mySamples.size(); i++)
    {
        if(!mySamples.get(i).hasAllGenotypeAlleles())
        {
            // found a sample that does not have all genotype alleles, so
            // return false.
            return(false);
        }
    }
    // All have all genotype alleles.
    return(true);
}


bool VcfRecordGenotype::isPhased(int sampleNum)
{
    if(sampleNum >= mySamples.size())
    {
        // Out of range sample index.
        return(false);
    }
    return(mySamples.get(sampleNum).isPhased());
}


bool VcfRecordGenotype::isUnphased(int sampleNum)
{
    if(sampleNum >= mySamples.size())
    {
        // Out of range sample index.
        return(false);
    }
    return(mySamples.get(sampleNum).isUnphased());
}


bool VcfRecordGenotype::hasAllGenotypeAlleles(int sampleNum)
{
    if(sampleNum >= mySamples.size())
    {
        // Out of range sample index.
        return(false);
    }
    return(mySamples.get(sampleNum).hasAllGenotypeAlleles());
}