File: VcfGenotypeSample.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 (356 lines) | stat: -rw-r--r-- 9,510 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 *  Copyright (C) 2012  Regents of the University of Michigan
 *
 *   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 "VcfGenotypeSample.h"
#include <stdlib.h>
#include <sstream>

const int VcfGenotypeSample::INVALID_GT = -1;
const int VcfGenotypeSample::MISSING_GT = -2;
const std::string VcfGenotypeSample::MISSING_FIELD = ".";

VcfGenotypeSample::VcfGenotypeSample()
    : VcfGenotypeField(),
      myFormatPtr(NULL),
      myPhased(false),
      myUnphased(false),
      myHasAllGenotypeAlleles(false),
      myNewGT(false),
      myGTs()
{
}


VcfGenotypeSample::~VcfGenotypeSample()
{
    myFormatPtr = NULL;
}


bool VcfGenotypeSample::read(IFILE filePtr, VcfGenotypeFormat& format)
{
    static const char* GT_DELIM = "\n\t:|/.";
    static const int END_GT = 2; // Ends at index 2 or less
    static const int PHASED_CHAR_POS = 3;
    static const int UNPHASED_CHAR_POS = 4;
    static const int MISSING_GT_POS = 5;

    // Clear out any previously set values.
    reset();
    
    myFormatPtr = &format;

    int gtIndex = format.getGTIndex();

    // Read the subfields.
    SUBFIELD_READ_STATUS readStatus = MORE_SUBFIELDS;  
    std::string* nextType = NULL;
    int subFieldIndex = 0;
    while(readStatus == MORE_SUBFIELDS)
    {
        // Get the field to write into.
        if(format.storeIndex(subFieldIndex))
        {
            nextType = &(myGenotypeSubFields.getNextEmpty());
            // Check if this is the GT field.
            if(subFieldIndex == gtIndex)
            {
                // There is a GT field, so set that all GT fields are there.
                // if any are missing it will be turned back to false.
                myHasAllGenotypeAlleles = true;
                // This is the GT field, so parse manually looking to see if it
                // is phased and store the genotypes.
                int stopChar = END_GT + 1;
                // Read until a new subfield is found.
                while(stopChar > END_GT)
                {
                    // TODO  have an option to autoparse the genotypes?
                    // todo - store the previous nextType len in order to
                    // do string conversion to ints...
                    stopChar = filePtr->readTilChar(GT_DELIM, *nextType);
                    if(stopChar == PHASED_CHAR_POS)
                    {
                        nextType->push_back('|');
                        myPhased = true;
                    }
                    else if(stopChar == UNPHASED_CHAR_POS)
                    {
                        nextType->push_back('/');
                        myUnphased = true;
                    }
                    else if(stopChar == MISSING_GT_POS)
                    {
                        nextType->push_back('.');
                        myHasAllGenotypeAlleles = false;
                    }
                }
                // Check if this is the END_GT signal.
                readStatus = getReadStatus(stopChar);
            }
            else
            {
                // more subfields to read.
                readStatus = readGenotypeSubField(filePtr, nextType);
            }
        }
        else
        {
            readStatus = readGenotypeSubField(filePtr, NULL);
        }
        ++subFieldIndex;
    }

    // subFieldIndex contains the number of fields in this sample.
    if(subFieldIndex > format.getOrigNumFields())
    {
        throw(std::runtime_error("VCF Number of Fields in a Sample does not match the Format."));
    }
    else if(subFieldIndex < format.getOrigNumFields())
    {
        // If there are no fields for this sample, enter the missing value.
        if(myGenotypeSubFields.size() == 0)
        {
            myGenotypeSubFields.getNextEmpty() = MISSING_FIELD;
        }
    }

    // Return true if there is a tab - it is just END_OF_FIELD.
    return(readStatus == END_OF_FIELD);
}


bool VcfGenotypeSample::write(IFILE filePtr)
{
    if(myNewGT)
    {
        updateGTString();
    }
    return(VcfGenotypeField::write(filePtr));
}

const std::string* VcfGenotypeSample::getString(const std::string& key)
{
    if(myFormatPtr == NULL)
    {
        return(NULL);
    }

    int index = myFormatPtr->getIndex(key);
    if(index != VcfGenotypeFormat::GENOTYPE_INDEX_NA)
    {
        // Check if it is out of range for this sample - means it 
        // is missing for this sample.
        if(index >= myGenotypeSubFields.size())
        {
            // missing for this sample.
            return(&MISSING_FIELD);
        }

        if((key == "GT") && myNewGT)
        {
            updateGTString();
        }
        return(&(myGenotypeSubFields.get(index)));
    }
    // key was not found, so return NULL.
    return(NULL);
}


