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");
60 //**********************************************************************************************************************
61 SffInfoCommand::SffInfoCommand(){
63 abort = true; calledHelp = true;
65 vector<string> tempOutNames;
66 outputTypes["fasta"] = tempOutNames;
67 outputTypes["flow"] = tempOutNames;
68 outputTypes["sfftxt"] = tempOutNames;
69 outputTypes["qfile"] = tempOutNames;
72 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
76 //**********************************************************************************************************************
78 SffInfoCommand::SffInfoCommand(string option) {
80 abort = false; calledHelp = false;
83 //allow user to run help
84 if(option == "help") { help(); abort = true; calledHelp = true; }
85 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
88 //valid paramters for this command
89 vector<string> myArray = setParameters();
91 OptionParser parser(option);
92 map<string, string> parameters = parser.getParameters();
94 ValidParameters validParameter;
95 //check to make sure all parameters are valid for command
96 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
97 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
100 //initialize outputTypes
101 vector<string> tempOutNames;
102 outputTypes["fasta"] = tempOutNames;
103 outputTypes["flow"] = tempOutNames;
104 outputTypes["sfftxt"] = tempOutNames;
105 outputTypes["qfile"] = tempOutNames;
107 //if the user changes the output directory command factory will send this info to us in the output parameter
108 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
110 //if the user changes the input directory command factory will send this info to us in the output parameter
111 string inputDir = validParameter.validFile(parameters, "inputdir", false); if (inputDir == "not found"){ inputDir = ""; }
113 sffFilename = validParameter.validFile(parameters, "sff", false);
114 if (sffFilename == "not found") { sffFilename = ""; }
116 m->splitAtDash(sffFilename, filenames);
118 //go through files and make sure they are good, if not, then disregard them
119 for (int i = 0; i < filenames.size(); i++) {
121 if (filenames[i] == "current") {
122 filenames[i] = m->getSFFFile();
123 if (filenames[i] != "") { m->mothurOut("Using " + filenames[i] + " as input file for the sff parameter where you had given current."); m->mothurOutEndLine(); }
125 m->mothurOut("You have no current sfffile, ignoring current."); m->mothurOutEndLine(); ignore=true;
126 //erase from file list
127 filenames.erase(filenames.begin()+i);
133 if (inputDir != "") {
134 string path = m->hasPath(filenames[i]);
135 //if the user has not given a path then, add inputdir. else leave path alone.
136 if (path == "") { filenames[i] = inputDir + filenames[i]; }
140 int ableToOpen = m->openInputFile(filenames[i], in, "noerror");
142 //if you can't open it, try default location
143 if (ableToOpen == 1) {
144 if (m->getDefaultPath() != "") { //default path is set
145 string tryPath = m->getDefaultPath() + m->getSimpleName(filenames[i]);
146 m->mothurOut("Unable to open " + filenames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
148 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
150 filenames[i] = tryPath;
154 //if you can't open it, try default location
155 if (ableToOpen == 1) {
156 if (m->getOutputDir() != "") { //default path is set
157 string tryPath = m->getOutputDir() + m->getSimpleName(filenames[i]);
158 m->mothurOut("Unable to open " + filenames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
160 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
162 filenames[i] = tryPath;
168 if (ableToOpen == 1) {
169 m->mothurOut("Unable to open " + filenames[i] + ". It will be disregarded."); m->mothurOutEndLine();
170 //erase from file list
171 filenames.erase(filenames.begin()+i);
173 }else { m->setSFFFile(filenames[i]); }
177 //make sure there is at least one valid file left
178 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
181 accnosName = validParameter.validFile(parameters, "accnos", false);
182 if (accnosName == "not found") { accnosName = ""; }
185 m->splitAtDash(accnosName, accnosFileNames);
187 //go through files and make sure they are good, if not, then disregard them
188 for (int i = 0; i < accnosFileNames.size(); i++) {
190 if (accnosFileNames[i] == "current") {
191 accnosFileNames[i] = m->getAccnosFile();
192 if (accnosFileNames[i] != "") { m->mothurOut("Using " + accnosFileNames[i] + " as input file for the accnos parameter where you had given current."); m->mothurOutEndLine(); }
194 m->mothurOut("You have no current accnosfile, ignoring current."); m->mothurOutEndLine(); ignore=true;
195 //erase from file list
196 accnosFileNames.erase(accnosFileNames.begin()+i);
203 if (inputDir != "") {
204 string path = m->hasPath(accnosFileNames[i]);
205 //if the user has not given a path then, add inputdir. else leave path alone.
206 if (path == "") { accnosFileNames[i] = inputDir + accnosFileNames[i]; }
210 int ableToOpen = m->openInputFile(accnosFileNames[i], in, "noerror");
212 //if you can't open it, try default location
213 if (ableToOpen == 1) {
214 if (m->getDefaultPath() != "") { //default path is set
215 string tryPath = m->getDefaultPath() + m->getSimpleName(accnosFileNames[i]);
216 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
218 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
220 accnosFileNames[i] = tryPath;
223 //if you can't open it, try default location
224 if (ableToOpen == 1) {
225 if (m->getOutputDir() != "") { //default path is set
226 string tryPath = m->getOutputDir() + m->getSimpleName(accnosFileNames[i]);
227 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
229 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
231 accnosFileNames[i] = tryPath;
236 if (ableToOpen == 1) {
237 m->mothurOut("Unable to open " + accnosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
238 //erase from file list
239 accnosFileNames.erase(accnosFileNames.begin()+i);
245 //make sure there is at least one valid file left
246 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
250 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(); }
253 string temp = validParameter.validFile(parameters, "qfile", false); if (temp == "not found"){ temp = "T"; }
254 qual = m->isTrue(temp);
256 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ temp = "T"; }
257 fasta = m->isTrue(temp);
259 temp = validParameter.validFile(parameters, "flow", false); if (temp == "not found"){ temp = "T"; }
260 flow = m->isTrue(temp);
262 temp = validParameter.validFile(parameters, "trim", false); if (temp == "not found"){ temp = "T"; }
263 trim = m->isTrue(temp);
265 temp = validParameter.validFile(parameters, "sfftxt", false);
266 if (temp == "not found") { temp = "F"; sfftxt = false; sfftxtFilename = ""; }
267 else if (m->isTrue(temp)) { sfftxt = true; sfftxtFilename = ""; }
270 if (inputDir != "") {
271 map<string,string>::iterator it = parameters.find("sfftxt");
272 //user has given a template file
273 if(it != parameters.end()){
274 string path = m->hasPath(it->second);
275 //if the user has not given a path then, add inputdir. else leave path alone.
276 if (path == "") { parameters["sfftxt"] = inputDir + it->second; }
280 sfftxtFilename = validParameter.validFile(parameters, "sfftxt", true);
281 if (sfftxtFilename == "not found") { sfftxtFilename = ""; }
282 else if (sfftxtFilename == "not open") { sfftxtFilename = ""; }
285 if ((sfftxtFilename == "") && (filenames.size() == 0)) {
286 //if there is a current sff file, use it
287 string filename = m->getSFFFile();
288 if (filename != "") { filenames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the sff parameter."); m->mothurOutEndLine(); }
289 else { m->mothurOut("[ERROR]: you must provide a valid sff or sfftxt file."); m->mothurOutEndLine(); abort=true; }
293 catch(exception& e) {
294 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
298 //**********************************************************************************************************************
299 int SffInfoCommand::execute(){
301 if (abort == true) { if (calledHelp) { return 0; } return 2; }
303 for (int s = 0; s < filenames.size(); s++) {
305 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
307 int start = time(NULL);
309 m->mothurOut("Extracting info from " + filenames[s] + " ..." ); m->mothurOutEndLine();
312 if (hasAccnos) { accnos = accnosFileNames[s]; }
314 int numReads = extractSffInfo(filenames[s], accnos);
316 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to extract " + toString(numReads) + ".");
319 if (sfftxtFilename != "") { parseSffTxt(); }
321 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
323 //set fasta file as new current fastafile
325 itTypes = outputTypes.find("fasta");
326 if (itTypes != outputTypes.end()) {
327 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
330 itTypes = outputTypes.find("qfile");
331 if (itTypes != outputTypes.end()) {
332 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
335 itTypes = outputTypes.find("flow");
336 if (itTypes != outputTypes.end()) {
337 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFlowFile(current); }
340 //report output filenames
341 m->mothurOutEndLine();
342 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
343 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
344 m->mothurOutEndLine();
348 catch(exception& e) {
349 m->errorOut(e, "SffInfoCommand", "execute");
353 //**********************************************************************************************************************
354 int SffInfoCommand::extractSffInfo(string input, string accnos){
357 if (outputDir == "") { outputDir += m->hasPath(input); }
359 if (accnos != "") { readAccnosFile(accnos); }
360 else { seqNames.clear(); }
362 ofstream outSfftxt, outFasta, outQual, outFlow;
363 string outFastaFileName, outQualFileName;
364 string sfftxtFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "sff.txt";
365 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "flow";
367 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "fasta";
368 outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "qual";
370 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw.fasta";
371 outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw.qual";
374 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); }
375 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
376 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName); }
377 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); }
380 in.open(input.c_str(), ios::binary);
383 readCommonHeader(in, header);
388 //check magic number and version
389 if (header.magicNumber != 779314790) { m->mothurOut("Magic Number is not correct, not a valid .sff file"); m->mothurOutEndLine(); return count; }
390 if (header.version != "0001") { m->mothurOut("Version is not supported, only support version 0001."); m->mothurOutEndLine(); return count; }
392 //print common header
393 if (sfftxt) { printCommonHeader(outSfftxt, header); }
394 if (flow) { outFlow << header.numFlowsPerRead << endl; }
396 //read through the sff file
403 readHeader(in, readheader);
407 readSeqData(in, read, header.numFlowsPerRead, readheader.numBases);
408 bool okay = sanityCheck(readheader, read);
409 if (!okay) { break; }
411 //if you have provided an accosfile and this seq is not in it, then dont print
412 if (seqNames.size() != 0) { if (seqNames.count(readheader.name) == 0) { print = false; } }
416 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read, readheader); }
417 if (fasta) { printFastaSeqData(outFasta, read, readheader); }
418 if (qual) { printQualSeqData(outQual, read, readheader); }
419 if (flow) { printFlowSeqData(outFlow, read, readheader); }
426 if((count+1) % 10000 == 0){ m->mothurOut(toString(count+1)); m->mothurOutEndLine(); }
428 if (m->control_pressed) { count = 0; break; }
430 if (count >= header.numReads) { break; }
434 if (!m->control_pressed) { if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } }
438 if (sfftxt) { outSfftxt.close(); }
439 if (fasta) { outFasta.close(); }
440 if (qual) { outQual.close(); }
441 if (flow) { outFlow.close(); }
445 catch(exception& e) {
446 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
450 //**********************************************************************************************************************
451 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
459 header.magicNumber = be_int4(*(unsigned int *)(&buffer));
465 for (int i = 0; i < 4; i++) { header.version += toString((int)(buffer9[i])); }
470 header.indexOffset = be_int8(*(unsigned long long *)(&buffer2));
475 header.indexLength = be_int4(*(unsigned int *)(&buffer3));
480 header.numReads = be_int4(*(unsigned int *)(&buffer4));
485 header.headerLength = be_int2(*(unsigned short *)(&buffer5));
490 header.keyLength = be_int2(*(unsigned short *)(&buffer6));
492 //read number of flow reads
495 header.numFlowsPerRead = be_int2(*(unsigned short *)(&buffer7));
500 header.flogramFormatCode = (int)(buffer8[0]);
503 char* tempBuffer = new char[header.numFlowsPerRead];
504 in.read(&(*tempBuffer), header.numFlowsPerRead);
505 header.flowChars = tempBuffer;
506 if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead); }
510 char* tempBuffer2 = new char[header.keyLength];
511 in.read(&(*tempBuffer2), header.keyLength);
512 header.keySequence = tempBuffer2;
513 if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength); }
514 delete[] tempBuffer2;
517 unsigned long long spotInFile = in.tellg();
518 unsigned long long spot = (spotInFile + 7)& ~7; // ~ inverts
522 m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
527 catch(exception& e) {
528 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
532 //**********************************************************************************************************************
533 int SffInfoCommand::readHeader(ifstream& in, Header& header){
541 header.headerLength = be_int2(*(unsigned short *)(&buffer));
546 header.nameLength = be_int2(*(unsigned short *)(&buffer2));
551 header.numBases = be_int4(*(unsigned int *)(&buffer3));
553 //read clip qual left
556 header.clipQualLeft = be_int2(*(unsigned short *)(&buffer4));
557 header.clipQualLeft = 5;
559 //read clip qual right
562 header.clipQualRight = be_int2(*(unsigned short *)(&buffer5));
564 //read clipAdapterLeft
567 header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
569 //read clipAdapterRight
572 header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
575 char* tempBuffer = new char[header.nameLength];
576 in.read(&(*tempBuffer), header.nameLength);
577 header.name = tempBuffer;
578 if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength); }
581 //extract info from name
582 decodeName(header.timestamp, header.region, header.xy, header.name);
585 unsigned long long spotInFile = in.tellg();
586 unsigned long long spot = (spotInFile + 7)& ~7;
590 m->mothurOut("Error reading sff header info."); m->mothurOutEndLine();
595 catch(exception& e) {
596 m->errorOut(e, "SffInfoCommand", "readHeader");
600 //**********************************************************************************************************************
601 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, int numBases){
607 read.flowgram.resize(numFlowReads);
608 for (int i = 0; i < numFlowReads; i++) {
611 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
615 read.flowIndex.resize(numBases);
616 for (int i = 0; i < numBases; i++) {
619 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
623 char* tempBuffer = new char[numBases];
624 in.read(&(*tempBuffer), numBases);
625 read.bases = tempBuffer;
626 if (read.bases.length() > numBases) { read.bases = read.bases.substr(0, numBases); }
630 read.qualScores.resize(numBases);
631 for (int i = 0; i < numBases; i++) {
634 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
638 unsigned long long spotInFile = in.tellg();
639 unsigned long long spot = (spotInFile + 7)& ~7;
643 m->mothurOut("Error reading."); m->mothurOutEndLine();
648 catch(exception& e) {
649 m->errorOut(e, "SffInfoCommand", "readSeqData");
653 //**********************************************************************************************************************
654 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
657 if (name.length() >= 6) {
658 string time = name.substr(0, 6);
659 unsigned int timeNum = m->fromBase36(time);
661 int q1 = timeNum / 60;
662 int sec = timeNum - 60 * q1;
664 int minute = q1 - 60 * q2;
666 int hr = q2 - 24 * q3;
668 int day = q3 - 32 * q4;
670 int mon = q4 - 13 * q5;
671 int year = 2000 + q5;
673 timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
676 if (name.length() >= 9) {
677 region = name.substr(7, 2);
679 string xyNum = name.substr(9);
680 unsigned int myXy = m->fromBase36(xyNum);
684 xy = toString(x) + "_" + toString(y);
689 catch(exception& e) {
690 m->errorOut(e, "SffInfoCommand", "decodeName");
694 //**********************************************************************************************************************
695 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
698 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
699 out << "Version: " << header.version << endl;
700 out << "Index Offset: " << header.indexOffset << endl;
701 out << "Index Length: " << header.indexLength << endl;
702 out << "Number of Reads: " << header.numReads << endl;
703 out << "Header Length: " << header.headerLength << endl;
704 out << "Key Length: " << header.keyLength << endl;
705 out << "Number of Flows: " << header.numFlowsPerRead << endl;
706 out << "Format Code: " << header.flogramFormatCode << endl;
707 out << "Flow Chars: " << header.flowChars << endl;
708 out << "Key Sequence: " << header.keySequence << endl << endl;
712 catch(exception& e) {
713 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
717 //**********************************************************************************************************************
718 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
721 out << ">" << header.name << endl;
722 out << "Run Prefix: " << header.timestamp << endl;
723 out << "Region #: " << header.region << endl;
724 out << "XY Location: " << header.xy << endl << endl;
726 out << "Run Name: " << endl;
727 out << "Analysis Name: " << endl;
728 out << "Full Path: " << endl << endl;
730 out << "Read Header Len: " << header.headerLength << endl;
731 out << "Name Length: " << header.nameLength << endl;
732 out << "# of Bases: " << header.numBases << endl;
733 out << "Clip Qual Left: " << header.clipQualLeft << endl;
734 out << "Clip Qual Right: " << header.clipQualRight << endl;
735 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
736 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
740 catch(exception& e) {
741 m->errorOut(e, "SffInfoCommand", "printHeader");
745 //**********************************************************************************************************************
746 bool SffInfoCommand::sanityCheck(Header& header, seqRead& read) {
749 string message = "[WARNING]: Your sff file may be corrupted! Sequence: " + header.name + "\n";
751 if (header.clipQualLeft > read.bases.length()) {
752 okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
754 if (header.clipQualRight > read.bases.length()) {
755 okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
757 if (header.clipQualLeft > read.qualScores.size()) {
758 okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
760 if (header.clipQualRight > read.qualScores.size()) {
761 okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
765 m->mothurOut(message); m->mothurOutEndLine();
770 catch(exception& e) {
771 m->errorOut(e, "SffInfoCommand", "sanityCheck");
775 //**********************************************************************************************************************
776 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
779 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
781 out << endl << "Flow Indexes: ";
783 for (int i = 0; i < read.flowIndex.size(); i++) { sum += read.flowIndex[i]; out << sum << '\t'; }
785 //make the bases you want to clip lowercase and the bases you want to keep upper case
786 if(header.clipQualRight == 0){ header.clipQualRight = read.bases.length(); }
787 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
788 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { read.bases[i] = toupper(read.bases[i]); }
789 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) { read.bases[i] = tolower(read.bases[i]); }
791 out << endl << "Bases: " << read.bases << endl << "Quality Scores: ";
792 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
799 catch(exception& e) {
800 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
804 //**********************************************************************************************************************
805 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
807 string seq = read.bases;
810 if(header.clipQualRight < header.clipQualLeft){
813 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
814 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
817 seq = seq.substr(header.clipQualLeft-1);
820 //if you wanted the sfftxt then you already converted the bases to the right case
822 //make the bases you want to clip lowercase and the bases you want to keep upper case
823 if(header.clipQualRight == 0){ header.clipQualRight = seq.length(); }
824 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
825 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { seq[i] = toupper(seq[i]); }
826 for (int i = (header.clipQualRight-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
830 out << ">" << header.name << " xy=" << header.xy << endl;
835 catch(exception& e) {
836 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
841 //**********************************************************************************************************************
842 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
846 if(header.clipQualRight < header.clipQualLeft){
847 out << ">" << header.name << " xy=" << header.xy << endl;
850 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
851 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
852 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { out << read.qualScores[i] << '\t'; }
855 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
856 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
859 out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
860 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
867 catch(exception& e) {
868 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
873 //**********************************************************************************************************************
874 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
876 if(header.clipQualRight > header.clipQualLeft){
879 for (int i = 0; i < header.clipQualRight; i++) { rightIndex += read.flowIndex[i]; }
881 out << header.name << ' ' << rightIndex;
882 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100); }
889 catch(exception& e) {
890 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
894 //**********************************************************************************************************************
895 int SffInfoCommand::readAccnosFile(string filename) {
901 m->openInputFile(filename, in);
905 in >> name; m->gobble(in);
907 seqNames.insert(name);
909 if (m->control_pressed) { seqNames.clear(); break; }
915 catch(exception& e) {
916 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
920 //**********************************************************************************************************************
921 int SffInfoCommand::parseSffTxt() {
925 m->openInputFile(sfftxtFilename, inSFF);
927 if (outputDir == "") { outputDir += m->hasPath(sfftxtFilename); }
930 ofstream outFasta, outQual, outFlow;
931 string outFastaFileName, outQualFileName;
932 string fileRoot = m->getRootName(m->getSimpleName(sfftxtFilename));
933 if (fileRoot.length() > 0) {
935 fileRoot = fileRoot.substr(0, fileRoot.length()-1);
936 fileRoot = m->getRootName(fileRoot);
939 string outFlowFileName = outputDir + fileRoot + "flow";
941 outFastaFileName = outputDir + fileRoot + "fasta";
942 outQualFileName = outputDir + fileRoot + "qual";
944 outFastaFileName = outputDir + fileRoot + "raw.fasta";
945 outQualFileName = outputDir + fileRoot + "raw.qual";
948 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
949 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qual"].push_back(outQualFileName); }
950 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); }
953 string commonHeader = m->getline(inSFF);
954 string magicNumber = m->getline(inSFF);
955 string version = m->getline(inSFF);
956 string indexOffset = m->getline(inSFF);
957 string indexLength = m->getline(inSFF);
958 int numReads = parseHeaderLineToInt(inSFF);
959 string headerLength = m->getline(inSFF);
960 string keyLength = m->getline(inSFF);
961 int numFlows = parseHeaderLineToInt(inSFF);
962 string flowgramCode = m->getline(inSFF);
963 string flowChars = m->getline(inSFF);
964 string keySequence = m->getline(inSFF);
969 if (flow) { outFlow << numFlows << endl; }
971 for(int i=0;i<numReads;i++){
974 if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
980 seqName = seqName.substr(1);
982 header.name = seqName;
984 string runPrefix = parseHeaderLineToString(inSFF); header.timestamp = runPrefix;
985 string regionNumber = parseHeaderLineToString(inSFF); header.region = regionNumber;
986 string xyLocation = parseHeaderLineToString(inSFF); header.xy = xyLocation;
989 string runName = parseHeaderLineToString(inSFF);
990 string analysisName = parseHeaderLineToString(inSFF);
991 string fullPath = parseHeaderLineToString(inSFF);
994 string readHeaderLen = parseHeaderLineToString(inSFF); convert(readHeaderLen, header.headerLength);
995 string nameLength = parseHeaderLineToString(inSFF); convert(nameLength, header.nameLength);
996 int numBases = parseHeaderLineToInt(inSFF); header.numBases = numBases;
997 string clipQualLeft = parseHeaderLineToString(inSFF); convert(clipQualLeft, header.clipQualLeft);
998 int clipQualRight = parseHeaderLineToInt(inSFF); header.clipQualRight = clipQualRight;
999 string clipAdapLeft = parseHeaderLineToString(inSFF); convert(clipAdapLeft, header.clipAdapterLeft);
1000 string clipAdapRight = parseHeaderLineToString(inSFF); convert(clipAdapRight, header.clipAdapterRight);
1006 vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows); read.flowgram = flowVector;
1007 vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases);
1010 vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
1011 for (int j = 1; j < flowIndices.size(); j++) { flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]); }
1012 read.flowIndex = flowIndicesAdjusted;
1014 string bases = parseHeaderLineToString(inSFF); read.bases = bases;
1015 vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases); read.qualScores = qualityScores;
1018 //if you have provided an accosfile and this seq is not in it, then dont print
1020 if (seqNames.size() != 0) { if (seqNames.count(header.name) == 0) { print = false; } }
1024 if (fasta) { printFastaSeqData(outFasta, read, header); }
1025 if (qual) { printQualSeqData(outQual, read, header); }
1026 if (flow) { printFlowSeqData(outFlow, read, header); }
1030 if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine(); }
1032 if (m->control_pressed) { break; }
1036 if (!m->control_pressed) { if((numReads) % 10000 != 0){ m->mothurOut(toString(numReads)); m->mothurOutEndLine(); } }
1040 if (fasta) { outFasta.close(); }
1041 if (qual) { outQual.close(); }
1042 if (flow) { outFlow.close(); }
1046 catch(exception& e) {
1047 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
1051 //**********************************************************************************************************************
1053 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
1057 while (!file.eof()) {
1059 char c = file.get();
1069 catch(exception& e) {
1070 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
1076 //**********************************************************************************************************************
1078 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
1082 while (!file.eof()) {
1083 char c = file.get();
1087 //text = m->getline(file);
1096 catch(exception& e) {
1097 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
1102 //**********************************************************************************************************************
1104 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
1106 vector<unsigned short> floatVector(length);
1108 while (!file.eof()) {
1109 char c = file.get();
1112 for(int i=0;i<length;i++){
1114 floatVector[i] = temp * 100;
1122 catch(exception& e) {
1123 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
1128 //**********************************************************************************************************************
1130 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
1132 vector<unsigned int> intVector(length);
1134 while (!file.eof()) {
1135 char c = file.get();
1137 for(int i=0;i<length;i++){
1138 file >> intVector[i];
1146 catch(exception& e) {
1147 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
1152 //**********************************************************************************************************************