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 //**********************************************************************************************************************
14 vector<string> SffInfoCommand::setParameters(){
16 CommandParameter psff("sff", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(psff);
17 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
18 CommandParameter psfftxt("sfftxt", "String", "", "", "", "", "",false,false); parameters.push_back(psfftxt);
19 CommandParameter pflow("flow", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pflow);
20 CommandParameter ptrim("trim", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(ptrim);
21 CommandParameter pfasta("fasta", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pfasta);
22 CommandParameter pqfile("name", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pqfile);
23 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
24 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
26 vector<string> myArray;
27 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
31 m->errorOut(e, "SffInfoCommand", "setParameters");
35 //**********************************************************************************************************************
36 string SffInfoCommand::getHelpString(){
38 string helpString = "";
39 helpString += "The sffinfo command reads a sff file and extracts the sequence data, or you can use it to parse a sfftxt file.\n";
40 helpString += "The sffinfo command parameters are sff, fasta, qfile, accnos, flow, sfftxt, and trim. sff is required. \n";
41 helpString += "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";
42 helpString += "The fasta parameter allows you to indicate if you would like a fasta formatted file generated. Default=True. \n";
43 helpString += "The qfile parameter allows you to indicate if you would like a quality file generated. Default=True. \n";
44 helpString += "The flow parameter allows you to indicate if you would like a flowgram file generated. Default=True. \n";
45 helpString += "The sfftxt parameter allows you to indicate if you would like a sff.txt file generated. Default=False. \n";
46 helpString += "If you want to parse an existing sfftxt file into flow, fasta and quality file, enter the file name using the sfftxt parameter. \n";
47 helpString += "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";
48 helpString += "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";
49 helpString += "Example sffinfo(sff=mySffFile.sff, trim=F).\n";
50 helpString += "Note: No spaces between parameter labels (i.e. sff), '=' and parameters (i.e.yourSffFileName).\n";
54 m->errorOut(e, "SffInfoCommand", "getHelpString");
58 //**********************************************************************************************************************
59 string SffInfoCommand::getOutputFileNameTag(string type, string inputName=""){
61 string outputFileName = "";
62 map<string, vector<string> >::iterator it;
64 //is this a type this command creates
65 it = outputTypes.find(type);
66 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
68 if (type == "fasta") { outputFileName = "fasta"; }
69 else if (type == "flow") { outputFileName = "flow"; }
70 else if (type == "sfftxt") { outputFileName = "sff.txt"; }
71 else if (type == "qfile") { outputFileName = "qual"; }
72 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
74 return outputFileName;
77 m->errorOut(e, "SffInfoCommand", "getOutputFileNameTag");
83 //**********************************************************************************************************************
84 SffInfoCommand::SffInfoCommand(){
86 abort = true; calledHelp = true;
88 vector<string> tempOutNames;
89 outputTypes["fasta"] = tempOutNames;
90 outputTypes["flow"] = tempOutNames;
91 outputTypes["sfftxt"] = tempOutNames;
92 outputTypes["qfile"] = tempOutNames;
95 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
99 //**********************************************************************************************************************
101 SffInfoCommand::SffInfoCommand(string option) {
103 abort = false; calledHelp = false;
106 //allow user to run help
107 if(option == "help") { help(); abort = true; calledHelp = true; }
108 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
111 //valid paramters for this command
112 vector<string> myArray = setParameters();
114 OptionParser parser(option);
115 map<string, string> parameters = parser.getParameters();
117 ValidParameters validParameter;
118 //check to make sure all parameters are valid for command
119 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
120 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
123 //initialize outputTypes
124 vector<string> tempOutNames;
125 outputTypes["fasta"] = tempOutNames;
126 outputTypes["flow"] = tempOutNames;
127 outputTypes["sfftxt"] = tempOutNames;
128 outputTypes["qfile"] = tempOutNames;
130 //if the user changes the output directory command factory will send this info to us in the output parameter
131 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
133 //if the user changes the input directory command factory will send this info to us in the output parameter
134 string inputDir = validParameter.validFile(parameters, "inputdir", false); if (inputDir == "not found"){ inputDir = ""; }
136 sffFilename = validParameter.validFile(parameters, "sff", false);
137 if (sffFilename == "not found") { sffFilename = ""; }
139 m->splitAtDash(sffFilename, filenames);
141 //go through files and make sure they are good, if not, then disregard them
142 for (int i = 0; i < filenames.size(); i++) {
144 if (filenames[i] == "current") {
145 filenames[i] = m->getSFFFile();
146 if (filenames[i] != "") { m->mothurOut("Using " + filenames[i] + " as input file for the sff parameter where you had given current."); m->mothurOutEndLine(); }
148 m->mothurOut("You have no current sfffile, ignoring current."); m->mothurOutEndLine(); ignore=true;
149 //erase from file list
150 filenames.erase(filenames.begin()+i);
156 if (inputDir != "") {
157 string path = m->hasPath(filenames[i]);
158 //if the user has not given a path then, add inputdir. else leave path alone.
159 if (path == "") { filenames[i] = inputDir + filenames[i]; }
163 int ableToOpen = m->openInputFile(filenames[i], in, "noerror");
165 //if you can't open it, try default location
166 if (ableToOpen == 1) {
167 if (m->getDefaultPath() != "") { //default path is set
168 string tryPath = m->getDefaultPath() + m->getSimpleName(filenames[i]);
169 m->mothurOut("Unable to open " + filenames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
171 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
173 filenames[i] = tryPath;
177 //if you can't open it, try default location
178 if (ableToOpen == 1) {
179 if (m->getOutputDir() != "") { //default path is set
180 string tryPath = m->getOutputDir() + m->getSimpleName(filenames[i]);
181 m->mothurOut("Unable to open " + filenames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
183 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
185 filenames[i] = tryPath;
191 if (ableToOpen == 1) {
192 m->mothurOut("Unable to open " + filenames[i] + ". It will be disregarded."); m->mothurOutEndLine();
193 //erase from file list
194 filenames.erase(filenames.begin()+i);
196 }else { m->setSFFFile(filenames[i]); }
200 //make sure there is at least one valid file left
201 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
204 accnosName = validParameter.validFile(parameters, "accnos", false);
205 if (accnosName == "not found") { accnosName = ""; }
208 m->splitAtDash(accnosName, accnosFileNames);
210 //go through files and make sure they are good, if not, then disregard them
211 for (int i = 0; i < accnosFileNames.size(); i++) {
213 if (accnosFileNames[i] == "current") {
214 accnosFileNames[i] = m->getAccnosFile();
215 if (accnosFileNames[i] != "") { m->mothurOut("Using " + accnosFileNames[i] + " as input file for the accnos parameter where you had given current."); m->mothurOutEndLine(); }
217 m->mothurOut("You have no current accnosfile, ignoring current."); m->mothurOutEndLine(); ignore=true;
218 //erase from file list
219 accnosFileNames.erase(accnosFileNames.begin()+i);
226 if (inputDir != "") {
227 string path = m->hasPath(accnosFileNames[i]);
228 //if the user has not given a path then, add inputdir. else leave path alone.
229 if (path == "") { accnosFileNames[i] = inputDir + accnosFileNames[i]; }
233 int ableToOpen = m->openInputFile(accnosFileNames[i], in, "noerror");
235 //if you can't open it, try default location
236 if (ableToOpen == 1) {
237 if (m->getDefaultPath() != "") { //default path is set
238 string tryPath = m->getDefaultPath() + m->getSimpleName(accnosFileNames[i]);
239 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
241 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
243 accnosFileNames[i] = tryPath;
246 //if you can't open it, try default location
247 if (ableToOpen == 1) {
248 if (m->getOutputDir() != "") { //default path is set
249 string tryPath = m->getOutputDir() + m->getSimpleName(accnosFileNames[i]);
250 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
252 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
254 accnosFileNames[i] = tryPath;
259 if (ableToOpen == 1) {
260 m->mothurOut("Unable to open " + accnosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
261 //erase from file list
262 accnosFileNames.erase(accnosFileNames.begin()+i);
268 //make sure there is at least one valid file left
269 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
273 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(); }
276 string temp = validParameter.validFile(parameters, "qfile", false); if (temp == "not found"){ temp = "T"; }
277 qual = m->isTrue(temp);
279 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ temp = "T"; }
280 fasta = m->isTrue(temp);
282 temp = validParameter.validFile(parameters, "flow", false); if (temp == "not found"){ temp = "T"; }
283 flow = m->isTrue(temp);
285 temp = validParameter.validFile(parameters, "trim", false); if (temp == "not found"){ temp = "T"; }
286 trim = m->isTrue(temp);
288 temp = validParameter.validFile(parameters, "sfftxt", false);
289 if (temp == "not found") { temp = "F"; sfftxt = false; sfftxtFilename = ""; }
290 else if (m->isTrue(temp)) { sfftxt = true; sfftxtFilename = ""; }
293 if (inputDir != "") {
294 map<string,string>::iterator it = parameters.find("sfftxt");
295 //user has given a template file
296 if(it != parameters.end()){
297 string path = m->hasPath(it->second);
298 //if the user has not given a path then, add inputdir. else leave path alone.
299 if (path == "") { parameters["sfftxt"] = inputDir + it->second; }
303 sfftxtFilename = validParameter.validFile(parameters, "sfftxt", true);
304 if (sfftxtFilename == "not found") { sfftxtFilename = ""; }
305 else if (sfftxtFilename == "not open") { sfftxtFilename = ""; }
308 if ((sfftxtFilename == "") && (filenames.size() == 0)) {
309 //if there is a current sff file, use it
310 string filename = m->getSFFFile();
311 if (filename != "") { filenames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the sff parameter."); m->mothurOutEndLine(); }
312 else { m->mothurOut("[ERROR]: you must provide a valid sff or sfftxt file."); m->mothurOutEndLine(); abort=true; }
316 catch(exception& e) {
317 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
321 //**********************************************************************************************************************
322 int SffInfoCommand::execute(){
324 if (abort == true) { if (calledHelp) { return 0; } return 2; }
326 for (int s = 0; s < filenames.size(); s++) {
328 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
330 int start = time(NULL);
332 filenames[s] = m->getFullPathName(filenames[s]);
333 m->mothurOut("Extracting info from " + filenames[s] + " ..." ); m->mothurOutEndLine();
336 if (hasAccnos) { accnos = accnosFileNames[s]; }
338 int numReads = extractSffInfo(filenames[s], accnos);
340 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to extract " + toString(numReads) + ".");
343 if (sfftxtFilename != "") { parseSffTxt(); }
345 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
347 //set fasta file as new current fastafile
349 itTypes = outputTypes.find("fasta");
350 if (itTypes != outputTypes.end()) {
351 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
354 itTypes = outputTypes.find("qfile");
355 if (itTypes != outputTypes.end()) {
356 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
359 itTypes = outputTypes.find("flow");
360 if (itTypes != outputTypes.end()) {
361 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFlowFile(current); }
364 //report output filenames
365 m->mothurOutEndLine();
366 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
367 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
368 m->mothurOutEndLine();
372 catch(exception& e) {
373 m->errorOut(e, "SffInfoCommand", "execute");
377 //**********************************************************************************************************************
378 int SffInfoCommand::extractSffInfo(string input, string accnos){
381 if (outputDir == "") { outputDir += m->hasPath(input); }
383 if (accnos != "") { readAccnosFile(accnos); }
384 else { seqNames.clear(); }
386 ofstream outSfftxt, outFasta, outQual, outFlow;
387 string outFastaFileName, outQualFileName;
388 string rootName = outputDir + m->getRootName(m->getSimpleName(input));
389 if(rootName.find_last_of(".") == rootName.npos){ rootName += "."; }
391 string sfftxtFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("sfftxt");
392 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("flow");
394 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("fasta");
395 outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("qfile");
397 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw." + getOutputFileNameTag("fasta");
398 outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw." + getOutputFileNameTag("qfile");
401 if (sfftxt) { m->openOutputFile(sfftxtFileName, outSfftxt); outSfftxt.setf(ios::fixed, ios::floatfield); outSfftxt.setf(ios::showpoint); outputNames.push_back(sfftxtFileName); outputTypes["sfftxt"].push_back(sfftxtFileName); }
402 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
403 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName); }
404 if (flow) { m->openOutputFile(outFlowFileName, outFlow); outputNames.push_back(outFlowFileName); outFlow.setf(ios::fixed, ios::floatfield); outFlow.setf(ios::showpoint); outputTypes["flow"].push_back(outFlowFileName); }
407 in.open(input.c_str(), ios::binary);
410 readCommonHeader(in, header);
415 //check magic number and version
416 if (header.magicNumber != 779314790) { m->mothurOut("Magic Number is not correct, not a valid .sff file"); m->mothurOutEndLine(); return count; }
417 if (header.version != "0001") { m->mothurOut("Version is not supported, only support version 0001."); m->mothurOutEndLine(); return count; }
419 //print common header
420 if (sfftxt) { printCommonHeader(outSfftxt, header); }
421 if (flow) { outFlow << header.numFlowsPerRead << endl; }
423 //read through the sff file
430 readHeader(in, readheader);
434 readSeqData(in, read, header.numFlowsPerRead, readheader.numBases);
435 bool okay = sanityCheck(readheader, read);
436 if (!okay) { break; }
438 //if you have provided an accosfile and this seq is not in it, then dont print
439 if (seqNames.size() != 0) { if (seqNames.count(readheader.name) == 0) { print = false; } }
443 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read, readheader); }
444 if (fasta) { printFastaSeqData(outFasta, read, readheader); }
445 if (qual) { printQualSeqData(outQual, read, readheader); }
446 if (flow) { printFlowSeqData(outFlow, read, readheader); }
453 if((count+1) % 10000 == 0){ m->mothurOut(toString(count+1)); m->mothurOutEndLine(); }
455 if (m->control_pressed) { count = 0; break; }
457 if (count >= header.numReads) { break; }
461 if (!m->control_pressed) { if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } }
465 if (sfftxt) { outSfftxt.close(); }
466 if (fasta) { outFasta.close(); }
467 if (qual) { outQual.close(); }
468 if (flow) { outFlow.close(); }
472 catch(exception& e) {
473 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
477 //**********************************************************************************************************************
478 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
486 header.magicNumber = be_int4(*(unsigned int *)(&buffer));
492 for (int i = 0; i < 4; i++) { header.version += toString((int)(buffer9[i])); }
497 header.indexOffset = be_int8(*(unsigned long long *)(&buffer2));
502 header.indexLength = be_int4(*(unsigned int *)(&buffer3));
507 header.numReads = be_int4(*(unsigned int *)(&buffer4));
512 header.headerLength = be_int2(*(unsigned short *)(&buffer5));
517 header.keyLength = be_int2(*(unsigned short *)(&buffer6));
519 //read number of flow reads
522 header.numFlowsPerRead = be_int2(*(unsigned short *)(&buffer7));
527 header.flogramFormatCode = (int)(buffer8[0]);
530 char* tempBuffer = new char[header.numFlowsPerRead];
531 in.read(&(*tempBuffer), header.numFlowsPerRead);
532 header.flowChars = tempBuffer;
533 if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead); }
537 char* tempBuffer2 = new char[header.keyLength];
538 in.read(&(*tempBuffer2), header.keyLength);
539 header.keySequence = tempBuffer2;
540 if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength); }
541 delete[] tempBuffer2;
544 unsigned long long spotInFile = in.tellg();
545 unsigned long long spot = (spotInFile + 7)& ~7; // ~ inverts
549 m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
554 catch(exception& e) {
555 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
559 //**********************************************************************************************************************
560 int SffInfoCommand::readHeader(ifstream& in, Header& header){
568 header.headerLength = be_int2(*(unsigned short *)(&buffer));
573 header.nameLength = be_int2(*(unsigned short *)(&buffer2));
578 header.numBases = be_int4(*(unsigned int *)(&buffer3));
580 //read clip qual left
583 header.clipQualLeft = be_int2(*(unsigned short *)(&buffer4));
584 header.clipQualLeft = 5;
586 //read clip qual right
589 header.clipQualRight = be_int2(*(unsigned short *)(&buffer5));
591 //read clipAdapterLeft
594 header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
596 //read clipAdapterRight
599 header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
602 char* tempBuffer = new char[header.nameLength];
603 in.read(&(*tempBuffer), header.nameLength);
604 header.name = tempBuffer;
605 if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength); }
608 //extract info from name
609 decodeName(header.timestamp, header.region, header.xy, header.name);
612 unsigned long long spotInFile = in.tellg();
613 unsigned long long spot = (spotInFile + 7)& ~7;
617 m->mothurOut("Error reading sff header info."); m->mothurOutEndLine();
622 catch(exception& e) {
623 m->errorOut(e, "SffInfoCommand", "readHeader");
627 //**********************************************************************************************************************
628 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, int numBases){
634 read.flowgram.resize(numFlowReads);
635 for (int i = 0; i < numFlowReads; i++) {
638 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
642 read.flowIndex.resize(numBases);
643 for (int i = 0; i < numBases; i++) {
646 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
650 char* tempBuffer = new char[numBases];
651 in.read(&(*tempBuffer), numBases);
652 read.bases = tempBuffer;
653 if (read.bases.length() > numBases) { read.bases = read.bases.substr(0, numBases); }
657 read.qualScores.resize(numBases);
658 for (int i = 0; i < numBases; i++) {
661 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
665 unsigned long long spotInFile = in.tellg();
666 unsigned long long spot = (spotInFile + 7)& ~7;
670 m->mothurOut("Error reading."); m->mothurOutEndLine();
675 catch(exception& e) {
676 m->errorOut(e, "SffInfoCommand", "readSeqData");
680 //**********************************************************************************************************************
681 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
684 if (name.length() >= 6) {
685 string time = name.substr(0, 6);
686 unsigned int timeNum = m->fromBase36(time);
688 int q1 = timeNum / 60;
689 int sec = timeNum - 60 * q1;
691 int minute = q1 - 60 * q2;
693 int hr = q2 - 24 * q3;
695 int day = q3 - 32 * q4;
697 int mon = q4 - 13 * q5;
698 int year = 2000 + q5;
700 timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
703 if (name.length() >= 9) {
704 region = name.substr(7, 2);
706 string xyNum = name.substr(9);
707 unsigned int myXy = m->fromBase36(xyNum);
711 xy = toString(x) + "_" + toString(y);
716 catch(exception& e) {
717 m->errorOut(e, "SffInfoCommand", "decodeName");
721 //**********************************************************************************************************************
722 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
725 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
726 out << "Version: " << header.version << endl;
727 out << "Index Offset: " << header.indexOffset << endl;
728 out << "Index Length: " << header.indexLength << endl;
729 out << "Number of Reads: " << header.numReads << endl;
730 out << "Header Length: " << header.headerLength << endl;
731 out << "Key Length: " << header.keyLength << endl;
732 out << "Number of Flows: " << header.numFlowsPerRead << endl;
733 out << "Format Code: " << header.flogramFormatCode << endl;
734 out << "Flow Chars: " << header.flowChars << endl;
735 out << "Key Sequence: " << header.keySequence << endl << endl;
739 catch(exception& e) {
740 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
744 //**********************************************************************************************************************
745 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
748 out << ">" << header.name << endl;
749 out << "Run Prefix: " << header.timestamp << endl;
750 out << "Region #: " << header.region << endl;
751 out << "XY Location: " << header.xy << endl << endl;
753 out << "Run Name: " << endl;
754 out << "Analysis Name: " << endl;
755 out << "Full Path: " << endl << endl;
757 out << "Read Header Len: " << header.headerLength << endl;
758 out << "Name Length: " << header.nameLength << endl;
759 out << "# of Bases: " << header.numBases << endl;
760 out << "Clip Qual Left: " << header.clipQualLeft << endl;
761 out << "Clip Qual Right: " << header.clipQualRight << endl;
762 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
763 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
767 catch(exception& e) {
768 m->errorOut(e, "SffInfoCommand", "printHeader");
772 //**********************************************************************************************************************
773 bool SffInfoCommand::sanityCheck(Header& header, seqRead& read) {
776 string message = "[WARNING]: Your sff file may be corrupted! Sequence: " + header.name + "\n";
778 if (header.clipQualLeft > read.bases.length()) {
779 okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
781 if (header.clipQualRight > read.bases.length()) {
782 okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
784 if (header.clipQualLeft > read.qualScores.size()) {
785 okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
787 if (header.clipQualRight > read.qualScores.size()) {
788 okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
792 m->mothurOut(message); m->mothurOutEndLine();
797 catch(exception& e) {
798 m->errorOut(e, "SffInfoCommand", "sanityCheck");
802 //**********************************************************************************************************************
803 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
806 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
808 out << endl << "Flow Indexes: ";
810 for (int i = 0; i < read.flowIndex.size(); i++) { sum += read.flowIndex[i]; out << sum << '\t'; }
812 //make the bases you want to clip lowercase and the bases you want to keep upper case
813 if(header.clipQualRight == 0){ header.clipQualRight = read.bases.length(); }
814 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
815 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { read.bases[i] = toupper(read.bases[i]); }
816 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) { read.bases[i] = tolower(read.bases[i]); }
818 out << endl << "Bases: " << read.bases << endl << "Quality Scores: ";
819 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
826 catch(exception& e) {
827 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
831 //**********************************************************************************************************************
832 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
834 string seq = read.bases;
837 if(header.clipQualRight < header.clipQualLeft){
840 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
841 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
844 seq = seq.substr(header.clipQualLeft-1);
847 //if you wanted the sfftxt then you already converted the bases to the right case
849 //make the bases you want to clip lowercase and the bases you want to keep upper case
850 if(header.clipQualRight == 0){ header.clipQualRight = seq.length(); }
851 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
852 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { seq[i] = toupper(seq[i]); }
853 for (int i = (header.clipQualRight-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
857 out << ">" << header.name << " xy=" << header.xy << endl;
862 catch(exception& e) {
863 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
868 //**********************************************************************************************************************
869 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
873 if(header.clipQualRight < header.clipQualLeft){
874 out << ">" << header.name << " xy=" << header.xy << endl;
877 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
878 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
879 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { out << read.qualScores[i] << '\t'; }
882 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
883 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
886 out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
887 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
894 catch(exception& e) {
895 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
900 //**********************************************************************************************************************
901 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
903 if(header.clipQualRight > header.clipQualLeft){
906 for (int i = 0; i < header.clipQualRight; i++) { rightIndex += read.flowIndex[i]; }
908 out << header.name << ' ' << rightIndex;
909 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100); }
916 catch(exception& e) {
917 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
921 //**********************************************************************************************************************
922 int SffInfoCommand::readAccnosFile(string filename) {
928 m->openInputFile(filename, in);
932 in >> name; m->gobble(in);
934 seqNames.insert(name);
936 if (m->control_pressed) { seqNames.clear(); break; }
942 catch(exception& e) {
943 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
947 //**********************************************************************************************************************
948 int SffInfoCommand::parseSffTxt() {
952 m->openInputFile(sfftxtFilename, inSFF);
954 if (outputDir == "") { outputDir += m->hasPath(sfftxtFilename); }
957 ofstream outFasta, outQual, outFlow;
958 string outFastaFileName, outQualFileName;
959 string fileRoot = m->getRootName(m->getSimpleName(sfftxtFilename));
960 if (fileRoot.length() > 0) {
962 fileRoot = fileRoot.substr(0, fileRoot.length()-1);
963 fileRoot = m->getRootName(fileRoot);
966 string outFlowFileName = outputDir + fileRoot + getOutputFileNameTag("flow");
968 outFastaFileName = outputDir + fileRoot + getOutputFileNameTag("fasta");
969 outQualFileName = outputDir + fileRoot + getOutputFileNameTag("qfile");
971 outFastaFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("fasta");
972 outQualFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("qfile");
975 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
976 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName); }
977 if (flow) { m->openOutputFile(outFlowFileName, outFlow); outputNames.push_back(outFlowFileName); outFlow.setf(ios::fixed, ios::floatfield); outFlow.setf(ios::showpoint); outputTypes["flow"].push_back(outFlowFileName); }
980 string commonHeader = m->getline(inSFF);
981 string magicNumber = m->getline(inSFF);
982 string version = m->getline(inSFF);
983 string indexOffset = m->getline(inSFF);
984 string indexLength = m->getline(inSFF);
985 int numReads = parseHeaderLineToInt(inSFF);
986 string headerLength = m->getline(inSFF);
987 string keyLength = m->getline(inSFF);
988 int numFlows = parseHeaderLineToInt(inSFF);
989 string flowgramCode = m->getline(inSFF);
990 string flowChars = m->getline(inSFF);
991 string keySequence = m->getline(inSFF);
996 if (flow) { outFlow << numFlows << endl; }
998 for(int i=0;i<numReads;i++){
1001 if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
1007 seqName = seqName.substr(1);
1009 header.name = seqName;
1011 string runPrefix = parseHeaderLineToString(inSFF); header.timestamp = runPrefix;
1012 string regionNumber = parseHeaderLineToString(inSFF); header.region = regionNumber;
1013 string xyLocation = parseHeaderLineToString(inSFF); header.xy = xyLocation;
1016 string runName = parseHeaderLineToString(inSFF);
1017 string analysisName = parseHeaderLineToString(inSFF);
1018 string fullPath = parseHeaderLineToString(inSFF);
1021 string readHeaderLen = parseHeaderLineToString(inSFF); convert(readHeaderLen, header.headerLength);
1022 string nameLength = parseHeaderLineToString(inSFF); convert(nameLength, header.nameLength);
1023 int numBases = parseHeaderLineToInt(inSFF); header.numBases = numBases;
1024 string clipQualLeft = parseHeaderLineToString(inSFF); convert(clipQualLeft, header.clipQualLeft);
1025 int clipQualRight = parseHeaderLineToInt(inSFF); header.clipQualRight = clipQualRight;
1026 string clipAdapLeft = parseHeaderLineToString(inSFF); convert(clipAdapLeft, header.clipAdapterLeft);
1027 string clipAdapRight = parseHeaderLineToString(inSFF); convert(clipAdapRight, header.clipAdapterRight);
1033 vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows); read.flowgram = flowVector;
1034 vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases);
1037 vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
1038 for (int j = 1; j < flowIndices.size(); j++) { flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]); }
1039 read.flowIndex = flowIndicesAdjusted;
1041 string bases = parseHeaderLineToString(inSFF); read.bases = bases;
1042 vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases); read.qualScores = qualityScores;
1045 //if you have provided an accosfile and this seq is not in it, then dont print
1047 if (seqNames.size() != 0) { if (seqNames.count(header.name) == 0) { print = false; } }
1051 if (fasta) { printFastaSeqData(outFasta, read, header); }
1052 if (qual) { printQualSeqData(outQual, read, header); }
1053 if (flow) { printFlowSeqData(outFlow, read, header); }
1057 if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine(); }
1059 if (m->control_pressed) { break; }
1063 if (!m->control_pressed) { if((numReads) % 10000 != 0){ m->mothurOut(toString(numReads)); m->mothurOutEndLine(); } }
1067 if (fasta) { outFasta.close(); }
1068 if (qual) { outQual.close(); }
1069 if (flow) { outFlow.close(); }
1073 catch(exception& e) {
1074 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
1078 //**********************************************************************************************************************
1080 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
1084 while (!file.eof()) {
1086 char c = file.get();
1096 catch(exception& e) {
1097 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
1103 //**********************************************************************************************************************
1105 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
1109 while (!file.eof()) {
1110 char c = file.get();
1114 //text = m->getline(file);
1123 catch(exception& e) {
1124 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
1129 //**********************************************************************************************************************
1131 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
1133 vector<unsigned short> floatVector(length);
1135 while (!file.eof()) {
1136 char c = file.get();
1139 for(int i=0;i<length;i++){
1141 floatVector[i] = temp * 100;
1149 catch(exception& e) {
1150 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
1155 //**********************************************************************************************************************
1157 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
1159 vector<unsigned int> intVector(length);
1161 while (!file.eof()) {
1162 char c = file.get();
1164 for(int i=0;i<length;i++){
1165 file >> intVector[i];
1173 catch(exception& e) {
1174 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
1179 //**********************************************************************************************************************