bool VcfGenotypeSample::setString(const std::string& key, const std::string& value)
{
    if(myFormatPtr == NULL)
    {
        return(false);
    }

    int index = myFormatPtr->getIndex(key);
    if(index != VcfGenotypeFormat::GENOTYPE_INDEX_NA)
    {
        // Found the type, so set it.
        myGenotypeSubFields.get(index) = value;

        if(key == "GT")
        {
            myGTs.clear();
            myNewGT = false;
            if(value.find('|') != std::string::npos)
            {
                myPhased = true;
            }
            if(value.find('/') != std::string::npos)
            {
                myUnphased = true;
            }
            if(value.find('.') != std::string::npos)
            {
                myHasAllGenotypeAlleles = false;
            }
            else
            {
                myHasAllGenotypeAlleles = true;
            }
        }
        return(true);
    }
    // field was not found, so return false.
    return(false);
}


int VcfGenotypeSample::getGT(unsigned int index)
{
    if(myGTs.empty())
    {
        if(!parseGT())
        {
            // Failed to parse GT, so return INVALID_GT.
            return(INVALID_GT);
        }
    }
    
    if(index < myGTs.size())
    {
        return(myGTs[index]);
    }
    // Out of range index.
    return(INVALID_GT);
}


void VcfGenotypeSample::setGT(unsigned int index, int newGt)
{
    if(myGTs.empty())
    {
        if(!parseGT())
        {
            // Failed to parse GT, so return INVALID_GT.
            throw(std::runtime_error("VCF failed to parse GT."));
        }
    }
    
    if(index < myGTs.size())
    {
        if(myGTs[index] != newGt)
        {
            myNewGT = true;
            myGTs[index] = newGt;
        }
    }
    else
    {
        // Out of range index.
        throw(std::runtime_error("VCF setGT called with out of range GT index."));
    }
}


int VcfGenotypeSample::getNumGTs()
{
    if(myGTs.empty())
    {
        if(!parseGT())
        {
            return(0);
        }
    }
    return(myGTs.size());
}


void VcfGenotypeSample::internal_reset()
{
    myFormatPtr = NULL;
    myPhased = false;
    myUnphased = false;
    myHasAllGenotypeAlleles = false;
    myGTs.clear();
    myNewGT = false;
}


bool VcfGenotypeSample::parseGT()
{
    // Parse the GT.
    const std::string* gtStr = getString("GT");
    myGTs.clear();
    myNewGT = false;
    if(gtStr == NULL)
    {
        // GT field not found.
        return(false);
    }

    // Parse til the end of the GT string
    char* startPos = NULL;
    char* endPos = (char*)gtStr->c_str();
    while((endPos != NULL) && (*endPos != '\0'))
    {
        startPos = endPos;
        if(*startPos == '.')
        {
            endPos = startPos + 1;
            // unknown, so set this index to be MISSING_GT.
            myGTs.push_back(MISSING_GT);
            continue;
        }
        if(*startPos == '|')
        {
            endPos = startPos + 1;
            continue;
        }
        if(*startPos == '/')
        {
            endPos = startPos + 1;
            continue;
        }
        // Should be an int, so parse it.
        unsigned long gtLong = strtoul(startPos, &endPos, 10);
        myGTs.push_back((int)gtLong);
    }
    return(true);
}


void VcfGenotypeSample::updateGTString()
{
    if(myNewGT)
    {
        int index = myFormatPtr->getIndex("GT");
        if(index != VcfGenotypeFormat::GENOTYPE_INDEX_NA)
        {
            // Check if it is out of range for this sample - means it 
            // is missing for this sample.
            if(index < myGenotypeSubFields.size())
            {
                std::stringstream gtSS;
                char phaseChar = '/';
                if(myPhased)
                {
                    phaseChar = '|';
                }
                gtSS << myGTs[0];
                for(unsigned int i = 1; i < myGTs.size(); i++)
                {
                    gtSS << phaseChar << myGTs[i];
                }
                myGenotypeSubFields.get(index) = gtSS.str();
                myNewGT = false;
            }
        }
    }
}