5 * Created by westcott on 7/7/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "sffinfocommand.h"
11 #include "endiannessmacros.h"
13 //**********************************************************************************************************************
15 SffInfoCommand::SffInfoCommand(string option) {
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"sff","qfile","fasta","flow","trim","accnos","sfftxt","outputdir","inputdir", "outputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string, string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 //check to make sure all parameters are valid for command
33 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
37 //if the user changes the output directory command factory will send this info to us in the output parameter
38 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
40 //if the user changes the input directory command factory will send this info to us in the output parameter
41 string inputDir = validParameter.validFile(parameters, "inputdir", false); if (inputDir == "not found"){ inputDir = ""; }
43 sffFilename = validParameter.validFile(parameters, "sff", false);
44 if (sffFilename == "not found") { m->mothurOut("sff is a required parameter for the sffinfo command."); m->mothurOutEndLine(); abort = true; }
46 splitAtDash(sffFilename, filenames);
48 //go through files and make sure they are good, if not, then disregard them
49 for (int i = 0; i < filenames.size(); i++) {
51 string path = hasPath(filenames[i]);
52 //if the user has not given a path then, add inputdir. else leave path alone.
53 if (path == "") { filenames[i] = inputDir + filenames[i]; }
57 int ableToOpen = openInputFile(filenames[i], in);
60 if (ableToOpen == 1) {
61 m->mothurOut(filenames[i] + " will be disregarded."); m->mothurOutEndLine();
62 //erase from file list
63 filenames.erase(filenames.begin()+i);
68 //make sure there is at least one valid file left
69 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
72 accnosName = validParameter.validFile(parameters, "accnos", false);
73 if (accnosName == "not found") { accnosName = ""; }
76 splitAtDash(accnosName, accnosFileNames);
78 //go through files and make sure they are good, if not, then disregard them
79 for (int i = 0; i < accnosFileNames.size(); i++) {
81 string path = hasPath(accnosFileNames[i]);
82 //if the user has not given a path then, add inputdir. else leave path alone.
83 if (path == "") { accnosFileNames[i] = inputDir + accnosFileNames[i]; }
87 int ableToOpen = openInputFile(accnosFileNames[i], in);
90 if (ableToOpen == 1) {
91 m->mothurOut(accnosFileNames[i] + " will be disregarded."); m->mothurOutEndLine();
92 //erase from file list
93 accnosFileNames.erase(accnosFileNames.begin()+i);
98 //make sure there is at least one valid file left
99 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
103 if (accnosFileNames.size() != filenames.size()) { abort = true; m->mothurOut("If you provide a accnos file, you must have one for each sff file."); m->mothurOutEndLine(); }
106 string temp = validParameter.validFile(parameters, "qfile", false); if (temp == "not found"){ temp = "T"; }
109 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ temp = "T"; }
110 fasta = isTrue(temp);
112 temp = validParameter.validFile(parameters, "flow", false); if (temp == "not found"){ temp = "F"; }
115 temp = validParameter.validFile(parameters, "trim", false); if (temp == "not found"){ temp = "T"; }
118 temp = validParameter.validFile(parameters, "sfftxt", false); if (temp == "not found"){ temp = "F"; }
119 sfftxt = isTrue(temp);
122 catch(exception& e) {
123 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
127 //**********************************************************************************************************************
129 void SffInfoCommand::help(){
131 m->mothurOut("The sffinfo command reads a sff file and outputs a .sff.txt file.\n");
133 m->mothurOut("Example sffinfo(sff=...).\n");
134 m->mothurOut("Note: No spaces between parameter labels (i.e. sff), '=' and parameters (i.e.yourSffFileName).\n\n");
136 catch(exception& e) {
137 m->errorOut(e, "SffInfoCommand", "help");
141 //**********************************************************************************************************************
143 SffInfoCommand::~SffInfoCommand(){}
145 //**********************************************************************************************************************
146 int SffInfoCommand::execute(){
149 if (abort == true) { return 0; }
151 for (int s = 0; s < filenames.size(); s++) {
153 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
155 int start = time(NULL);
157 m->mothurOut("Extracting info from " + filenames[s] + " ..." ); m->mothurOutEndLine();
160 if (hasAccnos) { accnos = accnosFileNames[s]; }
162 int numReads = extractSffInfo(filenames[s], accnos);
164 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to extract " + toString(numReads) + ".");
167 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
169 //report output filenames
170 m->mothurOutEndLine();
171 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
172 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
173 m->mothurOutEndLine();
177 catch(exception& e) {
178 m->errorOut(e, "SffInfoCommand", "execute");
182 //**********************************************************************************************************************
183 int SffInfoCommand::extractSffInfo(string input, string accnos){
186 if (outputDir == "") { outputDir += hasPath(input); }
188 if (accnos != "") { readAccnosFile(accnos); }
189 else { seqNames.clear(); }
191 ofstream outSfftxt, outFasta, outQual, outFlow;
192 string outFastaFileName, outQualFileName;
193 string sfftxtFileName = outputDir + getRootName(getSimpleName(input)) + "sff.txt";
194 string outFlowFileName = outputDir + getRootName(getSimpleName(input)) + "flow";
196 outFastaFileName = outputDir + getRootName(getSimpleName(input)) + "fasta";
197 outQualFileName = outputDir + getRootName(getSimpleName(input)) + "qual";
199 outFastaFileName = outputDir + getRootName(getSimpleName(input)) + "raw.fasta";
200 outQualFileName = outputDir + getRootName(getSimpleName(input)) + "raw.qual";
203 if (sfftxt) { openOutputFile(sfftxtFileName, outSfftxt); outSfftxt.setf(ios::fixed, ios::floatfield); outSfftxt.setf(ios::showpoint); outputNames.push_back(sfftxtFileName); }
204 if (fasta) { openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); }
205 if (qual) { openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); }
206 if (flow) { openOutputFile(outFlowFileName, outFlow); outputNames.push_back(outFlowFileName); }
209 in.open(input.c_str(), ios::binary);
212 readCommonHeader(in, header);
216 //check magic number and version
217 if (header.magicNumber != 779314790) { m->mothurOut("Magic Number is not correct, not a valid .sff file"); m->mothurOutEndLine(); return count; }
218 if (header.version != "0001") { m->mothurOut("Version is not supported, only support version 0001."); m->mothurOutEndLine(); return count; }
220 //print common header
221 if (sfftxt) { printCommonHeader(outSfftxt, header); }
223 //read through the sff file
230 readHeader(in, readheader);
234 readSeqData(in, read, header.numFlowsPerRead, readheader.numBases);
236 //if you have provided an accosfile and this seq is not in it, then dont print
237 if (seqNames.size() != 0) { if (seqNames.count(readheader.name) == 0) { print = false; } }
241 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read); }
242 if (fasta) { printFastaSeqData(outFasta, read, readheader); }
243 if (qual) { printQualSeqData(outQual, read, readheader); }
244 if (flow) { printFlowSeqData(outFlow, read, readheader); }
250 if((count+1) % 500 == 0){ m->mothurOut(toString(count+1)); m->mothurOutEndLine(); }
252 if (m->control_pressed) { count = 0; break; }
254 if (count >= header.numReads) { break; }
258 if (!m->control_pressed) { if((count) % 500 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } }
262 if (sfftxt) { outSfftxt.close(); }
263 if (fasta) { outFasta.close(); }
264 if (qual) { outQual.close(); }
265 if (flow) { outFlow.close(); }
269 catch(exception& e) {
270 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
274 //**********************************************************************************************************************
275 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
281 char buffer[sizeof(header.magicNumber)];
282 in.read(buffer, sizeof(header.magicNumber));
283 header.magicNumber = be_int4(*(unsigned int *)(&buffer));
289 for (int i = 0; i < 4; i++) { header.version += toString((int)(buffer9[i])); }
292 char buffer2 [sizeof(header.indexOffset)];
293 in.read(buffer2, sizeof(header.indexOffset));
294 header.indexOffset = be_int8(*(unsigned long int *)(&buffer2));
297 char buffer3 [sizeof(header.indexLength)];
298 in.read(buffer3, sizeof(header.indexLength));
299 header.indexLength = be_int4(*(unsigned int *)(&buffer3));
302 char buffer4 [sizeof(header.numReads)];
303 in.read(buffer4, sizeof(header.numReads));
304 header.numReads = be_int4(*(unsigned int *)(&buffer4));
307 char buffer5 [sizeof(header.headerLength)];
308 in.read(buffer5, sizeof(header.headerLength));
309 header.headerLength = be_int2(*(unsigned short *)(&buffer5));
312 char buffer6 [sizeof(header.keyLength)];
313 in.read(buffer6, sizeof(header.keyLength));
314 header.keyLength = be_int2(*(unsigned short *)(&buffer6));
316 //read number of flow reads
317 char buffer7 [sizeof(header.numFlowsPerRead)];
318 in.read(buffer7, sizeof(header.numFlowsPerRead));
319 header.numFlowsPerRead = be_int2(*(unsigned short *)(&buffer7));
324 header.flogramFormatCode = (int)(buffer8[0]);
327 char tempBuffer [header.numFlowsPerRead];
328 in.read(tempBuffer, header.numFlowsPerRead);
329 header.flowChars = tempBuffer;
330 if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead); }
333 char tempBuffer2 [header.keyLength];
334 in.read(tempBuffer2, header.keyLength);
335 header.keySequence = tempBuffer2;
336 if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength); }
339 int spotInFile = in.tellg();
340 int spot = (spotInFile + 7)& ~7; // ~ inverts
344 m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
349 catch(exception& e) {
350 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
354 //**********************************************************************************************************************
355 int SffInfoCommand::readHeader(ifstream& in, Header& header){
361 char buffer [sizeof(header.headerLength)];
362 in.read(buffer, sizeof(header.headerLength));
363 header.headerLength = be_int2(*(unsigned short *)(&buffer));
366 char buffer2 [sizeof(header.nameLength)];
367 in.read(buffer2, sizeof(header.nameLength));
368 header.nameLength = be_int2(*(unsigned short *)(&buffer2));
371 char buffer3 [sizeof(header.numBases)];
372 in.read(buffer3, sizeof(header.numBases));
373 header.numBases = be_int4(*(unsigned int *)(&buffer3));
375 //read clip qual left
376 char buffer4 [sizeof(header.clipQualLeft)];
377 in.read(buffer4, sizeof(header.clipQualLeft));
378 header.clipQualLeft = be_int2(*(unsigned short *)(&buffer4));
380 //read clip qual right
381 char buffer5 [sizeof(header.clipQualRight)];
382 in.read(buffer5, sizeof(header.clipQualRight));
383 header.clipQualRight = be_int2(*(unsigned short *)(&buffer5));
385 //read clipAdapterLeft
386 char buffer6 [sizeof(header.clipAdapterLeft)];
387 in.read(buffer6, sizeof(header.clipAdapterLeft));
388 header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
390 //read clipAdapterRight
391 char buffer7 [sizeof(header.clipAdapterRight)];
392 in.read(buffer7, sizeof(header.clipAdapterRight));
393 header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
396 char tempBuffer [header.nameLength];
397 in.read(tempBuffer, header.nameLength);
398 header.name = tempBuffer;
399 if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength); }
402 int spotInFile = in.tellg();
403 int spot = (spotInFile + 7)& ~7;
407 m->mothurOut("Error reading sff header info."); m->mothurOutEndLine();
412 catch(exception& e) {
413 m->errorOut(e, "SffInfoCommand", "readHeader");
417 //**********************************************************************************************************************
418 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, int numBases){
424 read.flowgram.resize(numFlowReads);
425 for (int i = 0; i < numFlowReads; i++) {
426 char buffer [sizeof(unsigned short)];
427 in.read(buffer, (sizeof(unsigned short)));
428 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
432 read.flowIndex.resize(numBases);
433 for (int i = 0; i < numBases; i++) {
436 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
440 char tempBuffer[numBases];
441 in.read(tempBuffer, numBases);
442 read.bases = tempBuffer;
443 if (read.bases.length() > numBases) { read.bases = read.bases.substr(0, numBases); }
446 read.qualScores.resize(numBases);
447 for (int i = 0; i < numBases; i++) {
450 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
454 int spotInFile = in.tellg();
455 int spot = (spotInFile + 7)& ~7;
459 m->mothurOut("Error reading."); m->mothurOutEndLine();
464 catch(exception& e) {
465 m->errorOut(e, "SffInfoCommand", "readSeqData");
469 //**********************************************************************************************************************
470 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
473 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
474 out << "Version: " << header.version << endl;
475 out << "Index Offset: " << header.indexOffset << endl;
476 out << "Index Length: " << header.indexLength << endl;
477 out << "Number of Reads: " << header.numReads << endl;
478 out << "Header Length: " << header.headerLength << endl;
479 out << "Key Length: " << header.keyLength << endl;
480 out << "Number of Flows: " << header.numFlowsPerRead << endl;
481 out << "Format Code: " << header.flogramFormatCode << endl;
482 out << "Flow Chars: " << header.flowChars << endl;
483 out << "Key Sequence: " << header.keySequence << endl << endl;
487 catch(exception& e) {
488 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
492 //**********************************************************************************************************************
493 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
496 out << ">" << header.name << endl;
497 out << "Run Prefix: " << endl;
498 out << "Region #: " << endl;
499 out << "XY Location: " << endl << endl;
501 out << "Run Name: " << endl;
502 out << "Analysis Name: " << endl;
503 out << "Full Path: " << endl << endl;
505 out << "Read Header Len: " << header.headerLength << endl;
506 out << "Name Length: " << header.nameLength << endl;
507 out << "# of Bases: " << header.numBases << endl;
508 out << "Clip Qual Left: " << header.clipQualLeft << endl;
509 out << "Clip Qual Right: " << header.clipQualRight << endl;
510 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
511 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
515 catch(exception& e) {
516 m->errorOut(e, "SffInfoCommand", "printHeader");
521 //**********************************************************************************************************************
522 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read) {
526 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
528 out << endl << "Flow Indexes: ";
530 for (int i = 0; i < read.flowIndex.size(); i++) { sum += read.flowIndex[i]; out << sum << '\t'; }
532 out << endl << "Bases: " << read.bases << endl << "Quality Scores: ";
533 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
538 catch(exception& e) {
539 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
543 //**********************************************************************************************************************
544 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
547 string seq = read.bases;
551 seq = seq.substr(header.clipQualLeft, (header.clipQualRight-header.clipQualLeft));
554 out << ">" << header.name << endl;
559 catch(exception& e) {
560 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
565 //**********************************************************************************************************************
566 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
570 out << ">" << header.name << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
571 for (int i = header.clipQualLeft; i < (header.clipQualRight-header.clipQualLeft); i++) { out << read.qualScores[i] << '\t'; }
573 out << ">" << header.name << " length=" << read.qualScores.size() << endl;
574 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
581 catch(exception& e) {
582 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
587 //**********************************************************************************************************************
588 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
591 out << ">" << header.name << endl;
592 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
597 catch(exception& e) {
598 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
602 //**********************************************************************************************************************
603 int SffInfoCommand::readAccnosFile(string filename) {
609 openInputFile(filename, in);
613 in >> name; gobble(in);
615 seqNames.insert(name);
617 if (m->control_pressed) { seqNames.clear(); break; }
623 catch(exception& e) {
624 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
628 //**********************************************************************************************************************/