5 // Created by Sarah Westcott on 2/3/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
9 #include "sortseqscommand.h"
10 #include "sequence.hpp"
11 #include "qualityscores.h"
13 //**********************************************************************************************************************
14 vector<string> SortSeqsCommand::setParameters(){
16 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FNGLT", "none","fasta",false,false); parameters.push_back(pfasta);
17 CommandParameter pflow("flow", "InputTypes", "", "", "none", "FNGLT", "none","flow",false,false); parameters.push_back(pflow);
18 CommandParameter pname("name", "InputTypes", "", "", "NameCount", "FNGLT", "none","name",false,false); parameters.push_back(pname);
19 CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "FNGLT", "none","count",false,false); parameters.push_back(pcount);
20 CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "FNGLT", "none","group",false,false); parameters.push_back(pgroup);
21 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "FNGLT", "none","taxonomy",false,false); parameters.push_back(ptaxonomy);
22 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "FNGLT", "none","qfile",false,false); parameters.push_back(pqfile);
23 CommandParameter plarge("large", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(plarge);
24 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(paccnos);
25 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
26 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
28 vector<string> myArray;
29 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
33 m->errorOut(e, "SortSeqsCommand", "setParameters");
37 //**********************************************************************************************************************
38 string SortSeqsCommand::getHelpString(){
40 string helpString = "";
41 helpString += "The sort.seqs command puts the sequences in the same order for the following file types: accnos fasta, name, group, count, taxonomy, flow or quality file.\n";
42 helpString += "The sort.seqs command parameters are accnos, fasta, name, group, count, taxonomy, flow, qfile and large.\n";
43 helpString += "The accnos file allows you to specify the order you want the files in. If none is provided, mothur will use the order of the first file it reads.\n";
44 helpString += "The large parameters is used to indicate your files are too large to fit in RAM.\n";
45 helpString += "The sort.seqs command should be in the following format: sort.seqs(fasta=yourFasta).\n";
46 helpString += "Example sort.seqs(fasta=amazon.fasta).\n";
47 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
51 m->errorOut(e, "SortSeqsCommand", "getHelpString");
55 //**********************************************************************************************************************
56 string SortSeqsCommand::getOutputPattern(string type) {
60 if (type == "fasta") { pattern = "[filename],sorted,[extension]"; }
61 else if (type == "taxonomy") { pattern = "[filename],sorted,[extension]"; }
62 else if (type == "name") { pattern = "[filename],sorted,[extension]"; }
63 else if (type == "group") { pattern = "[filename],sorted,[extension]"; }
64 else if (type == "count") { pattern = "[filename],sorted,[extension]"; }
65 else if (type == "flow") { pattern = "[filename],sorted,[extension]"; }
66 else if (type == "qfile") { pattern = "[filename],sorted,[extension]"; }
67 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
72 m->errorOut(e, "SortSeqsCommand", "getOutputPattern");
76 //**********************************************************************************************************************
77 SortSeqsCommand::SortSeqsCommand(){
79 abort = true; calledHelp = true;
81 vector<string> tempOutNames;
82 outputTypes["fasta"] = tempOutNames;
83 outputTypes["taxonomy"] = tempOutNames;
84 outputTypes["name"] = tempOutNames;
85 outputTypes["count"] = tempOutNames;
86 outputTypes["group"] = tempOutNames;
87 outputTypes["qfile"] = tempOutNames;
88 outputTypes["flow"] = tempOutNames;
91 m->errorOut(e, "SortSeqsCommand", "SortSeqsCommand");
95 //**********************************************************************************************************************
96 SortSeqsCommand::SortSeqsCommand(string option) {
98 abort = false; calledHelp = false;
100 //allow user to run help
101 if(option == "help") { help(); abort = true; calledHelp = true; }
102 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
105 vector<string> myArray = setParameters();
107 OptionParser parser(option);
108 map<string,string> parameters = parser.getParameters();
110 ValidParameters validParameter;
111 map<string,string>::iterator it;
113 //check to make sure all parameters are valid for command
114 for (it = parameters.begin(); it != parameters.end(); it++) {
115 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
118 //initialize outputTypes
119 vector<string> tempOutNames;
120 outputTypes["fasta"] = tempOutNames;
121 outputTypes["taxonomy"] = tempOutNames;
122 outputTypes["name"] = tempOutNames;
123 outputTypes["group"] = tempOutNames;
124 outputTypes["qfile"] = tempOutNames;
125 outputTypes["flow"] = tempOutNames;
126 outputTypes["count"] = tempOutNames;
128 //if the user changes the output directory command factory will send this info to us in the output parameter
129 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
131 //if the user changes the input directory command factory will send this info to us in the output parameter
132 string inputDir = validParameter.validFile(parameters, "inputdir", false);
133 if (inputDir == "not found"){ inputDir = ""; }
136 it = parameters.find("fasta");
137 //user has given a template file
138 if(it != parameters.end()){
139 path = m->hasPath(it->second);
140 //if the user has not given a path then, add inputdir. else leave path alone.
141 if (path == "") { parameters["fasta"] = inputDir + it->second; }
144 it = parameters.find("name");
145 //user has given a template file
146 if(it != parameters.end()){
147 path = m->hasPath(it->second);
148 //if the user has not given a path then, add inputdir. else leave path alone.
149 if (path == "") { parameters["name"] = inputDir + it->second; }
152 it = parameters.find("group");
153 //user has given a template file
154 if(it != parameters.end()){
155 path = m->hasPath(it->second);
156 //if the user has not given a path then, add inputdir. else leave path alone.
157 if (path == "") { parameters["group"] = inputDir + it->second; }
160 it = parameters.find("taxonomy");
161 //user has given a template file
162 if(it != parameters.end()){
163 path = m->hasPath(it->second);
164 //if the user has not given a path then, add inputdir. else leave path alone.
165 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
168 it = parameters.find("qfile");
169 //user has given a template file
170 if(it != parameters.end()){
171 path = m->hasPath(it->second);
172 //if the user has not given a path then, add inputdir. else leave path alone.
173 if (path == "") { parameters["qfile"] = inputDir + it->second; }
176 it = parameters.find("accnos");
177 //user has given a template file
178 if(it != parameters.end()){
179 path = m->hasPath(it->second);
180 //if the user has not given a path then, add inputdir. else leave path alone.
181 if (path == "") { parameters["accnos"] = inputDir + it->second; }
184 it = parameters.find("flow");
185 //user has given a template file
186 if(it != parameters.end()){
187 path = m->hasPath(it->second);
188 //if the user has not given a path then, add inputdir. else leave path alone.
189 if (path == "") { parameters["flow"] = inputDir + it->second; }
192 it = parameters.find("count");
193 //user has given a template file
194 if(it != parameters.end()){
195 path = m->hasPath(it->second);
196 //if the user has not given a path then, add inputdir. else leave path alone.
197 if (path == "") { parameters["count"] = inputDir + it->second; }
202 //check for parameters
203 accnosfile = validParameter.validFile(parameters, "accnos", true);
204 if (accnosfile == "not open") { accnosfile = ""; abort = true; }
205 else if (accnosfile == "not found") { accnosfile = ""; }
206 else { m->setAccnosFile(accnosfile); }
208 fastafile = validParameter.validFile(parameters, "fasta", true);
209 if (fastafile == "not open") { fastafile = ""; abort = true; }
210 else if (fastafile == "not found") { fastafile = ""; }
211 else { m->setFastaFile(fastafile); }
213 flowfile = validParameter.validFile(parameters, "flow", true);
214 if (flowfile == "not open") { flowfile = ""; abort = true; }
215 else if (flowfile == "not found") { flowfile = ""; }
216 else { m->setFlowFile(flowfile); }
218 namefile = validParameter.validFile(parameters, "name", true);
219 if (namefile == "not open") { namefile = ""; abort = true; }
220 else if (namefile == "not found") { namefile = ""; }
221 else { m->setNameFile(namefile); }
223 groupfile = validParameter.validFile(parameters, "group", true);
224 if (groupfile == "not open") { abort = true; }
225 else if (groupfile == "not found") { groupfile = ""; }
226 else { m->setGroupFile(groupfile); }
228 taxfile = validParameter.validFile(parameters, "taxonomy", true);
229 if (taxfile == "not open") { abort = true; }
230 else if (taxfile == "not found") { taxfile = ""; }
231 else { m->setTaxonomyFile(taxfile); }
233 qualfile = validParameter.validFile(parameters, "qfile", true);
234 if (qualfile == "not open") { abort = true; }
235 else if (qualfile == "not found") { qualfile = ""; }
236 else { m->setQualFile(qualfile); }
238 countfile = validParameter.validFile(parameters, "count", true);
239 if (countfile == "not open") { countfile = ""; abort = true; }
240 else if (countfile == "not found") { countfile = ""; }
241 else { m->setCountTableFile(countfile); }
243 if ((namefile != "") && (countfile != "")) {
244 m->mothurOut("[ERROR]: you may only use one of the following: name or count."); m->mothurOutEndLine(); abort = true;
247 if ((groupfile != "") && (countfile != "")) {
248 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
251 string temp = validParameter.validFile(parameters, "large", false); if (temp == "not found") { temp = "f"; }
252 large = m->isTrue(temp);
254 if ((fastafile == "") && (namefile == "") && (countfile == "") && (groupfile == "") && (taxfile == "") && (flowfile == "") && (qualfile == "")) { m->mothurOut("You must provide at least one of the following: fasta, name, group, count, taxonomy, flow or quality."); m->mothurOutEndLine(); abort = true; }
256 if (countfile == "") {
257 if ((fastafile != "") && (namefile == "")) {
258 vector<string> files; files.push_back(fastafile);
259 parser.getNameFile(files);
265 catch(exception& e) {
266 m->errorOut(e, "SortSeqsCommand", "SortSeqsCommand");
270 //**********************************************************************************************************************
272 int SortSeqsCommand::execute(){
275 if (abort == true) { if (calledHelp) { return 0; } return 2; }
277 //read through the correct file and output lines you want to keep
278 if (accnosfile != "") {
280 m->readAccnos(accnosfile, temp);
281 for (int i = 0; i < temp.size(); i++) { names[temp[i]] = i; }
282 m->mothurOut("\nUsing " + accnosfile + " to determine the order. It contains " + toString(temp.size()) + " representative sequences.\n");
285 if (fastafile != "") { readFasta(); }
286 if (flowfile != "") { readFlow(); }
287 if (qualfile != "") { readQual(); }
288 if (namefile != "") { readName(); }
289 if (groupfile != "") { readGroup(); }
290 if (countfile != "") { readCount(); }
291 if (taxfile != "") { readTax(); }
293 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
295 if (outputNames.size() != 0) {
296 m->mothurOutEndLine();
297 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
298 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
299 m->mothurOutEndLine();
301 //set fasta file as new current fastafile
303 itTypes = outputTypes.find("fasta");
304 if (itTypes != outputTypes.end()) {
305 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
308 itTypes = outputTypes.find("name");
309 if (itTypes != outputTypes.end()) {
310 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
313 itTypes = outputTypes.find("group");
314 if (itTypes != outputTypes.end()) {
315 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
319 itTypes = outputTypes.find("taxonomy");
320 if (itTypes != outputTypes.end()) {
321 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
324 itTypes = outputTypes.find("qfile");
325 if (itTypes != outputTypes.end()) {
326 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
329 itTypes = outputTypes.find("flow");
330 if (itTypes != outputTypes.end()) {
331 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFlowFile(current); }
334 itTypes = outputTypes.find("count");
335 if (itTypes != outputTypes.end()) {
336 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
343 catch(exception& e) {
344 m->errorOut(e, "SortSeqsCommand", "execute");
349 //**********************************************************************************************************************
350 int SortSeqsCommand::readFasta(){
352 string thisOutputDir = outputDir;
353 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
354 map<string, string> variables;
355 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(fastafile));
356 variables["[extension]"] = m->getExtension(fastafile);
357 string outputFileName = getOutputFileName("fasta", variables);
358 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
361 m->openOutputFile(outputFileName, out);
364 m->openInputFile(fastafile, in);
367 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
369 if (large) { //if the file is too large to fit in memory we can still process it, but the io will be very time consuming.
370 //read through the file looking for 1000 seqs at a time. Once we find them output them and start looking for the next 1000.
371 //this way we only store 1000 seqs in memory at a time.
373 int numNames = names.size();
374 int numNamesInFile = 0;
376 //to make sure we dont miss any seqs, add any seqs that are not in names but in the file to the end of names
378 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
380 Sequence currSeq(in);
381 name = currSeq.getName();
385 map<string, int>::iterator it = names.find(name);
386 if (it == names.end()) {
387 names[name] = numNames; numNames++;
388 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
396 int numLeft = names.size();
397 if (numNamesInFile < numLeft) { numLeft = numNamesInFile; }
399 int size = 1000; //assume that user can hold 1000 seqs in memory
400 if (numLeft < size) { size = numLeft; }
403 vector<Sequence> seqs; seqs.resize(size);
404 for (int i = 0; i < seqs.size(); i++) { seqs[i].setName(""); } //this is so if some of the seqs are missing we dont print out garbage
406 while (numLeft > 0) {
409 m->openInputFile(fastafile, in2);
411 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
414 int needToFind = size;
415 if (numLeft < size) { needToFind = numLeft; }
418 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
420 //stop reading if we already found the seqs we are looking for
421 if (found >= needToFind) { break; }
423 Sequence currSeq(in2);
424 name = currSeq.getName();
427 map<string, int>::iterator it = names.find(name);
428 if (it != names.end()) { //we found it, so put it in the vector in the right place.
429 //is it in the set of seqs we are looking for this time around
430 int thisSeqsPlace = it->second;
431 thisSeqsPlace -= (times * size);
432 if ((thisSeqsPlace < size) && (thisSeqsPlace >= 0)) {
433 seqs[thisSeqsPlace] = currSeq;
436 }else { m->mothurOut("[ERROR]: in logic of readFasta function.\n"); m->control_pressed = true; }
443 m->openOutputFileAppend(outputFileName, out2);
445 int output = seqs.size();
446 if (numLeft < seqs.size()) { output = numLeft; }
448 for (int i = 0; i < output; i++) {
449 if (seqs[i].getName() != "") { seqs[i].printSequence(out2); }
457 m->mothurOut("Ordered " + toString(numNamesInFile) + " sequences from " + fastafile + ".\n");
460 vector<Sequence> seqs; seqs.resize(names.size());
461 for (int i = 0; i < seqs.size(); i++) { seqs[i].setName(""); } //this is so if some of the seqs are missing we dont print out garbage
464 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
466 Sequence currSeq(in);
467 name = currSeq.getName();
470 map<string, int>::iterator it = names.find(name);
471 if (it != names.end()) { //we found it, so put it in the vector in the right place.
472 seqs[it->second] = currSeq;
473 }else { //if we cant find it then add it to the end
474 names[name] = seqs.size();
475 seqs.push_back(currSeq);
476 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
484 for (int i = 0; i < seqs.size(); i++) {
485 if (seqs[i].getName() != "") {
486 seqs[i].printSequence(out); count++;
491 m->mothurOut("Ordered " + toString(count) + " sequences from " + fastafile + ".\n");
494 }else { //read in file to fill names
498 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
500 Sequence currSeq(in);
501 name = currSeq.getName();
504 //if this name is in the accnos file
507 currSeq.printSequence(out);
514 m->mothurOut("\nUsing " + fastafile + " to determine the order. It contains " + toString(count) + " sequences.\n");
520 catch(exception& e) {
521 m->errorOut(e, "SortSeqsCommand", "readFasta");
525 //**********************************************************************************************************************
526 int SortSeqsCommand::readFlow(){
528 string thisOutputDir = outputDir;
529 if (outputDir == "") { thisOutputDir += m->hasPath(flowfile); }
530 map<string, string> variables;
531 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(flowfile));
532 variables["[extension]"] = m->getExtension(flowfile);
533 string outputFileName = getOutputFileName("flow", variables);
534 outputTypes["flow"].push_back(outputFileName); outputNames.push_back(outputFileName);
537 m->openOutputFile(outputFileName, out);
540 m->openInputFile(flowfile, in);
544 in >> numFlows; m->gobble(in);
546 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
548 if (large) { //if the file is too large to fit in memory we can still process it, but the io will be very time consuming.
549 //read through the file looking for 1000 seqs at a time. Once we find them output them and start looking for the next 1000.
550 //this way we only store 1000 seqs in memory at a time.
552 int numNames = names.size();
553 int numNamesInFile = 0;
555 //to make sure we dont miss any seqs, add any seqs that are not in names but in the file to the end of names
557 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
560 string rest = m->getline(in);
564 map<string, int>::iterator it = names.find(name);
565 if (it == names.end()) {
566 names[name] = numNames; numNames++;
567 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
575 int numLeft = names.size();
576 if (numNamesInFile < numLeft) { numLeft = numNamesInFile; }
578 int size = 1000; //assume that user can hold 1000 seqs in memory
579 if (numLeft < size) { size = numLeft; }
582 vector<string> seqs; seqs.resize(size, "");
584 while (numLeft > 0) {
587 m->openInputFile(flowfile, in2); in2 >> numFlows; m->gobble(in2);
589 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
592 int needToFind = size;
593 if (numLeft < size) { needToFind = numLeft; }
596 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
598 //stop reading if we already found the seqs we are looking for
599 if (found >= needToFind) { break; }
602 string rest = m->getline(in2);
605 map<string, int>::iterator it = names.find(name);
606 if (it != names.end()) { //we found it, so put it in the vector in the right place.
607 //is it in the set of seqs we are looking for this time around
608 int thisSeqsPlace = it->second;
609 thisSeqsPlace -= (times * size);
610 if ((thisSeqsPlace < size) && (thisSeqsPlace >= 0)) {
611 seqs[thisSeqsPlace] = (name +'\t' + rest);
614 }else { m->mothurOut("[ERROR]: in logic of readFlow function.\n"); m->control_pressed = true; }
621 m->openOutputFileAppend(outputFileName, out2);
623 int output = seqs.size();
624 if (numLeft < seqs.size()) { output = numLeft; }
626 for (int i = 0; i < output; i++) {
628 out2 << seqs[i] << endl;
637 m->mothurOut("Ordered " + toString(numNamesInFile) + " flows from " + flowfile + ".\n");
640 vector<string> seqs; seqs.resize(names.size(), "");
643 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
646 string rest = m->getline(in);
649 map<string, int>::iterator it = names.find(name);
650 if (it != names.end()) { //we found it, so put it in the vector in the right place.
651 seqs[it->second] = (name + '\t' + rest);
652 }else { //if we cant find it then add it to the end
653 names[name] = seqs.size();
654 seqs.push_back((name + '\t' + rest));
655 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
663 for (int i = 0; i < seqs.size(); i++) {
665 out << seqs[i] << endl;
671 m->mothurOut("Ordered " + toString(count) + " flows from " + flowfile + ".\n");
674 }else { //read in file to fill names
678 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
681 string rest = m->getline(in);
684 //if this name is in the accnos file
687 out << name << '\t' << rest << endl;
694 m->mothurOut("\nUsing " + flowfile + " to determine the order. It contains " + toString(count) + " flows.\n");
700 catch(exception& e) {
701 m->errorOut(e, "SortSeqsCommand", "readFlow");
706 //**********************************************************************************************************************
707 int SortSeqsCommand::readQual(){
709 string thisOutputDir = outputDir;
710 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
711 map<string, string> variables;
712 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(qualfile));
713 variables["[extension]"] = m->getExtension(qualfile);
714 string outputFileName = getOutputFileName("qfile", variables);
715 outputTypes["qfile"].push_back(outputFileName); outputNames.push_back(outputFileName);
718 m->openOutputFile(outputFileName, out);
721 m->openInputFile(qualfile, in);
724 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
726 if (large) { //if the file is too large to fit in memory we can still process it, but the io will be very time consuming.
727 //read through the file looking for 1000 seqs at a time. Once we find them output them and start looking for the next 1000.
728 //this way we only store 1000 seqs in memory at a time.
730 int numNames = names.size();
731 int numNamesInFile = 0;
733 //to make sure we dont miss any seqs, add any seqs that are not in names but in the file to the end of names
735 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
737 QualityScores currQual;
738 currQual = QualityScores(in);
739 name = currQual.getName();
743 map<string, int>::iterator it = names.find(name);
744 if (it == names.end()) {
745 names[name] = numNames; numNames++;
746 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
754 int numLeft = names.size();
755 if (numNamesInFile < numLeft) { numLeft = numNamesInFile; }
757 int size = 1000; //assume that user can hold 1000 seqs in memory
758 if (numLeft < size) { size = numLeft; }
762 vector<QualityScores> seqs; seqs.resize(size);
763 for (int i = 0; i < seqs.size(); i++) { seqs[i].setName(""); } //this is so if some of the seqs are missing we dont print out garbage
765 while (numLeft > 0) {
768 m->openInputFile(qualfile, in2);
770 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
773 int needToFind = size;
774 if (numLeft < size) { needToFind = numLeft; }
777 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
779 //stop reading if we already found the seqs we are looking for
780 if (found >= needToFind) { break; }
782 QualityScores currQual;
783 currQual = QualityScores(in2);
784 name = currQual.getName();
787 map<string, int>::iterator it = names.find(name);
788 if (it != names.end()) { //we found it, so put it in the vector in the right place.
789 //is it in the set of seqs we are looking for this time around
790 int thisSeqsPlace = it->second;
791 thisSeqsPlace -= (times * size);
792 if ((thisSeqsPlace < size) && (thisSeqsPlace >= 0)) {
793 seqs[thisSeqsPlace] = currQual;
796 }else { m->mothurOut("[ERROR]: in logic of readQual function.\n"); m->control_pressed = true; }
803 m->openOutputFileAppend(outputFileName, out2);
805 int output = seqs.size();
806 if (numLeft < seqs.size()) { output = numLeft; }
808 for (int i = 0; i < output; i++) {
809 if (seqs[i].getName() != "") {
810 seqs[i].printQScores(out2);
819 m->mothurOut("Ordered " + toString(numNamesInFile) + " sequences from " + qualfile + ".\n");
823 vector<QualityScores> seqs; seqs.resize(names.size());
824 for (int i = 0; i < seqs.size(); i++) { seqs[i].setName(""); } //this is so if some of the seqs are missing we dont print out garbage
827 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
829 QualityScores currQual;
830 currQual = QualityScores(in);
831 name = currQual.getName();
834 map<string, int>::iterator it = names.find(name);
835 if (it != names.end()) { //we found it, so put it in the vector in the right place.
836 seqs[it->second] = currQual;
837 }else { //if we cant find it then add it to the end
838 names[name] = seqs.size();
839 seqs.push_back(currQual);
840 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
848 for (int i = 0; i < seqs.size(); i++) {
849 if (seqs[i].getName() != "") { seqs[i].printQScores(out); count++; }
853 m->mothurOut("Ordered " + toString(count) + " sequences from " + qualfile + ".\n");
856 }else { //read in file to fill names
860 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
862 QualityScores currQual;
863 currQual = QualityScores(in);
867 if (currQual.getName() != "") {
868 //if this name is in the accnos file
869 names[currQual.getName()] = count;
871 currQual.printQScores(out);
878 m->mothurOut("\nUsing " + qualfile + " to determine the order. It contains " + toString(count) + " sequences.\n");
884 catch(exception& e) {
885 m->errorOut(e, "SortSeqsCommand", "readQual");
889 //**********************************************************************************************************************
890 int SortSeqsCommand::readName(){
892 string thisOutputDir = outputDir;
893 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
894 map<string, string> variables;
895 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(namefile));
896 variables["[extension]"] = m->getExtension(namefile);
897 string outputFileName = getOutputFileName("name", variables);
898 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
901 m->openOutputFile(outputFileName, out);
904 m->openInputFile(namefile, in);
905 string name, firstCol, secondCol;
907 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
909 vector<string> seqs; seqs.resize(names.size(), "");
912 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
914 in >> firstCol; m->gobble(in);
915 in >> secondCol; m->gobble(in);
917 if (firstCol != "") {
918 map<string, int>::iterator it = names.find(firstCol);
919 if (it != names.end()) { //we found it, so put it in the vector in the right place.
920 seqs[it->second] = firstCol + '\t' + secondCol;
921 }else { //if we cant find it then add it to the end
922 names[firstCol] = seqs.size();
923 seqs.push_back((firstCol + '\t' + secondCol));
924 m->mothurOut(firstCol + " was not in the contained the file which determined the order, adding it to the end.\n");
931 for (int i = 0; i < seqs.size(); i++) {
932 if (seqs[i] != "") { out << seqs[i] << endl; count++; }
936 m->mothurOut("Ordered " + toString(count) + " sequences from " + namefile + ".\n");
938 }else { //read in file to fill names
942 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
944 in >> firstCol; m->gobble(in);
945 in >> secondCol; m->gobble(in);
947 if (firstCol != "") {
948 //if this name is in the accnos file
949 names[firstCol] = count;
951 out << firstCol << '\t' << secondCol << endl;
958 m->mothurOut("\nUsing " + namefile + " to determine the order. It contains " + toString(count) + " representative sequences.\n");
963 catch(exception& e) {
964 m->errorOut(e, "SortSeqsCommand", "readName");
968 //**********************************************************************************************************************
969 int SortSeqsCommand::readCount(){
971 string thisOutputDir = outputDir;
972 if (outputDir == "") { thisOutputDir += m->hasPath(countfile); }
973 map<string, string> variables;
974 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
975 variables["[extension]"] = m->getExtension(countfile);
976 string outputFileName = getOutputFileName("count", variables);
977 outputTypes["count"].push_back(outputFileName); outputNames.push_back(outputFileName);
980 m->openOutputFile(outputFileName, out);
983 m->openInputFile(countfile, in);
984 string firstCol, rest;
986 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
988 vector<string> seqs; seqs.resize(names.size(), "");
990 string headers = m->getline(in); m->gobble(in);
993 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
995 in >> firstCol; m->gobble(in);
996 rest = m->getline(in); m->gobble(in);
998 if (firstCol != "") {
999 map<string, int>::iterator it = names.find(firstCol);
1000 if (it != names.end()) { //we found it, so put it in the vector in the right place.
1001 seqs[it->second] = firstCol + '\t' + rest;
1002 }else { //if we cant find it then add it to the end
1003 names[firstCol] = seqs.size();
1004 seqs.push_back((firstCol + '\t' + rest));
1005 m->mothurOut(firstCol + " was not in the contained the file which determined the order, adding it to the end.\n");
1012 out << headers << endl;
1013 for (int i = 0; i < seqs.size(); i++) {
1014 if (seqs[i] != "") { out << seqs[i] << endl; count++; }
1018 m->mothurOut("Ordered " + toString(count) + " sequences from " + countfile + ".\n");
1020 }else { //read in file to fill names
1023 string headers = m->getline(in); m->gobble(in);
1024 out << headers << endl;
1027 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
1029 in >> firstCol; m->gobble(in);
1030 rest = m->getline(in); m->gobble(in);
1032 if (firstCol != "") {
1033 //if this name is in the accnos file
1034 names[firstCol] = count;
1036 out << firstCol << '\t' << rest << endl;
1043 m->mothurOut("\nUsing " + countfile + " to determine the order. It contains " + toString(count) + " representative sequences.\n");
1048 catch(exception& e) {
1049 m->errorOut(e, "SortSeqsCommand", "readCount");
1053 //**********************************************************************************************************************
1054 int SortSeqsCommand::readGroup(){
1056 string thisOutputDir = outputDir;
1057 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
1058 map<string, string> variables;
1059 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
1060 variables["[extension]"] = m->getExtension(groupfile);
1061 string outputFileName = getOutputFileName("group", variables);
1062 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
1065 m->openOutputFile(outputFileName, out);
1068 m->openInputFile(groupfile, in);
1071 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
1073 vector<string> seqs; seqs.resize(names.size(), "");
1076 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
1078 in >> name; m->gobble(in);
1079 in >> group; m->gobble(in);
1082 map<string, int>::iterator it = names.find(name);
1083 if (it != names.end()) { //we found it, so put it in the vector in the right place.
1084 seqs[it->second] = name + '\t' + group;
1085 }else { //if we cant find it then add it to the end
1086 names[name] = seqs.size();
1087 seqs.push_back((name + '\t' + group));
1088 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
1095 for (int i = 0; i < seqs.size(); i++) {
1096 if (seqs[i] != "") { out << seqs[i] << endl; count++; }
1100 m->mothurOut("Ordered " + toString(count) + " sequences from " + groupfile + ".\n");
1102 }else { //read in file to fill names
1106 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
1108 in >> name; m->gobble(in);
1109 in >> group; m->gobble(in);
1112 //if this name is in the accnos file
1113 names[name] = count;
1115 out << name << '\t' << group << endl;
1122 m->mothurOut("\nUsing " + groupfile + " to determine the order. It contains " + toString(count) + " sequences.\n");
1127 catch(exception& e) {
1128 m->errorOut(e, "SortSeqsCommand", "readGroup");
1132 //**********************************************************************************************************************
1133 int SortSeqsCommand::readTax(){
1135 string thisOutputDir = outputDir;
1136 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
1137 map<string, string> variables;
1138 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(taxfile));
1139 variables["[extension]"] = m->getExtension(taxfile);
1140 string outputFileName = getOutputFileName("taxonomy", variables);
1142 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
1145 m->openOutputFile(outputFileName, out);
1148 m->openInputFile(taxfile, in);
1151 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
1153 vector<string> seqs; seqs.resize(names.size(), "");
1156 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
1158 in >> name; m->gobble(in);
1159 in >> tax; m->gobble(in);
1162 map<string, int>::iterator it = names.find(name);
1163 if (it != names.end()) { //we found it, so put it in the vector in the right place.
1164 seqs[it->second] = name + '\t' + tax;
1165 }else { //if we cant find it then add it to the end
1166 names[name] = seqs.size();
1167 seqs.push_back((name + '\t' + tax));
1168 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
1175 for (int i = 0; i < seqs.size(); i++) {
1176 if (seqs[i] != "") { out << seqs[i] << endl; count++; }
1180 m->mothurOut("Ordered " + toString(count) + " sequences from " + taxfile + ".\n");
1182 }else { //read in file to fill names
1186 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
1188 in >> name; m->gobble(in);
1189 in >> tax; m->gobble(in);
1192 //if this name is in the accnos file
1193 names[name] = count;
1195 out << name << '\t' << tax << endl;
1202 m->mothurOut("\nUsing " + taxfile + " to determine the order. It contains " + toString(count) + " sequences.\n");
1208 catch(exception& e) {
1209 m->errorOut(e, "SortSeqsCommand", "readTax");
1213 //**********************************************************************************************************************