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, "noerror");
59 //if you can't open it, try default location
60 if (ableToOpen == 1) {
61 if (m->getDefaultPath() != "") { //default path is set
62 string tryPath = m->getDefaultPath() + getSimpleName(filenames[i]);
63 m->mothurOut("Unable to open " + filenames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
64 ableToOpen = openInputFile(tryPath, in, "noerror");
65 filenames[i] = tryPath;
70 if (ableToOpen == 1) {
71 m->mothurOut("Unable to open " + filenames[i] + ". It will be disregarded."); m->mothurOutEndLine();
72 //erase from file list
73 filenames.erase(filenames.begin()+i);
78 //make sure there is at least one valid file left
79 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
82 accnosName = validParameter.validFile(parameters, "accnos", false);
83 if (accnosName == "not found") { accnosName = ""; }
86 splitAtDash(accnosName, accnosFileNames);
88 //go through files and make sure they are good, if not, then disregard them
89 for (int i = 0; i < accnosFileNames.size(); i++) {
91 string path = hasPath(accnosFileNames[i]);
92 //if the user has not given a path then, add inputdir. else leave path alone.
93 if (path == "") { accnosFileNames[i] = inputDir + accnosFileNames[i]; }
97 int ableToOpen = openInputFile(accnosFileNames[i], in, "noerror");
99 //if you can't open it, try default location
100 if (ableToOpen == 1) {
101 if (m->getDefaultPath() != "") { //default path is set
102 string tryPath = m->getDefaultPath() + getSimpleName(accnosFileNames[i]);
103 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
104 ableToOpen = openInputFile(tryPath, in, "noerror");
105 accnosFileNames[i] = tryPath;
110 if (ableToOpen == 1) {
111 m->mothurOut("Unable to open " + accnosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
112 //erase from file list
113 accnosFileNames.erase(accnosFileNames.begin()+i);
118 //make sure there is at least one valid file left
119 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
123 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(); }
126 string temp = validParameter.validFile(parameters, "qfile", false); if (temp == "not found"){ temp = "T"; }
129 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ temp = "T"; }
130 fasta = isTrue(temp);
132 temp = validParameter.validFile(parameters, "flow", false); if (temp == "not found"){ temp = "F"; }
135 temp = validParameter.validFile(parameters, "trim", false); if (temp == "not found"){ temp = "T"; }
138 temp = validParameter.validFile(parameters, "sfftxt", false); if (temp == "not found"){ temp = "F"; }
139 sfftxt = isTrue(temp);
142 catch(exception& e) {
143 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
147 //**********************************************************************************************************************
149 void SffInfoCommand::help(){
151 m->mothurOut("The sffinfo command reads a sff file and extracts the sequence data.\n");
152 m->mothurOut("The sffinfo command parameters are sff, fasta, qfile, accnos, flow, sfftxt, and trim. sff is required. \n");
153 m->mothurOut("The sff parameter allows you to enter the sff file you would like to extract data from. You may enter multiple files by separating them by -'s.\n");
154 m->mothurOut("The fasta parameter allows you to indicate if you would like a fasta formatted file generated. Default=True. \n");
155 m->mothurOut("The qfile parameter allows you to indicate if you would like a quality file generated. Default=True. \n");
156 m->mothurOut("The flow parameter allows you to indicate if you would like a flowgram file generated. Default=False. \n");
157 m->mothurOut("The sfftxt parameter allows you to indicate if you would like a sff.txt file generated. Default=False. \n");
158 m->mothurOut("The trim parameter allows you to indicate if you would like a sequences and quality scores trimmed to the clipQualLeft and clipQualRight values. Default=True. \n");
159 m->mothurOut("The accnos parameter allows you to provide a accnos file containing the names of the sequences you would like extracted. You may enter multiple files by separating them by -'s. \n");
160 m->mothurOut("Example sffinfo(sff=mySffFile.sff, trim=F).\n");
161 m->mothurOut("Note: No spaces between parameter labels (i.e. sff), '=' and parameters (i.e.yourSffFileName).\n\n");
163 catch(exception& e) {
164 m->errorOut(e, "SffInfoCommand", "help");
168 //**********************************************************************************************************************
170 SffInfoCommand::~SffInfoCommand(){}
172 //**********************************************************************************************************************
173 int SffInfoCommand::execute(){
176 if (abort == true) { return 0; }
178 for (int s = 0; s < filenames.size(); s++) {
180 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
182 int start = time(NULL);
184 m->mothurOut("Extracting info from " + filenames[s] + " ..." ); m->mothurOutEndLine();
187 if (hasAccnos) { accnos = accnosFileNames[s]; }
189 int numReads = extractSffInfo(filenames[s], accnos);
191 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to extract " + toString(numReads) + ".");
194 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
196 //report output filenames
197 m->mothurOutEndLine();
198 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
199 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
200 m->mothurOutEndLine();
204 catch(exception& e) {
205 m->errorOut(e, "SffInfoCommand", "execute");
209 //**********************************************************************************************************************
210 int SffInfoCommand::extractSffInfo(string input, string accnos){
213 if (outputDir == "") { outputDir += hasPath(input); }
215 if (accnos != "") { readAccnosFile(accnos); }
216 else { seqNames.clear(); }
218 ofstream outSfftxt, outFasta, outQual, outFlow;
219 string outFastaFileName, outQualFileName;
220 string sfftxtFileName = outputDir + getRootName(getSimpleName(input)) + "sff.txt";
221 string outFlowFileName = outputDir + getRootName(getSimpleName(input)) + "flow";
223 outFastaFileName = outputDir + getRootName(getSimpleName(input)) + "fasta";
224 outQualFileName = outputDir + getRootName(getSimpleName(input)) + "qual";
226 outFastaFileName = outputDir + getRootName(getSimpleName(input)) + "raw.fasta";
227 outQualFileName = outputDir + getRootName(getSimpleName(input)) + "raw.qual";
230 if (sfftxt) { openOutputFile(sfftxtFileName, outSfftxt); outSfftxt.setf(ios::fixed, ios::floatfield); outSfftxt.setf(ios::showpoint); outputNames.push_back(sfftxtFileName); }
231 if (fasta) { openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); }
232 if (qual) { openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); }
233 if (flow) { openOutputFile(outFlowFileName, outFlow); outputNames.push_back(outFlowFileName); }
236 in.open(input.c_str(), ios::binary);
239 readCommonHeader(in, header);
243 //check magic number and version
244 if (header.magicNumber != 779314790) { m->mothurOut("Magic Number is not correct, not a valid .sff file"); m->mothurOutEndLine(); return count; }
245 if (header.version != "0001") { m->mothurOut("Version is not supported, only support version 0001."); m->mothurOutEndLine(); return count; }
247 //print common header
248 if (sfftxt) { printCommonHeader(outSfftxt, header); }
250 //read through the sff file
257 readHeader(in, readheader);
261 readSeqData(in, read, header.numFlowsPerRead, readheader.numBases);
263 //if you have provided an accosfile and this seq is not in it, then dont print
264 if (seqNames.size() != 0) { if (seqNames.count(readheader.name) == 0) { print = false; } }
268 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read); }
269 if (fasta) { printFastaSeqData(outFasta, read, readheader); }
270 if (qual) { printQualSeqData(outQual, read, readheader); }
271 if (flow) { printFlowSeqData(outFlow, read, readheader); }
277 if((count+1) % 500 == 0){ m->mothurOut(toString(count+1)); m->mothurOutEndLine(); }
279 if (m->control_pressed) { count = 0; break; }
281 if (count >= header.numReads) { break; }
285 if (!m->control_pressed) { if((count) % 500 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } }
289 if (sfftxt) { outSfftxt.close(); }
290 if (fasta) { outFasta.close(); }
291 if (qual) { outQual.close(); }
292 if (flow) { outFlow.close(); }
296 catch(exception& e) {
297 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
301 //**********************************************************************************************************************
302 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
308 char buffer[sizeof(header.magicNumber)];
309 in.read(buffer, sizeof(header.magicNumber));
310 header.magicNumber = be_int4(*(unsigned int *)(&buffer));
316 for (int i = 0; i < 4; i++) { header.version += toString((int)(buffer9[i])); }
319 char buffer2 [sizeof(header.indexOffset)];
320 in.read(buffer2, sizeof(header.indexOffset));
321 header.indexOffset = be_int8(*(unsigned long int *)(&buffer2));
324 char buffer3 [sizeof(header.indexLength)];
325 in.read(buffer3, sizeof(header.indexLength));
326 header.indexLength = be_int4(*(unsigned int *)(&buffer3));
329 char buffer4 [sizeof(header.numReads)];
330 in.read(buffer4, sizeof(header.numReads));
331 header.numReads = be_int4(*(unsigned int *)(&buffer4));
334 char buffer5 [sizeof(header.headerLength)];
335 in.read(buffer5, sizeof(header.headerLength));
336 header.headerLength = be_int2(*(unsigned short *)(&buffer5));
339 char buffer6 [sizeof(header.keyLength)];
340 in.read(buffer6, sizeof(header.keyLength));
341 header.keyLength = be_int2(*(unsigned short *)(&buffer6));
343 //read number of flow reads
344 char buffer7 [sizeof(header.numFlowsPerRead)];
345 in.read(buffer7, sizeof(header.numFlowsPerRead));
346 header.numFlowsPerRead = be_int2(*(unsigned short *)(&buffer7));
351 header.flogramFormatCode = (int)(buffer8[0]);
354 char tempBuffer [header.numFlowsPerRead];
355 in.read(tempBuffer, header.numFlowsPerRead);
356 header.flowChars = tempBuffer;
357 if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead); }
360 char tempBuffer2 [header.keyLength];
361 in.read(tempBuffer2, header.keyLength);
362 header.keySequence = tempBuffer2;
363 if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength); }
366 int spotInFile = in.tellg();
367 int spot = (spotInFile + 7)& ~7; // ~ inverts
371 m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
376 catch(exception& e) {
377 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
381 //**********************************************************************************************************************
382 int SffInfoCommand::readHeader(ifstream& in, Header& header){
388 char buffer [sizeof(header.headerLength)];
389 in.read(buffer, sizeof(header.headerLength));
390 header.headerLength = be_int2(*(unsigned short *)(&buffer));
393 char buffer2 [sizeof(header.nameLength)];
394 in.read(buffer2, sizeof(header.nameLength));
395 header.nameLength = be_int2(*(unsigned short *)(&buffer2));
398 char buffer3 [sizeof(header.numBases)];
399 in.read(buffer3, sizeof(header.numBases));
400 header.numBases = be_int4(*(unsigned int *)(&buffer3));
402 //read clip qual left
403 char buffer4 [sizeof(header.clipQualLeft)];
404 in.read(buffer4, sizeof(header.clipQualLeft));
405 header.clipQualLeft = be_int2(*(unsigned short *)(&buffer4));
407 //read clip qual right
408 char buffer5 [sizeof(header.clipQualRight)];
409 in.read(buffer5, sizeof(header.clipQualRight));
410 header.clipQualRight = be_int2(*(unsigned short *)(&buffer5));
412 //read clipAdapterLeft
413 char buffer6 [sizeof(header.clipAdapterLeft)];
414 in.read(buffer6, sizeof(header.clipAdapterLeft));
415 header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
417 //read clipAdapterRight
418 char buffer7 [sizeof(header.clipAdapterRight)];
419 in.read(buffer7, sizeof(header.clipAdapterRight));
420 header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
423 char tempBuffer [header.nameLength];
424 in.read(tempBuffer, header.nameLength);
425 header.name = tempBuffer;
426 if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength); }
429 int spotInFile = in.tellg();
430 int spot = (spotInFile + 7)& ~7;
434 m->mothurOut("Error reading sff header info."); m->mothurOutEndLine();
439 catch(exception& e) {
440 m->errorOut(e, "SffInfoCommand", "readHeader");
444 //**********************************************************************************************************************
445 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, int numBases){
451 read.flowgram.resize(numFlowReads);
452 for (int i = 0; i < numFlowReads; i++) {
453 char buffer [sizeof(unsigned short)];
454 in.read(buffer, (sizeof(unsigned short)));
455 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
459 read.flowIndex.resize(numBases);
460 for (int i = 0; i < numBases; i++) {
463 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
467 char tempBuffer[numBases];
468 in.read(tempBuffer, numBases);
469 read.bases = tempBuffer;
470 if (read.bases.length() > numBases) { read.bases = read.bases.substr(0, numBases); }
473 read.qualScores.resize(numBases);
474 for (int i = 0; i < numBases; i++) {
477 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
481 int spotInFile = in.tellg();
482 int spot = (spotInFile + 7)& ~7;
486 m->mothurOut("Error reading."); m->mothurOutEndLine();
491 catch(exception& e) {
492 m->errorOut(e, "SffInfoCommand", "readSeqData");
496 //**********************************************************************************************************************
497 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
500 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
501 out << "Version: " << header.version << endl;
502 out << "Index Offset: " << header.indexOffset << endl;
503 out << "Index Length: " << header.indexLength << endl;
504 out << "Number of Reads: " << header.numReads << endl;
505 out << "Header Length: " << header.headerLength << endl;
506 out << "Key Length: " << header.keyLength << endl;
507 out << "Number of Flows: " << header.numFlowsPerRead << endl;
508 out << "Format Code: " << header.flogramFormatCode << endl;
509 out << "Flow Chars: " << header.flowChars << endl;
510 out << "Key Sequence: " << header.keySequence << endl << endl;
514 catch(exception& e) {
515 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
519 //**********************************************************************************************************************
520 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
523 out << ">" << header.name << endl;
524 out << "Run Prefix: " << endl;
525 out << "Region #: " << endl;
526 out << "XY Location: " << endl << endl;
528 out << "Run Name: " << endl;
529 out << "Analysis Name: " << endl;
530 out << "Full Path: " << endl << endl;
532 out << "Read Header Len: " << header.headerLength << endl;
533 out << "Name Length: " << header.nameLength << endl;
534 out << "# of Bases: " << header.numBases << endl;
535 out << "Clip Qual Left: " << header.clipQualLeft << endl;
536 out << "Clip Qual Right: " << header.clipQualRight << endl;
537 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
538 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
542 catch(exception& e) {
543 m->errorOut(e, "SffInfoCommand", "printHeader");
548 //**********************************************************************************************************************
549 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read) {
553 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
555 out << endl << "Flow Indexes: ";
557 for (int i = 0; i < read.flowIndex.size(); i++) { sum += read.flowIndex[i]; out << sum << '\t'; }
559 out << endl << "Bases: " << read.bases << endl << "Quality Scores: ";
560 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
565 catch(exception& e) {
566 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
570 //**********************************************************************************************************************
571 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
574 string seq = read.bases;
578 seq = seq.substr(header.clipQualLeft, (header.clipQualRight-header.clipQualLeft));
581 out << ">" << header.name << endl;
586 catch(exception& e) {
587 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
592 //**********************************************************************************************************************
593 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
597 out << ">" << header.name << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
598 for (int i = header.clipQualLeft; i < (header.clipQualRight-header.clipQualLeft); i++) { out << read.qualScores[i] << '\t'; }
600 out << ">" << header.name << " length=" << read.qualScores.size() << endl;
601 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
608 catch(exception& e) {
609 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
614 //**********************************************************************************************************************
615 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
618 out << ">" << header.name << endl;
619 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
624 catch(exception& e) {
625 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
629 //**********************************************************************************************************************
630 int SffInfoCommand::readAccnosFile(string filename) {
636 openInputFile(filename, in);
640 in >> name; gobble(in);
642 seqNames.insert(name);
644 if (m->control_pressed) { seqNames.clear(); break; }
650 catch(exception& e) {
651 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
655 //**********************************************************************************************************************/