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;
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> uniqueSffNames;// so we don't add the same sff multiple times
587 map<string, string>::iterator it;
588 set<string> namesToRemove;
589 for(int i=0;i<filehandles.size();i++){
590 for(int j=0;j<filehandles[0].size();j++){
591 if (filehandles[i][j] != "") {
592 if (namesToRemove.count(filehandles[i][j]) == 0) {
593 if(m->isBlank(filehandles[i][j])){
594 m->mothurRemove(filehandles[i][j]);
595 m->mothurRemove(filehandlesHeaders[i][j]);
596 namesToRemove.insert(filehandles[i][j]);
598 it = uniqueSffNames.find(filehandles[i][j]);
599 if (it == uniqueSffNames.end()) {
600 uniqueSffNames[filehandles[i][j]] = barcodeNameVector[i];
608 //append new header to reads
609 for (int i = 0; i < filehandles.size(); i++) {
610 for (int j = 0; j < filehandles[i].size(); j++) {
611 m->appendFiles(filehandles[i][j], filehandlesHeaders[i][j]);
612 m->renameFile(filehandlesHeaders[i][j], filehandles[i][j]);
613 m->mothurRemove(filehandlesHeaders[i][j]);
614 if (numSplitReads[i][j] == 0) { m->mothurRemove(filehandles[i][j]); }
618 //remove names for outputFileNames, just cleans up the output
619 for(int i = 0; i < outputNames.size(); i++) {
620 if (namesToRemove.count(outputNames[i]) != 0) {
621 outputNames.erase(outputNames.begin()+i);
626 if(m->isBlank(noMatchFile)){ m->mothurRemove(noMatchFile); }
627 else { outputNames.push_back(noMatchFile); outputTypes["sff"].push_back(noMatchFile); }
632 catch(exception& e) {
633 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
637 //**********************************************************************************************************************
638 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
646 header.magicNumber = be_int4(*(unsigned int *)(&buffer));
652 for (int i = 0; i < 4; i++) { header.version += toString((int)(buffer9[i])); }
657 header.indexOffset = be_int8(*(unsigned long long *)(&buffer2));
662 header.indexLength = be_int4(*(unsigned int *)(&buffer3));
667 header.numReads = be_int4(*(unsigned int *)(&buffer4));
672 header.headerLength = be_int2(*(unsigned short *)(&buffer5));
677 header.keyLength = be_int2(*(unsigned short *)(&buffer6));
679 //read number of flow reads
682 header.numFlowsPerRead = be_int2(*(unsigned short *)(&buffer7));
687 header.flogramFormatCode = (int)(buffer8[0]);
690 char* tempBuffer = new char[header.numFlowsPerRead];
691 in.read(&(*tempBuffer), header.numFlowsPerRead);
692 header.flowChars = tempBuffer;
693 if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead); }
697 char* tempBuffer2 = new char[header.keyLength];
698 in.read(&(*tempBuffer2), header.keyLength);
699 header.keySequence = tempBuffer2;
700 if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength); }
701 delete[] tempBuffer2;
704 unsigned long long spotInFile = in.tellg();
705 unsigned long long spot = (spotInFile + 7)& ~7; // ~ inverts
709 m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
715 catch(exception& e) {
716 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
720 //**********************************************************************************************************************
721 int SffInfoCommand::adjustCommonHeader(CommonHeader header){
724 char* mybuffer = new char[4];
726 in.open(currentFileName.c_str(), ios::binary);
730 for (int i = 0; i < filehandlesHeaders.size(); i++) {
731 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
733 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
734 out.write(mybuffer, in.gcount());
741 mybuffer = new char[4];
743 for (int i = 0; i < filehandlesHeaders.size(); i++) {
744 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
746 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
747 out.write(mybuffer, in.gcount());
754 mybuffer = new char[8];
756 for (int i = 0; i < filehandlesHeaders.size(); i++) {
757 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
759 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
760 out.write(mybuffer, in.gcount());
768 mybuffer = new char[4];
770 for (int i = 0; i < filehandlesHeaders.size(); i++) {
771 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
773 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
774 out.write(mybuffer, in.gcount());
781 mybuffer = new char[4];
784 for (int i = 0; i < filehandlesHeaders.size(); i++) {
785 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
787 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
788 //convert number of reads to 4 byte char*
789 char* thisbuffer = new char[4];
790 thisbuffer[0] = (numSplitReads[i][j] >> 24) & 0xFF;
791 thisbuffer[1] = (numSplitReads[i][j] >> 16) & 0xFF;
792 thisbuffer[2] = (numSplitReads[i][j] >> 8) & 0xFF;
793 thisbuffer[3] = numSplitReads[i][j] & 0xFF;
794 out.write(thisbuffer, 4);
801 mybuffer = new char[2];
803 for (int i = 0; i < filehandlesHeaders.size(); i++) {
804 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
806 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
807 out.write(mybuffer, in.gcount());
814 mybuffer = new char[2];
816 for (int i = 0; i < filehandlesHeaders.size(); i++) {
817 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
819 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
820 out.write(mybuffer, in.gcount());
826 //read number of flow reads
827 mybuffer = new char[2];
829 for (int i = 0; i < filehandlesHeaders.size(); i++) {
830 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
832 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
833 out.write(mybuffer, in.gcount());
840 mybuffer = new char[1];
842 for (int i = 0; i < filehandlesHeaders.size(); i++) {
843 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
845 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
846 out.write(mybuffer, in.gcount());
853 mybuffer = new char[header.numFlowsPerRead];
854 in.read(mybuffer,header.numFlowsPerRead);
855 for (int i = 0; i < filehandlesHeaders.size(); i++) {
856 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
858 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
859 out.write(mybuffer, in.gcount());
866 mybuffer = new char[header.keyLength];
867 in.read(mybuffer,header.keyLength);
868 for (int i = 0; i < filehandlesHeaders.size(); i++) {
869 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
871 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
872 out.write(mybuffer, in.gcount());
880 unsigned long long spotInFile = in.tellg();
881 unsigned long long spot = (spotInFile + 7)& ~7; // ~ inverts
884 mybuffer = new char[spot-spotInFile];
885 for (int i = 0; i < filehandlesHeaders.size(); i++) {
886 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
888 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
889 out.write(mybuffer, spot-spotInFile);
898 catch(exception& e) {
899 m->errorOut(e, "SffInfoCommand", "adjustCommonHeader");
903 //**********************************************************************************************************************
904 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, Header& header){
906 unsigned long long startSpotInFile = in.tellg();
909 /*****************************************/
915 header.headerLength = be_int2(*(unsigned short *)(&buffer));
920 header.nameLength = be_int2(*(unsigned short *)(&buffer2));
925 header.numBases = be_int4(*(unsigned int *)(&buffer3));
927 //read clip qual left
930 header.clipQualLeft = be_int2(*(unsigned short *)(&buffer4));
931 header.clipQualLeft = 5;
933 //read clip qual right
936 header.clipQualRight = be_int2(*(unsigned short *)(&buffer5));
938 //read clipAdapterLeft
941 header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
943 //read clipAdapterRight
946 header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
949 char* tempBuffer = new char[header.nameLength];
950 in.read(&(*tempBuffer), header.nameLength);
951 header.name = tempBuffer;
952 if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength); }
955 //extract info from name
956 decodeName(header.timestamp, header.region, header.xy, header.name);
959 unsigned long long spotInFile = in.tellg();
960 unsigned long long spot = (spotInFile + 7)& ~7;
963 /*****************************************/
967 read.flowgram.resize(numFlowReads);
968 for (int i = 0; i < numFlowReads; i++) {
971 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
975 read.flowIndex.resize(header.numBases);
976 for (int i = 0; i < header.numBases; i++) {
979 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
983 char* tempBuffer6 = new char[header.numBases];
984 in.read(&(*tempBuffer6), header.numBases);
985 read.bases = tempBuffer6;
986 if (read.bases.length() > header.numBases) { read.bases = read.bases.substr(0, header.numBases); }
987 delete[] tempBuffer6;
990 read.qualScores.resize(header.numBases);
991 for (int i = 0; i < header.numBases; i++) {
994 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
998 spotInFile = in.tellg();
999 spot = (spotInFile + 7)& ~7;
1004 mybuffer = new char [spot-startSpotInFile];
1006 m->openInputFile(currentFileName, in2);
1007 in2.seekg(startSpotInFile);
1008 in2.read(mybuffer,spot-startSpotInFile);
1011 int barcodeIndex, primerIndex;
1012 int trashCodeLength = findGroup(header, read, barcodeIndex, primerIndex);
1014 if(trashCodeLength == 0){
1016 m->openOutputFileAppend(filehandles[barcodeIndex][primerIndex], out);
1017 out.write(mybuffer, in2.gcount());
1020 numSplitReads[barcodeIndex][primerIndex]++;
1024 m->openOutputFileAppend(noMatchFile, out);
1025 out.write(mybuffer, in2.gcount());
1032 m->mothurOut("Error reading."); m->mothurOutEndLine();
1037 catch(exception& e) {
1038 m->errorOut(e, "SffInfoCommand", "readSeqData");
1042 //**********************************************************************************************************************
1043 int SffInfoCommand::findGroup(Header header, seqRead read, int& barcode, int& primer) {
1045 //find group read belongs to
1046 TrimOligos trimOligos(pdiffs, bdiffs, ldiffs, sdiffs, primers, barcodes, rbarcodes, revPrimer, linker, spacer);
1049 string trashCode = "";
1050 int currentSeqsDiffs = 0;
1052 string seq = read.bases;
1055 if(header.clipQualRight < header.clipQualLeft){
1058 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1059 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
1062 seq = seq.substr(header.clipQualLeft-1);
1065 //if you wanted the sfftxt then you already converted the bases to the right case
1067 //make the bases you want to clip lowercase and the bases you want to keep upper case
1068 if(header.clipQualRight == 0){ header.clipQualRight = seq.length(); }
1069 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
1070 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { seq[i] = toupper(seq[i]); }
1071 for (int i = (header.clipQualRight-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
1075 Sequence currSeq(header.name, seq);
1076 QualityScores currQual;
1078 if(numLinkers != 0){
1079 success = trimOligos.stripLinker(currSeq, currQual);
1080 if(success > ldiffs) { trashCode += 'k'; }
1081 else{ currentSeqsDiffs += success; }
1085 if(barcodes.size() != 0){
1086 success = trimOligos.stripBarcode(currSeq, currQual, barcode);
1087 if(success > bdiffs) { trashCode += 'b'; }
1088 else{ currentSeqsDiffs += success; }
1091 if(rbarcodes.size() != 0){
1092 success = trimOligos.stripRBarcode(currSeq, currQual, barcode);
1093 if(success > bdiffs) { trashCode += 'b'; }
1094 else{ currentSeqsDiffs += success; }
1097 if(numSpacers != 0){
1098 success = trimOligos.stripSpacer(currSeq, currQual);
1099 if(success > sdiffs) { trashCode += 's'; }
1100 else{ currentSeqsDiffs += success; }
1104 if(numFPrimers != 0){
1105 success = trimOligos.stripForward(currSeq, currQual, primer, true);
1106 if(success > pdiffs) { trashCode += 'f'; }
1107 else{ currentSeqsDiffs += success; }
1110 if (currentSeqsDiffs > tdiffs) { trashCode += 't'; }
1112 if(revPrimer.size() != 0){
1113 success = trimOligos.stripReverse(currSeq, currQual);
1114 if(!success) { trashCode += 'r'; }
1118 return trashCode.length();
1120 catch(exception& e) {
1121 m->errorOut(e, "SffInfoCommand", "findGroup");
1125 //**********************************************************************************************************************
1126 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
1129 if (name.length() >= 6) {
1130 string time = name.substr(0, 6);
1131 unsigned int timeNum = m->fromBase36(time);
1133 int q1 = timeNum / 60;
1134 int sec = timeNum - 60 * q1;
1136 int minute = q1 - 60 * q2;
1138 int hr = q2 - 24 * q3;
1140 int day = q3 - 32 * q4;
1142 int mon = q4 - 13 * q5;
1143 int year = 2000 + q5;
1145 timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
1148 if (name.length() >= 9) {
1149 region = name.substr(7, 2);
1151 string xyNum = name.substr(9);
1152 unsigned int myXy = m->fromBase36(xyNum);
1154 int y = myXy & 4095;
1156 xy = toString(x) + "_" + toString(y);
1161 catch(exception& e) {
1162 m->errorOut(e, "SffInfoCommand", "decodeName");
1166 //**********************************************************************************************************************
1167 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
1170 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
1171 out << "Version: " << header.version << endl;
1172 out << "Index Offset: " << header.indexOffset << endl;
1173 out << "Index Length: " << header.indexLength << endl;
1174 out << "Number of Reads: " << header.numReads << endl;
1175 out << "Header Length: " << header.headerLength << endl;
1176 out << "Key Length: " << header.keyLength << endl;
1177 out << "Number of Flows: " << header.numFlowsPerRead << endl;
1178 out << "Format Code: " << header.flogramFormatCode << endl;
1179 out << "Flow Chars: " << header.flowChars << endl;
1180 out << "Key Sequence: " << header.keySequence << endl << endl;
1184 catch(exception& e) {
1185 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
1189 //**********************************************************************************************************************
1190 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
1193 out << ">" << header.name << endl;
1194 out << "Run Prefix: " << header.timestamp << endl;
1195 out << "Region #: " << header.region << endl;
1196 out << "XY Location: " << header.xy << endl << endl;
1198 out << "Run Name: " << endl;
1199 out << "Analysis Name: " << endl;
1200 out << "Full Path: " << endl << endl;
1202 out << "Read Header Len: " << header.headerLength << endl;
1203 out << "Name Length: " << header.nameLength << endl;
1204 out << "# of Bases: " << header.numBases << endl;
1205 out << "Clip Qual Left: " << header.clipQualLeft << endl;
1206 out << "Clip Qual Right: " << header.clipQualRight << endl;
1207 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
1208 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
1212 catch(exception& e) {
1213 m->errorOut(e, "SffInfoCommand", "printHeader");
1217 //**********************************************************************************************************************
1218 bool SffInfoCommand::sanityCheck(Header& header, seqRead& read) {
1221 string message = "[WARNING]: Your sff file may be corrupted! Sequence: " + header.name + "\n";
1223 if (header.clipQualLeft > read.bases.length()) {
1224 okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
1226 if (header.clipQualRight > read.bases.length()) {
1227 okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
1229 if (header.clipQualLeft > read.qualScores.size()) {
1230 okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
1232 if (header.clipQualRight > read.qualScores.size()) {
1233 okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
1236 if (okay == false) {
1237 m->mothurOut(message); m->mothurOutEndLine();
1242 catch(exception& e) {
1243 m->errorOut(e, "SffInfoCommand", "sanityCheck");
1247 //**********************************************************************************************************************
1248 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
1250 out << "Flowgram: ";
1251 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
1253 out << endl << "Flow Indexes: ";
1255 for (int i = 0; i < read.flowIndex.size(); i++) { sum += read.flowIndex[i]; out << sum << '\t'; }
1257 //make the bases you want to clip lowercase and the bases you want to keep upper case
1258 if(header.clipQualRight == 0){ header.clipQualRight = read.bases.length(); }
1259 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
1260 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { read.bases[i] = toupper(read.bases[i]); }
1261 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) { read.bases[i] = tolower(read.bases[i]); }
1263 out << endl << "Bases: " << read.bases << endl << "Quality Scores: ";
1264 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
1267 out << endl << endl;
1271 catch(exception& e) {
1272 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
1276 //**********************************************************************************************************************
1277 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
1279 string seq = read.bases;
1282 if(header.clipQualRight < header.clipQualLeft){
1285 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1286 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
1289 seq = seq.substr(header.clipQualLeft-1);
1292 //if you wanted the sfftxt then you already converted the bases to the right case
1294 //make the bases you want to clip lowercase and the bases you want to keep upper case
1295 if(header.clipQualRight == 0){ header.clipQualRight = seq.length(); }
1296 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
1297 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { seq[i] = toupper(seq[i]); }
1298 for (int i = (header.clipQualRight-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
1302 out << ">" << header.name << " xy=" << header.xy << endl;
1307 catch(exception& e) {
1308 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
1313 //**********************************************************************************************************************
1314 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
1318 if(header.clipQualRight < header.clipQualLeft){
1319 out << ">" << header.name << " xy=" << header.xy << endl;
1320 out << "0\t0\t0\t0";
1322 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1323 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
1324 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { out << read.qualScores[i] << '\t'; }
1327 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
1328 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
1331 out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
1332 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
1339 catch(exception& e) {
1340 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
1345 //**********************************************************************************************************************
1346 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
1348 if(header.clipQualRight > header.clipQualLeft){
1351 for (int i = 0; i < header.clipQualRight; i++) { rightIndex += read.flowIndex[i]; }
1353 out << header.name << ' ' << rightIndex;
1354 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100); }
1361 catch(exception& e) {
1362 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
1366 //**********************************************************************************************************************
1367 int SffInfoCommand::readAccnosFile(string filename) {
1373 m->openInputFile(filename, in);
1377 in >> name; m->gobble(in);
1379 seqNames.insert(name);
1381 if (m->control_pressed) { seqNames.clear(); break; }
1387 catch(exception& e) {
1388 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
1392 //**********************************************************************************************************************
1393 int SffInfoCommand::parseSffTxt() {
1397 m->openInputFile(sfftxtFilename, inSFF);
1399 if (outputDir == "") { outputDir += m->hasPath(sfftxtFilename); }
1402 ofstream outFasta, outQual, outFlow;
1403 string outFastaFileName, outQualFileName;
1404 string fileRoot = m->getRootName(m->getSimpleName(sfftxtFilename));
1405 if (fileRoot.length() > 0) {
1407 fileRoot = fileRoot.substr(0, fileRoot.length()-1);
1408 fileRoot = m->getRootName(fileRoot);
1411 string outFlowFileName = outputDir + fileRoot + getOutputFileNameTag("flow");
1413 outFastaFileName = outputDir + fileRoot + getOutputFileNameTag("fasta");
1414 outQualFileName = outputDir + fileRoot + getOutputFileNameTag("qfile");
1416 outFastaFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("fasta");
1417 outQualFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("qfile");
1420 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
1421 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName); }
1422 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); }
1424 //read common header
1425 string commonHeader = m->getline(inSFF);
1426 string magicNumber = m->getline(inSFF);
1427 string version = m->getline(inSFF);
1428 string indexOffset = m->getline(inSFF);
1429 string indexLength = m->getline(inSFF);
1430 int numReads = parseHeaderLineToInt(inSFF);
1431 string headerLength = m->getline(inSFF);
1432 string keyLength = m->getline(inSFF);
1433 int numFlows = parseHeaderLineToInt(inSFF);
1434 string flowgramCode = m->getline(inSFF);
1435 string flowChars = m->getline(inSFF);
1436 string keySequence = m->getline(inSFF);
1441 if (flow) { outFlow << numFlows << endl; }
1443 for(int i=0;i<numReads;i++){
1446 if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
1452 seqName = seqName.substr(1);
1454 header.name = seqName;
1456 string runPrefix = parseHeaderLineToString(inSFF); header.timestamp = runPrefix;
1457 string regionNumber = parseHeaderLineToString(inSFF); header.region = regionNumber;
1458 string xyLocation = parseHeaderLineToString(inSFF); header.xy = xyLocation;
1461 string runName = parseHeaderLineToString(inSFF);
1462 string analysisName = parseHeaderLineToString(inSFF);
1463 string fullPath = parseHeaderLineToString(inSFF);
1466 string readHeaderLen = parseHeaderLineToString(inSFF); convert(readHeaderLen, header.headerLength);
1467 string nameLength = parseHeaderLineToString(inSFF); convert(nameLength, header.nameLength);
1468 int numBases = parseHeaderLineToInt(inSFF); header.numBases = numBases;
1469 string clipQualLeft = parseHeaderLineToString(inSFF); convert(clipQualLeft, header.clipQualLeft);
1470 int clipQualRight = parseHeaderLineToInt(inSFF); header.clipQualRight = clipQualRight;
1471 string clipAdapLeft = parseHeaderLineToString(inSFF); convert(clipAdapLeft, header.clipAdapterLeft);
1472 string clipAdapRight = parseHeaderLineToString(inSFF); convert(clipAdapRight, header.clipAdapterRight);
1478 vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows); read.flowgram = flowVector;
1479 vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases);
1482 vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
1483 for (int j = 1; j < flowIndices.size(); j++) { flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]); }
1484 read.flowIndex = flowIndicesAdjusted;
1486 string bases = parseHeaderLineToString(inSFF); read.bases = bases;
1487 vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases); read.qualScores = qualityScores;
1490 //if you have provided an accosfile and this seq is not in it, then dont print
1492 if (seqNames.size() != 0) { if (seqNames.count(header.name) == 0) { print = false; } }
1496 if (fasta) { printFastaSeqData(outFasta, read, header); }
1497 if (qual) { printQualSeqData(outQual, read, header); }
1498 if (flow) { printFlowSeqData(outFlow, read, header); }
1502 if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine(); }
1504 if (m->control_pressed) { break; }
1508 if (!m->control_pressed) { if((numReads) % 10000 != 0){ m->mothurOut(toString(numReads)); m->mothurOutEndLine(); } }
1512 if (fasta) { outFasta.close(); }
1513 if (qual) { outQual.close(); }
1514 if (flow) { outFlow.close(); }
1518 catch(exception& e) {
1519 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
1523 //**********************************************************************************************************************
1525 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
1529 while (!file.eof()) {
1531 char c = file.get();
1541 catch(exception& e) {
1542 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
1548 //**********************************************************************************************************************
1550 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
1554 while (!file.eof()) {
1555 char c = file.get();
1559 //text = m->getline(file);
1568 catch(exception& e) {
1569 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
1574 //**********************************************************************************************************************
1576 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
1578 vector<unsigned short> floatVector(length);
1580 while (!file.eof()) {
1581 char c = file.get();
1584 for(int i=0;i<length;i++){
1586 floatVector[i] = temp * 100;
1594 catch(exception& e) {
1595 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
1600 //**********************************************************************************************************************
1602 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
1604 vector<unsigned int> intVector(length);
1606 while (!file.eof()) {
1607 char c = file.get();
1609 for(int i=0;i<length;i++){
1610 file >> intVector[i];
1618 catch(exception& e) {
1619 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
1623 //***************************************************************************************************************
1625 bool SffInfoCommand::readOligos(string oligoFile){
1627 filehandles.clear();
1628 numSplitReads.clear();
1629 filehandlesHeaders.clear();
1632 m->openInputFile(oligoFile, inOligos);
1634 string type, oligo, group;
1636 int indexPrimer = 0;
1637 int indexBarcode = 0;
1639 while(!inOligos.eof()){
1644 while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
1645 m->gobble(inOligos);
1648 m->gobble(inOligos);
1649 //make type case insensitive
1650 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); }
1654 for(int i=0;i<oligo.length();i++){
1655 oligo[i] = toupper(oligo[i]);
1656 if(oligo[i] == 'U') { oligo[i] = 'T'; }
1659 if(type == "FORWARD"){
1662 // get rest of line in case there is a primer name
1663 while (!inOligos.eof()) {
1664 char c = inOligos.get();
1665 if (c == 10 || c == 13){ break; }
1666 else if (c == 32 || c == 9){;} //space or tab
1667 else { group += c; }
1670 //check for repeat barcodes
1671 map<string, int>::iterator itPrime = primers.find(oligo);
1672 if (itPrime != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
1674 primers[oligo]=indexPrimer; indexPrimer++;
1675 primerNameVector.push_back(group);
1676 }else if(type == "REVERSE"){
1677 //Sequence oligoRC("reverse", oligo);
1678 //oligoRC.reverseComplement();
1679 string oligoRC = reverseOligo(oligo);
1680 revPrimer.push_back(oligoRC);
1682 else if(type == "BARCODE"){
1685 //barcode lines can look like BARCODE atgcatgc groupName - for 454 seqs
1686 //or BARCODE atgcatgc atgcatgc groupName - for illumina data that has forward and reverse info
1688 while (!inOligos.eof()) {
1689 char c = inOligos.get();
1690 if (c == 10 || c == 13){ break; }
1691 else if (c == 32 || c == 9){;} //space or tab
1695 //then this is illumina data with 4 columns
1697 string reverseBarcode = reverseOligo(group); //reverse barcode
1700 //check for repeat barcodes
1701 map<string, int>::iterator itBar = rbarcodes.find(reverseBarcode);
1702 if (itBar != rbarcodes.end()) { m->mothurOut("barcode " + reverseBarcode + " is in your oligos file already."); m->mothurOutEndLine(); }
1704 rbarcodes[reverseBarcode]=indexBarcode;
1707 //check for repeat barcodes
1708 map<string, int>::iterator itBar = barcodes.find(oligo);
1709 if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
1711 barcodes[oligo]=indexBarcode; indexBarcode++;
1712 barcodeNameVector.push_back(group);
1714 }else if(type == "LINKER"){
1715 linker.push_back(oligo);
1716 }else if(type == "SPACER"){
1717 spacer.push_back(oligo);
1719 else{ m->mothurOut("[WARNING]: " + type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine(); }
1721 m->gobble(inOligos);
1725 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ split = 1; }
1727 //add in potential combos
1728 if(barcodeNameVector.size() == 0){
1730 barcodeNameVector.push_back("");
1733 if(primerNameVector.size() == 0){
1735 primerNameVector.push_back("");
1738 filehandles.resize(barcodeNameVector.size());
1739 for(int i=0;i<filehandles.size();i++){
1740 filehandles[i].assign(primerNameVector.size(), "");
1744 set<string> uniqueNames; //used to cleanup outputFileNames
1745 for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
1746 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
1748 string primerName = primerNameVector[itPrimer->second];
1749 string barcodeName = barcodeNameVector[itBar->second];
1751 string comboGroupName = "";
1752 string fastaFileName = "";
1753 string qualFileName = "";
1754 string nameFileName = "";
1756 if(primerName == ""){
1757 comboGroupName = barcodeNameVector[itBar->second];
1760 if(barcodeName == ""){
1761 comboGroupName = primerNameVector[itPrimer->second];
1764 comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
1769 string thisFilename = outputDir + m->getRootName(m->getSimpleName(currentFileName)) + comboGroupName + "." + getOutputFileNameTag("sff");
1770 if (uniqueNames.count(thisFilename) == 0) {
1771 outputNames.push_back(thisFilename);
1772 outputTypes["sff"].push_back(thisFilename);
1773 uniqueNames.insert(thisFilename);
1776 filehandles[itBar->second][itPrimer->second] = thisFilename;
1777 m->openOutputFile(thisFilename, temp); temp.close();
1781 numFPrimers = primers.size();
1782 numLinkers = linker.size();
1783 numSpacers = spacer.size();
1784 noMatchFile = outputDir + m->getRootName(m->getSimpleName(currentFileName)) + "scrap." + getOutputFileNameTag("sff");
1785 m->mothurRemove(noMatchFile);
1787 bool allBlank = true;
1788 for (int i = 0; i < barcodeNameVector.size(); i++) {
1789 if (barcodeNameVector[i] != "") {
1794 for (int i = 0; i < primerNameVector.size(); i++) {
1795 if (primerNameVector[i] != "") {
1801 filehandlesHeaders.resize(filehandles.size());
1802 numSplitReads.resize(filehandles.size());
1803 for (int i = 0; i < filehandles.size(); i++) {
1804 numSplitReads[i].resize(filehandles[i].size(), 0);
1805 for (int j = 0; j < filehandles[i].size(); j++) {
1806 filehandlesHeaders[i].push_back(filehandles[i][j]+"headers");
1811 m->mothurOut("[WARNING]: your oligos file does not contain any group names. mothur will not create a split the sff file."); m->mothurOutEndLine();
1819 catch(exception& e) {
1820 m->errorOut(e, "SffInfoCommand", "readOligos");
1824 //********************************************************************/
1825 string SffInfoCommand::reverseOligo(string oligo){
1827 string reverse = "";
1829 for(int i=oligo.length()-1;i>=0;i--){
1831 if(oligo[i] == 'A') { reverse += 'T'; }
1832 else if(oligo[i] == 'T'){ reverse += 'A'; }
1833 else if(oligo[i] == 'U'){ reverse += 'A'; }
1835 else if(oligo[i] == 'G'){ reverse += 'C'; }
1836 else if(oligo[i] == 'C'){ reverse += 'G'; }
1838 else if(oligo[i] == 'R'){ reverse += 'Y'; }
1839 else if(oligo[i] == 'Y'){ reverse += 'R'; }
1841 else if(oligo[i] == 'M'){ reverse += 'K'; }
1842 else if(oligo[i] == 'K'){ reverse += 'M'; }
1844 else if(oligo[i] == 'W'){ reverse += 'W'; }
1845 else if(oligo[i] == 'S'){ reverse += 'S'; }
1847 else if(oligo[i] == 'B'){ reverse += 'V'; }
1848 else if(oligo[i] == 'V'){ reverse += 'B'; }
1850 else if(oligo[i] == 'D'){ reverse += 'H'; }
1851 else if(oligo[i] == 'H'){ reverse += 'D'; }
1853 else { reverse += 'N'; }
1859 catch(exception& e) {
1860 m->errorOut(e, "SffInfoCommand", "reverseOligo");
1865 //**********************************************************************************************************************