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::getValidParameters(){
16 string Array[] = {"sff","qfile","fasta","flow","trim","accnos","sfftxt","outputdir","inputdir", "outputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "SffInfoCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 SffInfoCommand::SffInfoCommand(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["fasta"] = tempOutNames;
32 outputTypes["flow"] = tempOutNames;
33 outputTypes["sfftxt"] = tempOutNames;
34 outputTypes["qual"] = tempOutNames;
37 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
41 //**********************************************************************************************************************
42 vector<string> SffInfoCommand::getRequiredParameters(){
44 string Array[] = {"sff", "sfftxt", "or"};
45 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
49 m->errorOut(e, "SffInfoCommand", "getRequiredParameters");
53 //**********************************************************************************************************************
54 vector<string> SffInfoCommand::getRequiredFiles(){
56 vector<string> myArray;
60 m->errorOut(e, "SffInfoCommand", "getRequiredFiles");
64 //**********************************************************************************************************************
66 SffInfoCommand::SffInfoCommand(string option) {
71 //allow user to run help
72 if(option == "help") { help(); abort = true; }
75 //valid paramters for this command
76 string Array[] = {"sff","qfile","fasta","flow","trim","accnos","sfftxt","outputdir","inputdir", "outputdir"};
77 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
79 OptionParser parser(option);
80 map<string, string> parameters = parser.getParameters();
82 ValidParameters validParameter;
83 //check to make sure all parameters are valid for command
84 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
85 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
88 //initialize outputTypes
89 vector<string> tempOutNames;
90 outputTypes["fasta"] = tempOutNames;
91 outputTypes["flow"] = tempOutNames;
92 outputTypes["sfftxt"] = tempOutNames;
93 outputTypes["qual"] = tempOutNames;
95 //if the user changes the output directory command factory will send this info to us in the output parameter
96 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
98 //if the user changes the input directory command factory will send this info to us in the output parameter
99 string inputDir = validParameter.validFile(parameters, "inputdir", false); if (inputDir == "not found"){ inputDir = ""; }
101 sffFilename = validParameter.validFile(parameters, "sff", false);
102 if (sffFilename == "not found") { sffFilename = ""; }
104 m->splitAtDash(sffFilename, filenames);
106 //go through files and make sure they are good, if not, then disregard them
107 for (int i = 0; i < filenames.size(); i++) {
108 if (inputDir != "") {
109 string path = m->hasPath(filenames[i]);
110 //if the user has not given a path then, add inputdir. else leave path alone.
111 if (path == "") { filenames[i] = inputDir + filenames[i]; }
115 int ableToOpen = m->openInputFile(filenames[i], in, "noerror");
117 //if you can't open it, try default location
118 if (ableToOpen == 1) {
119 if (m->getDefaultPath() != "") { //default path is set
120 string tryPath = m->getDefaultPath() + m->getSimpleName(filenames[i]);
121 m->mothurOut("Unable to open " + filenames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
123 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
125 filenames[i] = tryPath;
129 //if you can't open it, try default location
130 if (ableToOpen == 1) {
131 if (m->getOutputDir() != "") { //default path is set
132 string tryPath = m->getOutputDir() + m->getSimpleName(filenames[i]);
133 m->mothurOut("Unable to open " + filenames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
135 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
137 filenames[i] = tryPath;
143 if (ableToOpen == 1) {
144 m->mothurOut("Unable to open " + filenames[i] + ". It will be disregarded."); m->mothurOutEndLine();
145 //erase from file list
146 filenames.erase(filenames.begin()+i);
151 //make sure there is at least one valid file left
152 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
155 accnosName = validParameter.validFile(parameters, "accnos", false);
156 if (accnosName == "not found") { accnosName = ""; }
159 m->splitAtDash(accnosName, accnosFileNames);
161 //go through files and make sure they are good, if not, then disregard them
162 for (int i = 0; i < accnosFileNames.size(); i++) {
163 if (inputDir != "") {
164 string path = m->hasPath(accnosFileNames[i]);
165 //if the user has not given a path then, add inputdir. else leave path alone.
166 if (path == "") { accnosFileNames[i] = inputDir + accnosFileNames[i]; }
170 int ableToOpen = m->openInputFile(accnosFileNames[i], in, "noerror");
172 //if you can't open it, try default location
173 if (ableToOpen == 1) {
174 if (m->getDefaultPath() != "") { //default path is set
175 string tryPath = m->getDefaultPath() + m->getSimpleName(accnosFileNames[i]);
176 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
178 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
180 accnosFileNames[i] = tryPath;
183 //if you can't open it, try default location
184 if (ableToOpen == 1) {
185 if (m->getOutputDir() != "") { //default path is set
186 string tryPath = m->getOutputDir() + m->getSimpleName(accnosFileNames[i]);
187 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
189 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
191 accnosFileNames[i] = tryPath;
196 if (ableToOpen == 1) {
197 m->mothurOut("Unable to open " + accnosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
198 //erase from file list
199 accnosFileNames.erase(accnosFileNames.begin()+i);
204 //make sure there is at least one valid file left
205 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
209 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(); }
212 string temp = validParameter.validFile(parameters, "qfile", false); if (temp == "not found"){ temp = "T"; }
213 qual = m->isTrue(temp);
215 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ temp = "T"; }
216 fasta = m->isTrue(temp);
218 temp = validParameter.validFile(parameters, "flow", false); if (temp == "not found"){ temp = "F"; }
219 flow = m->isTrue(temp);
221 temp = validParameter.validFile(parameters, "trim", false); if (temp == "not found"){ temp = "T"; }
222 trim = m->isTrue(temp);
224 temp = validParameter.validFile(parameters, "sfftxt", false);
225 if (temp == "not found") { temp = "F"; sfftxt = false; sfftxtFilename = ""; }
226 else if (m->isTrue(temp)) { sfftxt = true; sfftxtFilename = ""; }
229 if (inputDir != "") {
230 map<string,string>::iterator it = parameters.find("sfftxt");
231 //user has given a template file
232 if(it != parameters.end()){
233 string path = m->hasPath(it->second);
234 //if the user has not given a path then, add inputdir. else leave path alone.
235 if (path == "") { parameters["sfftxt"] = inputDir + it->second; }
239 sfftxtFilename = validParameter.validFile(parameters, "sfftxt", true);
240 if (sfftxtFilename == "not found") { sfftxtFilename = ""; }
241 else if (sfftxtFilename == "not open") { sfftxtFilename = ""; }
244 if ((sfftxtFilename == "") && (filenames.size() == 0)) { m->mothurOut("[ERROR]: you must provide a valid sff or sfftxt file."); m->mothurOutEndLine(); abort=true; }
247 catch(exception& e) {
248 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
252 //**********************************************************************************************************************
254 void SffInfoCommand::help(){
256 m->mothurOut("The sffinfo command reads a sff file and extracts the sequence data, or you can use it to parse a sfftxt file..\n");
257 m->mothurOut("The sffinfo command parameters are sff, fasta, qfile, accnos, flow, sfftxt, and trim. sff is required. \n");
258 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");
259 m->mothurOut("The fasta parameter allows you to indicate if you would like a fasta formatted file generated. Default=True. \n");
260 m->mothurOut("The qfile parameter allows you to indicate if you would like a quality file generated. Default=True. \n");
261 m->mothurOut("The flow parameter allows you to indicate if you would like a flowgram file generated. Default=False. \n");
262 m->mothurOut("The sfftxt parameter allows you to indicate if you would like a sff.txt file generated. Default=False. \n");
263 m->mothurOut("If you want to parse an existing sfftxt file into flow, fasta and quality file, enter the file name using the sfftxt parameter. \n");
264 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");
265 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");
266 m->mothurOut("Example sffinfo(sff=mySffFile.sff, trim=F).\n");
267 m->mothurOut("Note: No spaces between parameter labels (i.e. sff), '=' and parameters (i.e.yourSffFileName).\n\n");
269 catch(exception& e) {
270 m->errorOut(e, "SffInfoCommand", "help");
274 //**********************************************************************************************************************
276 SffInfoCommand::~SffInfoCommand(){}
278 //**********************************************************************************************************************
279 int SffInfoCommand::execute(){
282 if (abort == true) { return 0; }
284 for (int s = 0; s < filenames.size(); s++) {
286 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
288 int start = time(NULL);
290 m->mothurOut("Extracting info from " + filenames[s] + " ..." ); m->mothurOutEndLine();
293 if (hasAccnos) { accnos = accnosFileNames[s]; }
295 int numReads = extractSffInfo(filenames[s], accnos);
297 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to extract " + toString(numReads) + ".");
300 if (sfftxtFilename != "") { parseSffTxt(); }
302 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
304 //report output filenames
305 m->mothurOutEndLine();
306 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
307 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
308 m->mothurOutEndLine();
312 catch(exception& e) {
313 m->errorOut(e, "SffInfoCommand", "execute");
317 //**********************************************************************************************************************
318 int SffInfoCommand::extractSffInfo(string input, string accnos){
321 if (outputDir == "") { outputDir += m->hasPath(input); }
323 if (accnos != "") { readAccnosFile(accnos); }
324 else { seqNames.clear(); }
326 ofstream outSfftxt, outFasta, outQual, outFlow;
327 string outFastaFileName, outQualFileName;
328 string sfftxtFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "sff.txt";
329 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "flow";
331 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "fasta";
332 outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "qual";
334 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw.fasta";
335 outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw.qual";
338 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); }
339 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
340 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qual"].push_back(outQualFileName); }
341 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); }
344 in.open(input.c_str(), ios::binary);
347 readCommonHeader(in, header);
351 //check magic number and version
352 if (header.magicNumber != 779314790) { m->mothurOut("Magic Number is not correct, not a valid .sff file"); m->mothurOutEndLine(); return count; }
353 if (header.version != "0001") { m->mothurOut("Version is not supported, only support version 0001."); m->mothurOutEndLine(); return count; }
355 //print common header
356 if (sfftxt) { printCommonHeader(outSfftxt, header); }
357 if (flow) { outFlow << header.numFlowsPerRead << endl; }
359 //read through the sff file
366 readHeader(in, readheader);
370 readSeqData(in, read, header.numFlowsPerRead, readheader.numBases);
372 //if you have provided an accosfile and this seq is not in it, then dont print
373 if (seqNames.size() != 0) { if (seqNames.count(readheader.name) == 0) { print = false; } }
377 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read, readheader); }
378 if (fasta) { printFastaSeqData(outFasta, read, readheader); }
379 if (qual) { printQualSeqData(outQual, read, readheader); }
380 if (flow) { printFlowSeqData(outFlow, read, readheader); }
386 if((count+1) % 10000 == 0){ m->mothurOut(toString(count+1)); m->mothurOutEndLine(); }
388 if (m->control_pressed) { count = 0; break; }
390 if (count >= header.numReads) { break; }
394 if (!m->control_pressed) { if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } }
398 if (sfftxt) { outSfftxt.close(); }
399 if (fasta) { outFasta.close(); }
400 if (qual) { outQual.close(); }
401 if (flow) { outFlow.close(); }
405 catch(exception& e) {
406 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
410 //**********************************************************************************************************************
411 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
419 header.magicNumber = be_int4(*(unsigned int *)(&buffer));
425 for (int i = 0; i < 4; i++) { header.version += toString((int)(buffer9[i])); }
430 header.indexOffset = be_int8(*(unsigned long int *)(&buffer2));
435 header.indexLength = be_int4(*(unsigned int *)(&buffer3));
440 header.numReads = be_int4(*(unsigned int *)(&buffer4));
445 header.headerLength = be_int2(*(unsigned short *)(&buffer5));
450 header.keyLength = be_int2(*(unsigned short *)(&buffer6));
452 //read number of flow reads
455 header.numFlowsPerRead = be_int2(*(unsigned short *)(&buffer7));
460 header.flogramFormatCode = (int)(buffer8[0]);
463 char* tempBuffer = new char[header.numFlowsPerRead];
464 in.read(&(*tempBuffer), header.numFlowsPerRead);
465 header.flowChars = tempBuffer;
466 if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead); }
470 char* tempBuffer2 = new char[header.keyLength];
471 in.read(&(*tempBuffer2), header.keyLength);
472 header.keySequence = tempBuffer2;
473 if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength); }
474 delete[] tempBuffer2;
477 unsigned long int spotInFile = in.tellg();
478 unsigned long int spot = (spotInFile + 7)& ~7; // ~ inverts
482 m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
487 catch(exception& e) {
488 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
492 //**********************************************************************************************************************
493 int SffInfoCommand::readHeader(ifstream& in, Header& header){
501 header.headerLength = be_int2(*(unsigned short *)(&buffer));
506 header.nameLength = be_int2(*(unsigned short *)(&buffer2));
511 header.numBases = be_int4(*(unsigned int *)(&buffer3));
513 //read clip qual left
516 header.clipQualLeft = be_int2(*(unsigned short *)(&buffer4));
517 header.clipQualLeft = 5;
519 //read clip qual right
522 header.clipQualRight = be_int2(*(unsigned short *)(&buffer5));
524 //read clipAdapterLeft
527 header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
529 //read clipAdapterRight
532 header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
535 char* tempBuffer = new char[header.nameLength];
536 in.read(&(*tempBuffer), header.nameLength);
537 header.name = tempBuffer;
538 if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength); }
541 //extract info from name
542 decodeName(header.timestamp, header.region, header.xy, header.name);
545 unsigned long int spotInFile = in.tellg();
546 unsigned long int spot = (spotInFile + 7)& ~7;
550 m->mothurOut("Error reading sff header info."); m->mothurOutEndLine();
555 catch(exception& e) {
556 m->errorOut(e, "SffInfoCommand", "readHeader");
560 //**********************************************************************************************************************
561 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, int numBases){
567 read.flowgram.resize(numFlowReads);
568 for (int i = 0; i < numFlowReads; i++) {
571 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
575 read.flowIndex.resize(numBases);
576 for (int i = 0; i < numBases; i++) {
579 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
583 char* tempBuffer = new char[numBases];
584 in.read(&(*tempBuffer), numBases);
585 read.bases = tempBuffer;
586 if (read.bases.length() > numBases) { read.bases = read.bases.substr(0, numBases); }
590 read.qualScores.resize(numBases);
591 for (int i = 0; i < numBases; i++) {
594 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
598 unsigned long int spotInFile = in.tellg();
599 unsigned long int spot = (spotInFile + 7)& ~7;
603 m->mothurOut("Error reading."); m->mothurOutEndLine();
608 catch(exception& e) {
609 m->errorOut(e, "SffInfoCommand", "readSeqData");
613 //**********************************************************************************************************************
614 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
617 string time = name.substr(0, 6);
618 unsigned int timeNum = m->fromBase36(time);
620 int q1 = timeNum / 60;
621 int sec = timeNum - 60 * q1;
623 int minute = q1 - 60 * q2;
625 int hr = q2 - 24 * q3;
627 int day = q3 - 32 * q4;
629 int mon = q4 - 13 * q5;
630 int year = 2000 + q5;
632 timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
634 region = name.substr(7, 2);
636 string xyNum = name.substr(9);
637 unsigned int myXy = m->fromBase36(xyNum);
641 xy = toString(x) + "_" + toString(y);
645 catch(exception& e) {
646 m->errorOut(e, "SffInfoCommand", "decodeName");
650 //**********************************************************************************************************************
651 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
654 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
655 out << "Version: " << header.version << endl;
656 out << "Index Offset: " << header.indexOffset << endl;
657 out << "Index Length: " << header.indexLength << endl;
658 out << "Number of Reads: " << header.numReads << endl;
659 out << "Header Length: " << header.headerLength << endl;
660 out << "Key Length: " << header.keyLength << endl;
661 out << "Number of Flows: " << header.numFlowsPerRead << endl;
662 out << "Format Code: " << header.flogramFormatCode << endl;
663 out << "Flow Chars: " << header.flowChars << endl;
664 out << "Key Sequence: " << header.keySequence << endl << endl;
668 catch(exception& e) {
669 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
673 //**********************************************************************************************************************
674 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
677 out << ">" << header.name << endl;
678 out << "Run Prefix: " << header.timestamp << endl;
679 out << "Region #: " << header.region << endl;
680 out << "XY Location: " << header.xy << endl << endl;
682 out << "Run Name: " << endl;
683 out << "Analysis Name: " << endl;
684 out << "Full Path: " << endl << endl;
686 out << "Read Header Len: " << header.headerLength << endl;
687 out << "Name Length: " << header.nameLength << endl;
688 out << "# of Bases: " << header.numBases << endl;
689 out << "Clip Qual Left: " << header.clipQualLeft << endl;
690 out << "Clip Qual Right: " << header.clipQualRight << endl;
691 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
692 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
696 catch(exception& e) {
697 m->errorOut(e, "SffInfoCommand", "printHeader");
702 //**********************************************************************************************************************
703 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
707 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
709 out << endl << "Flow Indexes: ";
711 for (int i = 0; i < read.flowIndex.size(); i++) { sum += read.flowIndex[i]; out << sum << '\t'; }
713 //make the bases you want to clip lowercase and the bases you want to keep upper case
714 if(header.clipQualRight == 0){ header.clipQualRight = read.bases.length(); }
715 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
716 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { read.bases[i] = toupper(read.bases[i]); }
717 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) { read.bases[i] = tolower(read.bases[i]); }
719 out << endl << "Bases: " << read.bases << endl << "Quality Scores: ";
720 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
727 catch(exception& e) {
728 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
732 //**********************************************************************************************************************
733 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
736 string seq = read.bases;
739 if(header.clipQualRight < header.clipQualLeft){
742 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
743 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
746 seq = seq.substr(header.clipQualLeft-1);
749 //if you wanted the sfftxt then you already converted the bases to the right case
751 //make the bases you want to clip lowercase and the bases you want to keep upper case
752 if(header.clipQualRight == 0){ header.clipQualRight = seq.length(); }
753 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
754 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { seq[i] = toupper(seq[i]); }
755 for (int i = (header.clipQualRight-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
759 out << ">" << header.name << " xy=" << header.xy << endl;
764 catch(exception& e) {
765 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
770 //**********************************************************************************************************************
771 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
775 if(header.clipQualRight < header.clipQualLeft){
778 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
779 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
780 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { out << read.qualScores[i] << '\t'; }
783 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
784 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
787 out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
788 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
795 catch(exception& e) {
796 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
801 //**********************************************************************************************************************
802 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
804 if(header.clipQualRight > header.clipQualLeft){
807 for (int i = 0; i < header.clipQualRight; i++) { rightIndex += read.flowIndex[i]; }
809 out << header.name << ' ' << rightIndex;
810 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100); }
817 catch(exception& e) {
818 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
822 //**********************************************************************************************************************
823 int SffInfoCommand::readAccnosFile(string filename) {
829 m->openInputFile(filename, in);
833 in >> name; m->gobble(in);
835 seqNames.insert(name);
837 if (m->control_pressed) { seqNames.clear(); break; }
843 catch(exception& e) {
844 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
848 //**********************************************************************************************************************
849 int SffInfoCommand::parseSffTxt() {
853 m->openInputFile(sfftxtFilename, inSFF);
855 if (outputDir == "") { outputDir += m->hasPath(sfftxtFilename); }
858 ofstream outFasta, outQual, outFlow;
859 string outFastaFileName, outQualFileName;
860 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "flow";
862 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "fasta";
863 outQualFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "qual";
865 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "raw.fasta";
866 outQualFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "raw.qual";
869 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
870 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qual"].push_back(outQualFileName); }
871 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); }
874 string commonHeader = m->getline(inSFF);
875 string magicNumber = m->getline(inSFF);
876 string version = m->getline(inSFF);
877 string indexOffset = m->getline(inSFF);
878 string indexLength = m->getline(inSFF);
879 int numReads = parseHeaderLineToInt(inSFF);
880 string headerLength = m->getline(inSFF);
881 string keyLength = m->getline(inSFF);
882 int numFlows = parseHeaderLineToInt(inSFF);
883 string flowgramCode = m->getline(inSFF);
884 string flowChars = m->getline(inSFF);
885 string keySequence = m->getline(inSFF);
890 if (flow) { outFlow << numFlows << endl; }
892 for(int i=0;i<numReads;i++){
895 if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
901 seqName = seqName.substr(1);
903 header.name = seqName;
905 string runPrefix = parseHeaderLineToString(inSFF); header.timestamp = runPrefix;
906 string regionNumber = parseHeaderLineToString(inSFF); header.region = regionNumber;
907 string xyLocation = parseHeaderLineToString(inSFF); header.xy = xyLocation;
910 string runName = parseHeaderLineToString(inSFF);
911 string analysisName = parseHeaderLineToString(inSFF);
912 string fullPath = parseHeaderLineToString(inSFF);
915 string readHeaderLen = parseHeaderLineToString(inSFF); convert(readHeaderLen, header.headerLength);
916 string nameLength = parseHeaderLineToString(inSFF); convert(nameLength, header.nameLength);
917 int numBases = parseHeaderLineToInt(inSFF); header.numBases = numBases;
918 string clipQualLeft = parseHeaderLineToString(inSFF); convert(clipQualLeft, header.clipQualLeft);
919 int clipQualRight = parseHeaderLineToInt(inSFF); header.clipQualRight = clipQualRight;
920 string clipAdapLeft = parseHeaderLineToString(inSFF); convert(clipAdapLeft, header.clipAdapterLeft);
921 string clipAdapRight = parseHeaderLineToString(inSFF); convert(clipAdapRight, header.clipAdapterRight);
927 vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows); read.flowgram = flowVector;
928 vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases);
931 vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
932 for (int j = 1; j < flowIndices.size(); j++) { flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]); }
933 read.flowIndex = flowIndicesAdjusted;
935 string bases = parseHeaderLineToString(inSFF); read.bases = bases;
936 vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases); read.qualScores = qualityScores;
939 //if you have provided an accosfile and this seq is not in it, then dont print
941 if (seqNames.size() != 0) { if (seqNames.count(header.name) == 0) { print = false; } }
945 if (fasta) { printFastaSeqData(outFasta, read, header); }
946 if (qual) { printQualSeqData(outQual, read, header); }
947 if (flow) { printFlowSeqData(outFlow, read, header); }
951 if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine(); }
953 if (m->control_pressed) { break; }
957 if (!m->control_pressed) { if((numReads) % 10000 != 0){ m->mothurOut(toString(numReads)); m->mothurOutEndLine(); } }
961 if (fasta) { outFasta.close(); }
962 if (qual) { outQual.close(); }
963 if (flow) { outFlow.close(); }
967 catch(exception& e) {
968 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
972 //**********************************************************************************************************************
974 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
978 while (!file.eof()) {
990 catch(exception& e) {
991 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
997 //**********************************************************************************************************************
999 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
1003 while (!file.eof()) {
1004 char c = file.get();
1008 //text = m->getline(file);
1017 catch(exception& e) {
1018 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
1023 //**********************************************************************************************************************
1025 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
1027 vector<unsigned short> floatVector(length);
1029 while (!file.eof()) {
1030 char c = file.get();
1033 for(int i=0;i<length;i++){
1035 floatVector[i] = temp * 100;
1043 catch(exception& e) {
1044 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
1049 //**********************************************************************************************************************
1051 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
1053 vector<unsigned int> intVector(length);
1055 while (!file.eof()) {
1056 char c = file.get();
1058 for(int i=0;i<length;i++){
1059 file >> intVector[i];
1067 catch(exception& e) {
1068 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
1073 //**********************************************************************************************************************