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(){
28 abort = true; calledHelp = true;
29 vector<string> tempOutNames;
30 outputTypes["fasta"] = tempOutNames;
31 outputTypes["flow"] = tempOutNames;
32 outputTypes["sfftxt"] = tempOutNames;
33 outputTypes["qfile"] = tempOutNames;
36 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
40 //**********************************************************************************************************************
41 vector<string> SffInfoCommand::getRequiredParameters(){
43 string Array[] = {"sff", "sfftxt", "or"};
44 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
48 m->errorOut(e, "SffInfoCommand", "getRequiredParameters");
52 //**********************************************************************************************************************
53 vector<string> SffInfoCommand::getRequiredFiles(){
55 vector<string> myArray;
59 m->errorOut(e, "SffInfoCommand", "getRequiredFiles");
63 //**********************************************************************************************************************
65 SffInfoCommand::SffInfoCommand(string option) {
67 abort = false; calledHelp = false;
70 //allow user to run help
71 if(option == "help") { help(); abort = true; calledHelp = true; }
74 //valid paramters for this command
75 string Array[] = {"sff","qfile","fasta","flow","trim","accnos","sfftxt","outputdir","inputdir", "outputdir"};
76 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
78 OptionParser parser(option);
79 map<string, string> parameters = parser.getParameters();
81 ValidParameters validParameter;
82 //check to make sure all parameters are valid for command
83 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //initialize outputTypes
88 vector<string> tempOutNames;
89 outputTypes["fasta"] = tempOutNames;
90 outputTypes["flow"] = tempOutNames;
91 outputTypes["sfftxt"] = tempOutNames;
92 outputTypes["qfile"] = tempOutNames;
94 //if the user changes the output directory command factory will send this info to us in the output parameter
95 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
97 //if the user changes the input directory command factory will send this info to us in the output parameter
98 string inputDir = validParameter.validFile(parameters, "inputdir", false); if (inputDir == "not found"){ inputDir = ""; }
100 sffFilename = validParameter.validFile(parameters, "sff", false);
101 if (sffFilename == "not found") { sffFilename = ""; }
103 m->splitAtDash(sffFilename, filenames);
105 //go through files and make sure they are good, if not, then disregard them
106 for (int i = 0; i < filenames.size(); i++) {
107 if (inputDir != "") {
108 string path = m->hasPath(filenames[i]);
109 //if the user has not given a path then, add inputdir. else leave path alone.
110 if (path == "") { filenames[i] = inputDir + filenames[i]; }
114 int ableToOpen = m->openInputFile(filenames[i], in, "noerror");
116 //if you can't open it, try default location
117 if (ableToOpen == 1) {
118 if (m->getDefaultPath() != "") { //default path is set
119 string tryPath = m->getDefaultPath() + m->getSimpleName(filenames[i]);
120 m->mothurOut("Unable to open " + filenames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
122 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
124 filenames[i] = tryPath;
128 //if you can't open it, try default location
129 if (ableToOpen == 1) {
130 if (m->getOutputDir() != "") { //default path is set
131 string tryPath = m->getOutputDir() + m->getSimpleName(filenames[i]);
132 m->mothurOut("Unable to open " + filenames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
134 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
136 filenames[i] = tryPath;
142 if (ableToOpen == 1) {
143 m->mothurOut("Unable to open " + filenames[i] + ". It will be disregarded."); m->mothurOutEndLine();
144 //erase from file list
145 filenames.erase(filenames.begin()+i);
150 //make sure there is at least one valid file left
151 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
154 accnosName = validParameter.validFile(parameters, "accnos", false);
155 if (accnosName == "not found") { accnosName = ""; }
158 m->splitAtDash(accnosName, accnosFileNames);
160 //go through files and make sure they are good, if not, then disregard them
161 for (int i = 0; i < accnosFileNames.size(); i++) {
162 if (inputDir != "") {
163 string path = m->hasPath(accnosFileNames[i]);
164 //if the user has not given a path then, add inputdir. else leave path alone.
165 if (path == "") { accnosFileNames[i] = inputDir + accnosFileNames[i]; }
169 int ableToOpen = m->openInputFile(accnosFileNames[i], in, "noerror");
171 //if you can't open it, try default location
172 if (ableToOpen == 1) {
173 if (m->getDefaultPath() != "") { //default path is set
174 string tryPath = m->getDefaultPath() + m->getSimpleName(accnosFileNames[i]);
175 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
177 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
179 accnosFileNames[i] = tryPath;
182 //if you can't open it, try default location
183 if (ableToOpen == 1) {
184 if (m->getOutputDir() != "") { //default path is set
185 string tryPath = m->getOutputDir() + m->getSimpleName(accnosFileNames[i]);
186 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
188 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
190 accnosFileNames[i] = tryPath;
195 if (ableToOpen == 1) {
196 m->mothurOut("Unable to open " + accnosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
197 //erase from file list
198 accnosFileNames.erase(accnosFileNames.begin()+i);
203 //make sure there is at least one valid file left
204 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
208 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(); }
211 string temp = validParameter.validFile(parameters, "qfile", false); if (temp == "not found"){ temp = "T"; }
212 qual = m->isTrue(temp);
214 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ temp = "T"; }
215 fasta = m->isTrue(temp);
217 temp = validParameter.validFile(parameters, "flow", false); if (temp == "not found"){ temp = "F"; }
218 flow = m->isTrue(temp);
220 temp = validParameter.validFile(parameters, "trim", false); if (temp == "not found"){ temp = "T"; }
221 trim = m->isTrue(temp);
223 temp = validParameter.validFile(parameters, "sfftxt", false);
224 if (temp == "not found") { temp = "F"; sfftxt = false; sfftxtFilename = ""; }
225 else if (m->isTrue(temp)) { sfftxt = true; sfftxtFilename = ""; }
228 if (inputDir != "") {
229 map<string,string>::iterator it = parameters.find("sfftxt");
230 //user has given a template file
231 if(it != parameters.end()){
232 string path = m->hasPath(it->second);
233 //if the user has not given a path then, add inputdir. else leave path alone.
234 if (path == "") { parameters["sfftxt"] = inputDir + it->second; }
238 sfftxtFilename = validParameter.validFile(parameters, "sfftxt", true);
239 if (sfftxtFilename == "not found") { sfftxtFilename = ""; }
240 else if (sfftxtFilename == "not open") { sfftxtFilename = ""; }
243 if ((sfftxtFilename == "") && (filenames.size() == 0)) { m->mothurOut("[ERROR]: you must provide a valid sff or sfftxt file."); m->mothurOutEndLine(); abort=true; }
246 catch(exception& e) {
247 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
251 //**********************************************************************************************************************
253 void SffInfoCommand::help(){
255 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");
256 m->mothurOut("The sffinfo command parameters are sff, fasta, qfile, accnos, flow, sfftxt, and trim. sff is required. \n");
257 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");
258 m->mothurOut("The fasta parameter allows you to indicate if you would like a fasta formatted file generated. Default=True. \n");
259 m->mothurOut("The qfile parameter allows you to indicate if you would like a quality file generated. Default=True. \n");
260 m->mothurOut("The flow parameter allows you to indicate if you would like a flowgram file generated. Default=False. \n");
261 m->mothurOut("The sfftxt parameter allows you to indicate if you would like a sff.txt file generated. Default=False. \n");
262 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");
263 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");
264 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");
265 m->mothurOut("Example sffinfo(sff=mySffFile.sff, trim=F).\n");
266 m->mothurOut("Note: No spaces between parameter labels (i.e. sff), '=' and parameters (i.e.yourSffFileName).\n\n");
268 catch(exception& e) {
269 m->errorOut(e, "SffInfoCommand", "help");
273 //**********************************************************************************************************************
275 SffInfoCommand::~SffInfoCommand(){}
277 //**********************************************************************************************************************
278 int SffInfoCommand::execute(){
281 if (abort == true) { if (calledHelp) { return 0; } return 2; }
283 for (int s = 0; s < filenames.size(); s++) {
285 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
287 int start = time(NULL);
289 m->mothurOut("Extracting info from " + filenames[s] + " ..." ); m->mothurOutEndLine();
292 if (hasAccnos) { accnos = accnosFileNames[s]; }
294 int numReads = extractSffInfo(filenames[s], accnos);
296 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to extract " + toString(numReads) + ".");
299 if (sfftxtFilename != "") { parseSffTxt(); }
301 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
303 //set fasta file as new current fastafile
305 itTypes = outputTypes.find("fasta");
306 if (itTypes != outputTypes.end()) {
307 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
310 itTypes = outputTypes.find("qfile");
311 if (itTypes != outputTypes.end()) {
312 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
315 //report output filenames
316 m->mothurOutEndLine();
317 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
318 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
319 m->mothurOutEndLine();
323 catch(exception& e) {
324 m->errorOut(e, "SffInfoCommand", "execute");
328 //**********************************************************************************************************************
329 int SffInfoCommand::extractSffInfo(string input, string accnos){
332 if (outputDir == "") { outputDir += m->hasPath(input); }
334 if (accnos != "") { readAccnosFile(accnos); }
335 else { seqNames.clear(); }
337 ofstream outSfftxt, outFasta, outQual, outFlow;
338 string outFastaFileName, outQualFileName;
339 string sfftxtFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "sff.txt";
340 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "flow";
342 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "fasta";
343 outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "qual";
345 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw.fasta";
346 outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw.qual";
349 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); }
350 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
351 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName); }
352 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); }
355 in.open(input.c_str(), ios::binary);
358 readCommonHeader(in, header);
362 //check magic number and version
363 if (header.magicNumber != 779314790) { m->mothurOut("Magic Number is not correct, not a valid .sff file"); m->mothurOutEndLine(); return count; }
364 if (header.version != "0001") { m->mothurOut("Version is not supported, only support version 0001."); m->mothurOutEndLine(); return count; }
366 //print common header
367 if (sfftxt) { printCommonHeader(outSfftxt, header); }
368 if (flow) { outFlow << header.numFlowsPerRead << endl; }
370 //read through the sff file
377 readHeader(in, readheader);
381 readSeqData(in, read, header.numFlowsPerRead, readheader.numBases);
383 //if you have provided an accosfile and this seq is not in it, then dont print
384 if (seqNames.size() != 0) { if (seqNames.count(readheader.name) == 0) { print = false; } }
388 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read, readheader); }
389 if (fasta) { printFastaSeqData(outFasta, read, readheader); }
390 if (qual) { printQualSeqData(outQual, read, readheader); }
391 if (flow) { printFlowSeqData(outFlow, read, readheader); }
397 if((count+1) % 10000 == 0){ m->mothurOut(toString(count+1)); m->mothurOutEndLine(); }
399 if (m->control_pressed) { count = 0; break; }
401 if (count >= header.numReads) { break; }
405 if (!m->control_pressed) { if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } }
409 if (sfftxt) { outSfftxt.close(); }
410 if (fasta) { outFasta.close(); }
411 if (qual) { outQual.close(); }
412 if (flow) { outFlow.close(); }
416 catch(exception& e) {
417 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
421 //**********************************************************************************************************************
422 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
430 header.magicNumber = be_int4(*(unsigned int *)(&buffer));
436 for (int i = 0; i < 4; i++) { header.version += toString((int)(buffer9[i])); }
441 header.indexOffset = be_int8(*(unsigned long int *)(&buffer2));
446 header.indexLength = be_int4(*(unsigned int *)(&buffer3));
451 header.numReads = be_int4(*(unsigned int *)(&buffer4));
456 header.headerLength = be_int2(*(unsigned short *)(&buffer5));
461 header.keyLength = be_int2(*(unsigned short *)(&buffer6));
463 //read number of flow reads
466 header.numFlowsPerRead = be_int2(*(unsigned short *)(&buffer7));
471 header.flogramFormatCode = (int)(buffer8[0]);
474 char* tempBuffer = new char[header.numFlowsPerRead];
475 in.read(&(*tempBuffer), header.numFlowsPerRead);
476 header.flowChars = tempBuffer;
477 if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead); }
481 char* tempBuffer2 = new char[header.keyLength];
482 in.read(&(*tempBuffer2), header.keyLength);
483 header.keySequence = tempBuffer2;
484 if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength); }
485 delete[] tempBuffer2;
488 unsigned long int spotInFile = in.tellg();
489 unsigned long int spot = (spotInFile + 7)& ~7; // ~ inverts
493 m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
498 catch(exception& e) {
499 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
503 //**********************************************************************************************************************
504 int SffInfoCommand::readHeader(ifstream& in, Header& header){
512 header.headerLength = be_int2(*(unsigned short *)(&buffer));
517 header.nameLength = be_int2(*(unsigned short *)(&buffer2));
522 header.numBases = be_int4(*(unsigned int *)(&buffer3));
524 //read clip qual left
527 header.clipQualLeft = be_int2(*(unsigned short *)(&buffer4));
528 header.clipQualLeft = 5;
530 //read clip qual right
533 header.clipQualRight = be_int2(*(unsigned short *)(&buffer5));
535 //read clipAdapterLeft
538 header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
540 //read clipAdapterRight
543 header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
546 char* tempBuffer = new char[header.nameLength];
547 in.read(&(*tempBuffer), header.nameLength);
548 header.name = tempBuffer;
549 if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength); }
552 //extract info from name
553 decodeName(header.timestamp, header.region, header.xy, header.name);
556 unsigned long int spotInFile = in.tellg();
557 unsigned long int spot = (spotInFile + 7)& ~7;
561 m->mothurOut("Error reading sff header info."); m->mothurOutEndLine();
566 catch(exception& e) {
567 m->errorOut(e, "SffInfoCommand", "readHeader");
571 //**********************************************************************************************************************
572 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, int numBases){
578 read.flowgram.resize(numFlowReads);
579 for (int i = 0; i < numFlowReads; i++) {
582 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
586 read.flowIndex.resize(numBases);
587 for (int i = 0; i < numBases; i++) {
590 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
594 char* tempBuffer = new char[numBases];
595 in.read(&(*tempBuffer), numBases);
596 read.bases = tempBuffer;
597 if (read.bases.length() > numBases) { read.bases = read.bases.substr(0, numBases); }
601 read.qualScores.resize(numBases);
602 for (int i = 0; i < numBases; i++) {
605 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
609 unsigned long int spotInFile = in.tellg();
610 unsigned long int spot = (spotInFile + 7)& ~7;
614 m->mothurOut("Error reading."); m->mothurOutEndLine();
619 catch(exception& e) {
620 m->errorOut(e, "SffInfoCommand", "readSeqData");
624 //**********************************************************************************************************************
625 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
628 string time = name.substr(0, 6);
629 unsigned int timeNum = m->fromBase36(time);
631 int q1 = timeNum / 60;
632 int sec = timeNum - 60 * q1;
634 int minute = q1 - 60 * q2;
636 int hr = q2 - 24 * q3;
638 int day = q3 - 32 * q4;
640 int mon = q4 - 13 * q5;
641 int year = 2000 + q5;
643 timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
645 region = name.substr(7, 2);
647 string xyNum = name.substr(9);
648 unsigned int myXy = m->fromBase36(xyNum);
652 xy = toString(x) + "_" + toString(y);
656 catch(exception& e) {
657 m->errorOut(e, "SffInfoCommand", "decodeName");
661 //**********************************************************************************************************************
662 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
665 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
666 out << "Version: " << header.version << endl;
667 out << "Index Offset: " << header.indexOffset << endl;
668 out << "Index Length: " << header.indexLength << endl;
669 out << "Number of Reads: " << header.numReads << endl;
670 out << "Header Length: " << header.headerLength << endl;
671 out << "Key Length: " << header.keyLength << endl;
672 out << "Number of Flows: " << header.numFlowsPerRead << endl;
673 out << "Format Code: " << header.flogramFormatCode << endl;
674 out << "Flow Chars: " << header.flowChars << endl;
675 out << "Key Sequence: " << header.keySequence << endl << endl;
679 catch(exception& e) {
680 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
684 //**********************************************************************************************************************
685 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
688 out << ">" << header.name << endl;
689 out << "Run Prefix: " << header.timestamp << endl;
690 out << "Region #: " << header.region << endl;
691 out << "XY Location: " << header.xy << endl << endl;
693 out << "Run Name: " << endl;
694 out << "Analysis Name: " << endl;
695 out << "Full Path: " << endl << endl;
697 out << "Read Header Len: " << header.headerLength << endl;
698 out << "Name Length: " << header.nameLength << endl;
699 out << "# of Bases: " << header.numBases << endl;
700 out << "Clip Qual Left: " << header.clipQualLeft << endl;
701 out << "Clip Qual Right: " << header.clipQualRight << endl;
702 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
703 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
707 catch(exception& e) {
708 m->errorOut(e, "SffInfoCommand", "printHeader");
713 //**********************************************************************************************************************
714 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
718 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
720 out << endl << "Flow Indexes: ";
722 for (int i = 0; i < read.flowIndex.size(); i++) { sum += read.flowIndex[i]; out << sum << '\t'; }
724 //make the bases you want to clip lowercase and the bases you want to keep upper case
725 if(header.clipQualRight == 0){ header.clipQualRight = read.bases.length(); }
726 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
727 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { read.bases[i] = toupper(read.bases[i]); }
728 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) { read.bases[i] = tolower(read.bases[i]); }
730 out << endl << "Bases: " << read.bases << endl << "Quality Scores: ";
731 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
738 catch(exception& e) {
739 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
743 //**********************************************************************************************************************
744 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
747 string seq = read.bases;
750 if(header.clipQualRight < header.clipQualLeft){
753 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
754 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
757 seq = seq.substr(header.clipQualLeft-1);
760 //if you wanted the sfftxt then you already converted the bases to the right case
762 //make the bases you want to clip lowercase and the bases you want to keep upper case
763 if(header.clipQualRight == 0){ header.clipQualRight = seq.length(); }
764 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
765 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { seq[i] = toupper(seq[i]); }
766 for (int i = (header.clipQualRight-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
770 out << ">" << header.name << " xy=" << header.xy << endl;
775 catch(exception& e) {
776 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
781 //**********************************************************************************************************************
782 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
786 if(header.clipQualRight < header.clipQualLeft){
789 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
790 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
791 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { out << read.qualScores[i] << '\t'; }
794 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
795 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
798 out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
799 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
806 catch(exception& e) {
807 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
812 //**********************************************************************************************************************
813 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
815 if(header.clipQualRight > header.clipQualLeft){
818 for (int i = 0; i < header.clipQualRight; i++) { rightIndex += read.flowIndex[i]; }
820 out << header.name << ' ' << rightIndex;
821 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100); }
828 catch(exception& e) {
829 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
833 //**********************************************************************************************************************
834 int SffInfoCommand::readAccnosFile(string filename) {
840 m->openInputFile(filename, in);
844 in >> name; m->gobble(in);
846 seqNames.insert(name);
848 if (m->control_pressed) { seqNames.clear(); break; }
854 catch(exception& e) {
855 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
859 //**********************************************************************************************************************
860 int SffInfoCommand::parseSffTxt() {
864 m->openInputFile(sfftxtFilename, inSFF);
866 if (outputDir == "") { outputDir += m->hasPath(sfftxtFilename); }
869 ofstream outFasta, outQual, outFlow;
870 string outFastaFileName, outQualFileName;
871 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "flow";
873 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "fasta";
874 outQualFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "qual";
876 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "raw.fasta";
877 outQualFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "raw.qual";
880 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
881 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qual"].push_back(outQualFileName); }
882 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); }
885 string commonHeader = m->getline(inSFF);
886 string magicNumber = m->getline(inSFF);
887 string version = m->getline(inSFF);
888 string indexOffset = m->getline(inSFF);
889 string indexLength = m->getline(inSFF);
890 int numReads = parseHeaderLineToInt(inSFF);
891 string headerLength = m->getline(inSFF);
892 string keyLength = m->getline(inSFF);
893 int numFlows = parseHeaderLineToInt(inSFF);
894 string flowgramCode = m->getline(inSFF);
895 string flowChars = m->getline(inSFF);
896 string keySequence = m->getline(inSFF);
901 if (flow) { outFlow << numFlows << endl; }
903 for(int i=0;i<numReads;i++){
906 if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
912 seqName = seqName.substr(1);
914 header.name = seqName;
916 string runPrefix = parseHeaderLineToString(inSFF); header.timestamp = runPrefix;
917 string regionNumber = parseHeaderLineToString(inSFF); header.region = regionNumber;
918 string xyLocation = parseHeaderLineToString(inSFF); header.xy = xyLocation;
921 string runName = parseHeaderLineToString(inSFF);
922 string analysisName = parseHeaderLineToString(inSFF);
923 string fullPath = parseHeaderLineToString(inSFF);
926 string readHeaderLen = parseHeaderLineToString(inSFF); convert(readHeaderLen, header.headerLength);
927 string nameLength = parseHeaderLineToString(inSFF); convert(nameLength, header.nameLength);
928 int numBases = parseHeaderLineToInt(inSFF); header.numBases = numBases;
929 string clipQualLeft = parseHeaderLineToString(inSFF); convert(clipQualLeft, header.clipQualLeft);
930 int clipQualRight = parseHeaderLineToInt(inSFF); header.clipQualRight = clipQualRight;
931 string clipAdapLeft = parseHeaderLineToString(inSFF); convert(clipAdapLeft, header.clipAdapterLeft);
932 string clipAdapRight = parseHeaderLineToString(inSFF); convert(clipAdapRight, header.clipAdapterRight);
938 vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows); read.flowgram = flowVector;
939 vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases);
942 vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
943 for (int j = 1; j < flowIndices.size(); j++) { flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]); }
944 read.flowIndex = flowIndicesAdjusted;
946 string bases = parseHeaderLineToString(inSFF); read.bases = bases;
947 vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases); read.qualScores = qualityScores;
950 //if you have provided an accosfile and this seq is not in it, then dont print
952 if (seqNames.size() != 0) { if (seqNames.count(header.name) == 0) { print = false; } }
956 if (fasta) { printFastaSeqData(outFasta, read, header); }
957 if (qual) { printQualSeqData(outQual, read, header); }
958 if (flow) { printFlowSeqData(outFlow, read, header); }
962 if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine(); }
964 if (m->control_pressed) { break; }
968 if (!m->control_pressed) { if((numReads) % 10000 != 0){ m->mothurOut(toString(numReads)); m->mothurOutEndLine(); } }
972 if (fasta) { outFasta.close(); }
973 if (qual) { outQual.close(); }
974 if (flow) { outFlow.close(); }
978 catch(exception& e) {
979 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
983 //**********************************************************************************************************************
985 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
989 while (!file.eof()) {
1001 catch(exception& e) {
1002 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
1008 //**********************************************************************************************************************
1010 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
1014 while (!file.eof()) {
1015 char c = file.get();
1019 //text = m->getline(file);
1028 catch(exception& e) {
1029 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
1034 //**********************************************************************************************************************
1036 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
1038 vector<unsigned short> floatVector(length);
1040 while (!file.eof()) {
1041 char c = file.get();
1044 for(int i=0;i<length;i++){
1046 floatVector[i] = temp * 100;
1054 catch(exception& e) {
1055 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
1060 //**********************************************************************************************************************
1062 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
1064 vector<unsigned int> intVector(length);
1066 while (!file.eof()) {
1067 char c = file.get();
1069 for(int i=0;i<length;i++){
1070 file >> intVector[i];
1078 catch(exception& e) {
1079 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
1084 //**********************************************************************************************************************