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
|
/*
* Copyright (C) 2010-2011 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 "VcfHeader.h"
VcfHeader::VcfHeader()
: myHeaderLines()
{
reset();
}
VcfHeader::~VcfHeader()
{
}
bool VcfHeader::read(IFILE filePtr)
{
// Reading, so clean out this header.
reset();
if(filePtr == NULL)
{
// No file was passed in.
myStatus.setStatus(StatGenStatus::FAIL_ORDER,
"Need to pass in an open file ptr to VcfHeader::read.");
return(false);
}
// Read until the header line has been read (after the meta lines).
while(!myHasHeaderLine)
{
// Increase the size of headerlines by 1 to fit the new line.
myHeaderLines.resize(myHeaderLines.size() + 1);
// Read the next line from the file into the header structure.
String& newStr = myHeaderLines.back();
if(newStr.ReadLine(filePtr) < 0)
{
// Error, unable to read an entire header from the file.
myStatus.setStatus(StatGenStatus::INVALID,
"Error reading VCF Meta/Header, EOF found before the header line.");
return(false);
}
if(newStr.Length() <= 2)
{
// A header/meta line must have at least 2 characters
// ## or # and 8 fields, so if less than 2 characters,
// error.
myStatus.setStatus(StatGenStatus::INVALID,
"Error reading VCF Meta/Header, line without at least 2 characters found before the header line.");
return(false);
}
// Check if it is a header (first char is # and 2nd one is not).
if((newStr[0] == '#') && (newStr[1] != '#'))
{
myHasHeaderLine = true;
// Parse the header line to get the sample information.
myParsedHeaderLine.ReplaceColumns(newStr, '\t');
}
else if((newStr[0] != '#') || (newStr[1] != '#'))
{
// A meta line must start with "##", we expect meta lines until
// the header line is found.
myStatus.setStatus(StatGenStatus::INVALID,
"Error reading VCF Meta/Header, line not starting with '##' found before the header line.");
return(false);
}
}
return(true);
}
bool VcfHeader::write(IFILE filePtr)
{
if(filePtr == NULL)
{
// No file was passed in.
myStatus.setStatus(StatGenStatus::FAIL_ORDER,
"Need to pass in an open file ptr to VcfHeader::write.");
return(false);
}
// Make sure the last header line is synced with the parsed header line.
syncHeaderLine();
int numWritten = 0;
int numExpected = 0;
for(std::vector<String>::iterator iter = myHeaderLines.begin();
iter != myHeaderLines.end(); iter++)
{
numWritten += ifprintf(filePtr, "%s\n", iter->c_str());
// expected to write string + new line.
numExpected += iter->Length();
numExpected += 1;
}
if(numWritten != numExpected)
{
myStatus.setStatus(StatGenStatus::FAIL_IO,
"Failed writing VCF Meta/Header.");
}
return(numWritten == numExpected);
}
void VcfHeader::reset()
{
myHasHeaderLine = false;
myHeaderLines.clear();
}
// Return the error after a failed call.
const StatGenStatus& VcfHeader::getStatus()
{
return(myStatus);
}
int VcfHeader::getNumMetaLines()
{
int numHeaderLines = myHeaderLines.size();
if((numHeaderLines >= 1) && (myHasHeaderLine))
{
// Remove the header line from the count.
return(numHeaderLines-1);
}
return(numHeaderLines);
}
const char* VcfHeader::getMetaLine(unsigned int index)
{
if(index >= myHeaderLines.size())
{
return(NULL);
}
else
{
return(myHeaderLines[index].c_str());
}
return(NULL);
}
const char* VcfHeader::getHeaderLine()
{
// Make sure the last header line is synced with the parsed header line.
syncHeaderLine();
if(myHasHeaderLine)
{
return(myHeaderLines.back().c_str());
}
return(NULL);
}
int VcfHeader::getNumSamples() const
{
if(!myHasHeaderLine)
{
return(0);
}
int numFields = myParsedHeaderLine.Length();
if(numFields > NUM_NON_SAMPLE_HEADER_COLS)
{
// There are samples.
return(numFields - NUM_NON_SAMPLE_HEADER_COLS);
}
// No sample fields
return(0);
}
const char* VcfHeader::getSampleName(unsigned int index) const
{
if(!myHasHeaderLine)
{
// No header.
return(NULL);
}
int position = index + NUM_NON_SAMPLE_HEADER_COLS;
if(position >= myParsedHeaderLine.Length())
{
// Out of range.
return(NULL);
}
return(myParsedHeaderLine[position].c_str());
}
int VcfHeader::getSampleIndex(const char* sampleName) const
{
if(!myHasHeaderLine)
{
// No header.
return(-1);
}
for(int index = NUM_NON_SAMPLE_HEADER_COLS;
index < myParsedHeaderLine.Length(); index++)
{
if(myParsedHeaderLine[index] == sampleName)
{
// Found.
return(index - NUM_NON_SAMPLE_HEADER_COLS);
}
}
// Not found.
return(-1);
}
void VcfHeader::removeSample(unsigned int index)
{
int position = index + NUM_NON_SAMPLE_HEADER_COLS;
if(position >= myParsedHeaderLine.Length())
{
// Out of range, so just return, nothing to remove.
return;
}
// Remove it from the parsed header line.
myParsedHeaderLine.Delete(position);
// Removed a sample, so clear the header line so the next time it is
// accessed it will be reset based on the existing samples.
String& hdrLine = myHeaderLines.back();
hdrLine.Clear();
}
bool VcfHeader::appendMetaLine(const char* metaLine)
{
// Check that the line starts with "##".
if(strncmp(metaLine, "##", 2) != 0)
{
// Does not start with "##"
return(false);
}
if(!myHasHeaderLine)
{
// No header line, so just add to the end of the vector.
myHeaderLines.push_back(metaLine);
return(true);
}
// There is a header line, so insert this just before that line.
// The headerLine is one position before "end".
std::vector<String>::iterator headerLine = myHeaderLines.end();
--headerLine;
// Insert just before the header line.
myHeaderLines.insert(headerLine, metaLine);
return(true);
}
bool VcfHeader::addHeaderLine(const char* headerLine)
{
// Check that the line starts with "#".
if(strncmp(headerLine, "#", 1) != 0)
{
// Does not start with "#"
return(false);
}
if(myHasHeaderLine)
{
// There is a header line, so replace the current line.
myHeaderLines.back() = headerLine;
}
else
{
// There is not a header line, so add it
myHeaderLines.push_back(headerLine);
}
myHasHeaderLine = true;
// Parse the header line to get the sample information.
myParsedHeaderLine.ReplaceColumns(headerLine, '\t');
return(true);
}
void VcfHeader::syncHeaderLine()
{
if(!myHasHeaderLine)
{
// No header line, so nothing to sync.
return;
}
// Get the last header line and see if it is set.
String& hdrLine = myHeaderLines.back();
if(hdrLine.IsEmpty())
{
// The header line is not set, so set it.
for(int i = 0; i < myParsedHeaderLine.Length(); i++)
{
if(i != 0)
{
hdrLine += '\t';
}
hdrLine += myParsedHeaderLine[i];
}
}
}
|