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++) {
119 if (inputDir != "") {
120 string path = m->hasPath(filenames[i]);
121 //if the user has not given a path then, add inputdir. else leave path alone.
122 if (path == "") { filenames[i] = inputDir + filenames[i]; }
126 int ableToOpen = m->openInputFile(filenames[i], in, "noerror");
128 //if you can't open it, try default location
129 if (ableToOpen == 1) {
130 if (m->getDefaultPath() != "") { //default path is set
131 string tryPath = m->getDefaultPath() + m->getSimpleName(filenames[i]);
132 m->mothurOut("Unable to open " + filenames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
134 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
136 filenames[i] = tryPath;
140 //if you can't open it, try default location
141 if (ableToOpen == 1) {
142 if (m->getOutputDir() != "") { //default path is set
143 string tryPath = m->getOutputDir() + m->getSimpleName(filenames[i]);
144 m->mothurOut("Unable to open " + filenames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
146 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
148 filenames[i] = tryPath;
154 if (ableToOpen == 1) {
155 m->mothurOut("Unable to open " + filenames[i] + ". It will be disregarded."); m->mothurOutEndLine();
156 //erase from file list
157 filenames.erase(filenames.begin()+i);
162 //make sure there is at least one valid file left
163 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
166 accnosName = validParameter.validFile(parameters, "accnos", false);
167 if (accnosName == "not found") { accnosName = ""; }
170 m->splitAtDash(accnosName, accnosFileNames);
172 //go through files and make sure they are good, if not, then disregard them
173 for (int i = 0; i < accnosFileNames.size(); i++) {
174 if (inputDir != "") {
175 string path = m->hasPath(accnosFileNames[i]);
176 //if the user has not given a path then, add inputdir. else leave path alone.
177 if (path == "") { accnosFileNames[i] = inputDir + accnosFileNames[i]; }
181 int ableToOpen = m->openInputFile(accnosFileNames[i], in, "noerror");
183 //if you can't open it, try default location
184 if (ableToOpen == 1) {
185 if (m->getDefaultPath() != "") { //default path is set
186 string tryPath = m->getDefaultPath() + m->getSimpleName(accnosFileNames[i]);
187 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
189 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
191 accnosFileNames[i] = tryPath;
194 //if you can't open it, try default location
195 if (ableToOpen == 1) {
196 if (m->getOutputDir() != "") { //default path is set
197 string tryPath = m->getOutputDir() + m->getSimpleName(accnosFileNames[i]);
198 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
200 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
202 accnosFileNames[i] = tryPath;
207 if (ableToOpen == 1) {
208 m->mothurOut("Unable to open " + accnosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
209 //erase from file list
210 accnosFileNames.erase(accnosFileNames.begin()+i);
215 //make sure there is at least one valid file left
216 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
220 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(); }
223 string temp = validParameter.validFile(parameters, "qfile", false); if (temp == "not found"){ temp = "T"; }
224 qual = m->isTrue(temp);
226 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ temp = "T"; }
227 fasta = m->isTrue(temp);
229 temp = validParameter.validFile(parameters, "flow", false); if (temp == "not found"){ temp = "F"; }
230 flow = m->isTrue(temp);
232 temp = validParameter.validFile(parameters, "trim", false); if (temp == "not found"){ temp = "T"; }
233 trim = m->isTrue(temp);
235 temp = validParameter.validFile(parameters, "sfftxt", false);
236 if (temp == "not found") { temp = "F"; sfftxt = false; sfftxtFilename = ""; }
237 else if (m->isTrue(temp)) { sfftxt = true; sfftxtFilename = ""; }
240 if (inputDir != "") {
241 map<string,string>::iterator it = parameters.find("sfftxt");
242 //user has given a template file
243 if(it != parameters.end()){
244 string path = m->hasPath(it->second);
245 //if the user has not given a path then, add inputdir. else leave path alone.
246 if (path == "") { parameters["sfftxt"] = inputDir + it->second; }
250 sfftxtFilename = validParameter.validFile(parameters, "sfftxt", true);
251 if (sfftxtFilename == "not found") { sfftxtFilename = ""; }
252 else if (sfftxtFilename == "not open") { sfftxtFilename = ""; }
255 if ((sfftxtFilename == "") && (filenames.size() == 0)) {
256 //if there is a current fasta file, use it
257 string filename = m->getSFFFile();
258 if (filename != "") { filenames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the sff parameter."); m->mothurOutEndLine(); }
259 else { m->mothurOut("[ERROR]: you must provide a valid sff or sfftxt file."); m->mothurOutEndLine(); abort=true; }
263 catch(exception& e) {
264 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
268 //**********************************************************************************************************************
269 int SffInfoCommand::execute(){
272 if (abort == true) { if (calledHelp) { return 0; } return 2; }
274 for (int s = 0; s < filenames.size(); s++) {
276 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
278 int start = time(NULL);
280 m->mothurOut("Extracting info from " + filenames[s] + " ..." ); m->mothurOutEndLine();
283 if (hasAccnos) { accnos = accnosFileNames[s]; }
285 int numReads = extractSffInfo(filenames[s], accnos);
287 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to extract " + toString(numReads) + ".");
290 if (sfftxtFilename != "") { parseSffTxt(); }
292 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
294 //set fasta file as new current fastafile
296 itTypes = outputTypes.find("fasta");
297 if (itTypes != outputTypes.end()) {
298 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
301 itTypes = outputTypes.find("qfile");
302 if (itTypes != outputTypes.end()) {
303 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
306 //report output filenames
307 m->mothurOutEndLine();
308 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
309 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
310 m->mothurOutEndLine();
314 catch(exception& e) {
315 m->errorOut(e, "SffInfoCommand", "execute");
319 //**********************************************************************************************************************
320 int SffInfoCommand::extractSffInfo(string input, string accnos){
323 if (outputDir == "") { outputDir += m->hasPath(input); }
325 if (accnos != "") { readAccnosFile(accnos); }
326 else { seqNames.clear(); }
328 ofstream outSfftxt, outFasta, outQual, outFlow;
329 string outFastaFileName, outQualFileName;
330 string sfftxtFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "sff.txt";
331 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "flow";
333 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "fasta";
334 outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "qual";
336 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw.fasta";
337 outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw.qual";
340 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); }
341 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
342 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName); }
343 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); }
346 in.open(input.c_str(), ios::binary);
349 readCommonHeader(in, header);
354 //check magic number and version
355 if (header.magicNumber != 779314790) { m->mothurOut("Magic Number is not correct, not a valid .sff file"); m->mothurOutEndLine(); return count; }
356 if (header.version != "0001") { m->mothurOut("Version is not supported, only support version 0001."); m->mothurOutEndLine(); return count; }
358 //print common header
359 if (sfftxt) { printCommonHeader(outSfftxt, header); }
360 if (flow) { outFlow << header.numFlowsPerRead << endl; }
362 //read through the sff file
369 readHeader(in, readheader);
373 readSeqData(in, read, header.numFlowsPerRead, readheader.numBases);
375 //if you have provided an accosfile and this seq is not in it, then dont print
376 if (seqNames.size() != 0) { if (seqNames.count(readheader.name) == 0) { print = false; } }
380 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read, readheader); }
381 if (fasta) { printFastaSeqData(outFasta, read, readheader); }
382 if (qual) { printQualSeqData(outQual, read, readheader); }
383 if (flow) { printFlowSeqData(outFlow, read, readheader); }
390 if((count+1) % 10000 == 0){ m->mothurOut(toString(count+1)); m->mothurOutEndLine(); }
392 if (m->control_pressed) { count = 0; break; }
394 if (count >= header.numReads) { break; }
398 if (!m->control_pressed) { if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } }
402 if (sfftxt) { outSfftxt.close(); }
403 if (fasta) { outFasta.close(); }
404 if (qual) { outQual.close(); }
405 if (flow) { outFlow.close(); }
409 catch(exception& e) {
410 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
414 //**********************************************************************************************************************
415 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
423 header.magicNumber = be_int4(*(unsigned int *)(&buffer));
429 for (int i = 0; i < 4; i++) { header.version += toString((int)(buffer9[i])); }
434 header.indexOffset = be_int8(*(unsigned long int *)(&buffer2));
439 header.indexLength = be_int4(*(unsigned int *)(&buffer3));
444 header.numReads = be_int4(*(unsigned int *)(&buffer4));
449 header.headerLength = be_int2(*(unsigned short *)(&buffer5));
454 header.keyLength = be_int2(*(unsigned short *)(&buffer6));
456 //read number of flow reads
459 header.numFlowsPerRead = be_int2(*(unsigned short *)(&buffer7));
464 header.flogramFormatCode = (int)(buffer8[0]);
467 char* tempBuffer = new char[header.numFlowsPerRead];
468 in.read(&(*tempBuffer), header.numFlowsPerRead);
469 header.flowChars = tempBuffer;
470 if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead); }
474 char* tempBuffer2 = new char[header.keyLength];
475 in.read(&(*tempBuffer2), header.keyLength);
476 header.keySequence = tempBuffer2;
477 if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength); }
478 delete[] tempBuffer2;
481 unsigned long int spotInFile = in.tellg();
482 unsigned long int spot = (spotInFile + 7)& ~7; // ~ inverts
486 m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
491 catch(exception& e) {
492 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
496 //**********************************************************************************************************************
497 int SffInfoCommand::readHeader(ifstream& in, Header& header){
505 header.headerLength = be_int2(*(unsigned short *)(&buffer));
510 header.nameLength = be_int2(*(unsigned short *)(&buffer2));
515 header.numBases = be_int4(*(unsigned int *)(&buffer3));
517 //read clip qual left
520 header.clipQualLeft = be_int2(*(unsigned short *)(&buffer4));
521 header.clipQualLeft = 5;
523 //read clip qual right
526 header.clipQualRight = be_int2(*(unsigned short *)(&buffer5));
528 //read clipAdapterLeft
531 header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
533 //read clipAdapterRight
536 header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
539 char* tempBuffer = new char[header.nameLength];
540 in.read(&(*tempBuffer), header.nameLength);
541 header.name = tempBuffer;
542 if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength); }
545 //extract info from name
546 decodeName(header.timestamp, header.region, header.xy, header.name);
549 unsigned long int spotInFile = in.tellg();
550 unsigned long int spot = (spotInFile + 7)& ~7;
554 m->mothurOut("Error reading sff header info."); m->mothurOutEndLine();
559 catch(exception& e) {
560 m->errorOut(e, "SffInfoCommand", "readHeader");
564 //**********************************************************************************************************************
565 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, int numBases){
571 read.flowgram.resize(numFlowReads);
572 for (int i = 0; i < numFlowReads; i++) {
575 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
579 read.flowIndex.resize(numBases);
580 for (int i = 0; i < numBases; i++) {
583 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
587 char* tempBuffer = new char[numBases];
588 in.read(&(*tempBuffer), numBases);
589 read.bases = tempBuffer;
590 if (read.bases.length() > numBases) { read.bases = read.bases.substr(0, numBases); }
594 read.qualScores.resize(numBases);
595 for (int i = 0; i < numBases; i++) {
598 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
602 unsigned long int spotInFile = in.tellg();
603 unsigned long int spot = (spotInFile + 7)& ~7;
607 m->mothurOut("Error reading."); m->mothurOutEndLine();
612 catch(exception& e) {
613 m->errorOut(e, "SffInfoCommand", "readSeqData");
617 //**********************************************************************************************************************
618 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
621 if (name.length() >= 6) {
622 string time = name.substr(0, 6);
623 unsigned int timeNum = m->fromBase36(time);
625 int q1 = timeNum / 60;
626 int sec = timeNum - 60 * q1;
628 int minute = q1 - 60 * q2;
630 int hr = q2 - 24 * q3;
632 int day = q3 - 32 * q4;
634 int mon = q4 - 13 * q5;
635 int year = 2000 + q5;
637 timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
640 if (name.length() >= 9) {
641 region = name.substr(7, 2);
643 string xyNum = name.substr(9);
644 unsigned int myXy = m->fromBase36(xyNum);
648 xy = toString(x) + "_" + toString(y);
653 catch(exception& e) {
654 m->errorOut(e, "SffInfoCommand", "decodeName");
658 //**********************************************************************************************************************
659 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
662 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
663 out << "Version: " << header.version << endl;
664 out << "Index Offset: " << header.indexOffset << endl;
665 out << "Index Length: " << header.indexLength << endl;
666 out << "Number of Reads: " << header.numReads << endl;
667 out << "Header Length: " << header.headerLength << endl;
668 out << "Key Length: " << header.keyLength << endl;
669 out << "Number of Flows: " << header.numFlowsPerRead << endl;
670 out << "Format Code: " << header.flogramFormatCode << endl;
671 out << "Flow Chars: " << header.flowChars << endl;
672 out << "Key Sequence: " << header.keySequence << endl << endl;
676 catch(exception& e) {
677 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
681 //**********************************************************************************************************************
682 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
685 out << ">" << header.name << endl;
686 out << "Run Prefix: " << header.timestamp << endl;
687 out << "Region #: " << header.region << endl;
688 out << "XY Location: " << header.xy << endl << endl;
690 out << "Run Name: " << endl;
691 out << "Analysis Name: " << endl;
692 out << "Full Path: " << endl << endl;
694 out << "Read Header Len: " << header.headerLength << endl;
695 out << "Name Length: " << header.nameLength << endl;
696 out << "# of Bases: " << header.numBases << endl;
697 out << "Clip Qual Left: " << header.clipQualLeft << endl;
698 out << "Clip Qual Right: " << header.clipQualRight << endl;
699 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
700 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
704 catch(exception& e) {
705 m->errorOut(e, "SffInfoCommand", "printHeader");
710 //**********************************************************************************************************************
711 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
715 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
717 out << endl << "Flow Indexes: ";
719 for (int i = 0; i < read.flowIndex.size(); i++) { sum += read.flowIndex[i]; out << sum << '\t'; }
721 //make the bases you want to clip lowercase and the bases you want to keep upper case
722 if(header.clipQualRight == 0){ header.clipQualRight = read.bases.length(); }
723 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
724 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { read.bases[i] = toupper(read.bases[i]); }
725 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) { read.bases[i] = tolower(read.bases[i]); }
727 out << endl << "Bases: " << read.bases << endl << "Quality Scores: ";
728 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
735 catch(exception& e) {
736 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
740 //**********************************************************************************************************************
741 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
744 string seq = read.bases;
747 if(header.clipQualRight < header.clipQualLeft){
750 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
751 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
754 seq = seq.substr(header.clipQualLeft-1);
757 //if you wanted the sfftxt then you already converted the bases to the right case
759 //make the bases you want to clip lowercase and the bases you want to keep upper case
760 if(header.clipQualRight == 0){ header.clipQualRight = seq.length(); }
761 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
762 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { seq[i] = toupper(seq[i]); }
763 for (int i = (header.clipQualRight-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
767 out << ">" << header.name << " xy=" << header.xy << endl;
772 catch(exception& e) {
773 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
778 //**********************************************************************************************************************
779 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
783 if(header.clipQualRight < header.clipQualLeft){
784 out << ">" << header.name << " xy=" << header.xy << endl;
787 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
788 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
789 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { out << read.qualScores[i] << '\t'; }
792 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
793 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
796 out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
797 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
804 catch(exception& e) {
805 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
810 //**********************************************************************************************************************
811 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
813 if(header.clipQualRight > header.clipQualLeft){
816 for (int i = 0; i < header.clipQualRight; i++) { rightIndex += read.flowIndex[i]; }
818 out << header.name << ' ' << rightIndex;
819 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100); }
826 catch(exception& e) {
827 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
831 //**********************************************************************************************************************
832 int SffInfoCommand::readAccnosFile(string filename) {
838 m->openInputFile(filename, in);
842 in >> name; m->gobble(in);
844 seqNames.insert(name);
846 if (m->control_pressed) { seqNames.clear(); break; }
852 catch(exception& e) {
853 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
857 //**********************************************************************************************************************
858 int SffInfoCommand::parseSffTxt() {
862 m->openInputFile(sfftxtFilename, inSFF);
864 if (outputDir == "") { outputDir += m->hasPath(sfftxtFilename); }
867 ofstream outFasta, outQual, outFlow;
868 string outFastaFileName, outQualFileName;
869 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "flow";
871 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "fasta";
872 outQualFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "qual";
874 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "raw.fasta";
875 outQualFileName = outputDir + m->getRootName(m->getSimpleName(sfftxtFilename)) + "raw.qual";
878 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
879 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qual"].push_back(outQualFileName); }
880 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); }
883 string commonHeader = m->getline(inSFF);
884 string magicNumber = m->getline(inSFF);
885 string version = m->getline(inSFF);
886 string indexOffset = m->getline(inSFF);
887 string indexLength = m->getline(inSFF);
888 int numReads = parseHeaderLineToInt(inSFF);
889 string headerLength = m->getline(inSFF);
890 string keyLength = m->getline(inSFF);
891 int numFlows = parseHeaderLineToInt(inSFF);
892 string flowgramCode = m->getline(inSFF);
893 string flowChars = m->getline(inSFF);
894 string keySequence = m->getline(inSFF);
899 if (flow) { outFlow << numFlows << endl; }
901 for(int i=0;i<numReads;i++){
904 if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
910 seqName = seqName.substr(1);
912 header.name = seqName;
914 string runPrefix = parseHeaderLineToString(inSFF); header.timestamp = runPrefix;
915 string regionNumber = parseHeaderLineToString(inSFF); header.region = regionNumber;
916 string xyLocation = parseHeaderLineToString(inSFF); header.xy = xyLocation;
919 string runName = parseHeaderLineToString(inSFF);
920 string analysisName = parseHeaderLineToString(inSFF);
921 string fullPath = parseHeaderLineToString(inSFF);
924 string readHeaderLen = parseHeaderLineToString(inSFF); convert(readHeaderLen, header.headerLength);
925 string nameLength = parseHeaderLineToString(inSFF); convert(nameLength, header.nameLength);
926 int numBases = parseHeaderLineToInt(inSFF); header.numBases = numBases;
927 string clipQualLeft = parseHeaderLineToString(inSFF); convert(clipQualLeft, header.clipQualLeft);
928 int clipQualRight = parseHeaderLineToInt(inSFF); header.clipQualRight = clipQualRight;
929 string clipAdapLeft = parseHeaderLineToString(inSFF); convert(clipAdapLeft, header.clipAdapterLeft);
930 string clipAdapRight = parseHeaderLineToString(inSFF); convert(clipAdapRight, header.clipAdapterRight);
936 vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows); read.flowgram = flowVector;
937 vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases);
940 vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
941 for (int j = 1; j < flowIndices.size(); j++) { flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]); }
942 read.flowIndex = flowIndicesAdjusted;
944 string bases = parseHeaderLineToString(inSFF); read.bases = bases;
945 vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases); read.qualScores = qualityScores;
948 //if you have provided an accosfile and this seq is not in it, then dont print
950 if (seqNames.size() != 0) { if (seqNames.count(header.name) == 0) { print = false; } }
954 if (fasta) { printFastaSeqData(outFasta, read, header); }
955 if (qual) { printQualSeqData(outQual, read, header); }
956 if (flow) { printFlowSeqData(outFlow, read, header); }
960 if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine(); }
962 if (m->control_pressed) { break; }
966 if (!m->control_pressed) { if((numReads) % 10000 != 0){ m->mothurOut(toString(numReads)); m->mothurOutEndLine(); } }
970 if (fasta) { outFasta.close(); }
971 if (qual) { outQual.close(); }
972 if (flow) { outFlow.close(); }
976 catch(exception& e) {
977 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
981 //**********************************************************************************************************************
983 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
987 while (!file.eof()) {
999 catch(exception& e) {
1000 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
1006 //**********************************************************************************************************************
1008 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
1012 while (!file.eof()) {
1013 char c = file.get();
1017 //text = m->getline(file);
1026 catch(exception& e) {
1027 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
1032 //**********************************************************************************************************************
1034 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
1036 vector<unsigned short> floatVector(length);
1038 while (!file.eof()) {
1039 char c = file.get();
1042 for(int i=0;i<length;i++){
1044 floatVector[i] = temp * 100;
1052 catch(exception& e) {
1053 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
1058 //**********************************************************************************************************************
1060 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
1062 vector<unsigned int> intVector(length);
1064 while (!file.eof()) {
1065 char c = file.get();
1067 for(int i=0;i<length;i++){
1068 file >> intVector[i];
1076 catch(exception& e) {
1077 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
1082 //**********************************************************************************************************************