5 * Created by westcott on 7/7/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "sffinfocommand.h"
11 #include "endiannessmacros.h"
12 #include "trimoligos.h"
13 #include "sequence.hpp"
14 #include "qualityscores.h"
16 //**********************************************************************************************************************
17 vector<string> SffInfoCommand::setParameters(){
19 CommandParameter psff("sff", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(psff);
20 CommandParameter poligos("oligos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(poligos);
21 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
22 CommandParameter psfftxt("sfftxt", "String", "", "", "", "", "",false,false); parameters.push_back(psfftxt);
23 CommandParameter pflow("flow", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pflow);
24 CommandParameter ptrim("trim", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(ptrim);
25 CommandParameter pfasta("fasta", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pfasta);
26 CommandParameter pqfile("name", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pqfile);
27 CommandParameter ppdiffs("pdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(ppdiffs);
28 CommandParameter pbdiffs("bdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pbdiffs);
29 CommandParameter pldiffs("ldiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pldiffs);
30 CommandParameter psdiffs("sdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(psdiffs);
31 CommandParameter ptdiffs("tdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(ptdiffs);
32 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
33 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
35 vector<string> myArray;
36 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
40 m->errorOut(e, "SffInfoCommand", "setParameters");
44 //**********************************************************************************************************************
45 string SffInfoCommand::getHelpString(){
47 string helpString = "";
48 helpString += "The sffinfo command reads a sff file and extracts the sequence data, or you can use it to parse a sfftxt file.\n";
49 helpString += "The sffinfo command parameters are sff, fasta, qfile, accnos, flow, sfftxt, oligos, bdiffs, tdiffs, ldiffs, sdiffs, pdiffs and trim. sff is required. \n";
50 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";
51 helpString += "The fasta parameter allows you to indicate if you would like a fasta formatted file generated. Default=True. \n";
52 helpString += "The qfile parameter allows you to indicate if you would like a quality file generated. Default=True. \n";
53 helpString += "The oligos parameter allows you to provide an oligos file to split your sff file into separate sff files by barcode. \n";
54 helpString += "The tdiffs parameter is used to specify the total number of differences allowed in the sequence. The default is pdiffs + bdiffs + sdiffs + ldiffs.\n";
55 helpString += "The bdiffs parameter is used to specify the number of differences allowed in the barcode. The default is 0.\n";
56 helpString += "The pdiffs parameter is used to specify the number of differences allowed in the primer. The default is 0.\n";
57 helpString += "The ldiffs parameter is used to specify the number of differences allowed in the linker. The default is 0.\n";
58 helpString += "The sdiffs parameter is used to specify the number of differences allowed in the spacer. The default is 0.\n";
59 helpString += "The flow parameter allows you to indicate if you would like a flowgram file generated. Default=True. \n";
60 helpString += "The sfftxt parameter allows you to indicate if you would like a sff.txt file generated. Default=False. \n";
61 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";
62 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";
63 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";
64 helpString += "Example sffinfo(sff=mySffFile.sff, trim=F).\n";
65 helpString += "Note: No spaces between parameter labels (i.e. sff), '=' and parameters (i.e.yourSffFileName).\n";
69 m->errorOut(e, "SffInfoCommand", "getHelpString");
73 //**********************************************************************************************************************
74 string SffInfoCommand::getOutputFileNameTag(string type, string inputName=""){
76 string outputFileName = "";
77 map<string, vector<string> >::iterator it;
79 //is this a type this command creates
80 it = outputTypes.find(type);
81 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
83 if (type == "fasta") { outputFileName = "fasta"; }
84 else if (type == "flow") { outputFileName = "flow"; }
85 else if (type == "sfftxt") { outputFileName = "sff.txt"; }
86 else if (type == "sff") { outputFileName = "sff"; }
87 else if (type == "qfile") { outputFileName = "qual"; }
88 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
90 return outputFileName;
93 m->errorOut(e, "SffInfoCommand", "getOutputFileNameTag");
99 //**********************************************************************************************************************
100 SffInfoCommand::SffInfoCommand(){
102 abort = true; calledHelp = true;
104 vector<string> tempOutNames;
105 outputTypes["fasta"] = tempOutNames;
106 outputTypes["flow"] = tempOutNames;
107 outputTypes["sfftxt"] = tempOutNames;
108 outputTypes["qfile"] = tempOutNames;
109 outputTypes["sff"] = tempOutNames;
111 catch(exception& e) {
112 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
116 //**********************************************************************************************************************
118 SffInfoCommand::SffInfoCommand(string option) {
120 abort = false; calledHelp = false;
121 hasAccnos = false; hasOligos = false;
124 //allow user to run help
125 if(option == "help") { help(); abort = true; calledHelp = true; }
126 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
129 //valid paramters for this command
130 vector<string> myArray = setParameters();
132 OptionParser parser(option);
133 map<string, string> parameters = parser.getParameters();
135 ValidParameters validParameter;
136 //check to make sure all parameters are valid for command
137 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
138 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
141 //initialize outputTypes
142 vector<string> tempOutNames;
143 outputTypes["fasta"] = tempOutNames;
144 outputTypes["flow"] = tempOutNames;
145 outputTypes["sfftxt"] = tempOutNames;
146 outputTypes["qfile"] = tempOutNames;
147 outputTypes["sff"] = tempOutNames;
149 //if the user changes the output directory command factory will send this info to us in the output parameter
150 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
152 //if the user changes the input directory command factory will send this info to us in the output parameter
153 string inputDir = validParameter.validFile(parameters, "inputdir", false); if (inputDir == "not found"){ inputDir = ""; }
155 sffFilename = validParameter.validFile(parameters, "sff", false);
156 if (sffFilename == "not found") { sffFilename = ""; }
158 m->splitAtDash(sffFilename, filenames);
160 //go through files and make sure they are good, if not, then disregard them
161 for (int i = 0; i < filenames.size(); i++) {
163 if (filenames[i] == "current") {
164 filenames[i] = m->getSFFFile();
165 if (filenames[i] != "") { m->mothurOut("Using " + filenames[i] + " as input file for the sff parameter where you had given current."); m->mothurOutEndLine(); }
167 m->mothurOut("You have no current sfffile, ignoring current."); m->mothurOutEndLine(); ignore=true;
168 //erase from file list
169 filenames.erase(filenames.begin()+i);
175 if (inputDir != "") {
176 string path = m->hasPath(filenames[i]);
177 //if the user has not given a path then, add inputdir. else leave path alone.
178 if (path == "") { filenames[i] = inputDir + filenames[i]; }
182 int ableToOpen = m->openInputFile(filenames[i], in, "noerror");
184 //if you can't open it, try default location
185 if (ableToOpen == 1) {
186 if (m->getDefaultPath() != "") { //default path is set
187 string tryPath = m->getDefaultPath() + m->getSimpleName(filenames[i]);
188 m->mothurOut("Unable to open " + filenames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
190 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
192 filenames[i] = tryPath;
196 //if you can't open it, try default location
197 if (ableToOpen == 1) {
198 if (m->getOutputDir() != "") { //default path is set
199 string tryPath = m->getOutputDir() + m->getSimpleName(filenames[i]);
200 m->mothurOut("Unable to open " + filenames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
202 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
204 filenames[i] = tryPath;
210 if (ableToOpen == 1) {
211 m->mothurOut("Unable to open " + filenames[i] + ". It will be disregarded."); m->mothurOutEndLine();
212 //erase from file list
213 filenames.erase(filenames.begin()+i);
215 }else { m->setSFFFile(filenames[i]); }
219 //make sure there is at least one valid file left
220 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
223 accnosName = validParameter.validFile(parameters, "accnos", false);
224 if (accnosName == "not found") { accnosName = ""; }
227 m->splitAtDash(accnosName, accnosFileNames);
229 //go through files and make sure they are good, if not, then disregard them
230 for (int i = 0; i < accnosFileNames.size(); i++) {
232 if (accnosFileNames[i] == "current") {
233 accnosFileNames[i] = m->getAccnosFile();
234 if (accnosFileNames[i] != "") { m->mothurOut("Using " + accnosFileNames[i] + " as input file for the accnos parameter where you had given current."); m->mothurOutEndLine(); }
236 m->mothurOut("You have no current accnosfile, ignoring current."); m->mothurOutEndLine(); ignore=true;
237 //erase from file list
238 accnosFileNames.erase(accnosFileNames.begin()+i);
245 if (inputDir != "") {
246 string path = m->hasPath(accnosFileNames[i]);
247 //if the user has not given a path then, add inputdir. else leave path alone.
248 if (path == "") { accnosFileNames[i] = inputDir + accnosFileNames[i]; }
252 int ableToOpen = m->openInputFile(accnosFileNames[i], in, "noerror");
254 //if you can't open it, try default location
255 if (ableToOpen == 1) {
256 if (m->getDefaultPath() != "") { //default path is set
257 string tryPath = m->getDefaultPath() + m->getSimpleName(accnosFileNames[i]);
258 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
260 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
262 accnosFileNames[i] = tryPath;
265 //if you can't open it, try default location
266 if (ableToOpen == 1) {
267 if (m->getOutputDir() != "") { //default path is set
268 string tryPath = m->getOutputDir() + m->getSimpleName(accnosFileNames[i]);
269 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
271 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
273 accnosFileNames[i] = tryPath;
278 if (ableToOpen == 1) {
279 m->mothurOut("Unable to open " + accnosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
280 //erase from file list
281 accnosFileNames.erase(accnosFileNames.begin()+i);
287 //make sure there is at least one valid file left
288 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
291 oligosfile = validParameter.validFile(parameters, "oligos", false);
292 if (oligosfile == "not found") { oligosfile = ""; }
295 m->splitAtDash(oligosfile, oligosFileNames);
297 //go through files and make sure they are good, if not, then disregard them
298 for (int i = 0; i < oligosFileNames.size(); i++) {
300 if (oligosFileNames[i] == "current") {
301 oligosFileNames[i] = m->getOligosFile();
302 if (oligosFileNames[i] != "") { m->mothurOut("Using " + oligosFileNames[i] + " as input file for the accnos parameter where you had given current."); m->mothurOutEndLine(); }
304 m->mothurOut("You have no current oligosfile, ignoring current."); m->mothurOutEndLine(); ignore=true;
305 //erase from file list
306 oligosFileNames.erase(oligosFileNames.begin()+i);
313 if (inputDir != "") {
314 string path = m->hasPath(oligosFileNames[i]);
315 //if the user has not given a path then, add inputdir. else leave path alone.
316 if (path == "") { oligosFileNames[i] = inputDir + oligosFileNames[i]; }
320 int ableToOpen = m->openInputFile(oligosFileNames[i], in, "noerror");
322 //if you can't open it, try default location
323 if (ableToOpen == 1) {
324 if (m->getDefaultPath() != "") { //default path is set
325 string tryPath = m->getDefaultPath() + m->getSimpleName(oligosFileNames[i]);
326 m->mothurOut("Unable to open " + oligosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
328 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
330 oligosFileNames[i] = tryPath;
333 //if you can't open it, try default location
334 if (ableToOpen == 1) {
335 if (m->getOutputDir() != "") { //default path is set
336 string tryPath = m->getOutputDir() + m->getSimpleName(oligosFileNames[i]);
337 m->mothurOut("Unable to open " + oligosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
339 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
341 oligosFileNames[i] = tryPath;
346 if (ableToOpen == 1) {
347 m->mothurOut("Unable to open " + oligosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
348 //erase from file list
349 oligosFileNames.erase(oligosFileNames.begin()+i);
355 //make sure there is at least one valid file left
356 if (oligosFileNames.size() == 0) { m->mothurOut("no valid oligos files."); m->mothurOutEndLine(); abort = true; }
361 if (oligosFileNames.size() != filenames.size()) { abort = true; m->mothurOut("If you provide a oligos file, you must have one for each sff file."); m->mothurOutEndLine(); }
365 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(); }
368 string temp = validParameter.validFile(parameters, "qfile", false); if (temp == "not found"){ temp = "T"; }
369 qual = m->isTrue(temp);
371 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ temp = "T"; }
372 fasta = m->isTrue(temp);
374 temp = validParameter.validFile(parameters, "flow", false); if (temp == "not found"){ temp = "T"; }
375 flow = m->isTrue(temp);
377 temp = validParameter.validFile(parameters, "trim", false); if (temp == "not found"){ temp = "T"; }
378 trim = m->isTrue(temp);
380 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found") { temp = "0"; }
381 m->mothurConvert(temp, bdiffs);
383 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found") { temp = "0"; }
384 m->mothurConvert(temp, pdiffs);
386 temp = validParameter.validFile(parameters, "ldiffs", false); if (temp == "not found") { temp = "0"; }
387 m->mothurConvert(temp, ldiffs);
389 temp = validParameter.validFile(parameters, "sdiffs", false); if (temp == "not found") { temp = "0"; }
390 m->mothurConvert(temp, sdiffs);
392 temp = validParameter.validFile(parameters, "tdiffs", false); if (temp == "not found") { int tempTotal = pdiffs + bdiffs + ldiffs + sdiffs; temp = toString(tempTotal); }
393 m->mothurConvert(temp, tdiffs);
395 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs + ldiffs + sdiffs; }
397 temp = validParameter.validFile(parameters, "sfftxt", false);
398 if (temp == "not found") { temp = "F"; sfftxt = false; sfftxtFilename = ""; }
399 else if (m->isTrue(temp)) { sfftxt = true; sfftxtFilename = ""; }
402 if (inputDir != "") {
403 map<string,string>::iterator it = parameters.find("sfftxt");
404 //user has given a template file
405 if(it != parameters.end()){
406 string path = m->hasPath(it->second);
407 //if the user has not given a path then, add inputdir. else leave path alone.
408 if (path == "") { parameters["sfftxt"] = inputDir + it->second; }
412 sfftxtFilename = validParameter.validFile(parameters, "sfftxt", true);
413 if (sfftxtFilename == "not found") { sfftxtFilename = ""; }
414 else if (sfftxtFilename == "not open") { sfftxtFilename = ""; }
417 if ((sfftxtFilename == "") && (filenames.size() == 0)) {
418 //if there is a current sff file, use it
419 string filename = m->getSFFFile();
420 if (filename != "") { filenames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the sff parameter."); m->mothurOutEndLine(); }
421 else { m->mothurOut("[ERROR]: you must provide a valid sff or sfftxt file."); m->mothurOutEndLine(); abort=true; }
427 catch(exception& e) {
428 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
432 //**********************************************************************************************************************
433 int SffInfoCommand::execute(){
435 if (abort == true) { if (calledHelp) { return 0; } return 2; }
437 for (int s = 0; s < filenames.size(); s++) {
439 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
441 int start = time(NULL);
443 filenames[s] = m->getFullPathName(filenames[s]);
444 m->mothurOut("Extracting info from " + filenames[s] + " ..." ); m->mothurOutEndLine();
447 if (hasAccnos) { accnos = accnosFileNames[s]; }
450 if (hasOligos) { oligos = oligosFileNames[s]; }
452 int numReads = extractSffInfo(filenames[s], accnos, oligos);
454 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to extract " + toString(numReads) + ".");
457 if (sfftxtFilename != "") { parseSffTxt(); }
459 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
461 //set fasta file as new current fastafile
463 itTypes = outputTypes.find("fasta");
464 if (itTypes != outputTypes.end()) {
465 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
468 itTypes = outputTypes.find("qfile");
469 if (itTypes != outputTypes.end()) {
470 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
473 itTypes = outputTypes.find("flow");
474 if (itTypes != outputTypes.end()) {
475 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFlowFile(current); }
478 //report output filenames
479 m->mothurOutEndLine();
480 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
481 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
482 m->mothurOutEndLine();
486 catch(exception& e) {
487 m->errorOut(e, "SffInfoCommand", "execute");
491 //**********************************************************************************************************************
492 int SffInfoCommand::extractSffInfo(string input, string accnos, string oligos){
494 currentFileName = input;
495 if (outputDir == "") { outputDir += m->hasPath(input); }
497 if (accnos != "") { readAccnosFile(accnos); }
498 else { seqNames.clear(); }
500 if (oligos != "") { readOligos(oligos); split = 2; }
502 ofstream outSfftxt, outFasta, outQual, outFlow;
503 string outFastaFileName, outQualFileName;
504 string rootName = outputDir + m->getRootName(m->getSimpleName(input));
505 if(rootName.find_last_of(".") == rootName.npos){ rootName += "."; }
507 string sfftxtFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("sfftxt");
508 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("flow");
510 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("fasta");
511 outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("qfile");
513 outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw." + getOutputFileNameTag("fasta");
514 outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw." + getOutputFileNameTag("qfile");
517 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); }
518 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
519 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName); }
520 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); }
523 in.open(input.c_str(), ios::binary);
526 readCommonHeader(in, header);
531 //check magic number and version
532 if (header.magicNumber != 779314790) { m->mothurOut("Magic Number is not correct, not a valid .sff file"); m->mothurOutEndLine(); return count; }
533 if (header.version != "0001") { m->mothurOut("Version is not supported, only support version 0001."); m->mothurOutEndLine(); return count; }
535 //print common header
536 if (sfftxt) { printCommonHeader(outSfftxt, header); }
537 if (flow) { outFlow << header.numFlowsPerRead << endl; }
539 //read through the sff file
545 seqRead read; Header readheader;
546 readSeqData(in, read, header.numFlowsPerRead, readheader);
547 bool okay = sanityCheck(readheader, read);
548 if (!okay) { break; }
550 //if you have provided an accosfile and this seq is not in it, then dont print
551 if (seqNames.size() != 0) { if (seqNames.count(readheader.name) == 0) { print = false; } }
555 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read, readheader); }
556 if (fasta) { printFastaSeqData(outFasta, read, readheader); }
557 if (qual) { printQualSeqData(outQual, read, readheader); }
558 if (flow) { printFlowSeqData(outFlow, read, readheader); }
565 if((count+1) % 10000 == 0){ m->mothurOut(toString(count+1)); m->mothurOutEndLine(); }
567 if (m->control_pressed) { count = 0; break; }
569 if (count >= header.numReads) { break; }
573 if (!m->control_pressed) { if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } }
577 if (sfftxt) { outSfftxt.close(); }
578 if (fasta) { outFasta.close(); }
579 if (qual) { outQual.close(); }
580 if (flow) { outFlow.close(); }
583 //create new common headers for each file with the correct number of reads
584 adjustCommonHeader(header);
586 map<string, string>::iterator it;
587 set<string> namesToRemove;
588 for(int i=0;i<filehandles.size();i++){
589 for(int j=0;j<filehandles[0].size();j++){
590 if (filehandles[i][j] != "") {
591 if (namesToRemove.count(filehandles[i][j]) == 0) {
592 if(m->isBlank(filehandles[i][j])){
593 m->mothurRemove(filehandles[i][j]);
594 m->mothurRemove(filehandlesHeaders[i][j]);
595 namesToRemove.insert(filehandles[i][j]);
602 //append new header to reads
603 for (int i = 0; i < filehandles.size(); i++) {
604 for (int j = 0; j < filehandles[i].size(); j++) {
605 m->appendFiles(filehandles[i][j], filehandlesHeaders[i][j]);
606 m->renameFile(filehandlesHeaders[i][j], filehandles[i][j]);
607 m->mothurRemove(filehandlesHeaders[i][j]);
608 if (numSplitReads[i][j] == 0) { m->mothurRemove(filehandles[i][j]); }
612 //remove names for outputFileNames, just cleans up the output
613 for(int i = 0; i < outputNames.size(); i++) {
614 if (namesToRemove.count(outputNames[i]) != 0) {
615 outputNames.erase(outputNames.begin()+i);
620 if(m->isBlank(noMatchFile)){ m->mothurRemove(noMatchFile); }
621 else { outputNames.push_back(noMatchFile); outputTypes["sff"].push_back(noMatchFile); }
626 catch(exception& e) {
627 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
631 //**********************************************************************************************************************
632 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
640 header.magicNumber = be_int4(*(unsigned int *)(&buffer));
646 for (int i = 0; i < 4; i++) { header.version += toString((int)(buffer9[i])); }
651 header.indexOffset = be_int8(*(unsigned long long *)(&buffer2));
656 header.indexLength = be_int4(*(unsigned int *)(&buffer3));
661 header.numReads = be_int4(*(unsigned int *)(&buffer4));
666 header.headerLength = be_int2(*(unsigned short *)(&buffer5));
671 header.keyLength = be_int2(*(unsigned short *)(&buffer6));
673 //read number of flow reads
676 header.numFlowsPerRead = be_int2(*(unsigned short *)(&buffer7));
681 header.flogramFormatCode = (int)(buffer8[0]);
684 char* tempBuffer = new char[header.numFlowsPerRead];
685 in.read(&(*tempBuffer), header.numFlowsPerRead);
686 header.flowChars = tempBuffer;
687 if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead); }
691 char* tempBuffer2 = new char[header.keyLength];
692 in.read(&(*tempBuffer2), header.keyLength);
693 header.keySequence = tempBuffer2;
694 if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength); }
695 delete[] tempBuffer2;
698 unsigned long long spotInFile = in.tellg();
699 unsigned long long spot = (spotInFile + 7)& ~7; // ~ inverts
703 m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
709 catch(exception& e) {
710 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
714 //**********************************************************************************************************************
715 int SffInfoCommand::adjustCommonHeader(CommonHeader header){
718 char* mybuffer = new char[4];
720 in.open(currentFileName.c_str(), ios::binary);
724 for (int i = 0; i < filehandlesHeaders.size(); i++) {
725 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
727 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
728 out.write(mybuffer, in.gcount());
735 mybuffer = new char[4];
737 for (int i = 0; i < filehandlesHeaders.size(); i++) {
738 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
740 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
741 out.write(mybuffer, in.gcount());
748 mybuffer = new char[8];
750 for (int i = 0; i < filehandlesHeaders.size(); i++) {
751 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
753 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
754 out.write(mybuffer, in.gcount());
762 mybuffer = new char[4];
764 for (int i = 0; i < filehandlesHeaders.size(); i++) {
765 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
767 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
768 out.write(mybuffer, in.gcount());
775 mybuffer = new char[4];
778 for (int i = 0; i < filehandlesHeaders.size(); i++) {
779 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
781 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
782 //convert number of reads to 4 byte char*
783 char* thisbuffer = new char[4];
784 thisbuffer[0] = (numSplitReads[i][j] >> 24) & 0xFF;
785 thisbuffer[1] = (numSplitReads[i][j] >> 16) & 0xFF;
786 thisbuffer[2] = (numSplitReads[i][j] >> 8) & 0xFF;
787 thisbuffer[3] = numSplitReads[i][j] & 0xFF;
788 out.write(thisbuffer, 4);
795 mybuffer = new char[2];
797 for (int i = 0; i < filehandlesHeaders.size(); i++) {
798 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
800 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
801 out.write(mybuffer, in.gcount());
808 mybuffer = new char[2];
810 for (int i = 0; i < filehandlesHeaders.size(); i++) {
811 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
813 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
814 out.write(mybuffer, in.gcount());
820 //read number of flow reads
821 mybuffer = new char[2];
823 for (int i = 0; i < filehandlesHeaders.size(); i++) {
824 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
826 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
827 out.write(mybuffer, in.gcount());
834 mybuffer = new char[1];
836 for (int i = 0; i < filehandlesHeaders.size(); i++) {
837 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
839 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
840 out.write(mybuffer, in.gcount());
847 mybuffer = new char[header.numFlowsPerRead];
848 in.read(mybuffer,header.numFlowsPerRead);
849 for (int i = 0; i < filehandlesHeaders.size(); i++) {
850 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
852 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
853 out.write(mybuffer, in.gcount());
860 mybuffer = new char[header.keyLength];
861 in.read(mybuffer,header.keyLength);
862 for (int i = 0; i < filehandlesHeaders.size(); i++) {
863 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
865 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
866 out.write(mybuffer, in.gcount());
874 unsigned long long spotInFile = in.tellg();
875 unsigned long long spot = (spotInFile + 7)& ~7; // ~ inverts
878 mybuffer = new char[spot-spotInFile];
879 for (int i = 0; i < filehandlesHeaders.size(); i++) {
880 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
882 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
883 out.write(mybuffer, spot-spotInFile);
892 catch(exception& e) {
893 m->errorOut(e, "SffInfoCommand", "adjustCommonHeader");
897 //**********************************************************************************************************************
898 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, Header& header){
900 unsigned long long startSpotInFile = in.tellg();
903 /*****************************************/
909 header.headerLength = be_int2(*(unsigned short *)(&buffer));
914 header.nameLength = be_int2(*(unsigned short *)(&buffer2));
919 header.numBases = be_int4(*(unsigned int *)(&buffer3));
921 //read clip qual left
924 header.clipQualLeft = be_int2(*(unsigned short *)(&buffer4));
925 header.clipQualLeft = 5;
927 //read clip qual right
930 header.clipQualRight = be_int2(*(unsigned short *)(&buffer5));
932 //read clipAdapterLeft
935 header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
937 //read clipAdapterRight
940 header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
943 char* tempBuffer = new char[header.nameLength];
944 in.read(&(*tempBuffer), header.nameLength);
945 header.name = tempBuffer;
946 if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength); }
949 //extract info from name
950 decodeName(header.timestamp, header.region, header.xy, header.name);
953 unsigned long long spotInFile = in.tellg();
954 unsigned long long spot = (spotInFile + 7)& ~7;
957 /*****************************************/
961 read.flowgram.resize(numFlowReads);
962 for (int i = 0; i < numFlowReads; i++) {
965 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
969 read.flowIndex.resize(header.numBases);
970 for (int i = 0; i < header.numBases; i++) {
973 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
977 char* tempBuffer6 = new char[header.numBases];
978 in.read(&(*tempBuffer6), header.numBases);
979 read.bases = tempBuffer6;
980 if (read.bases.length() > header.numBases) { read.bases = read.bases.substr(0, header.numBases); }
981 delete[] tempBuffer6;
984 read.qualScores.resize(header.numBases);
985 for (int i = 0; i < header.numBases; i++) {
988 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
992 spotInFile = in.tellg();
993 spot = (spotInFile + 7)& ~7;
998 mybuffer = new char [spot-startSpotInFile];
1000 m->openInputFile(currentFileName, in2);
1001 in2.seekg(startSpotInFile);
1002 in2.read(mybuffer,spot-startSpotInFile);
1005 int barcodeIndex, primerIndex;
1006 int trashCodeLength = findGroup(header, read, barcodeIndex, primerIndex);
1008 if(trashCodeLength == 0){
1010 m->openOutputFileAppend(filehandles[barcodeIndex][primerIndex], out);
1011 out.write(mybuffer, in2.gcount());
1014 numSplitReads[barcodeIndex][primerIndex]++;
1018 m->openOutputFileAppend(noMatchFile, out);
1019 out.write(mybuffer, in2.gcount());
1026 m->mothurOut("Error reading."); m->mothurOutEndLine();
1031 catch(exception& e) {
1032 m->errorOut(e, "SffInfoCommand", "readSeqData");
1036 //**********************************************************************************************************************
1037 int SffInfoCommand::findGroup(Header header, seqRead read, int& barcode, int& primer) {
1039 //find group read belongs to
1040 TrimOligos trimOligos(pdiffs, bdiffs, ldiffs, sdiffs, primers, barcodes, revPrimer, linker, spacer);
1043 string trashCode = "";
1044 int currentSeqsDiffs = 0;
1046 string seq = read.bases;
1049 if(header.clipQualRight < header.clipQualLeft){
1052 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1053 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
1056 seq = seq.substr(header.clipQualLeft-1);
1059 //if you wanted the sfftxt then you already converted the bases to the right case
1061 //make the bases you want to clip lowercase and the bases you want to keep upper case
1062 if(header.clipQualRight == 0){ header.clipQualRight = seq.length(); }
1063 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
1064 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { seq[i] = toupper(seq[i]); }
1065 for (int i = (header.clipQualRight-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
1069 Sequence currSeq(header.name, seq);
1070 QualityScores currQual;
1072 if(numLinkers != 0){
1073 success = trimOligos.stripLinker(currSeq, currQual);
1074 if(success > ldiffs) { trashCode += 'k'; }
1075 else{ currentSeqsDiffs += success; }
1079 if(barcodes.size() != 0){
1080 success = trimOligos.stripBarcode(currSeq, currQual, barcode);
1081 if(success > bdiffs) { trashCode += 'b'; }
1082 else{ currentSeqsDiffs += success; }
1085 if(numSpacers != 0){
1086 success = trimOligos.stripSpacer(currSeq, currQual);
1087 if(success > sdiffs) { trashCode += 's'; }
1088 else{ currentSeqsDiffs += success; }
1092 if(numFPrimers != 0){
1093 success = trimOligos.stripForward(currSeq, currQual, primer, true);
1094 if(success > pdiffs) { trashCode += 'f'; }
1095 else{ currentSeqsDiffs += success; }
1098 if (currentSeqsDiffs > tdiffs) { trashCode += 't'; }
1100 if(revPrimer.size() != 0){
1101 success = trimOligos.stripReverse(currSeq, currQual);
1102 if(!success) { trashCode += 'r'; }
1106 return trashCode.length();
1108 catch(exception& e) {
1109 m->errorOut(e, "SffInfoCommand", "findGroup");
1113 //**********************************************************************************************************************
1114 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
1117 if (name.length() >= 6) {
1118 string time = name.substr(0, 6);
1119 unsigned int timeNum = m->fromBase36(time);
1121 int q1 = timeNum / 60;
1122 int sec = timeNum - 60 * q1;
1124 int minute = q1 - 60 * q2;
1126 int hr = q2 - 24 * q3;
1128 int day = q3 - 32 * q4;
1130 int mon = q4 - 13 * q5;
1131 int year = 2000 + q5;
1133 timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
1136 if (name.length() >= 9) {
1137 region = name.substr(7, 2);
1139 string xyNum = name.substr(9);
1140 unsigned int myXy = m->fromBase36(xyNum);
1142 int y = myXy & 4095;
1144 xy = toString(x) + "_" + toString(y);
1149 catch(exception& e) {
1150 m->errorOut(e, "SffInfoCommand", "decodeName");
1154 //**********************************************************************************************************************
1155 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
1158 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
1159 out << "Version: " << header.version << endl;
1160 out << "Index Offset: " << header.indexOffset << endl;
1161 out << "Index Length: " << header.indexLength << endl;
1162 out << "Number of Reads: " << header.numReads << endl;
1163 out << "Header Length: " << header.headerLength << endl;
1164 out << "Key Length: " << header.keyLength << endl;
1165 out << "Number of Flows: " << header.numFlowsPerRead << endl;
1166 out << "Format Code: " << header.flogramFormatCode << endl;
1167 out << "Flow Chars: " << header.flowChars << endl;
1168 out << "Key Sequence: " << header.keySequence << endl << endl;
1172 catch(exception& e) {
1173 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
1177 //**********************************************************************************************************************
1178 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
1181 out << ">" << header.name << endl;
1182 out << "Run Prefix: " << header.timestamp << endl;
1183 out << "Region #: " << header.region << endl;
1184 out << "XY Location: " << header.xy << endl << endl;
1186 out << "Run Name: " << endl;
1187 out << "Analysis Name: " << endl;
1188 out << "Full Path: " << endl << endl;
1190 out << "Read Header Len: " << header.headerLength << endl;
1191 out << "Name Length: " << header.nameLength << endl;
1192 out << "# of Bases: " << header.numBases << endl;
1193 out << "Clip Qual Left: " << header.clipQualLeft << endl;
1194 out << "Clip Qual Right: " << header.clipQualRight << endl;
1195 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
1196 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
1200 catch(exception& e) {
1201 m->errorOut(e, "SffInfoCommand", "printHeader");
1205 //**********************************************************************************************************************
1206 bool SffInfoCommand::sanityCheck(Header& header, seqRead& read) {
1209 string message = "[WARNING]: Your sff file may be corrupted! Sequence: " + header.name + "\n";
1211 if (header.clipQualLeft > read.bases.length()) {
1212 okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
1214 if (header.clipQualRight > read.bases.length()) {
1215 okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
1217 if (header.clipQualLeft > read.qualScores.size()) {
1218 okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
1220 if (header.clipQualRight > read.qualScores.size()) {
1221 okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
1224 if (okay == false) {
1225 m->mothurOut(message); m->mothurOutEndLine();
1230 catch(exception& e) {
1231 m->errorOut(e, "SffInfoCommand", "sanityCheck");
1235 //**********************************************************************************************************************
1236 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
1238 out << "Flowgram: ";
1239 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
1241 out << endl << "Flow Indexes: ";
1243 for (int i = 0; i < read.flowIndex.size(); i++) { sum += read.flowIndex[i]; out << sum << '\t'; }
1245 //make the bases you want to clip lowercase and the bases you want to keep upper case
1246 if(header.clipQualRight == 0){ header.clipQualRight = read.bases.length(); }
1247 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
1248 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { read.bases[i] = toupper(read.bases[i]); }
1249 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) { read.bases[i] = tolower(read.bases[i]); }
1251 out << endl << "Bases: " << read.bases << endl << "Quality Scores: ";
1252 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
1255 out << endl << endl;
1259 catch(exception& e) {
1260 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
1264 //**********************************************************************************************************************
1265 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
1267 string seq = read.bases;
1270 if(header.clipQualRight < header.clipQualLeft){
1273 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1274 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
1277 seq = seq.substr(header.clipQualLeft-1);
1280 //if you wanted the sfftxt then you already converted the bases to the right case
1282 //make the bases you want to clip lowercase and the bases you want to keep upper case
1283 if(header.clipQualRight == 0){ header.clipQualRight = seq.length(); }
1284 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
1285 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { seq[i] = toupper(seq[i]); }
1286 for (int i = (header.clipQualRight-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
1290 out << ">" << header.name << " xy=" << header.xy << endl;
1295 catch(exception& e) {
1296 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
1301 //**********************************************************************************************************************
1302 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
1306 if(header.clipQualRight < header.clipQualLeft){
1307 out << ">" << header.name << " xy=" << header.xy << endl;
1308 out << "0\t0\t0\t0";
1310 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1311 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
1312 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { out << read.qualScores[i] << '\t'; }
1315 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
1316 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
1319 out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
1320 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
1327 catch(exception& e) {
1328 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
1333 //**********************************************************************************************************************
1334 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
1336 if(header.clipQualRight > header.clipQualLeft){
1339 for (int i = 0; i < header.clipQualRight; i++) { rightIndex += read.flowIndex[i]; }
1341 out << header.name << ' ' << rightIndex;
1342 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100); }
1349 catch(exception& e) {
1350 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
1354 //**********************************************************************************************************************
1355 int SffInfoCommand::readAccnosFile(string filename) {
1361 m->openInputFile(filename, in);
1365 in >> name; m->gobble(in);
1367 seqNames.insert(name);
1369 if (m->control_pressed) { seqNames.clear(); break; }
1375 catch(exception& e) {
1376 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
1380 //**********************************************************************************************************************
1381 int SffInfoCommand::parseSffTxt() {
1385 m->openInputFile(sfftxtFilename, inSFF);
1387 if (outputDir == "") { outputDir += m->hasPath(sfftxtFilename); }
1390 ofstream outFasta, outQual, outFlow;
1391 string outFastaFileName, outQualFileName;
1392 string fileRoot = m->getRootName(m->getSimpleName(sfftxtFilename));
1393 if (fileRoot.length() > 0) {
1395 fileRoot = fileRoot.substr(0, fileRoot.length()-1);
1396 fileRoot = m->getRootName(fileRoot);
1399 string outFlowFileName = outputDir + fileRoot + getOutputFileNameTag("flow");
1401 outFastaFileName = outputDir + fileRoot + getOutputFileNameTag("fasta");
1402 outQualFileName = outputDir + fileRoot + getOutputFileNameTag("qfile");
1404 outFastaFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("fasta");
1405 outQualFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("qfile");
1408 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
1409 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName); }
1410 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); }
1412 //read common header
1413 string commonHeader = m->getline(inSFF);
1414 string magicNumber = m->getline(inSFF);
1415 string version = m->getline(inSFF);
1416 string indexOffset = m->getline(inSFF);
1417 string indexLength = m->getline(inSFF);
1418 int numReads = parseHeaderLineToInt(inSFF);
1419 string headerLength = m->getline(inSFF);
1420 string keyLength = m->getline(inSFF);
1421 int numFlows = parseHeaderLineToInt(inSFF);
1422 string flowgramCode = m->getline(inSFF);
1423 string flowChars = m->getline(inSFF);
1424 string keySequence = m->getline(inSFF);
1429 if (flow) { outFlow << numFlows << endl; }
1431 for(int i=0;i<numReads;i++){
1434 if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
1440 seqName = seqName.substr(1);
1442 header.name = seqName;
1444 string runPrefix = parseHeaderLineToString(inSFF); header.timestamp = runPrefix;
1445 string regionNumber = parseHeaderLineToString(inSFF); header.region = regionNumber;
1446 string xyLocation = parseHeaderLineToString(inSFF); header.xy = xyLocation;
1449 string runName = parseHeaderLineToString(inSFF);
1450 string analysisName = parseHeaderLineToString(inSFF);
1451 string fullPath = parseHeaderLineToString(inSFF);
1454 string readHeaderLen = parseHeaderLineToString(inSFF); convert(readHeaderLen, header.headerLength);
1455 string nameLength = parseHeaderLineToString(inSFF); convert(nameLength, header.nameLength);
1456 int numBases = parseHeaderLineToInt(inSFF); header.numBases = numBases;
1457 string clipQualLeft = parseHeaderLineToString(inSFF); convert(clipQualLeft, header.clipQualLeft);
1458 int clipQualRight = parseHeaderLineToInt(inSFF); header.clipQualRight = clipQualRight;
1459 string clipAdapLeft = parseHeaderLineToString(inSFF); convert(clipAdapLeft, header.clipAdapterLeft);
1460 string clipAdapRight = parseHeaderLineToString(inSFF); convert(clipAdapRight, header.clipAdapterRight);
1466 vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows); read.flowgram = flowVector;
1467 vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases);
1470 vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
1471 for (int j = 1; j < flowIndices.size(); j++) { flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]); }
1472 read.flowIndex = flowIndicesAdjusted;
1474 string bases = parseHeaderLineToString(inSFF); read.bases = bases;
1475 vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases); read.qualScores = qualityScores;
1478 //if you have provided an accosfile and this seq is not in it, then dont print
1480 if (seqNames.size() != 0) { if (seqNames.count(header.name) == 0) { print = false; } }
1484 if (fasta) { printFastaSeqData(outFasta, read, header); }
1485 if (qual) { printQualSeqData(outQual, read, header); }
1486 if (flow) { printFlowSeqData(outFlow, read, header); }
1490 if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine(); }
1492 if (m->control_pressed) { break; }
1496 if (!m->control_pressed) { if((numReads) % 10000 != 0){ m->mothurOut(toString(numReads)); m->mothurOutEndLine(); } }
1500 if (fasta) { outFasta.close(); }
1501 if (qual) { outQual.close(); }
1502 if (flow) { outFlow.close(); }
1506 catch(exception& e) {
1507 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
1511 //**********************************************************************************************************************
1513 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
1517 while (!file.eof()) {
1519 char c = file.get();
1529 catch(exception& e) {
1530 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
1536 //**********************************************************************************************************************
1538 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
1542 while (!file.eof()) {
1543 char c = file.get();
1547 //text = m->getline(file);
1556 catch(exception& e) {
1557 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
1562 //**********************************************************************************************************************
1564 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
1566 vector<unsigned short> floatVector(length);
1568 while (!file.eof()) {
1569 char c = file.get();
1572 for(int i=0;i<length;i++){
1574 floatVector[i] = temp * 100;
1582 catch(exception& e) {
1583 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
1588 //**********************************************************************************************************************
1590 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
1592 vector<unsigned int> intVector(length);
1594 while (!file.eof()) {
1595 char c = file.get();
1597 for(int i=0;i<length;i++){
1598 file >> intVector[i];
1606 catch(exception& e) {
1607 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
1611 //***************************************************************************************************************
1613 bool SffInfoCommand::readOligos(string oligoFile){
1615 filehandles.clear();
1616 numSplitReads.clear();
1617 filehandlesHeaders.clear();
1620 m->openInputFile(oligoFile, inOligos);
1622 string type, oligo, group;
1624 int indexPrimer = 0;
1625 int indexBarcode = 0;
1627 while(!inOligos.eof()){
1632 while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
1633 m->gobble(inOligos);
1636 m->gobble(inOligos);
1637 //make type case insensitive
1638 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); }
1642 for(int i=0;i<oligo.length();i++){
1643 oligo[i] = toupper(oligo[i]);
1644 if(oligo[i] == 'U') { oligo[i] = 'T'; }
1647 if(type == "FORWARD"){
1650 // get rest of line in case there is a primer name
1651 while (!inOligos.eof()) {
1652 char c = inOligos.get();
1653 if (c == 10 || c == 13){ break; }
1654 else if (c == 32 || c == 9){;} //space or tab
1655 else { group += c; }
1658 //check for repeat barcodes
1659 map<string, int>::iterator itPrime = primers.find(oligo);
1660 if (itPrime != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
1662 primers[oligo]=indexPrimer; indexPrimer++;
1663 primerNameVector.push_back(group);
1664 }else if(type == "REVERSE"){
1665 //Sequence oligoRC("reverse", oligo);
1666 //oligoRC.reverseComplement();
1667 string oligoRC = reverseOligo(oligo);
1668 revPrimer.push_back(oligoRC);
1670 else if(type == "BARCODE"){
1673 //check for repeat barcodes
1674 map<string, int>::iterator itBar = barcodes.find(oligo);
1675 if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
1677 barcodes[oligo]=indexBarcode; indexBarcode++;
1678 barcodeNameVector.push_back(group);
1679 }else if(type == "LINKER"){
1680 linker.push_back(oligo);
1681 }else if(type == "SPACER"){
1682 spacer.push_back(oligo);
1684 else{ m->mothurOut("[WARNING]: " + type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine(); }
1686 m->gobble(inOligos);
1690 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ split = 1; }
1692 //add in potential combos
1693 if(barcodeNameVector.size() == 0){
1695 barcodeNameVector.push_back("");
1698 if(primerNameVector.size() == 0){
1700 primerNameVector.push_back("");
1703 filehandles.resize(barcodeNameVector.size());
1704 for(int i=0;i<filehandles.size();i++){
1705 filehandles[i].assign(primerNameVector.size(), "");
1709 set<string> uniqueNames; //used to cleanup outputFileNames
1710 for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
1711 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
1713 string primerName = primerNameVector[itPrimer->second];
1714 string barcodeName = barcodeNameVector[itBar->second];
1716 string comboGroupName = "";
1717 string fastaFileName = "";
1718 string qualFileName = "";
1719 string nameFileName = "";
1721 if(primerName == ""){
1722 comboGroupName = barcodeNameVector[itBar->second];
1725 if(barcodeName == ""){
1726 comboGroupName = primerNameVector[itPrimer->second];
1729 comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
1734 string thisFilename = outputDir + m->getRootName(m->getSimpleName(currentFileName)) + comboGroupName + "." + getOutputFileNameTag("sff");
1735 if (uniqueNames.count(thisFilename) == 0) {
1736 outputNames.push_back(thisFilename);
1737 outputTypes["sff"].push_back(thisFilename);
1738 uniqueNames.insert(thisFilename);
1741 filehandles[itBar->second][itPrimer->second] = thisFilename;
1742 m->openOutputFile(thisFilename, temp); temp.close();
1746 numFPrimers = primers.size();
1747 numLinkers = linker.size();
1748 numSpacers = spacer.size();
1749 noMatchFile = outputDir + m->getRootName(m->getSimpleName(currentFileName)) + "scrap." + getOutputFileNameTag("sff");
1750 m->mothurRemove(noMatchFile);
1752 bool allBlank = true;
1753 for (int i = 0; i < barcodeNameVector.size(); i++) {
1754 if (barcodeNameVector[i] != "") {
1759 for (int i = 0; i < primerNameVector.size(); i++) {
1760 if (primerNameVector[i] != "") {
1766 filehandlesHeaders.resize(filehandles.size());
1767 numSplitReads.resize(filehandles.size());
1768 for (int i = 0; i < filehandles.size(); i++) {
1769 numSplitReads[i].resize(filehandles[i].size(), 0);
1770 for (int j = 0; j < filehandles[i].size(); j++) {
1771 filehandlesHeaders[i].push_back(filehandles[i][j]+"headers");
1776 m->mothurOut("[WARNING]: your oligos file does not contain any group names. mothur will not create a split the sff file."); m->mothurOutEndLine();
1784 catch(exception& e) {
1785 m->errorOut(e, "SffInfoCommand", "readOligos");
1789 //********************************************************************/
1790 string SffInfoCommand::reverseOligo(string oligo){
1792 string reverse = "";
1794 for(int i=oligo.length()-1;i>=0;i--){
1796 if(oligo[i] == 'A') { reverse += 'T'; }
1797 else if(oligo[i] == 'T'){ reverse += 'A'; }
1798 else if(oligo[i] == 'U'){ reverse += 'A'; }
1800 else if(oligo[i] == 'G'){ reverse += 'C'; }
1801 else if(oligo[i] == 'C'){ reverse += 'G'; }
1803 else if(oligo[i] == 'R'){ reverse += 'Y'; }
1804 else if(oligo[i] == 'Y'){ reverse += 'R'; }
1806 else if(oligo[i] == 'M'){ reverse += 'K'; }
1807 else if(oligo[i] == 'K'){ reverse += 'M'; }
1809 else if(oligo[i] == 'W'){ reverse += 'W'; }
1810 else if(oligo[i] == 'S'){ reverse += 'S'; }
1812 else if(oligo[i] == 'B'){ reverse += 'V'; }
1813 else if(oligo[i] == 'V'){ reverse += 'B'; }
1815 else if(oligo[i] == 'D'){ reverse += 'H'; }
1816 else if(oligo[i] == 'H'){ reverse += 'D'; }
1818 else { reverse += 'N'; }
1824 catch(exception& e) {
1825 m->errorOut(e, "SffInfoCommand", "reverseOligo");
1830 //**********************************************************************************************************************