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, rbarcodes, 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(rbarcodes.size() != 0){
1086 success = trimOligos.stripRBarcode(currSeq, currQual, barcode);
1087 if(success > bdiffs) { trashCode += 'b'; }
1088 else{ currentSeqsDiffs += success; }
1091 if(numSpacers != 0){
1092 success = trimOligos.stripSpacer(currSeq, currQual);
1093 if(success > sdiffs) { trashCode += 's'; }
1094 else{ currentSeqsDiffs += success; }
1098 if(numFPrimers != 0){
1099 success = trimOligos.stripForward(currSeq, currQual, primer, true);
1100 if(success > pdiffs) { trashCode += 'f'; }
1101 else{ currentSeqsDiffs += success; }
1104 if (currentSeqsDiffs > tdiffs) { trashCode += 't'; }
1106 if(revPrimer.size() != 0){
1107 success = trimOligos.stripReverse(currSeq, currQual);
1108 if(!success) { trashCode += 'r'; }
1112 return trashCode.length();
1114 catch(exception& e) {
1115 m->errorOut(e, "SffInfoCommand", "findGroup");
1119 //**********************************************************************************************************************
1120 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
1123 if (name.length() >= 6) {
1124 string time = name.substr(0, 6);
1125 unsigned int timeNum = m->fromBase36(time);
1127 int q1 = timeNum / 60;
1128 int sec = timeNum - 60 * q1;
1130 int minute = q1 - 60 * q2;
1132 int hr = q2 - 24 * q3;
1134 int day = q3 - 32 * q4;
1136 int mon = q4 - 13 * q5;
1137 int year = 2000 + q5;
1139 timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
1142 if (name.length() >= 9) {
1143 region = name.substr(7, 2);
1145 string xyNum = name.substr(9);
1146 unsigned int myXy = m->fromBase36(xyNum);
1148 int y = myXy & 4095;
1150 xy = toString(x) + "_" + toString(y);
1155 catch(exception& e) {
1156 m->errorOut(e, "SffInfoCommand", "decodeName");
1160 //**********************************************************************************************************************
1161 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
1164 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
1165 out << "Version: " << header.version << endl;
1166 out << "Index Offset: " << header.indexOffset << endl;
1167 out << "Index Length: " << header.indexLength << endl;
1168 out << "Number of Reads: " << header.numReads << endl;
1169 out << "Header Length: " << header.headerLength << endl;
1170 out << "Key Length: " << header.keyLength << endl;
1171 out << "Number of Flows: " << header.numFlowsPerRead << endl;
1172 out << "Format Code: " << header.flogramFormatCode << endl;
1173 out << "Flow Chars: " << header.flowChars << endl;
1174 out << "Key Sequence: " << header.keySequence << endl << endl;
1178 catch(exception& e) {
1179 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
1183 //**********************************************************************************************************************
1184 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
1187 out << ">" << header.name << endl;
1188 out << "Run Prefix: " << header.timestamp << endl;
1189 out << "Region #: " << header.region << endl;
1190 out << "XY Location: " << header.xy << endl << endl;
1192 out << "Run Name: " << endl;
1193 out << "Analysis Name: " << endl;
1194 out << "Full Path: " << endl << endl;
1196 out << "Read Header Len: " << header.headerLength << endl;
1197 out << "Name Length: " << header.nameLength << endl;
1198 out << "# of Bases: " << header.numBases << endl;
1199 out << "Clip Qual Left: " << header.clipQualLeft << endl;
1200 out << "Clip Qual Right: " << header.clipQualRight << endl;
1201 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
1202 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
1206 catch(exception& e) {
1207 m->errorOut(e, "SffInfoCommand", "printHeader");
1211 //**********************************************************************************************************************
1212 bool SffInfoCommand::sanityCheck(Header& header, seqRead& read) {
1215 string message = "[WARNING]: Your sff file may be corrupted! Sequence: " + header.name + "\n";
1217 if (header.clipQualLeft > read.bases.length()) {
1218 okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
1220 if (header.clipQualRight > read.bases.length()) {
1221 okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
1223 if (header.clipQualLeft > read.qualScores.size()) {
1224 okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
1226 if (header.clipQualRight > read.qualScores.size()) {
1227 okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
1230 if (okay == false) {
1231 m->mothurOut(message); m->mothurOutEndLine();
1236 catch(exception& e) {
1237 m->errorOut(e, "SffInfoCommand", "sanityCheck");
1241 //**********************************************************************************************************************
1242 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
1244 out << "Flowgram: ";
1245 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
1247 out << endl << "Flow Indexes: ";
1249 for (int i = 0; i < read.flowIndex.size(); i++) { sum += read.flowIndex[i]; out << sum << '\t'; }
1251 //make the bases you want to clip lowercase and the bases you want to keep upper case
1252 if(header.clipQualRight == 0){ header.clipQualRight = read.bases.length(); }
1253 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
1254 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { read.bases[i] = toupper(read.bases[i]); }
1255 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) { read.bases[i] = tolower(read.bases[i]); }
1257 out << endl << "Bases: " << read.bases << endl << "Quality Scores: ";
1258 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
1261 out << endl << endl;
1265 catch(exception& e) {
1266 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
1270 //**********************************************************************************************************************
1271 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
1273 string seq = read.bases;
1276 if(header.clipQualRight < header.clipQualLeft){
1279 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1280 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
1283 seq = seq.substr(header.clipQualLeft-1);
1286 //if you wanted the sfftxt then you already converted the bases to the right case
1288 //make the bases you want to clip lowercase and the bases you want to keep upper case
1289 if(header.clipQualRight == 0){ header.clipQualRight = seq.length(); }
1290 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
1291 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { seq[i] = toupper(seq[i]); }
1292 for (int i = (header.clipQualRight-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
1296 out << ">" << header.name << " xy=" << header.xy << endl;
1301 catch(exception& e) {
1302 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
1307 //**********************************************************************************************************************
1308 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
1312 if(header.clipQualRight < header.clipQualLeft){
1313 out << ">" << header.name << " xy=" << header.xy << endl;
1314 out << "0\t0\t0\t0";
1316 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1317 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
1318 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { out << read.qualScores[i] << '\t'; }
1321 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
1322 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
1325 out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
1326 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
1333 catch(exception& e) {
1334 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
1339 //**********************************************************************************************************************
1340 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
1342 if(header.clipQualRight > header.clipQualLeft){
1345 for (int i = 0; i < header.clipQualRight; i++) { rightIndex += read.flowIndex[i]; }
1347 out << header.name << ' ' << rightIndex;
1348 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100); }
1355 catch(exception& e) {
1356 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
1360 //**********************************************************************************************************************
1361 int SffInfoCommand::readAccnosFile(string filename) {
1367 m->openInputFile(filename, in);
1371 in >> name; m->gobble(in);
1373 seqNames.insert(name);
1375 if (m->control_pressed) { seqNames.clear(); break; }
1381 catch(exception& e) {
1382 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
1386 //**********************************************************************************************************************
1387 int SffInfoCommand::parseSffTxt() {
1391 m->openInputFile(sfftxtFilename, inSFF);
1393 if (outputDir == "") { outputDir += m->hasPath(sfftxtFilename); }
1396 ofstream outFasta, outQual, outFlow;
1397 string outFastaFileName, outQualFileName;
1398 string fileRoot = m->getRootName(m->getSimpleName(sfftxtFilename));
1399 if (fileRoot.length() > 0) {
1401 fileRoot = fileRoot.substr(0, fileRoot.length()-1);
1402 fileRoot = m->getRootName(fileRoot);
1405 string outFlowFileName = outputDir + fileRoot + getOutputFileNameTag("flow");
1407 outFastaFileName = outputDir + fileRoot + getOutputFileNameTag("fasta");
1408 outQualFileName = outputDir + fileRoot + getOutputFileNameTag("qfile");
1410 outFastaFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("fasta");
1411 outQualFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("qfile");
1414 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
1415 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName); }
1416 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); }
1418 //read common header
1419 string commonHeader = m->getline(inSFF);
1420 string magicNumber = m->getline(inSFF);
1421 string version = m->getline(inSFF);
1422 string indexOffset = m->getline(inSFF);
1423 string indexLength = m->getline(inSFF);
1424 int numReads = parseHeaderLineToInt(inSFF);
1425 string headerLength = m->getline(inSFF);
1426 string keyLength = m->getline(inSFF);
1427 int numFlows = parseHeaderLineToInt(inSFF);
1428 string flowgramCode = m->getline(inSFF);
1429 string flowChars = m->getline(inSFF);
1430 string keySequence = m->getline(inSFF);
1435 if (flow) { outFlow << numFlows << endl; }
1437 for(int i=0;i<numReads;i++){
1440 if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
1446 seqName = seqName.substr(1);
1448 header.name = seqName;
1450 string runPrefix = parseHeaderLineToString(inSFF); header.timestamp = runPrefix;
1451 string regionNumber = parseHeaderLineToString(inSFF); header.region = regionNumber;
1452 string xyLocation = parseHeaderLineToString(inSFF); header.xy = xyLocation;
1455 string runName = parseHeaderLineToString(inSFF);
1456 string analysisName = parseHeaderLineToString(inSFF);
1457 string fullPath = parseHeaderLineToString(inSFF);
1460 string readHeaderLen = parseHeaderLineToString(inSFF); convert(readHeaderLen, header.headerLength);
1461 string nameLength = parseHeaderLineToString(inSFF); convert(nameLength, header.nameLength);
1462 int numBases = parseHeaderLineToInt(inSFF); header.numBases = numBases;
1463 string clipQualLeft = parseHeaderLineToString(inSFF); convert(clipQualLeft, header.clipQualLeft);
1464 int clipQualRight = parseHeaderLineToInt(inSFF); header.clipQualRight = clipQualRight;
1465 string clipAdapLeft = parseHeaderLineToString(inSFF); convert(clipAdapLeft, header.clipAdapterLeft);
1466 string clipAdapRight = parseHeaderLineToString(inSFF); convert(clipAdapRight, header.clipAdapterRight);
1472 vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows); read.flowgram = flowVector;
1473 vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases);
1476 vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
1477 for (int j = 1; j < flowIndices.size(); j++) { flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]); }
1478 read.flowIndex = flowIndicesAdjusted;
1480 string bases = parseHeaderLineToString(inSFF); read.bases = bases;
1481 vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases); read.qualScores = qualityScores;
1484 //if you have provided an accosfile and this seq is not in it, then dont print
1486 if (seqNames.size() != 0) { if (seqNames.count(header.name) == 0) { print = false; } }
1490 if (fasta) { printFastaSeqData(outFasta, read, header); }
1491 if (qual) { printQualSeqData(outQual, read, header); }
1492 if (flow) { printFlowSeqData(outFlow, read, header); }
1496 if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine(); }
1498 if (m->control_pressed) { break; }
1502 if (!m->control_pressed) { if((numReads) % 10000 != 0){ m->mothurOut(toString(numReads)); m->mothurOutEndLine(); } }
1506 if (fasta) { outFasta.close(); }
1507 if (qual) { outQual.close(); }
1508 if (flow) { outFlow.close(); }
1512 catch(exception& e) {
1513 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
1517 //**********************************************************************************************************************
1519 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
1523 while (!file.eof()) {
1525 char c = file.get();
1535 catch(exception& e) {
1536 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
1542 //**********************************************************************************************************************
1544 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
1548 while (!file.eof()) {
1549 char c = file.get();
1553 //text = m->getline(file);
1562 catch(exception& e) {
1563 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
1568 //**********************************************************************************************************************
1570 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
1572 vector<unsigned short> floatVector(length);
1574 while (!file.eof()) {
1575 char c = file.get();
1578 for(int i=0;i<length;i++){
1580 floatVector[i] = temp * 100;
1588 catch(exception& e) {
1589 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
1594 //**********************************************************************************************************************
1596 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
1598 vector<unsigned int> intVector(length);
1600 while (!file.eof()) {
1601 char c = file.get();
1603 for(int i=0;i<length;i++){
1604 file >> intVector[i];
1612 catch(exception& e) {
1613 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
1617 //***************************************************************************************************************
1619 bool SffInfoCommand::readOligos(string oligoFile){
1621 filehandles.clear();
1622 numSplitReads.clear();
1623 filehandlesHeaders.clear();
1626 m->openInputFile(oligoFile, inOligos);
1628 string type, oligo, group;
1630 int indexPrimer = 0;
1631 int indexBarcode = 0;
1633 while(!inOligos.eof()){
1638 while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
1639 m->gobble(inOligos);
1642 m->gobble(inOligos);
1643 //make type case insensitive
1644 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); }
1648 for(int i=0;i<oligo.length();i++){
1649 oligo[i] = toupper(oligo[i]);
1650 if(oligo[i] == 'U') { oligo[i] = 'T'; }
1653 if(type == "FORWARD"){
1656 // get rest of line in case there is a primer name
1657 while (!inOligos.eof()) {
1658 char c = inOligos.get();
1659 if (c == 10 || c == 13){ break; }
1660 else if (c == 32 || c == 9){;} //space or tab
1661 else { group += c; }
1664 //check for repeat barcodes
1665 map<string, int>::iterator itPrime = primers.find(oligo);
1666 if (itPrime != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
1668 primers[oligo]=indexPrimer; indexPrimer++;
1669 primerNameVector.push_back(group);
1670 }else if(type == "REVERSE"){
1671 //Sequence oligoRC("reverse", oligo);
1672 //oligoRC.reverseComplement();
1673 string oligoRC = reverseOligo(oligo);
1674 revPrimer.push_back(oligoRC);
1676 else if(type == "BARCODE"){
1679 //barcode lines can look like BARCODE atgcatgc groupName - for 454 seqs
1680 //or BARCODE atgcatgc atgcatgc groupName - for illumina data that has forward and reverse info
1682 while (!inOligos.eof()) {
1683 char c = inOligos.get();
1684 if (c == 10 || c == 13){ break; }
1685 else if (c == 32 || c == 9){;} //space or tab
1689 //then this is illumina data with 4 columns
1691 string reverseBarcode = reverseOligo(group); //reverse barcode
1694 //check for repeat barcodes
1695 map<string, int>::iterator itBar = rbarcodes.find(reverseBarcode);
1696 if (itBar != rbarcodes.end()) { m->mothurOut("barcode " + reverseBarcode + " is in your oligos file already."); m->mothurOutEndLine(); }
1698 rbarcodes[reverseBarcode]=indexBarcode;
1701 //check for repeat barcodes
1702 map<string, int>::iterator itBar = barcodes.find(oligo);
1703 if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
1705 barcodes[oligo]=indexBarcode; indexBarcode++;
1706 barcodeNameVector.push_back(group);
1708 }else if(type == "LINKER"){
1709 linker.push_back(oligo);
1710 }else if(type == "SPACER"){
1711 spacer.push_back(oligo);
1713 else{ m->mothurOut("[WARNING]: " + type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine(); }
1715 m->gobble(inOligos);
1719 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ split = 1; }
1721 //add in potential combos
1722 if(barcodeNameVector.size() == 0){
1724 barcodeNameVector.push_back("");
1727 if(primerNameVector.size() == 0){
1729 primerNameVector.push_back("");
1732 filehandles.resize(barcodeNameVector.size());
1733 for(int i=0;i<filehandles.size();i++){
1734 filehandles[i].assign(primerNameVector.size(), "");
1738 set<string> uniqueNames; //used to cleanup outputFileNames
1739 for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
1740 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
1742 string primerName = primerNameVector[itPrimer->second];
1743 string barcodeName = barcodeNameVector[itBar->second];
1745 string comboGroupName = "";
1746 string fastaFileName = "";
1747 string qualFileName = "";
1748 string nameFileName = "";
1750 if(primerName == ""){
1751 comboGroupName = barcodeNameVector[itBar->second];
1754 if(barcodeName == ""){
1755 comboGroupName = primerNameVector[itPrimer->second];
1758 comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
1763 string thisFilename = outputDir + m->getRootName(m->getSimpleName(currentFileName)) + comboGroupName + "." + getOutputFileNameTag("sff");
1764 if (uniqueNames.count(thisFilename) == 0) {
1765 outputNames.push_back(thisFilename);
1766 outputTypes["sff"].push_back(thisFilename);
1767 uniqueNames.insert(thisFilename);
1770 filehandles[itBar->second][itPrimer->second] = thisFilename;
1771 m->openOutputFile(thisFilename, temp); temp.close();
1775 numFPrimers = primers.size();
1776 numLinkers = linker.size();
1777 numSpacers = spacer.size();
1778 noMatchFile = outputDir + m->getRootName(m->getSimpleName(currentFileName)) + "scrap." + getOutputFileNameTag("sff");
1779 m->mothurRemove(noMatchFile);
1781 bool allBlank = true;
1782 for (int i = 0; i < barcodeNameVector.size(); i++) {
1783 if (barcodeNameVector[i] != "") {
1788 for (int i = 0; i < primerNameVector.size(); i++) {
1789 if (primerNameVector[i] != "") {
1795 filehandlesHeaders.resize(filehandles.size());
1796 numSplitReads.resize(filehandles.size());
1797 for (int i = 0; i < filehandles.size(); i++) {
1798 numSplitReads[i].resize(filehandles[i].size(), 0);
1799 for (int j = 0; j < filehandles[i].size(); j++) {
1800 filehandlesHeaders[i].push_back(filehandles[i][j]+"headers");
1805 m->mothurOut("[WARNING]: your oligos file does not contain any group names. mothur will not create a split the sff file."); m->mothurOutEndLine();
1813 catch(exception& e) {
1814 m->errorOut(e, "SffInfoCommand", "readOligos");
1818 //********************************************************************/
1819 string SffInfoCommand::reverseOligo(string oligo){
1821 string reverse = "";
1823 for(int i=oligo.length()-1;i>=0;i--){
1825 if(oligo[i] == 'A') { reverse += 'T'; }
1826 else if(oligo[i] == 'T'){ reverse += 'A'; }
1827 else if(oligo[i] == 'U'){ reverse += 'A'; }
1829 else if(oligo[i] == 'G'){ reverse += 'C'; }
1830 else if(oligo[i] == 'C'){ reverse += 'G'; }
1832 else if(oligo[i] == 'R'){ reverse += 'Y'; }
1833 else if(oligo[i] == 'Y'){ reverse += 'R'; }
1835 else if(oligo[i] == 'M'){ reverse += 'K'; }
1836 else if(oligo[i] == 'K'){ reverse += 'M'; }
1838 else if(oligo[i] == 'W'){ reverse += 'W'; }
1839 else if(oligo[i] == 'S'){ reverse += 'S'; }
1841 else if(oligo[i] == 'B'){ reverse += 'V'; }
1842 else if(oligo[i] == 'V'){ reverse += 'B'; }
1844 else if(oligo[i] == 'D'){ reverse += 'H'; }
1845 else if(oligo[i] == 'H'){ reverse += 'D'; }
1847 else { reverse += 'N'; }
1853 catch(exception& e) {
1854 m->errorOut(e, "SffInfoCommand", "reverseOligo");
1859 //**********************************************************************************************************************