5 * Created by westcott on 7/7/10.
\r
6 * Copyright 2010 Schloss Lab. All rights reserved.
\r
10 #include "sffinfocommand.h"
\r
11 #include "endiannessmacros.h"
\r
12 #include "trimoligos.h"
\r
13 #include "sequence.hpp"
\r
14 #include "qualityscores.h"
\r
16 //**********************************************************************************************************************
\r
17 vector<string> SffInfoCommand::setParameters(){
\r
19 CommandParameter psff("sff", "InputTypes", "", "", "none", "none", "none","",false,false,true); parameters.push_back(psff);
\r
20 CommandParameter poligos("oligos", "InputTypes", "", "", "oligosGroup", "none", "none","",false,false); parameters.push_back(poligos);
\r
21 CommandParameter pgroup("group", "InputTypes", "", "", "oligosGroup", "none", "none","",false,false); parameters.push_back(pgroup);
\r
22 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(paccnos);
\r
23 CommandParameter psfftxt("sfftxt", "String", "", "", "", "", "","",false,false); parameters.push_back(psfftxt);
\r
24 CommandParameter pflow("flow", "Boolean", "", "T", "", "", "","flow",false,false); parameters.push_back(pflow);
\r
25 CommandParameter ptrim("trim", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(ptrim);
\r
26 CommandParameter pfasta("fasta", "Boolean", "", "T", "", "", "","fasta",false,false); parameters.push_back(pfasta);
\r
27 CommandParameter pqfile("qfile", "Boolean", "", "T", "", "", "","qfile",false,false); parameters.push_back(pqfile);
\r
28 CommandParameter ppdiffs("pdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(ppdiffs);
\r
29 CommandParameter pbdiffs("bdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(pbdiffs);
\r
30 CommandParameter pldiffs("ldiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(pldiffs);
\r
31 CommandParameter psdiffs("sdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(psdiffs);
\r
32 CommandParameter ptdiffs("tdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(ptdiffs);
\r
33 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
\r
34 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
\r
36 vector<string> myArray;
\r
37 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
\r
40 catch(exception& e) {
\r
41 m->errorOut(e, "SffInfoCommand", "setParameters");
\r
45 //**********************************************************************************************************************
\r
46 string SffInfoCommand::getHelpString(){
\r
48 string helpString = "";
\r
49 helpString += "The sffinfo command reads a sff file and extracts the sequence data, or you can use it to parse a sfftxt file.\n";
\r
50 helpString += "The sffinfo command parameters are sff, fasta, qfile, accnos, flow, sfftxt, oligos, group, bdiffs, tdiffs, ldiffs, sdiffs, pdiffs and trim. sff is required. \n";
\r
51 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";
\r
52 helpString += "The fasta parameter allows you to indicate if you would like a fasta formatted file generated. Default=True. \n";
\r
53 helpString += "The qfile parameter allows you to indicate if you would like a quality file generated. Default=True. \n";
\r
54 helpString += "The oligos parameter allows you to provide an oligos file to split your sff file into separate sff files by barcode. \n";
\r
55 helpString += "The group parameter allows you to provide a group file to split your sff file into separate sff files by group. \n";
\r
56 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";
\r
57 helpString += "The bdiffs parameter is used to specify the number of differences allowed in the barcode. The default is 0.\n";
\r
58 helpString += "The pdiffs parameter is used to specify the number of differences allowed in the primer. The default is 0.\n";
\r
59 helpString += "The ldiffs parameter is used to specify the number of differences allowed in the linker. The default is 0.\n";
\r
60 helpString += "The sdiffs parameter is used to specify the number of differences allowed in the spacer. The default is 0.\n";
\r
61 helpString += "The flow parameter allows you to indicate if you would like a flowgram file generated. Default=True. \n";
\r
62 helpString += "The sfftxt parameter allows you to indicate if you would like a sff.txt file generated. Default=False. \n";
\r
63 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";
\r
64 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";
\r
65 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";
\r
66 helpString += "Example sffinfo(sff=mySffFile.sff, trim=F).\n";
\r
67 helpString += "Note: No spaces between parameter labels (i.e. sff), '=' and parameters (i.e.yourSffFileName).\n";
\r
70 catch(exception& e) {
\r
71 m->errorOut(e, "SffInfoCommand", "getHelpString");
\r
76 //**********************************************************************************************************************
\r
77 string SffInfoCommand::getOutputPattern(string type) {
\r
79 string pattern = "";
\r
81 if (type == "fasta") { pattern = "[filename],fasta-[filename],[tag],fasta"; }
\r
82 else if (type == "flow") { pattern = "[filename],flow"; }
\r
83 else if (type == "sfftxt") { pattern = "[filename],sff.txt"; }
\r
84 else if (type == "sff") { pattern = "[filename],[group],sff"; }
\r
85 else if (type == "qfile") { pattern = "[filename],qual-[filename],[tag],qual"; }
\r
86 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
\r
90 catch(exception& e) {
\r
91 m->errorOut(e, "SffInfoCommand", "getOutputPattern");
\r
95 //**********************************************************************************************************************
\r
96 SffInfoCommand::SffInfoCommand(){
\r
98 abort = true; calledHelp = true;
\r
100 vector<string> tempOutNames;
\r
101 outputTypes["fasta"] = tempOutNames;
\r
102 outputTypes["flow"] = tempOutNames;
\r
103 outputTypes["sfftxt"] = tempOutNames;
\r
104 outputTypes["qfile"] = tempOutNames;
\r
105 outputTypes["sff"] = tempOutNames;
\r
107 catch(exception& e) {
\r
108 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
\r
112 //**********************************************************************************************************************
\r
114 SffInfoCommand::SffInfoCommand(string option) {
\r
116 abort = false; calledHelp = false;
\r
117 hasAccnos = false; hasOligos = false; hasGroup = false;
\r
120 //allow user to run help
\r
121 if(option == "help") { help(); abort = true; calledHelp = true; }
\r
122 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
\r
125 //valid paramters for this command
\r
126 vector<string> myArray = setParameters();
\r
128 OptionParser parser(option);
\r
129 map<string, string> parameters = parser.getParameters();
\r
131 ValidParameters validParameter;
\r
132 //check to make sure all parameters are valid for command
\r
133 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
\r
134 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
\r
137 //initialize outputTypes
\r
138 vector<string> tempOutNames;
\r
139 outputTypes["fasta"] = tempOutNames;
\r
140 outputTypes["flow"] = tempOutNames;
\r
141 outputTypes["sfftxt"] = tempOutNames;
\r
142 outputTypes["qfile"] = tempOutNames;
\r
143 outputTypes["sff"] = tempOutNames;
\r
145 //if the user changes the output directory command factory will send this info to us in the output parameter
\r
146 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
\r
148 //if the user changes the input directory command factory will send this info to us in the output parameter
\r
149 string inputDir = validParameter.validFile(parameters, "inputdir", false); if (inputDir == "not found"){ inputDir = ""; }
\r
151 sffFilename = validParameter.validFile(parameters, "sff", false);
\r
152 if (sffFilename == "not found") { sffFilename = ""; }
\r
154 m->splitAtDash(sffFilename, filenames);
\r
156 //go through files and make sure they are good, if not, then disregard them
\r
157 for (int i = 0; i < filenames.size(); i++) {
\r
158 bool ignore = false;
\r
159 if (filenames[i] == "current") {
\r
160 filenames[i] = m->getSFFFile();
\r
161 if (filenames[i] != "") { m->mothurOut("Using " + filenames[i] + " as input file for the sff parameter where you had given current."); m->mothurOutEndLine(); }
\r
163 m->mothurOut("You have no current sfffile, ignoring current."); m->mothurOutEndLine(); ignore=true;
\r
164 //erase from file list
\r
165 filenames.erase(filenames.begin()+i);
\r
171 if (inputDir != "") {
\r
172 string path = m->hasPath(filenames[i]);
\r
173 //if the user has not given a path then, add inputdir. else leave path alone.
\r
174 if (path == "") { filenames[i] = inputDir + filenames[i]; }
\r
178 int ableToOpen = m->openInputFile(filenames[i], in, "noerror");
\r
180 //if you can't open it, try default location
\r
181 if (ableToOpen == 1) {
\r
182 if (m->getDefaultPath() != "") { //default path is set
\r
183 string tryPath = m->getDefaultPath() + m->getSimpleName(filenames[i]);
\r
184 m->mothurOut("Unable to open " + filenames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
\r
186 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
\r
188 filenames[i] = tryPath;
\r
192 //if you can't open it, try default location
\r
193 if (ableToOpen == 1) {
\r
194 if (m->getOutputDir() != "") { //default path is set
\r
195 string tryPath = m->getOutputDir() + m->getSimpleName(filenames[i]);
\r
196 m->mothurOut("Unable to open " + filenames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
\r
198 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
\r
200 filenames[i] = tryPath;
\r
206 if (ableToOpen == 1) {
\r
207 m->mothurOut("Unable to open " + filenames[i] + ". It will be disregarded."); m->mothurOutEndLine();
\r
208 //erase from file list
\r
209 filenames.erase(filenames.begin()+i);
\r
211 }else { m->setSFFFile(filenames[i]); }
\r
215 //make sure there is at least one valid file left
\r
216 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
\r
219 accnosName = validParameter.validFile(parameters, "accnos", false);
\r
220 if (accnosName == "not found") { accnosName = ""; }
\r
223 m->splitAtDash(accnosName, accnosFileNames);
\r
225 //go through files and make sure they are good, if not, then disregard them
\r
226 for (int i = 0; i < accnosFileNames.size(); i++) {
\r
227 bool ignore = false;
\r
228 if (accnosFileNames[i] == "current") {
\r
229 accnosFileNames[i] = m->getAccnosFile();
\r
230 if (accnosFileNames[i] != "") { m->mothurOut("Using " + accnosFileNames[i] + " as input file for the accnos parameter where you had given current."); m->mothurOutEndLine(); }
\r
232 m->mothurOut("You have no current accnosfile, ignoring current."); m->mothurOutEndLine(); ignore=true;
\r
233 //erase from file list
\r
234 accnosFileNames.erase(accnosFileNames.begin()+i);
\r
241 if (inputDir != "") {
\r
242 string path = m->hasPath(accnosFileNames[i]);
\r
243 //if the user has not given a path then, add inputdir. else leave path alone.
\r
244 if (path == "") { accnosFileNames[i] = inputDir + accnosFileNames[i]; }
\r
248 int ableToOpen = m->openInputFile(accnosFileNames[i], in, "noerror");
\r
250 //if you can't open it, try default location
\r
251 if (ableToOpen == 1) {
\r
252 if (m->getDefaultPath() != "") { //default path is set
\r
253 string tryPath = m->getDefaultPath() + m->getSimpleName(accnosFileNames[i]);
\r
254 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
\r
256 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
\r
258 accnosFileNames[i] = tryPath;
\r
261 //if you can't open it, try default location
\r
262 if (ableToOpen == 1) {
\r
263 if (m->getOutputDir() != "") { //default path is set
\r
264 string tryPath = m->getOutputDir() + m->getSimpleName(accnosFileNames[i]);
\r
265 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
\r
267 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
\r
269 accnosFileNames[i] = tryPath;
\r
274 if (ableToOpen == 1) {
\r
275 m->mothurOut("Unable to open " + accnosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
\r
276 //erase from file list
\r
277 accnosFileNames.erase(accnosFileNames.begin()+i);
\r
283 //make sure there is at least one valid file left
\r
284 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
\r
287 oligosfile = validParameter.validFile(parameters, "oligos", false);
\r
288 if (oligosfile == "not found") { oligosfile = ""; }
\r
291 m->splitAtDash(oligosfile, oligosFileNames);
\r
293 //go through files and make sure they are good, if not, then disregard them
\r
294 for (int i = 0; i < oligosFileNames.size(); i++) {
\r
295 bool ignore = false;
\r
296 if (oligosFileNames[i] == "current") {
\r
297 oligosFileNames[i] = m->getOligosFile();
\r
298 if (oligosFileNames[i] != "") { m->mothurOut("Using " + oligosFileNames[i] + " as input file for the oligos parameter where you had given current."); m->mothurOutEndLine(); }
\r
300 m->mothurOut("You have no current oligosfile, ignoring current."); m->mothurOutEndLine(); ignore=true;
\r
301 //erase from file list
\r
302 oligosFileNames.erase(oligosFileNames.begin()+i);
\r
309 if (inputDir != "") {
\r
310 string path = m->hasPath(oligosFileNames[i]);
\r
311 //if the user has not given a path then, add inputdir. else leave path alone.
\r
312 if (path == "") { oligosFileNames[i] = inputDir + oligosFileNames[i]; }
\r
316 int ableToOpen = m->openInputFile(oligosFileNames[i], in, "noerror");
\r
318 //if you can't open it, try default location
\r
319 if (ableToOpen == 1) {
\r
320 if (m->getDefaultPath() != "") { //default path is set
\r
321 string tryPath = m->getDefaultPath() + m->getSimpleName(oligosFileNames[i]);
\r
322 m->mothurOut("Unable to open " + oligosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
\r
324 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
\r
326 oligosFileNames[i] = tryPath;
\r
329 //if you can't open it, try default location
\r
330 if (ableToOpen == 1) {
\r
331 if (m->getOutputDir() != "") { //default path is set
\r
332 string tryPath = m->getOutputDir() + m->getSimpleName(oligosFileNames[i]);
\r
333 m->mothurOut("Unable to open " + oligosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
\r
335 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
\r
337 oligosFileNames[i] = tryPath;
\r
342 if (ableToOpen == 1) {
\r
343 m->mothurOut("Unable to open " + oligosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
\r
344 //erase from file list
\r
345 oligosFileNames.erase(oligosFileNames.begin()+i);
\r
351 //make sure there is at least one valid file left
\r
352 if (oligosFileNames.size() == 0) { m->mothurOut("no valid oligos files."); m->mothurOutEndLine(); abort = true; }
\r
355 groupfile = validParameter.validFile(parameters, "group", false);
\r
356 if (groupfile == "not found") { groupfile = ""; }
\r
359 m->splitAtDash(groupfile, groupFileNames);
\r
361 //go through files and make sure they are good, if not, then disregard them
\r
362 for (int i = 0; i < groupFileNames.size(); i++) {
\r
363 bool ignore = false;
\r
364 if (groupFileNames[i] == "current") {
\r
365 groupFileNames[i] = m->getGroupFile();
\r
366 if (groupFileNames[i] != "") { m->mothurOut("Using " + groupFileNames[i] + " as input file for the group parameter where you had given current."); m->mothurOutEndLine(); }
\r
368 m->mothurOut("You have no current group file, ignoring current."); m->mothurOutEndLine(); ignore=true;
\r
369 //erase from file list
\r
370 groupFileNames.erase(groupFileNames.begin()+i);
\r
377 if (inputDir != "") {
\r
378 string path = m->hasPath(groupFileNames[i]);
\r
379 //if the user has not given a path then, add inputdir. else leave path alone.
\r
380 if (path == "") { groupFileNames[i] = inputDir + groupFileNames[i]; }
\r
384 int ableToOpen = m->openInputFile(groupFileNames[i], in, "noerror");
\r
386 //if you can't open it, try default location
\r
387 if (ableToOpen == 1) {
\r
388 if (m->getDefaultPath() != "") { //default path is set
\r
389 string tryPath = m->getDefaultPath() + m->getSimpleName(groupFileNames[i]);
\r
390 m->mothurOut("Unable to open " + groupFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
\r
392 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
\r
394 groupFileNames[i] = tryPath;
\r
397 //if you can't open it, try default location
\r
398 if (ableToOpen == 1) {
\r
399 if (m->getOutputDir() != "") { //default path is set
\r
400 string tryPath = m->getOutputDir() + m->getSimpleName(groupFileNames[i]);
\r
401 m->mothurOut("Unable to open " + groupFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
\r
403 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
\r
405 groupFileNames[i] = tryPath;
\r
410 if (ableToOpen == 1) {
\r
411 m->mothurOut("Unable to open " + groupFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
\r
412 //erase from file list
\r
413 groupFileNames.erase(groupFileNames.begin()+i);
\r
419 //make sure there is at least one valid file left
\r
420 if (groupFileNames.size() == 0) { m->mothurOut("no valid group files."); m->mothurOutEndLine(); abort = true; }
\r
425 if (groupFileNames.size() != filenames.size()) { abort = true; m->mothurOut("If you provide a group file, you must have one for each sff file."); m->mothurOutEndLine(); }
\r
430 if (oligosFileNames.size() != filenames.size()) { abort = true; m->mothurOut("If you provide an oligos file, you must have one for each sff file."); m->mothurOutEndLine(); }
\r
433 if (hasGroup && hasOligos) { m->mothurOut("You must enter ONLY ONE of the following: oligos or group."); m->mothurOutEndLine(); abort = true;}
\r
436 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(); }
\r
439 string temp = validParameter.validFile(parameters, "qfile", false); if (temp == "not found"){ temp = "T"; }
\r
440 qual = m->isTrue(temp);
\r
442 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ temp = "T"; }
\r
443 fasta = m->isTrue(temp);
\r
445 temp = validParameter.validFile(parameters, "flow", false); if (temp == "not found"){ temp = "T"; }
\r
446 flow = m->isTrue(temp);
\r
448 temp = validParameter.validFile(parameters, "trim", false); if (temp == "not found"){ temp = "T"; }
\r
449 trim = m->isTrue(temp);
\r
451 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found") { temp = "0"; }
\r
452 m->mothurConvert(temp, bdiffs);
\r
454 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found") { temp = "0"; }
\r
455 m->mothurConvert(temp, pdiffs);
\r
457 temp = validParameter.validFile(parameters, "ldiffs", false); if (temp == "not found") { temp = "0"; }
\r
458 m->mothurConvert(temp, ldiffs);
\r
460 temp = validParameter.validFile(parameters, "sdiffs", false); if (temp == "not found") { temp = "0"; }
\r
461 m->mothurConvert(temp, sdiffs);
\r
463 temp = validParameter.validFile(parameters, "tdiffs", false); if (temp == "not found") { int tempTotal = pdiffs + bdiffs + ldiffs + sdiffs; temp = toString(tempTotal); }
\r
464 m->mothurConvert(temp, tdiffs);
\r
466 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs + ldiffs + sdiffs; }
\r
468 temp = validParameter.validFile(parameters, "sfftxt", false);
\r
469 if (temp == "not found") { temp = "F"; sfftxt = false; sfftxtFilename = ""; }
\r
470 else if (m->isTrue(temp)) { sfftxt = true; sfftxtFilename = ""; }
\r
472 //you are a filename
\r
473 if (inputDir != "") {
\r
474 map<string,string>::iterator it = parameters.find("sfftxt");
\r
475 //user has given a template file
\r
476 if(it != parameters.end()){
\r
477 string path = m->hasPath(it->second);
\r
478 //if the user has not given a path then, add inputdir. else leave path alone.
\r
479 if (path == "") { parameters["sfftxt"] = inputDir + it->second; }
\r
483 sfftxtFilename = validParameter.validFile(parameters, "sfftxt", true);
\r
484 if (sfftxtFilename == "not found") { sfftxtFilename = ""; }
\r
485 else if (sfftxtFilename == "not open") { sfftxtFilename = ""; }
\r
488 if ((sfftxtFilename == "") && (filenames.size() == 0)) {
\r
489 //if there is a current sff file, use it
\r
490 string filename = m->getSFFFile();
\r
491 if (filename != "") { filenames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the sff parameter."); m->mothurOutEndLine(); }
\r
492 else { m->mothurOut("[ERROR]: you must provide a valid sff or sfftxt file."); m->mothurOutEndLine(); abort=true; }
\r
498 catch(exception& e) {
\r
499 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
\r
503 //**********************************************************************************************************************
\r
504 int SffInfoCommand::execute(){
\r
506 if (abort == true) { if (calledHelp) { return 0; } return 2; }
\r
508 for (int s = 0; s < filenames.size(); s++) {
\r
510 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
\r
512 int start = time(NULL);
\r
514 filenames[s] = m->getFullPathName(filenames[s]);
\r
515 m->mothurOut("Extracting info from " + filenames[s] + " ..." ); m->mothurOutEndLine();
\r
517 string accnos = "";
\r
518 if (hasAccnos) { accnos = accnosFileNames[s]; }
\r
520 string oligos = "";
\r
521 if (hasOligos) { oligos = oligosFileNames[s]; }
\r
522 if (hasGroup) { oligos = groupFileNames[s]; }
\r
524 int numReads = extractSffInfo(filenames[s], accnos, oligos);
\r
526 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to extract " + toString(numReads) + ".");
\r
529 if (sfftxtFilename != "") { parseSffTxt(); }
\r
531 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
\r
533 //set fasta file as new current fastafile
\r
534 string current = "";
\r
535 itTypes = outputTypes.find("fasta");
\r
536 if (itTypes != outputTypes.end()) {
\r
537 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
\r
540 itTypes = outputTypes.find("qfile");
\r
541 if (itTypes != outputTypes.end()) {
\r
542 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
\r
545 itTypes = outputTypes.find("flow");
\r
546 if (itTypes != outputTypes.end()) {
\r
547 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFlowFile(current); }
\r
550 //report output filenames
\r
551 m->mothurOutEndLine();
\r
552 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
\r
553 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
\r
554 m->mothurOutEndLine();
\r
558 catch(exception& e) {
\r
559 m->errorOut(e, "SffInfoCommand", "execute");
\r
563 //**********************************************************************************************************************
\r
564 int SffInfoCommand::extractSffInfo(string input, string accnos, string oligos){
\r
566 currentFileName = input;
\r
567 if (outputDir == "") { outputDir += m->hasPath(input); }
\r
569 if (accnos != "") { readAccnosFile(accnos); }
\r
570 else { seqNames.clear(); }
\r
572 if (hasOligos) { readOligos(oligos); split = 2; }
\r
573 if (hasGroup) { readGroup(oligos); split = 2; }
\r
575 ofstream outSfftxt, outFasta, outQual, outFlow;
\r
576 string outFastaFileName, outQualFileName;
\r
577 string rootName = outputDir + m->getRootName(m->getSimpleName(input));
\r
578 if(rootName.find_last_of(".") == rootName.npos){ rootName += "."; }
\r
580 map<string, string> variables;
\r
581 variables["[filename]"] = rootName;
\r
582 string sfftxtFileName = getOutputFileName("sfftxt",variables);
\r
583 string outFlowFileName = getOutputFileName("flow",variables);
\r
584 if (!trim) { variables["[tag]"] = "raw"; }
\r
585 outFastaFileName = getOutputFileName("fasta",variables);
\r
586 outQualFileName = getOutputFileName("qfile",variables);
\r
588 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); }
\r
589 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
\r
590 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName); }
\r
591 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); }
\r
594 m->openInputFileBinary(input, in);
\r
596 CommonHeader header;
\r
597 readCommonHeader(in, header);
\r
601 //check magic number and version
\r
602 if (header.magicNumber != 779314790) { m->mothurOut("Magic Number is not correct, not a valid .sff file"); m->mothurOutEndLine(); return count; }
\r
603 if (header.version != "0001") { m->mothurOut("Version is not supported, only support version 0001."); m->mothurOutEndLine(); return count; }
\r
605 //print common header
\r
606 if (sfftxt) { printCommonHeader(outSfftxt, header); }
\r
607 if (flow) { outFlow << header.numFlowsPerRead << endl; }
\r
609 //read through the sff file
\r
610 while (!in.eof()) {
\r
615 seqRead read; Header readheader;
\r
616 readSeqData(in, read, header.numFlowsPerRead, readheader);
\r
618 bool okay = sanityCheck(readheader, read);
\r
619 if (!okay) { break; }
\r
621 //if you have provided an accosfile and this seq is not in it, then dont print
\r
622 if (seqNames.size() != 0) { if (seqNames.count(readheader.name) == 0) { print = false; } }
\r
626 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read, readheader); }
\r
627 if (fasta) { printFastaSeqData(outFasta, read, readheader); }
\r
628 if (qual) { printQualSeqData(outQual, read, readheader); }
\r
629 if (flow) { printFlowSeqData(outFlow, read, readheader); }
\r
635 if((count+1) % 10000 == 0){ m->mothurOut(toString(count+1)); m->mothurOutEndLine(); }
\r
637 if (m->control_pressed) { count = 0; break; }
\r
639 if (count >= header.numReads) { break; }
\r
643 if (!m->control_pressed) { if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } }
\r
647 if (sfftxt) { outSfftxt.close(); }
\r
648 if (fasta) { outFasta.close(); }
\r
649 if (qual) { outQual.close(); }
\r
650 if (flow) { outFlow.close(); }
\r
653 //create new common headers for each file with the correct number of reads
\r
654 adjustCommonHeader(header);
\r
656 if (hasGroup) { delete groupMap; }
\r
658 //cout << "here" << endl;
\r
659 map<string, string>::iterator it;
\r
660 set<string> namesToRemove;
\r
661 for(int i=0;i<filehandles.size();i++){
\r
662 for(int j=0;j<filehandles[0].size();j++){
\r
663 //cout << i << '\t' << '\t' << j << '\t' << filehandles[i][j] << endl;
\r
664 if (filehandles[i][j] != "") {
\r
665 if (namesToRemove.count(filehandles[i][j]) == 0) {
\r
666 if(m->isBlank(filehandles[i][j])){
\r
667 //cout << i << '\t' << '\t' << j << '\t' << filehandles[i][j] << " is blank removing" << endl;
\r
668 m->mothurRemove(filehandles[i][j]);
\r
669 m->mothurRemove(filehandlesHeaders[i][j]);
\r
670 namesToRemove.insert(filehandles[i][j]);
\r
676 //cout << "here2" << endl;
\r
677 //append new header to reads
\r
678 for (int i = 0; i < filehandles.size(); i++) {
\r
679 for (int j = 0; j < filehandles[i].size(); j++) {
\r
680 m->appendBinaryFiles(filehandles[i][j], filehandlesHeaders[i][j]);
\r
681 m->renameFile(filehandlesHeaders[i][j], filehandles[i][j]);
\r
682 m->mothurRemove(filehandlesHeaders[i][j]);
\r
683 //cout << i << '\t' << '\t' << j << '\t' << filehandles[i][j] << " done appending headers and removing " << filehandlesHeaders[i][j] << endl;
\r
684 if (numSplitReads[i][j] == 0) { m->mothurRemove(filehandles[i][j]); }
\r
687 //cout << "here3" << endl;
\r
688 //remove names for outputFileNames, just cleans up the output
\r
689 for(int i = 0; i < outputNames.size(); i++) {
\r
690 if (namesToRemove.count(outputNames[i]) != 0) {
\r
691 //cout << "erasing " << i << '\t' << outputNames[i] << endl;
\r
692 outputNames.erase(outputNames.begin()+i);
\r
696 //cout << "here4" << endl;
\r
697 if(m->isBlank(noMatchFile)){ m->mothurRemove(noMatchFile); }
\r
698 else { outputNames.push_back(noMatchFile); outputTypes["sff"].push_back(noMatchFile); }
\r
703 catch(exception& e) {
\r
704 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
\r
708 //**********************************************************************************************************************
\r
709 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
\r
714 //read magic number
\r
716 in.read(buffer, 4);
\r
717 header.magicNumber = be_int4(*(unsigned int *)(&buffer));
\r
721 in.read(buffer9, 4);
\r
722 header.version = "";
\r
723 for (int i = 0; i < 4; i++) { header.version += toString((int)(buffer9[i])); }
\r
727 in.read(buffer2, 8);
\r
728 header.indexOffset = be_int8(*(unsigned long long *)(&buffer2));
\r
730 //read index length
\r
732 in.read(buffer3, 4);
\r
733 header.indexLength = be_int4(*(unsigned int *)(&buffer3));
\r
737 in.read(buffer4, 4);
\r
738 header.numReads = be_int4(*(unsigned int *)(&buffer4));
\r
740 if (m->debug) { m->mothurOut("[DEBUG]: numReads = " + toString(header.numReads) + "\n"); }
\r
742 //read header length
\r
744 in.read(buffer5, 2);
\r
745 header.headerLength = be_int2(*(unsigned short *)(&buffer5));
\r
749 in.read(buffer6, 2);
\r
750 header.keyLength = be_int2(*(unsigned short *)(&buffer6));
\r
752 //read number of flow reads
\r
754 in.read(buffer7, 2);
\r
755 header.numFlowsPerRead = be_int2(*(unsigned short *)(&buffer7));
\r
759 in.read(buffer8, 1);
\r
760 header.flogramFormatCode = (int)(buffer8[0]);
\r
763 char* tempBuffer = new char[header.numFlowsPerRead];
\r
764 in.read(&(*tempBuffer), header.numFlowsPerRead);
\r
765 header.flowChars = tempBuffer;
\r
766 if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead); }
\r
767 delete[] tempBuffer;
\r
770 char* tempBuffer2 = new char[header.keyLength];
\r
771 in.read(&(*tempBuffer2), header.keyLength);
\r
772 header.keySequence = tempBuffer2;
\r
773 if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength); }
\r
774 delete[] tempBuffer2;
\r
776 /* Pad to 8 chars */
\r
777 unsigned long long spotInFile = in.tellg();
\r
778 unsigned long long spot = (spotInFile + 7)& ~7; // ~ inverts
\r
782 m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
\r
788 catch(exception& e) {
\r
789 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
\r
793 //**********************************************************************************************************************
\r
794 int SffInfoCommand::adjustCommonHeader(CommonHeader header){
\r
796 string endian = m->findEdianness();
\r
797 char* mybuffer = new char[4];
\r
799 m->openInputFileBinary(currentFileName, in);
\r
801 ofstream outNoMatchHeader;
\r
802 string tempNoHeader = "tempNoMatchHeader";
\r
803 m->openOutputFileBinary(tempNoHeader, outNoMatchHeader);
\r
806 in.read(mybuffer,4);
\r
807 for (int i = 0; i < filehandlesHeaders.size(); i++) {
\r
808 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
\r
810 m->openOutputFileBinaryAppend(filehandlesHeaders[i][j], out);
\r
811 out.write(mybuffer, in.gcount());
\r
815 outNoMatchHeader.write(mybuffer, in.gcount());
\r
819 mybuffer = new char[4];
\r
820 in.read(mybuffer,4);
\r
821 for (int i = 0; i < filehandlesHeaders.size(); i++) {
\r
822 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
\r
824 m->openOutputFileBinaryAppend(filehandlesHeaders[i][j], out);
\r
825 out.write(mybuffer, in.gcount());
\r
829 outNoMatchHeader.write(mybuffer, in.gcount());
\r
833 mybuffer = new char[8];
\r
834 in.read(mybuffer,8);
\r
835 unsigned long long offset = 0;
\r
836 char* thisbuffer = new char[8];
\r
837 thisbuffer[0] = (offset >> 56) & 0xFF;
\r
838 thisbuffer[1] = (offset >> 48) & 0xFF;
\r
839 thisbuffer[2] = (offset >> 40) & 0xFF;
\r
840 thisbuffer[3] = (offset >> 32) & 0xFF;
\r
841 thisbuffer[4] = (offset >> 24) & 0xFF;
\r
842 thisbuffer[5] = (offset >> 16) & 0xFF;
\r
843 thisbuffer[6] = (offset >> 8) & 0xFF;
\r
844 thisbuffer[7] = offset & 0xFF;
\r
845 for (int i = 0; i < filehandlesHeaders.size(); i++) {
\r
846 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
\r
848 m->openOutputFileBinaryAppend(filehandlesHeaders[i][j], out);
\r
849 out.write(thisbuffer, 8);
\r
853 outNoMatchHeader.write(thisbuffer, 8);
\r
854 delete[] thisbuffer;
\r
858 //read index length
\r
859 mybuffer = new char[4];
\r
860 in.read(mybuffer,4);
\r
862 char* thisbuffer2 = new char[4];
\r
863 thisbuffer2[0] = (offset >> 24) & 0xFF;
\r
864 thisbuffer2[1] = (offset >> 16) & 0xFF;
\r
865 thisbuffer2[2] = (offset >> 8) & 0xFF;
\r
866 thisbuffer2[3] = offset & 0xFF;
\r
867 for (int i = 0; i < filehandlesHeaders.size(); i++) {
\r
868 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
\r
870 m->openOutputFileBinaryAppend(filehandlesHeaders[i][j], out);
\r
871 out.write(thisbuffer2, 4);
\r
875 outNoMatchHeader.write(thisbuffer2, 4);
\r
876 delete[] thisbuffer2;
\r
880 mybuffer = new char[4];
\r
881 in.read(mybuffer,4);
\r
883 for (int i = 0; i < filehandlesHeaders.size(); i++) {
\r
884 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
\r
885 char* thisbuffer = new char[4];
\r
886 if (endian == "BIG_ENDIAN") {
\r
887 thisbuffer[0] = (numSplitReads[i][j] >> 24) & 0xFF;
\r
888 thisbuffer[1] = (numSplitReads[i][j] >> 16) & 0xFF;
\r
889 thisbuffer[2] = (numSplitReads[i][j] >> 8) & 0xFF;
\r
890 thisbuffer[3] = numSplitReads[i][j] & 0xFF;
\r
892 thisbuffer[0] = numSplitReads[i][j] & 0xFF;
\r
893 thisbuffer[1] = (numSplitReads[i][j] >> 8) & 0xFF;
\r
894 thisbuffer[2] = (numSplitReads[i][j] >> 16) & 0xFF;
\r
895 thisbuffer[3] = (numSplitReads[i][j] >> 24) & 0xFF;
\r
898 m->openOutputFileBinaryAppend(filehandlesHeaders[i][j], out);
\r
899 out.write(thisbuffer, 4);
\r
901 delete[] thisbuffer;
\r
904 char* thisbuffer3 = new char[4];
\r
905 if (endian == "BIG_ENDIAN") {
\r
906 thisbuffer3[0] = (numNoMatch >> 24) & 0xFF;
\r
907 thisbuffer3[1] = (numNoMatch >> 16) & 0xFF;
\r
908 thisbuffer3[2] = (numNoMatch >> 8) & 0xFF;
\r
909 thisbuffer3[3] = numNoMatch & 0xFF;
\r
911 thisbuffer3[0] = numNoMatch & 0xFF;
\r
912 thisbuffer3[1] = (numNoMatch >> 8) & 0xFF;
\r
913 thisbuffer3[2] = (numNoMatch >> 16) & 0xFF;
\r
914 thisbuffer3[3] = (numNoMatch >> 24) & 0xFF;
\r
916 outNoMatchHeader.write(thisbuffer3, 4);
\r
917 delete[] thisbuffer3;
\r
920 //read header length
\r
921 mybuffer = new char[2];
\r
922 in.read(mybuffer,2);
\r
923 for (int i = 0; i < filehandlesHeaders.size(); i++) {
\r
924 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
\r
926 m->openOutputFileBinaryAppend(filehandlesHeaders[i][j], out);
\r
927 out.write(mybuffer, in.gcount());
\r
931 outNoMatchHeader.write(mybuffer, in.gcount());
\r
935 mybuffer = new char[2];
\r
936 in.read(mybuffer,2);
\r
937 for (int i = 0; i < filehandlesHeaders.size(); i++) {
\r
938 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
\r
940 m->openOutputFileBinaryAppend(filehandlesHeaders[i][j], out);
\r
941 out.write(mybuffer, in.gcount());
\r
945 outNoMatchHeader.write(mybuffer, in.gcount());
\r
948 //read number of flow reads
\r
949 mybuffer = new char[2];
\r
950 in.read(mybuffer,2);
\r
951 for (int i = 0; i < filehandlesHeaders.size(); i++) {
\r
952 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
\r
954 m->openOutputFileBinaryAppend(filehandlesHeaders[i][j], out);
\r
955 out.write(mybuffer, in.gcount());
\r
959 outNoMatchHeader.write(mybuffer, in.gcount());
\r
963 mybuffer = new char[1];
\r
964 in.read(mybuffer,1);
\r
965 for (int i = 0; i < filehandlesHeaders.size(); i++) {
\r
966 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
\r
968 m->openOutputFileBinaryAppend(filehandlesHeaders[i][j], out);
\r
969 out.write(mybuffer, in.gcount());
\r
973 outNoMatchHeader.write(mybuffer, in.gcount());
\r
977 mybuffer = new char[header.numFlowsPerRead];
\r
978 in.read(mybuffer,header.numFlowsPerRead);
\r
979 for (int i = 0; i < filehandlesHeaders.size(); i++) {
\r
980 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
\r
982 m->openOutputFileBinaryAppend(filehandlesHeaders[i][j], out);
\r
983 out.write(mybuffer, in.gcount());
\r
987 outNoMatchHeader.write(mybuffer, in.gcount());
\r
991 mybuffer = new char[header.keyLength];
\r
992 in.read(mybuffer,header.keyLength);
\r
993 for (int i = 0; i < filehandlesHeaders.size(); i++) {
\r
994 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
\r
996 m->openOutputFileBinaryAppend(filehandlesHeaders[i][j], out);
\r
997 out.write(mybuffer, in.gcount());
\r
1001 outNoMatchHeader.write(mybuffer, in.gcount());
\r
1002 delete[] mybuffer;
\r
1005 /* Pad to 8 chars */
\r
1006 unsigned long long spotInFile = in.tellg();
\r
1007 unsigned long long spot = (spotInFile + 7)& ~7; // ~ inverts
\r
1010 mybuffer = new char[spot-spotInFile];
\r
1011 for (int i = 0; i < filehandlesHeaders.size(); i++) {
\r
1012 for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
\r
1014 m->openOutputFileBinaryAppend(filehandlesHeaders[i][j], out);
\r
1015 out.write(mybuffer, spot-spotInFile);
\r
1019 outNoMatchHeader.write(mybuffer, spot-spotInFile);
\r
1020 outNoMatchHeader.close();
\r
1021 delete[] mybuffer;
\r
1024 m->appendBinaryFiles(noMatchFile, tempNoHeader);
\r
1025 m->renameFile(tempNoHeader, noMatchFile);
\r
1026 m->mothurRemove(tempNoHeader);
\r
1031 catch(exception& e) {
\r
1032 m->errorOut(e, "SffInfoCommand", "adjustCommonHeader");
\r
1036 //**********************************************************************************************************************
\r
1037 bool SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, Header& header){
\r
1039 unsigned long long startSpotInFile = in.tellg();
\r
1042 /*****************************************/
\r
1045 //read header length
\r
1047 in.read(buffer, 2);
\r
1048 header.headerLength = be_int2(*(unsigned short *)(&buffer));
\r
1050 //read name length
\r
1052 in.read(buffer2, 2);
\r
1053 header.nameLength = be_int2(*(unsigned short *)(&buffer2));
\r
1057 in.read(buffer3, 4);
\r
1058 header.numBases = be_int4(*(unsigned int *)(&buffer3));
\r
1061 //read clip qual left
\r
1063 in.read(buffer4, 2);
\r
1064 header.clipQualLeft = be_int2(*(unsigned short *)(&buffer4));
\r
1065 header.clipQualLeft = 5;
\r
1068 //read clip qual right
\r
1070 in.read(buffer5, 2);
\r
1071 header.clipQualRight = be_int2(*(unsigned short *)(&buffer5));
\r
1074 //read clipAdapterLeft
\r
1076 in.read(buffer6, 2);
\r
1077 header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
\r
1080 //read clipAdapterRight
\r
1082 in.read(buffer7, 2);
\r
1083 header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
\r
1087 char* tempBuffer = new char[header.nameLength];
\r
1088 in.read(&(*tempBuffer), header.nameLength);
\r
1089 header.name = tempBuffer;
\r
1090 if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength); }
\r
1092 delete[] tempBuffer;
\r
1094 //extract info from name
\r
1095 decodeName(header.timestamp, header.region, header.xy, header.name);
\r
1097 /* Pad to 8 chars */
\r
1098 unsigned long long spotInFile = in.tellg();
\r
1099 unsigned long long spot = (spotInFile + 7)& ~7;
\r
1102 /*****************************************/
\r
1106 read.flowgram.resize(numFlowReads);
\r
1107 for (int i = 0; i < numFlowReads; i++) {
\r
1109 in.read(buffer, 2);
\r
1110 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
\r
1114 read.flowIndex.resize(header.numBases);
\r
1115 for (int i = 0; i < header.numBases; i++) {
\r
1118 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
\r
1122 char* tempBuffer6 = new char[header.numBases];
\r
1123 in.read(&(*tempBuffer6), header.numBases);
\r
1124 read.bases = tempBuffer6;
\r
1125 if (read.bases.length() > header.numBases) { read.bases = read.bases.substr(0, header.numBases); }
\r
1126 delete[] tempBuffer6;
\r
1128 //read qual scores
\r
1129 read.qualScores.resize(header.numBases);
\r
1130 for (int i = 0; i < header.numBases; i++) {
\r
1133 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
\r
1136 /* Pad to 8 chars */
\r
1137 spotInFile = in.tellg();
\r
1138 spot = (spotInFile + 7)& ~7;
\r
1143 int barcodeIndex, primerIndex, trashCodeLength;
\r
1145 if (hasOligos) { trashCodeLength = findGroup(header, read, barcodeIndex, primerIndex); }
\r
1146 else if (hasGroup) { trashCodeLength = findGroup(header, read, barcodeIndex, primerIndex, "groupMode"); }
\r
1147 else { m->mothurOut("[ERROR]: uh oh, we shouldn't be here...\n"); }
\r
1150 mybuffer = new char [spot-startSpotInFile];
\r
1153 m->openInputFileBinary(currentFileName, in2);
\r
1154 in2.seekg(startSpotInFile);
\r
1155 in2.read(mybuffer,spot-startSpotInFile);
\r
1158 if(trashCodeLength == 0){
\r
1160 m->openOutputFileBinaryAppend(filehandles[barcodeIndex][primerIndex], out);
\r
1161 out.write(mybuffer, in2.gcount());
\r
1163 numSplitReads[barcodeIndex][primerIndex]++;
\r
1167 m->openOutputFileBinaryAppend(noMatchFile, out);
\r
1168 out.write(mybuffer, in2.gcount());
\r
1172 delete[] mybuffer;
\r
1177 m->mothurOut("Error reading."); m->mothurOutEndLine();
\r
1180 if (in.eof()) { return true; }
\r
1184 catch(exception& e) {
\r
1185 m->errorOut(e, "SffInfoCommand", "readSeqData");
\r
1189 //**********************************************************************************************************************
\r
1190 int SffInfoCommand::findGroup(Header header, seqRead read, int& barcode, int& primer) {
\r
1192 //find group read belongs to
\r
1193 TrimOligos trimOligos(pdiffs, bdiffs, ldiffs, sdiffs, primers, barcodes, revPrimer, linker, spacer);
\r
1196 string trashCode = "";
\r
1197 int currentSeqsDiffs = 0;
\r
1199 string seq = read.bases;
\r
1202 if(header.clipQualRight < header.clipQualLeft){
\r
1203 if (header.clipQualRight == 0) { //don't trim right
\r
1204 seq = seq.substr(header.clipQualLeft-1);
\r
1209 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
\r
1210 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
\r
1213 seq = seq.substr(header.clipQualLeft-1);
\r
1216 //if you wanted the sfftxt then you already converted the bases to the right case
\r
1218 int endValue = header.clipQualRight;
\r
1219 //make the bases you want to clip lowercase and the bases you want to keep upper case
\r
1220 if(endValue == 0){ endValue = seq.length(); }
\r
1221 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
\r
1222 for (int i = (header.clipQualLeft-1); i < (endValue-1); i++) { seq[i] = toupper(seq[i]); }
\r
1223 for (int i = (endValue-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
\r
1227 Sequence currSeq(header.name, seq);
\r
1228 QualityScores currQual;
\r
1230 if(numLinkers != 0){
\r
1231 success = trimOligos.stripLinker(currSeq, currQual);
\r
1232 if(success > ldiffs) { trashCode += 'k'; }
\r
1233 else{ currentSeqsDiffs += success; }
\r
1237 if(barcodes.size() != 0){
\r
1238 success = trimOligos.stripBarcode(currSeq, currQual, barcode);
\r
1239 if(success > bdiffs) { trashCode += 'b'; }
\r
1240 else{ currentSeqsDiffs += success; }
\r
1243 if(numSpacers != 0){
\r
1244 success = trimOligos.stripSpacer(currSeq, currQual);
\r
1245 if(success > sdiffs) { trashCode += 's'; }
\r
1246 else{ currentSeqsDiffs += success; }
\r
1250 if(numFPrimers != 0){
\r
1251 success = trimOligos.stripForward(currSeq, currQual, primer, true);
\r
1252 if(success > pdiffs) { trashCode += 'f'; }
\r
1253 else{ currentSeqsDiffs += success; }
\r
1256 if (currentSeqsDiffs > tdiffs) { trashCode += 't'; }
\r
1258 if(revPrimer.size() != 0){
\r
1259 success = trimOligos.stripReverse(currSeq, currQual);
\r
1260 if(!success) { trashCode += 'r'; }
\r
1264 return trashCode.length();
\r
1266 catch(exception& e) {
\r
1267 m->errorOut(e, "SffInfoCommand", "findGroup");
\r
1271 //**********************************************************************************************************************
\r
1272 int SffInfoCommand::findGroup(Header header, seqRead read, int& barcode, int& primer, string groupMode) {
\r
1274 string trashCode = "";
\r
1277 string group = groupMap->getGroup(header.name);
\r
1278 if (group == "not found") { trashCode += "g"; } //scrap for group
\r
1279 else { //find file group
\r
1280 map<string, int>::iterator it = barcodes.find(group);
\r
1281 if (it != barcodes.end()) {
\r
1282 barcode = it->second;
\r
1283 }else { trashCode += "g"; }
\r
1286 return trashCode.length();
\r
1288 catch(exception& e) {
\r
1289 m->errorOut(e, "SffInfoCommand", "findGroup");
\r
1293 //**********************************************************************************************************************
\r
1294 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
\r
1297 if (name.length() >= 6) {
\r
1298 string time = name.substr(0, 6);
\r
1299 unsigned int timeNum = m->fromBase36(time);
\r
1301 int q1 = timeNum / 60;
\r
1302 int sec = timeNum - 60 * q1;
\r
1304 int minute = q1 - 60 * q2;
\r
1306 int hr = q2 - 24 * q3;
\r
1308 int day = q3 - 32 * q4;
\r
1310 int mon = q4 - 13 * q5;
\r
1311 int year = 2000 + q5;
\r
1313 timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
\r
1316 if (name.length() >= 9) {
\r
1317 region = name.substr(7, 2);
\r
1319 string xyNum = name.substr(9);
\r
1320 unsigned int myXy = m->fromBase36(xyNum);
\r
1321 int x = myXy >> 12;
\r
1322 int y = myXy & 4095;
\r
1324 xy = toString(x) + "_" + toString(y);
\r
1329 catch(exception& e) {
\r
1330 m->errorOut(e, "SffInfoCommand", "decodeName");
\r
1334 //**********************************************************************************************************************
\r
1335 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
\r
1338 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
\r
1339 out << "Version: " << header.version << endl;
\r
1340 out << "Index Offset: " << header.indexOffset << endl;
\r
1341 out << "Index Length: " << header.indexLength << endl;
\r
1342 out << "Number of Reads: " << header.numReads << endl;
\r
1343 out << "Header Length: " << header.headerLength << endl;
\r
1344 out << "Key Length: " << header.keyLength << endl;
\r
1345 out << "Number of Flows: " << header.numFlowsPerRead << endl;
\r
1346 out << "Format Code: " << header.flogramFormatCode << endl;
\r
1347 out << "Flow Chars: " << header.flowChars << endl;
\r
1348 out << "Key Sequence: " << header.keySequence << endl << endl;
\r
1352 catch(exception& e) {
\r
1353 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
\r
1357 //**********************************************************************************************************************
\r
1358 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
\r
1361 out << ">" << header.name << endl;
\r
1362 out << "Run Prefix: " << header.timestamp << endl;
\r
1363 out << "Region #: " << header.region << endl;
\r
1364 out << "XY Location: " << header.xy << endl << endl;
\r
1366 out << "Run Name: " << endl;
\r
1367 out << "Analysis Name: " << endl;
\r
1368 out << "Full Path: " << endl << endl;
\r
1370 out << "Read Header Len: " << header.headerLength << endl;
\r
1371 out << "Name Length: " << header.nameLength << endl;
\r
1372 out << "# of Bases: " << header.numBases << endl;
\r
1373 out << "Clip Qual Left: " << header.clipQualLeft << endl;
\r
1374 out << "Clip Qual Right: " << header.clipQualRight << endl;
\r
1375 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
\r
1376 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
\r
1380 catch(exception& e) {
\r
1381 m->errorOut(e, "SffInfoCommand", "printHeader");
\r
1385 //**********************************************************************************************************************
\r
1386 bool SffInfoCommand::sanityCheck(Header& header, seqRead& read) {
\r
1389 string message = "[WARNING]: Your sff file may be corrupted! Sequence: " + header.name + "\n";
\r
1391 if (header.clipQualLeft > read.bases.length()) {
\r
1392 okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
\r
1394 if (header.clipQualRight > read.bases.length()) {
\r
1395 okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
\r
1397 if (header.clipQualLeft > read.qualScores.size()) {
\r
1398 okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
\r
1400 if (header.clipQualRight > read.qualScores.size()) {
\r
1401 okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
\r
1404 if (okay == false) {
\r
1405 m->mothurOut(message); m->mothurOutEndLine();
\r
1410 catch(exception& e) {
\r
1411 m->errorOut(e, "SffInfoCommand", "sanityCheck");
\r
1415 //**********************************************************************************************************************
\r
1416 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
\r
1418 out << "Flowgram: ";
\r
1419 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t'; }
\r
1421 out << endl << "Flow Indexes: ";
\r
1423 for (int i = 0; i < read.flowIndex.size(); i++) { sum += read.flowIndex[i]; out << sum << '\t'; }
\r
1425 //make the bases you want to clip lowercase and the bases you want to keep upper case
\r
1426 int endValue = header.clipQualRight;
\r
1427 if(endValue == 0){ endValue = read.bases.length(); }
\r
1428 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
\r
1429 for (int i = (header.clipQualLeft-1); i < (endValue-1); i++) { read.bases[i] = toupper(read.bases[i]); }
\r
1430 for (int i = (endValue-1); i < read.bases.length(); i++) { read.bases[i] = tolower(read.bases[i]); }
\r
1432 out << endl << "Bases: " << read.bases << endl << "Quality Scores: ";
\r
1433 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
\r
1436 out << endl << endl;
\r
1440 catch(exception& e) {
\r
1441 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
\r
1445 //**********************************************************************************************************************
\r
1446 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
\r
1448 string seq = read.bases;
\r
1451 if(header.clipQualRight < header.clipQualLeft){
\r
1452 if (header.clipQualRight == 0) { //don't trim right
\r
1453 seq = seq.substr(header.clipQualLeft-1);
\r
1458 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
\r
1459 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
\r
1462 seq = seq.substr(header.clipQualLeft-1);
\r
1465 //if you wanted the sfftxt then you already converted the bases to the right case
\r
1467 int endValue = header.clipQualRight;
\r
1468 //make the bases you want to clip lowercase and the bases you want to keep upper case
\r
1469 if(endValue == 0){ endValue = seq.length(); }
\r
1470 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]); }
\r
1471 for (int i = (header.clipQualLeft-1); i < (endValue-1); i++) { seq[i] = toupper(seq[i]); }
\r
1472 for (int i = (endValue-1); i < seq.length(); i++) { seq[i] = tolower(seq[i]); }
\r
1476 out << ">" << header.name << " xy=" << header.xy << endl;
\r
1477 out << seq << endl;
\r
1481 catch(exception& e) {
\r
1482 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
\r
1487 //**********************************************************************************************************************
\r
1488 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
\r
1492 if(header.clipQualRight < header.clipQualLeft){
\r
1493 if (header.clipQualRight == 0) { //don't trim right
\r
1494 out << ">" << header.name << " xy=" << header.xy << " length=" << (read.qualScores.size()-header.clipQualLeft) << endl;
\r
1495 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
\r
1497 out << ">" << header.name << " xy=" << header.xy << endl;
\r
1498 out << "0\t0\t0\t0";
\r
1501 else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
\r
1502 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
\r
1503 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) { out << read.qualScores[i] << '\t'; }
\r
1506 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
\r
1507 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
\r
1510 out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
\r
1511 for (int i = 0; i < read.qualScores.size(); i++) { out << read.qualScores[i] << '\t'; }
\r
1518 catch(exception& e) {
\r
1519 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
\r
1524 //**********************************************************************************************************************
\r
1525 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
\r
1528 int endValue = header.clipQualRight;
\r
1529 if (header.clipQualRight == 0) {
\r
1530 endValue = read.flowIndex.size();
\r
1531 if (m->debug) { m->mothurOut("[DEBUG]: " + header.name + " has clipQualRight=0.\n"); }
\r
1533 if(endValue > header.clipQualLeft){
\r
1535 int rightIndex = 0;
\r
1536 for (int i = 0; i < endValue; i++) { rightIndex += read.flowIndex[i]; }
\r
1538 out << header.name << ' ' << rightIndex;
\r
1539 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100); }
\r
1546 catch(exception& e) {
\r
1547 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
\r
1551 //**********************************************************************************************************************
\r
1552 int SffInfoCommand::readAccnosFile(string filename) {
\r
1554 //remove old names
\r
1558 m->openInputFile(filename, in);
\r
1562 in >> name; m->gobble(in);
\r
1564 seqNames.insert(name);
\r
1566 if (m->control_pressed) { seqNames.clear(); break; }
\r
1572 catch(exception& e) {
\r
1573 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
\r
1577 //**********************************************************************************************************************
\r
1578 int SffInfoCommand::parseSffTxt() {
\r
1582 m->openInputFile(sfftxtFilename, inSFF);
\r
1584 if (outputDir == "") { outputDir += m->hasPath(sfftxtFilename); }
\r
1586 //output file names
\r
1587 ofstream outFasta, outQual, outFlow;
\r
1588 string outFastaFileName, outQualFileName;
\r
1589 string fileRoot = m->getRootName(m->getSimpleName(sfftxtFilename));
\r
1590 if (fileRoot.length() > 0) {
\r
1592 fileRoot = fileRoot.substr(0, fileRoot.length()-1);
\r
1593 fileRoot = m->getRootName(fileRoot);
\r
1596 map<string, string> variables;
\r
1597 variables["[filename]"] = fileRoot;
\r
1598 string sfftxtFileName = getOutputFileName("sfftxt",variables);
\r
1599 string outFlowFileName = getOutputFileName("flow",variables);
\r
1600 if (!trim) { variables["[tag]"] = "raw"; }
\r
1601 outFastaFileName = getOutputFileName("fasta",variables);
\r
1602 outQualFileName = getOutputFileName("qfile",variables);
\r
1604 if (fasta) { m->openOutputFile(outFastaFileName, outFasta); outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
\r
1605 if (qual) { m->openOutputFile(outQualFileName, outQual); outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName); }
\r
1606 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); }
\r
1608 //read common header
\r
1609 string commonHeader = m->getline(inSFF);
\r
1610 string magicNumber = m->getline(inSFF);
\r
1611 string version = m->getline(inSFF);
\r
1612 string indexOffset = m->getline(inSFF);
\r
1613 string indexLength = m->getline(inSFF);
\r
1614 int numReads = parseHeaderLineToInt(inSFF);
\r
1615 string headerLength = m->getline(inSFF);
\r
1616 string keyLength = m->getline(inSFF);
\r
1617 int numFlows = parseHeaderLineToInt(inSFF);
\r
1618 string flowgramCode = m->getline(inSFF);
\r
1619 string flowChars = m->getline(inSFF);
\r
1620 string keySequence = m->getline(inSFF);
\r
1625 if (flow) { outFlow << numFlows << endl; }
\r
1627 for(int i=0;i<numReads;i++){
\r
1630 if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
\r
1634 //parse read header
\r
1636 seqName = seqName.substr(1);
\r
1638 header.name = seqName;
\r
1640 string runPrefix = parseHeaderLineToString(inSFF); header.timestamp = runPrefix;
\r
1641 string regionNumber = parseHeaderLineToString(inSFF); header.region = regionNumber;
\r
1642 string xyLocation = parseHeaderLineToString(inSFF); header.xy = xyLocation;
\r
1645 string runName = parseHeaderLineToString(inSFF);
\r
1646 string analysisName = parseHeaderLineToString(inSFF);
\r
1647 string fullPath = parseHeaderLineToString(inSFF);
\r
1650 string readHeaderLen = parseHeaderLineToString(inSFF); convert(readHeaderLen, header.headerLength);
\r
1651 string nameLength = parseHeaderLineToString(inSFF); convert(nameLength, header.nameLength);
\r
1652 int numBases = parseHeaderLineToInt(inSFF); header.numBases = numBases;
\r
1653 string clipQualLeft = parseHeaderLineToString(inSFF); convert(clipQualLeft, header.clipQualLeft);
\r
1654 int clipQualRight = parseHeaderLineToInt(inSFF); header.clipQualRight = clipQualRight;
\r
1655 string clipAdapLeft = parseHeaderLineToString(inSFF); convert(clipAdapLeft, header.clipAdapterLeft);
\r
1656 string clipAdapRight = parseHeaderLineToString(inSFF); convert(clipAdapRight, header.clipAdapterRight);
\r
1662 vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows); read.flowgram = flowVector;
\r
1663 vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases);
\r
1665 //adjust for print
\r
1666 vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
\r
1667 for (int j = 1; j < flowIndices.size(); j++) { flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]); }
\r
1668 read.flowIndex = flowIndicesAdjusted;
\r
1670 string bases = parseHeaderLineToString(inSFF); read.bases = bases;
\r
1671 vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases); read.qualScores = qualityScores;
\r
1674 //if you have provided an accosfile and this seq is not in it, then dont print
\r
1675 bool print = true;
\r
1676 if (seqNames.size() != 0) { if (seqNames.count(header.name) == 0) { print = false; } }
\r
1680 if (fasta) { printFastaSeqData(outFasta, read, header); }
\r
1681 if (qual) { printQualSeqData(outQual, read, header); }
\r
1682 if (flow) { printFlowSeqData(outFlow, read, header); }
\r
1686 if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine(); }
\r
1688 if (m->control_pressed) { break; }
\r
1692 if (!m->control_pressed) { if((numReads) % 10000 != 0){ m->mothurOut(toString(numReads)); m->mothurOutEndLine(); } }
\r
1696 if (fasta) { outFasta.close(); }
\r
1697 if (qual) { outQual.close(); }
\r
1698 if (flow) { outFlow.close(); }
\r
1702 catch(exception& e) {
\r
1703 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
\r
1707 //**********************************************************************************************************************
\r
1709 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
\r
1713 while (!file.eof()) {
\r
1715 char c = file.get();
\r
1725 catch(exception& e) {
\r
1726 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
\r
1732 //**********************************************************************************************************************
\r
1734 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
\r
1738 while (!file.eof()) {
\r
1739 char c = file.get();
\r
1742 //m->gobble(file);
\r
1743 //text = m->getline(file);
\r
1752 catch(exception& e) {
\r
1753 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
\r
1758 //**********************************************************************************************************************
\r
1760 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
\r
1762 vector<unsigned short> floatVector(length);
\r
1764 while (!file.eof()) {
\r
1765 char c = file.get();
\r
1768 for(int i=0;i<length;i++){
\r
1770 floatVector[i] = temp * 100;
\r
1776 return floatVector;
\r
1778 catch(exception& e) {
\r
1779 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
\r
1784 //**********************************************************************************************************************
\r
1786 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
\r
1788 vector<unsigned int> intVector(length);
\r
1790 while (!file.eof()) {
\r
1791 char c = file.get();
\r
1793 for(int i=0;i<length;i++){
\r
1794 file >> intVector[i];
\r
1802 catch(exception& e) {
\r
1803 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
\r
1807 //***************************************************************************************************************
\r
1809 bool SffInfoCommand::readOligos(string oligoFile){
\r
1811 filehandles.clear();
\r
1812 numSplitReads.clear();
\r
1813 filehandlesHeaders.clear();
\r
1815 ifstream inOligos;
\r
1816 m->openInputFile(oligoFile, inOligos);
\r
1818 string type, oligo, group;
\r
1820 int indexPrimer = 0;
\r
1821 int indexBarcode = 0;
\r
1823 while(!inOligos.eof()){
\r
1827 if(type[0] == '#'){
\r
1828 while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
\r
1829 m->gobble(inOligos);
\r
1832 m->gobble(inOligos);
\r
1833 //make type case insensitive
\r
1834 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); }
\r
1836 inOligos >> oligo;
\r
1838 for(int i=0;i<oligo.length();i++){
\r
1839 oligo[i] = toupper(oligo[i]);
\r
1840 if(oligo[i] == 'U') { oligo[i] = 'T'; }
\r
1843 if(type == "FORWARD"){
\r
1846 // get rest of line in case there is a primer name
\r
1847 while (!inOligos.eof()) {
\r
1848 char c = inOligos.get();
\r
1849 if (c == 10 || c == 13 || c == -1){ break; }
\r
1850 else if (c == 32 || c == 9){;} //space or tab
\r
1851 else { group += c; }
\r
1854 //check for repeat barcodes
\r
1855 map<string, int>::iterator itPrime = primers.find(oligo);
\r
1856 if (itPrime != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
\r
1858 primers[oligo]=indexPrimer; indexPrimer++;
\r
1859 primerNameVector.push_back(group);
\r
1861 }else if(type == "REVERSE"){
\r
1862 //Sequence oligoRC("reverse", oligo);
\r
1863 //oligoRC.reverseComplement();
\r
1864 string oligoRC = reverseOligo(oligo);
\r
1865 revPrimer.push_back(oligoRC);
\r
1867 else if(type == "BARCODE"){
\r
1868 inOligos >> group;
\r
1871 //check for repeat barcodes
\r
1872 map<string, int>::iterator itBar = barcodes.find(oligo);
\r
1873 if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
\r
1875 barcodes[oligo]=indexBarcode; indexBarcode++;
\r
1876 barcodeNameVector.push_back(group);
\r
1877 }else if(type == "LINKER"){
\r
1878 linker.push_back(oligo);
\r
1879 }else if(type == "SPACER"){
\r
1880 spacer.push_back(oligo);
\r
1882 else{ m->mothurOut("[WARNING]: " + type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine(); }
\r
1884 m->gobble(inOligos);
\r
1888 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ split = 1; }
\r
1890 //add in potential combos
\r
1891 if(barcodeNameVector.size() == 0){
\r
1893 barcodeNameVector.push_back("");
\r
1896 if(primerNameVector.size() == 0){
\r
1898 primerNameVector.push_back("");
\r
1901 filehandles.resize(barcodeNameVector.size());
\r
1902 for(int i=0;i<filehandles.size();i++){
\r
1903 filehandles[i].assign(primerNameVector.size(), "");
\r
1907 set<string> uniqueNames; //used to cleanup outputFileNames
\r
1908 for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
\r
1909 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
\r
1911 string primerName = primerNameVector[itPrimer->second];
\r
1912 string barcodeName = barcodeNameVector[itBar->second];
\r
1914 string comboGroupName = "";
\r
1915 string fastaFileName = "";
\r
1916 string qualFileName = "";
\r
1917 string nameFileName = "";
\r
1919 if(primerName == ""){
\r
1920 comboGroupName = barcodeNameVector[itBar->second];
\r
1923 if(barcodeName == ""){
\r
1924 comboGroupName = primerNameVector[itPrimer->second];
\r
1927 comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
\r
1932 map<string, string> variables;
\r
1933 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(currentFileName));
\r
1934 variables["[group]"] = comboGroupName;
\r
1935 string thisFilename = getOutputFileName("sff",variables);
\r
1936 if (uniqueNames.count(thisFilename) == 0) {
\r
1937 outputNames.push_back(thisFilename);
\r
1938 outputTypes["sff"].push_back(thisFilename);
\r
1939 uniqueNames.insert(thisFilename);
\r
1942 filehandles[itBar->second][itPrimer->second] = thisFilename;
\r
1943 temp.open(thisFilename.c_str(), ios::binary); temp.close();
\r
1947 numFPrimers = primers.size();
\r
1948 numLinkers = linker.size();
\r
1949 numSpacers = spacer.size();
\r
1950 map<string, string> variables;
\r
1951 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(currentFileName));
\r
1952 variables["[group]"] = "scrap";
\r
1953 noMatchFile = getOutputFileName("sff",variables);
\r
1954 m->mothurRemove(noMatchFile);
\r
1958 bool allBlank = true;
\r
1959 for (int i = 0; i < barcodeNameVector.size(); i++) {
\r
1960 if (barcodeNameVector[i] != "") {
\r
1965 for (int i = 0; i < primerNameVector.size(); i++) {
\r
1966 if (primerNameVector[i] != "") {
\r
1972 filehandlesHeaders.resize(filehandles.size());
\r
1973 numSplitReads.resize(filehandles.size());
\r
1974 for (int i = 0; i < filehandles.size(); i++) {
\r
1975 numSplitReads[i].resize(filehandles[i].size(), 0);
\r
1976 for (int j = 0; j < filehandles[i].size(); j++) {
\r
1977 filehandlesHeaders[i].push_back(filehandles[i][j]+"headers");
\r
1982 m->mothurOut("[WARNING]: your oligos file does not contain any group names. mothur will not create a split the sff file."); m->mothurOutEndLine();
\r
1990 catch(exception& e) {
\r
1991 m->errorOut(e, "SffInfoCommand", "readOligos");
\r
1995 //***************************************************************************************************************
\r
1997 bool SffInfoCommand::readGroup(string oligoFile){
\r
1999 filehandles.clear();
\r
2000 numSplitReads.clear();
\r
2001 filehandlesHeaders.clear();
\r
2004 groupMap = new GroupMap();
\r
2005 groupMap->readMap(oligoFile);
\r
2007 //like barcodeNameVector - no primer names
\r
2008 vector<string> groups = groupMap->getNamesOfGroups();
\r
2010 filehandles.resize(groups.size());
\r
2011 for (int i = 0; i < filehandles.size(); i++) {
\r
2012 for (int j = 0; j < 1; j++) {
\r
2014 map<string, string> variables;
\r
2015 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(currentFileName));
\r
2016 variables["[group]"] = groups[i];
\r
2017 string thisFilename = getOutputFileName("sff",variables);
\r
2018 outputNames.push_back(thisFilename);
\r
2019 outputTypes["sff"].push_back(thisFilename);
\r
2022 m->openOutputFileBinary(thisFilename, temp); temp.close();
\r
2023 filehandles[i].push_back(thisFilename);
\r
2024 barcodes[groups[i]] = i;
\r
2028 map<string, string> variables;
\r
2029 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(currentFileName));
\r
2030 variables["[group]"] = "scrap";
\r
2031 noMatchFile = getOutputFileName("sff",variables);
\r
2032 m->mothurRemove(noMatchFile);
\r
2036 filehandlesHeaders.resize(groups.size());
\r
2037 numSplitReads.resize(filehandles.size());
\r
2038 for (int i = 0; i < filehandles.size(); i++) {
\r
2039 numSplitReads[i].resize(filehandles[i].size(), 0);
\r
2040 for (int j = 0; j < filehandles[i].size(); j++) {
\r
2042 string thisHeader = filehandles[i][j]+"headers";
\r
2043 m->openOutputFileBinary(thisHeader, temp); temp.close();
\r
2044 filehandlesHeaders[i].push_back(thisHeader);
\r
2051 catch(exception& e) {
\r
2052 m->errorOut(e, "SffInfoCommand", "readGroup");
\r
2057 //********************************************************************/
\r
2058 string SffInfoCommand::reverseOligo(string oligo){
\r
2060 string reverse = "";
\r
2062 for(int i=oligo.length()-1;i>=0;i--){
\r
2064 if(oligo[i] == 'A') { reverse += 'T'; }
\r
2065 else if(oligo[i] == 'T'){ reverse += 'A'; }
\r
2066 else if(oligo[i] == 'U'){ reverse += 'A'; }
\r
2068 else if(oligo[i] == 'G'){ reverse += 'C'; }
\r
2069 else if(oligo[i] == 'C'){ reverse += 'G'; }
\r
2071 else if(oligo[i] == 'R'){ reverse += 'Y'; }
\r
2072 else if(oligo[i] == 'Y'){ reverse += 'R'; }
\r
2074 else if(oligo[i] == 'M'){ reverse += 'K'; }
\r
2075 else if(oligo[i] == 'K'){ reverse += 'M'; }
\r
2077 else if(oligo[i] == 'W'){ reverse += 'W'; }
\r
2078 else if(oligo[i] == 'S'){ reverse += 'S'; }
\r
2080 else if(oligo[i] == 'B'){ reverse += 'V'; }
\r
2081 else if(oligo[i] == 'V'){ reverse += 'B'; }
\r
2083 else if(oligo[i] == 'D'){ reverse += 'H'; }
\r
2084 else if(oligo[i] == 'H'){ reverse += 'D'; }
\r
2086 else { reverse += 'N'; }
\r
2092 catch(exception& e) {
\r
2093 m->errorOut(e, "SffInfoCommand", "reverseOligo");
\r
2098 //**********************************************************************************************************************
\r