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", "", "F", "", "", "",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=False. \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; }
87 //valid paramters for this command
88 vector<string> myArray = setParameters();
90 OptionParser parser(option);
91 map<string, string> parameters = parser.getParameters();
93 ValidParameters validParameter;
94 //check to make sure all parameters are valid for command
95 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
96 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
99 //initialize outputTypes
100 vector<string> tempOutNames;
101 outputTypes["fasta"] = tempOutNames;
102 outputTypes["flow"] = tempOutNames;
103 outputTypes["sfftxt"] = tempOutNames;
104 outputTypes["qfile"] = tempOutNames;
106 //if the user changes the output directory command factory will send this info to us in the output parameter
107 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
109 //if the user changes the input directory command factory will send this info to us in the output parameter
110 string inputDir = validParameter.validFile(parameters, "inputdir", false); if (inputDir == "not found"){ inputDir = ""; }
112 sffFilename = validParameter.validFile(parameters, "sff", false);
113 if (sffFilename == "not found") { sffFilename = ""; }
115 m->splitAtDash(sffFilename, filenames);
117 //go through files and make sure they are good, if not, then disregard them
118 for (int i = 0; i < filenames.size(); i++) {
120 if (filenames[i] == "current") {
121 filenames[i] = m->getSFFFile();
122 if (filenames[i] != "") { m->mothurOut("Using " + filenames[i] + " as input file for the sff parameter where you had given current."); m->mothurOutEndLine(); }
124 m->mothurOut("You have no current sfffile, ignoring current."); m->mothurOutEndLine(); ignore=true;
125 //erase from file list
126 filenames.erase(filenames.begin()+i);
132 if (inputDir != "") {
133 string path = m->hasPath(filenames[i]);
134 //if the user has not given a path then, add inputdir. else leave path alone.
135 if (path == "") { filenames[i] = inputDir + filenames[i]; }
139 int ableToOpen = m->openInputFile(filenames[i], in, "noerror");
141 //if you can't open it, try default location
142 if (ableToOpen == 1) {
143 if (m->getDefaultPath() != "") { //default path is set
144 string tryPath = m->getDefaultPath() + m->getSimpleName(filenames[i]);
145 m->mothurOut("Unable to open " + filenames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
147 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
149 filenames[i] = tryPath;
153 //if you can't open it, try default location
154 if (ableToOpen == 1) {
155 if (m->getOutputDir() != "") { //default path is set
156 string tryPath = m->getOutputDir() + m->getSimpleName(filenames[i]);
157 m->mothurOut("Unable to open " + filenames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
159 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
161 filenames[i] = tryPath;
167 if (ableToOpen == 1) {
168 m->mothurOut("Unable to open " + filenames[i] + ". It will be disregarded."); m->mothurOutEndLine();
169 //erase from file list
170 filenames.erase(filenames.begin()+i);
176 //make sure there is at least one valid file left
177 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
180 accnosName = validParameter.validFile(parameters, "accnos", false);
181 if (accnosName == "not found") { accnosName = ""; }
184 m->splitAtDash(accnosName, accnosFileNames);
186 //go through files and make sure they are good, if not, then disregard them
187 for (int i = 0; i < accnosFileNames.size(); i++) {
189 if (accnosFileNames[i] == "current") {
190 accnosFileNames[i] = m->getAccnosFile();
191 if (accnosFileNames[i] != "") { m->mothurOut("Using " + accnosFileNames[i] + " as input file for the accnos parameter where you had given current."); m->mothurOutEndLine(); }
193 m->mothurOut("You have no current accnosfile, ignoring current."); m->mothurOutEndLine(); ignore=true;
194 //erase from file list
195 accnosFileNames.erase(accnosFileNames.begin()+i);
202 if (inputDir != "") {
203 string path = m->hasPath(accnosFileNames[i]);
204 //if the user has not given a path then, add inputdir. else leave path alone.
205 if (path == "") { accnosFileNames[i] = inputDir + accnosFileNames[i]; }
209 int ableToOpen = m->openInputFile(accnosFileNames[i], in, "noerror");
211 //if you can't open it, try default location
212 if (ableToOpen == 1) {
213 if (m->getDefaultPath() != "") { //default path is set
214 string tryPath = m->getDefaultPath() + m->getSimpleName(accnosFileNames[i]);
215 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
217 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
219 accnosFileNames[i] = tryPath;
222 //if you can't open it, try default location
223 if (ableToOpen == 1) {
224 if (m->getOutputDir() != "") { //default path is set
225 string tryPath = m->getOutputDir() + m->getSimpleName(accnosFileNames[i]);
226 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
228 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
230 accnosFileNames[i] = tryPath;
235 if (ableToOpen == 1) {
236 m->mothurOut("Unable to open " + accnosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
237 //erase from file list
238 accnosFileNames.erase(accnosFileNames.begin()+i);
244 //make sure there is at least one valid file left
245 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
249 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(); }
252 string temp = validParameter.validFile(parameters, "qfile", false); if (temp == "not found"){ temp = "T"; }
253 qual = m->isTrue(temp);
255 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ temp = "T"; }
256 fasta = m->isTrue(temp);
258 temp = validParameter.validFile(parameters, "flow", false); if (temp == "not found"){ temp = "F"; }
259 flow = m->isTrue(temp);
261 temp = validParameter.validFile(parameters, "trim", false); if (temp == "not found"){ temp = "T"; }
262 trim = m->isTrue(temp);
264 temp = validParameter.validFile(parameters, "sfftxt", false);
265 if (temp == "not found") { temp = "F"; sfftxt = false; sfftxtFilename = ""; }
266 else if (m->isTrue(temp)) { sfftxt = true; sfftxtFilename = ""; }
269 if (inputDir != "") {
270 map<string,string>::iterator it = parameters.find("sfftxt");
271 //user has given a template file
272 if(it != parameters.end()){
273 string path = m->hasPath(it->second);
274 //if the user has not given a path then, add inputdir. else leave path alone.
275 if (path == "") { parameters["sfftxt"] = inputDir + it->second; }
279 sfftxtFilename = validParameter.validFile(parameters, "sfftxt", true);
280 if (sfftxtFilename == "not found") { sfftxtFilename = ""; }
281 else if (sfftxtFilename == "not open") { sfftxtFilename = ""; }
284 if ((sfftxtFilename == "") && (filenames.size() == 0)) {
285 //if there is a current fasta file, use it
286 string filename = m->getSFFFile();
287 if (filename != "") { filenames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the sff parameter."); m->mothurOutEndLine(); }
288 else { m->mothurOut("[ERROR]: you must provide a valid sff or sfftxt file."); m->mothurOutEndLine(); abort=true; }
292 catch(exception& e) {
293 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
297 //**********************************************************************************************************************
298 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++) { remove(outputNames[i].c_str()); } 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++) { remove(outputNames[i].c_str()); } 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);
409 //if you have provided an accosfile and this seq is not in it, then dont print
410 if (seqNames.size() != 0) { if (seqNames.count(readheader.name) == 0) { print = false; } }
414 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read, readheader); }
415 if (fasta) { printFastaSeqData(outFasta, read, readheader); }
416 if (qual) { printQualSeqData(outQual, read, readheader); }
417 if (flow) { printFlowSeqData(outFlow, read, readheader); }
424 if((count+1) % 10000 == 0){ m->mothurOut(toString(count+1)); m->mothurOutEndLine(); }
426 if (m->control_pressed) { count = 0; break; }
428 if (count >= header.numReads) { break; }
432 if (!m->control_pressed) { if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } }
436 if (sfftxt) { outSfftxt.close(); }
437 if (fasta) { outFasta.close(); }
438 if (qual) { outQual.close(); }
439 if (flow) { outFlow.close(); }
443 catch(exception& e) {
444 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
448 //**********************************************************************************************************************
449 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
457 header.magicNumber = be_int4(*(unsigned int *)(&buffer));
463 for (int i = 0; i < 4; i++) { header.version += toString((int)(buffer9[i])); }
468 header.indexOffset = be_int8(*(unsigned long int *)(&buffer2));
473 header.indexLength = be_int4(*(unsigned int *)(&buffer3));
478 header.numReads = be_int4(*(unsigned int *)(&buffer4));
483 header.headerLength = be_int2(*(unsigned short *)(&buffer5));
488 header.keyLength = be_int2(*(unsigned short *)(&buffer6));
490 //read number of flow reads
493 header.numFlowsPerRead = be_int2(*(unsigned short *)(&buffer7));
498 header.flogramFormatCode = (int)(buffer8[0]);
501 char* tempBuffer = new char[header.numFlowsPerRead];
502 in.read(&(*tempBuffer), header.numFlowsPerRead);
503 header.flowChars = tempBuffer;
504 if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead); }
508 char* tempBuffer2 = new char[header.keyLength];
509 in.read(&(*tempBuffer2), header.keyLength);
510 header.keySequence = tempBuffer2;
511 if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength); }
512 delete[] tempBuffer2;
515 unsigned long int spotInFile = in.tellg();
516 unsigned long int spot = (spotInFile + 7)& ~7; // ~ inverts
520 m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
525 catch(exception& e) {
526 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
530 //**********************************************************************************************************************
531 int SffInfoCommand::readHeader(ifstream& in, Header& header){
539 header.headerLength = be_int2(*(unsigned short *)(&buffer));
544 header.nameLength = be_int2(*(unsigned short *)(&buffer2));
549 header.numBases = be_int4(*(unsigned int *)(&buffer3));
551 //read clip qual left
554 header.clipQualLeft = be_int2(*(unsigned short *)(&buffer4));
555 header.clipQualLeft = 5;
557 //read clip qual right
560 header.clipQualRight = be_int2(*(unsigned short *)(&buffer5));
562 //read clipAdapterLeft
565 header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
567 //read clipAdapterRight
570 header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
573 char* tempBuffer = new char[header.nameLength];
574 in.read(&(*tempBuffer), header.nameLength);
575 header.name = tempBuffer;
576 if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength); }
579 //extract info from name
580 decodeName(header.timestamp, header.region, header.xy, header.name);
583 unsigned long int spotInFile = in.tellg();
584 unsigned long int spot = (spotInFile + 7)& ~7;
588 m->mothurOut("Error reading sff header info."); m->mothurOutEndLine();
593 catch(exception& e) {
594 m->errorOut(e, "SffInfoCommand", "readHeader");
598 //**********************************************************************************************************************
599 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, int numBases){
605 read.flowgram.resize(numFlowReads);
606 for (int i = 0; i < numFlowReads; i++) {
609 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
613 read.flowIndex.resize(numBases);
614 for (int i = 0; i < numBases; i++) {
617 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
621 char* tempBuffer = new char[numBases];
622 in.read(&(*tempBuffer), numBases);
623 read.bases = tempBuffer;
624 if (read.bases.length() > numBases) { read.bases = read.bases.substr(0, numBases); }
628 read.qualScores.resize(numBases);
629 for (int i = 0; i < numBases; i++) {
632 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
636 unsigned long int spotInFile = in.tellg();
637 unsigned long int spot = (spotInFile + 7)& ~7;
641 m->mothurOut("Error reading."); m->mothurOutEndLine();
646 catch(exception& e) {
647 m->errorOut(e, "SffInfoCommand", "readSeqData");
651 //**********************************************************************************************************************
652 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
655 if (name.length() >= 6) {
656 string time = name.substr(0, 6);
657 unsigned int timeNum = m->fromBase36(time);
659 int q1 = timeNum / 60;
660 int sec = timeNum - 60 * q1;
662 int minute = q1 - 60 * q2;
664 int hr = q2 - 24 * q3;
666 int day = q3 - 32 * q4;
668 int mon = q4 - 13 * q5;
669 int year = 2000 + q5;
671 timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
674 if (name.length() >= 9) {
675 region = name.substr(7, 2);
677 string xyNum = name.substr(9);
678 unsigned int myXy = m->fromBase36(xyNum);
682 xy = toString(x) + "_" + toString(y);
687 catch(exception& e) {
688 m->errorOut(e, "SffInfoCommand", "decodeName");
692 //**********************************************************************************************************************
693 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
696 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
697 out << "Version: " << header.version << endl;
698 out << "Index Offset: " << header.indexOffset << endl;
699 out << "Index Length: " << header.indexLength << endl;
700 out << "Number of Reads: " << header.numReads << endl;
701 out << "Header Length: " << header.headerLength << endl;
702 out << "Key Length: " << header.keyLength << endl;
703 out << "Number of Flows: " << header.numFlowsPerRead << endl;
704 out << "Format Code: " << header.flogramFormatCode << endl;
705 out << "Flow Chars: " << header.flowChars << endl;
706 out << "Key Sequence: " << header.keySequence << endl << endl;
710 catch(exception& e) {
711 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
715 //**********************************************************************************************************************
716 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
719 out << ">" << header.name << endl;
720 out << "Run Prefix: " << header.timestamp << endl;
721 out << "Region #: " << header.region << endl;
722 out << "XY Location: " << header.xy << endl << endl;
724 out << "Run Name: " << endl;
725 out << "Analysis Name: " << endl;
726 out << "Full Path: " << endl << endl;
728 out << "Read Header Len: " << header.headerLength << endl;
729 out << "Name Length: " << header.nameLength << endl;
730 out << "# of Bases: " << header.numBases << endl;
731 out << "Clip Qual Left: " << header.clipQualLeft << endl;
732 out << "Clip Qual Right: " << header.clipQualRight << endl;
733 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
734 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
738 catch(exception& e) {
739 m->errorOut(e, "SffInfoCommand", "printHeader");
744 //**********************************************************************************************************************
745 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
749 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
751 out << endl << "Flow Indexes: ";
753 for (int i = 0; i < read.flowIndex.size(); i++) { sum += read.flowIndex[i]; out << sum << '\t'; }
755 //make the bases you want to clip lowercase and the bases you want to keep upper case
756 if(header.clipQualRight == 0){ header.clipQualRight = read.bases.length(); }
757 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
758 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { read.bases[i] = toupper(read.bases[i]); }
759 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) { read.bases[i] = tolower(read.bases[i]); }
761 out << endl << "Bases: " << read.bases << endl << "Quality Scores: ";
762 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
769 catch(exception& e) {
770 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
774 //**********************************************************************************************************************
775 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
778 string seq = read.bases;
781 if(header.clipQualRight < header.clipQualLeft){
784 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
785 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
788 seq = seq.substr(header.clipQualLeft-1);
791 //if you wanted the sfftxt then you already converted the bases to the right case
793 //make the bases you want to clip lowercase and the bases you want to keep upper case
794 if(header.clipQualRight == 0){ header.clipQualRight = seq.length(); }
795 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
796 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { seq[i] = toupper(seq[i]); }
797 for (int i = (header.clipQualRight-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
801 out << ">" << header.name << " xy=" << header.xy << endl;
806 catch(exception& e) {
807 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
812 //**********************************************************************************************************************
813 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
817 if(header.clipQualRight < header.clipQualLeft){
818 out << ">" << header.name << " xy=" << header.xy << endl;
821 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
822 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
823 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { out << read.qualScores[i] << '\t'; }
826 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
827 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
830 out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
831 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
838 catch(exception& e) {
839 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
844 //**********************************************************************************************************************
845 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
847 if(header.clipQualRight > header.clipQualLeft){
850 for (int i = 0; i < header.clipQualRight; i++) { rightIndex += read.flowIndex[i]; }
852 out << header.name << ' ' << rightIndex;
853 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100); }
860 catch(exception& e) {
861 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
865 //**********************************************************************************************************************
866 int SffInfoCommand::readAccnosFile(string filename) {
872 m->openInputFile(filename, in);
876 in >> name; m->gobble(in);
878 seqNames.insert(name);
880 if (m->control_pressed) { seqNames.clear(); break; }
886 catch(exception& e) {
887 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
891 //**********************************************************************************************************************
892 int SffInfoCommand::parseSffTxt() {
896 m->openInputFile(sfftxtFilename, inSFF);
898 if (outputDir == "") { outputDir += m->hasPath(sfftxtFilename); }
901 ofstream outFasta, outQual, outFlow;
902 string outFastaFileName, outQualFileName;
903 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "flow";
905 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "fasta";
906 outQualFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "qual";
908 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "raw.fasta";
909 outQualFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "raw.qual";
912 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
913 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qual"].push_back(outQualFileName); }
914 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); }
917 string commonHeader = m->getline(inSFF);
918 string magicNumber = m->getline(inSFF);
919 string version = m->getline(inSFF);
920 string indexOffset = m->getline(inSFF);
921 string indexLength = m->getline(inSFF);
922 int numReads = parseHeaderLineToInt(inSFF);
923 string headerLength = m->getline(inSFF);
924 string keyLength = m->getline(inSFF);
925 int numFlows = parseHeaderLineToInt(inSFF);
926 string flowgramCode = m->getline(inSFF);
927 string flowChars = m->getline(inSFF);
928 string keySequence = m->getline(inSFF);
933 if (flow) { outFlow << numFlows << endl; }
935 for(int i=0;i<numReads;i++){
938 if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
944 seqName = seqName.substr(1);
946 header.name = seqName;
948 string runPrefix = parseHeaderLineToString(inSFF); header.timestamp = runPrefix;
949 string regionNumber = parseHeaderLineToString(inSFF); header.region = regionNumber;
950 string xyLocation = parseHeaderLineToString(inSFF); header.xy = xyLocation;
953 string runName = parseHeaderLineToString(inSFF);
954 string analysisName = parseHeaderLineToString(inSFF);
955 string fullPath = parseHeaderLineToString(inSFF);
958 string readHeaderLen = parseHeaderLineToString(inSFF); convert(readHeaderLen, header.headerLength);
959 string nameLength = parseHeaderLineToString(inSFF); convert(nameLength, header.nameLength);
960 int numBases = parseHeaderLineToInt(inSFF); header.numBases = numBases;
961 string clipQualLeft = parseHeaderLineToString(inSFF); convert(clipQualLeft, header.clipQualLeft);
962 int clipQualRight = parseHeaderLineToInt(inSFF); header.clipQualRight = clipQualRight;
963 string clipAdapLeft = parseHeaderLineToString(inSFF); convert(clipAdapLeft, header.clipAdapterLeft);
964 string clipAdapRight = parseHeaderLineToString(inSFF); convert(clipAdapRight, header.clipAdapterRight);
970 vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows); read.flowgram = flowVector;
971 vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases);
974 vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
975 for (int j = 1; j < flowIndices.size(); j++) { flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]); }
976 read.flowIndex = flowIndicesAdjusted;
978 string bases = parseHeaderLineToString(inSFF); read.bases = bases;
979 vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases); read.qualScores = qualityScores;
982 //if you have provided an accosfile and this seq is not in it, then dont print
984 if (seqNames.size() != 0) { if (seqNames.count(header.name) == 0) { print = false; } }
988 if (fasta) { printFastaSeqData(outFasta, read, header); }
989 if (qual) { printQualSeqData(outQual, read, header); }
990 if (flow) { printFlowSeqData(outFlow, read, header); }
994 if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine(); }
996 if (m->control_pressed) { break; }
1000 if (!m->control_pressed) { if((numReads) % 10000 != 0){ m->mothurOut(toString(numReads)); m->mothurOutEndLine(); } }
1004 if (fasta) { outFasta.close(); }
1005 if (qual) { outQual.close(); }
1006 if (flow) { outFlow.close(); }
1010 catch(exception& e) {
1011 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
1015 //**********************************************************************************************************************
1017 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
1021 while (!file.eof()) {
1023 char c = file.get();
1033 catch(exception& e) {
1034 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
1040 //**********************************************************************************************************************
1042 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
1046 while (!file.eof()) {
1047 char c = file.get();
1051 //text = m->getline(file);
1060 catch(exception& e) {
1061 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
1066 //**********************************************************************************************************************
1068 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
1070 vector<unsigned short> floatVector(length);
1072 while (!file.eof()) {
1073 char c = file.get();
1076 for(int i=0;i<length;i++){
1078 floatVector[i] = temp * 100;
1086 catch(exception& e) {
1087 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
1092 //**********************************************************************************************************************
1094 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
1096 vector<unsigned int> intVector(length);
1098 while (!file.eof()) {
1099 char c = file.get();
1101 for(int i=0;i<length;i++){
1102 file >> intVector[i];
1110 catch(exception& e) {
1111 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
1116 //**********************************************************************************************************************