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",false,false); parameters.push_back(pfasta);
17 CommandParameter pflow("flow", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pflow);
18 CommandParameter pname("name", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pname);
19 CommandParameter pgroup("group", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pgroup);
20 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(ptaxonomy);
21 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pqfile);
22 CommandParameter plarge("large", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(plarge);
23 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
24 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
25 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
27 vector<string> myArray;
28 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
32 m->errorOut(e, "SortSeqsCommand", "setParameters");
36 //**********************************************************************************************************************
37 string SortSeqsCommand::getHelpString(){
39 string helpString = "";
40 helpString += "The sort.seqs command puts the sequences in the same order for the following file types: accnos fasta, name, group, taxonomy, flow or quality file.\n";
41 helpString += "The sort.seqs command parameters are accnos, fasta, name, group, taxonomy, flow, qfile and large.\n";
42 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";
43 helpString += "The large parameters is used to indicate your files are too large to fit in RAM.\n";
44 helpString += "The sort.seqs command should be in the following format: sort.seqs(fasta=yourFasta).\n";
45 helpString += "Example sort.seqs(fasta=amazon.fasta).\n";
46 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
50 m->errorOut(e, "SortSeqsCommand", "getHelpString");
56 //**********************************************************************************************************************
57 SortSeqsCommand::SortSeqsCommand(){
59 abort = true; calledHelp = true;
61 vector<string> tempOutNames;
62 outputTypes["fasta"] = tempOutNames;
63 outputTypes["taxonomy"] = tempOutNames;
64 outputTypes["name"] = tempOutNames;
65 outputTypes["group"] = tempOutNames;
66 outputTypes["qfile"] = tempOutNames;
67 outputTypes["flow"] = tempOutNames;
70 m->errorOut(e, "SortSeqsCommand", "SortSeqsCommand");
74 //**********************************************************************************************************************
75 SortSeqsCommand::SortSeqsCommand(string option) {
77 abort = false; calledHelp = false;
79 //allow user to run help
80 if(option == "help") { help(); abort = true; calledHelp = true; }
81 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
84 vector<string> myArray = setParameters();
86 OptionParser parser(option);
87 map<string,string> parameters = parser.getParameters();
89 ValidParameters validParameter;
90 map<string,string>::iterator it;
92 //check to make sure all parameters are valid for command
93 for (it = parameters.begin(); it != parameters.end(); it++) {
94 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
97 //initialize outputTypes
98 vector<string> tempOutNames;
99 outputTypes["fasta"] = tempOutNames;
100 outputTypes["taxonomy"] = tempOutNames;
101 outputTypes["name"] = tempOutNames;
102 outputTypes["group"] = tempOutNames;
103 outputTypes["qfile"] = tempOutNames;
104 outputTypes["flow"] = tempOutNames;
106 //if the user changes the output directory command factory will send this info to us in the output parameter
107 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
109 //if the user changes the input directory command factory will send this info to us in the output parameter
110 string inputDir = validParameter.validFile(parameters, "inputdir", false);
111 if (inputDir == "not found"){ inputDir = ""; }
114 it = parameters.find("fasta");
115 //user has given a template file
116 if(it != parameters.end()){
117 path = m->hasPath(it->second);
118 //if the user has not given a path then, add inputdir. else leave path alone.
119 if (path == "") { parameters["fasta"] = inputDir + it->second; }
122 it = parameters.find("name");
123 //user has given a template file
124 if(it != parameters.end()){
125 path = m->hasPath(it->second);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { parameters["name"] = inputDir + it->second; }
130 it = parameters.find("group");
131 //user has given a template file
132 if(it != parameters.end()){
133 path = m->hasPath(it->second);
134 //if the user has not given a path then, add inputdir. else leave path alone.
135 if (path == "") { parameters["group"] = inputDir + it->second; }
138 it = parameters.find("taxonomy");
139 //user has given a template file
140 if(it != parameters.end()){
141 path = m->hasPath(it->second);
142 //if the user has not given a path then, add inputdir. else leave path alone.
143 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
146 it = parameters.find("qfile");
147 //user has given a template file
148 if(it != parameters.end()){
149 path = m->hasPath(it->second);
150 //if the user has not given a path then, add inputdir. else leave path alone.
151 if (path == "") { parameters["qfile"] = inputDir + it->second; }
154 it = parameters.find("accnos");
155 //user has given a template file
156 if(it != parameters.end()){
157 path = m->hasPath(it->second);
158 //if the user has not given a path then, add inputdir. else leave path alone.
159 if (path == "") { parameters["accnos"] = inputDir + it->second; }
162 it = parameters.find("flow");
163 //user has given a template file
164 if(it != parameters.end()){
165 path = m->hasPath(it->second);
166 //if the user has not given a path then, add inputdir. else leave path alone.
167 if (path == "") { parameters["flow"] = inputDir + it->second; }
172 //check for parameters
173 accnosfile = validParameter.validFile(parameters, "accnos", true);
174 if (accnosfile == "not open") { accnosfile = ""; abort = true; }
175 else if (accnosfile == "not found") { accnosfile = ""; }
176 else { m->setAccnosFile(accnosfile); }
178 fastafile = validParameter.validFile(parameters, "fasta", true);
179 if (fastafile == "not open") { fastafile = ""; abort = true; }
180 else if (fastafile == "not found") { fastafile = ""; }
181 else { m->setFastaFile(fastafile); }
183 flowfile = validParameter.validFile(parameters, "flow", true);
184 if (flowfile == "not open") { flowfile = ""; abort = true; }
185 else if (flowfile == "not found") { flowfile = ""; }
186 else { m->setFlowFile(flowfile); }
188 namefile = validParameter.validFile(parameters, "name", true);
189 if (namefile == "not open") { namefile = ""; abort = true; }
190 else if (namefile == "not found") { namefile = ""; }
191 else { m->setNameFile(namefile); }
193 groupfile = validParameter.validFile(parameters, "group", true);
194 if (groupfile == "not open") { abort = true; }
195 else if (groupfile == "not found") { groupfile = ""; }
196 else { m->setGroupFile(groupfile); }
198 taxfile = validParameter.validFile(parameters, "taxonomy", true);
199 if (taxfile == "not open") { abort = true; }
200 else if (taxfile == "not found") { taxfile = ""; }
201 else { m->setTaxonomyFile(taxfile); }
203 qualfile = validParameter.validFile(parameters, "qfile", true);
204 if (qualfile == "not open") { abort = true; }
205 else if (qualfile == "not found") { qualfile = ""; }
206 else { m->setQualFile(qualfile); }
208 string temp = validParameter.validFile(parameters, "large", false); if (temp == "not found") { temp = "f"; }
209 large = m->isTrue(temp);
211 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (taxfile == "") && (flowfile == "") && (qualfile == "")) { m->mothurOut("You must provide at least one of the following: fasta, name, group, taxonomy, flow or quality."); m->mothurOutEndLine(); abort = true; }
213 if ((fastafile != "") && (namefile == "")) {
214 vector<string> files; files.push_back(fastafile);
215 parser.getNameFile(files);
220 catch(exception& e) {
221 m->errorOut(e, "SortSeqsCommand", "SortSeqsCommand");
225 //**********************************************************************************************************************
227 int SortSeqsCommand::execute(){
230 if (abort == true) { if (calledHelp) { return 0; } return 2; }
232 //read through the correct file and output lines you want to keep
233 if (accnosfile != "") { readAccnos(); }
234 if (fastafile != "") { readFasta(); }
235 if (flowfile != "") { readFlow(); }
236 if (qualfile != "") { readQual(); }
237 if (namefile != "") { readName(); }
238 if (groupfile != "") { readGroup(); }
239 if (taxfile != "") { readTax(); }
241 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
243 if (outputNames.size() != 0) {
244 m->mothurOutEndLine();
245 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
246 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
247 m->mothurOutEndLine();
249 //set fasta file as new current fastafile
251 itTypes = outputTypes.find("fasta");
252 if (itTypes != outputTypes.end()) {
253 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
256 itTypes = outputTypes.find("name");
257 if (itTypes != outputTypes.end()) {
258 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
261 itTypes = outputTypes.find("group");
262 if (itTypes != outputTypes.end()) {
263 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
267 itTypes = outputTypes.find("taxonomy");
268 if (itTypes != outputTypes.end()) {
269 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
272 itTypes = outputTypes.find("qfile");
273 if (itTypes != outputTypes.end()) {
274 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
277 itTypes = outputTypes.find("flow");
278 if (itTypes != outputTypes.end()) {
279 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFlowFile(current); }
286 catch(exception& e) {
287 m->errorOut(e, "SortSeqsCommand", "execute");
292 //**********************************************************************************************************************
293 int SortSeqsCommand::readFasta(){
295 string thisOutputDir = outputDir;
296 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
297 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "sorted" + m->getExtension(fastafile);
298 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
301 m->openOutputFile(outputFileName, out);
304 m->openInputFile(fastafile, in);
307 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
309 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.
310 //read through the file looking for 1000 seqs at a time. Once we find them output them and start looking for the next 1000.
311 //this way we only store 1000 seqs in memory at a time.
313 int numNames = names.size();
314 int numNamesInFile = 0;
316 //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
318 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
320 Sequence currSeq(in);
321 name = currSeq.getName();
325 map<string, int>::iterator it = names.find(name);
326 if (it == names.end()) {
327 names[name] = numNames; numNames++;
328 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
336 int numLeft = names.size();
337 if (numNamesInFile < numLeft) { numLeft = numNamesInFile; }
339 int size = 1000; //assume that user can hold 1000 seqs in memory
340 if (numLeft < size) { size = numLeft; }
343 vector<Sequence> seqs; seqs.resize(size);
344 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
346 while (numLeft > 0) {
349 m->openInputFile(fastafile, in2);
351 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
354 int needToFind = size;
355 if (numLeft < size) { needToFind = numLeft; }
358 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
360 //stop reading if we already found the seqs we are looking for
361 if (found >= needToFind) { break; }
363 Sequence currSeq(in2);
364 name = currSeq.getName();
367 map<string, int>::iterator it = names.find(name);
368 if (it != names.end()) { //we found it, so put it in the vector in the right place.
369 //is it in the set of seqs we are looking for this time around
370 int thisSeqsPlace = it->second;
371 thisSeqsPlace -= (times * size);
372 if ((thisSeqsPlace < size) && (thisSeqsPlace >= 0)) {
373 seqs[thisSeqsPlace] = currSeq;
376 }else { m->mothurOut("[ERROR]: in logic of readFasta function.\n"); m->control_pressed = true; }
383 m->openOutputFileAppend(outputFileName, out2);
385 int output = seqs.size();
386 if (numLeft < seqs.size()) { output = numLeft; }
388 for (int i = 0; i < output; i++) {
389 if (seqs[i].getName() != "") { seqs[i].printSequence(out2); }
397 m->mothurOut("Ordered " + toString(numNamesInFile) + " sequences from " + fastafile + ".\n");
400 vector<Sequence> seqs; seqs.resize(names.size());
401 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
404 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
406 Sequence currSeq(in);
407 name = currSeq.getName();
410 map<string, int>::iterator it = names.find(name);
411 if (it != names.end()) { //we found it, so put it in the vector in the right place.
412 seqs[it->second] = currSeq;
413 }else { //if we cant find it then add it to the end
414 names[name] = seqs.size();
415 seqs.push_back(currSeq);
416 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
424 for (int i = 0; i < seqs.size(); i++) {
425 if (seqs[i].getName() != "") {
426 seqs[i].printSequence(out); count++;
431 m->mothurOut("Ordered " + toString(count) + " sequences from " + fastafile + ".\n");
434 }else { //read in file to fill names
438 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
440 Sequence currSeq(in);
441 name = currSeq.getName();
444 //if this name is in the accnos file
447 currSeq.printSequence(out);
454 m->mothurOut("\nUsing " + fastafile + " to determine the order. It contains " + toString(count) + " sequences.\n");
460 catch(exception& e) {
461 m->errorOut(e, "SortSeqsCommand", "readFasta");
465 //**********************************************************************************************************************
466 int SortSeqsCommand::readFlow(){
468 string thisOutputDir = outputDir;
469 if (outputDir == "") { thisOutputDir += m->hasPath(flowfile); }
470 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowfile)) + "sorted" + m->getExtension(flowfile);
471 outputTypes["flow"].push_back(outputFileName); outputNames.push_back(outputFileName);
474 m->openOutputFile(outputFileName, out);
477 m->openInputFile(flowfile, in);
481 in >> numFlows; m->gobble(in);
483 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
485 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.
486 //read through the file looking for 1000 seqs at a time. Once we find them output them and start looking for the next 1000.
487 //this way we only store 1000 seqs in memory at a time.
489 int numNames = names.size();
490 int numNamesInFile = 0;
492 //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
494 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
497 string rest = m->getline(in);
501 map<string, int>::iterator it = names.find(name);
502 if (it == names.end()) {
503 names[name] = numNames; numNames++;
504 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
512 int numLeft = names.size();
513 if (numNamesInFile < numLeft) { numLeft = numNamesInFile; }
515 int size = 1000; //assume that user can hold 1000 seqs in memory
516 if (numLeft < size) { size = numLeft; }
519 vector<string> seqs; seqs.resize(size, "");
521 while (numLeft > 0) {
524 m->openInputFile(flowfile, in2); in2 >> numFlows; m->gobble(in2);
526 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
529 int needToFind = size;
530 if (numLeft < size) { needToFind = numLeft; }
533 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
535 //stop reading if we already found the seqs we are looking for
536 if (found >= needToFind) { break; }
539 string rest = m->getline(in2);
542 map<string, int>::iterator it = names.find(name);
543 if (it != names.end()) { //we found it, so put it in the vector in the right place.
544 //is it in the set of seqs we are looking for this time around
545 int thisSeqsPlace = it->second;
546 thisSeqsPlace -= (times * size);
547 if ((thisSeqsPlace < size) && (thisSeqsPlace >= 0)) {
548 seqs[thisSeqsPlace] = (name +'\t' + rest);
551 }else { m->mothurOut("[ERROR]: in logic of readFlow function.\n"); m->control_pressed = true; }
558 m->openOutputFileAppend(outputFileName, out2);
560 int output = seqs.size();
561 if (numLeft < seqs.size()) { output = numLeft; }
563 for (int i = 0; i < output; i++) {
565 out2 << seqs[i] << endl;
574 m->mothurOut("Ordered " + toString(numNamesInFile) + " flows from " + flowfile + ".\n");
577 vector<string> seqs; seqs.resize(names.size(), "");
580 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
583 string rest = m->getline(in);
586 map<string, int>::iterator it = names.find(name);
587 if (it != names.end()) { //we found it, so put it in the vector in the right place.
588 seqs[it->second] = (name + '\t' + rest);
589 }else { //if we cant find it then add it to the end
590 names[name] = seqs.size();
591 seqs.push_back((name + '\t' + rest));
592 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
600 for (int i = 0; i < seqs.size(); i++) {
602 out << seqs[i] << endl;
608 m->mothurOut("Ordered " + toString(count) + " flows from " + flowfile + ".\n");
611 }else { //read in file to fill names
615 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
618 string rest = m->getline(in);
621 //if this name is in the accnos file
624 out << name << '\t' << rest << endl;
631 m->mothurOut("\nUsing " + flowfile + " to determine the order. It contains " + toString(count) + " flows.\n");
637 catch(exception& e) {
638 m->errorOut(e, "SortSeqsCommand", "readFlow");
643 //**********************************************************************************************************************
644 int SortSeqsCommand::readQual(){
646 string thisOutputDir = outputDir;
647 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
648 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "sorted" + m->getExtension(qualfile);
649 outputTypes["qfile"].push_back(outputFileName); outputNames.push_back(outputFileName);
652 m->openOutputFile(outputFileName, out);
655 m->openInputFile(qualfile, in);
658 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
660 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.
661 //read through the file looking for 1000 seqs at a time. Once we find them output them and start looking for the next 1000.
662 //this way we only store 1000 seqs in memory at a time.
664 int numNames = names.size();
665 int numNamesInFile = 0;
667 //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
669 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
671 QualityScores currQual;
672 currQual = QualityScores(in);
673 name = currQual.getName();
677 map<string, int>::iterator it = names.find(name);
678 if (it == names.end()) {
679 names[name] = numNames; numNames++;
680 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
688 int numLeft = names.size();
689 if (numNamesInFile < numLeft) { numLeft = numNamesInFile; }
691 int size = 1000; //assume that user can hold 1000 seqs in memory
692 if (numLeft < size) { size = numLeft; }
696 vector<QualityScores> seqs; seqs.resize(size);
697 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
699 while (numLeft > 0) {
702 m->openInputFile(qualfile, in2);
704 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
707 int needToFind = size;
708 if (numLeft < size) { needToFind = numLeft; }
711 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
713 //stop reading if we already found the seqs we are looking for
714 if (found >= needToFind) { break; }
716 QualityScores currQual;
717 currQual = QualityScores(in2);
718 name = currQual.getName();
721 map<string, int>::iterator it = names.find(name);
722 if (it != names.end()) { //we found it, so put it in the vector in the right place.
723 //is it in the set of seqs we are looking for this time around
724 int thisSeqsPlace = it->second;
725 thisSeqsPlace -= (times * size);
726 if ((thisSeqsPlace < size) && (thisSeqsPlace >= 0)) {
727 seqs[thisSeqsPlace] = currQual;
730 }else { m->mothurOut("[ERROR]: in logic of readQual function.\n"); m->control_pressed = true; }
737 m->openOutputFileAppend(outputFileName, out2);
739 int output = seqs.size();
740 if (numLeft < seqs.size()) { output = numLeft; }
742 for (int i = 0; i < output; i++) {
743 if (seqs[i].getName() != "") {
744 seqs[i].printQScores(out2);
753 m->mothurOut("Ordered " + toString(numNamesInFile) + " sequences from " + qualfile + ".\n");
757 vector<QualityScores> seqs; seqs.resize(names.size());
758 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
761 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
763 QualityScores currQual;
764 currQual = QualityScores(in);
765 name = currQual.getName();
768 map<string, int>::iterator it = names.find(name);
769 if (it != names.end()) { //we found it, so put it in the vector in the right place.
770 seqs[it->second] = currQual;
771 }else { //if we cant find it then add it to the end
772 names[name] = seqs.size();
773 seqs.push_back(currQual);
774 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
782 for (int i = 0; i < seqs.size(); i++) {
783 if (seqs[i].getName() != "") { seqs[i].printQScores(out); count++; }
787 m->mothurOut("Ordered " + toString(count) + " sequences from " + qualfile + ".\n");
790 }else { //read in file to fill names
794 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
796 QualityScores currQual;
797 currQual = QualityScores(in);
801 if (currQual.getName() != "") {
802 //if this name is in the accnos file
803 names[currQual.getName()] = count;
805 currQual.printQScores(out);
812 m->mothurOut("\nUsing " + qualfile + " to determine the order. It contains " + toString(count) + " sequences.\n");
818 catch(exception& e) {
819 m->errorOut(e, "SortSeqsCommand", "readQual");
823 //**********************************************************************************************************************
824 int SortSeqsCommand::readName(){
826 string thisOutputDir = outputDir;
827 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
828 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "sorted" + m->getExtension(namefile);
829 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
832 m->openOutputFile(outputFileName, out);
835 m->openInputFile(namefile, in);
836 string name, firstCol, secondCol;
838 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
840 vector<string> seqs; seqs.resize(names.size(), "");
843 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
845 in >> firstCol; m->gobble(in);
846 in >> secondCol; m->gobble(in);
848 if (firstCol != "") {
849 map<string, int>::iterator it = names.find(firstCol);
850 if (it != names.end()) { //we found it, so put it in the vector in the right place.
851 seqs[it->second] = firstCol + '\t' + secondCol;
852 }else { //if we cant find it then add it to the end
853 names[firstCol] = seqs.size();
854 seqs.push_back((firstCol + '\t' + secondCol));
855 m->mothurOut(firstCol + " was not in the contained the file which determined the order, adding it to the end.\n");
862 for (int i = 0; i < seqs.size(); i++) {
863 if (seqs[i] != "") { out << seqs[i] << endl; count++; }
867 m->mothurOut("Ordered " + toString(count) + " sequences from " + namefile + ".\n");
869 }else { //read in file to fill names
873 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
875 in >> firstCol; m->gobble(in);
876 in >> secondCol; m->gobble(in);
878 if (firstCol != "") {
879 //if this name is in the accnos file
880 names[firstCol] = count;
882 out << firstCol << '\t' << secondCol << endl;
889 m->mothurOut("\nUsing " + namefile + " to determine the order. It contains " + toString(count) + " representative sequences.\n");
894 catch(exception& e) {
895 m->errorOut(e, "SortSeqsCommand", "readName");
900 //**********************************************************************************************************************
901 int SortSeqsCommand::readGroup(){
903 string thisOutputDir = outputDir;
904 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
905 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
906 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
909 m->openOutputFile(outputFileName, out);
912 m->openInputFile(groupfile, in);
915 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
917 vector<string> seqs; seqs.resize(names.size(), "");
920 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
922 in >> name; m->gobble(in);
923 in >> group; m->gobble(in);
926 map<string, int>::iterator it = names.find(name);
927 if (it != names.end()) { //we found it, so put it in the vector in the right place.
928 seqs[it->second] = name + '\t' + group;
929 }else { //if we cant find it then add it to the end
930 names[name] = seqs.size();
931 seqs.push_back((name + '\t' + group));
932 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
939 for (int i = 0; i < seqs.size(); i++) {
940 if (seqs[i] != "") { out << seqs[i] << endl; count++; }
944 m->mothurOut("Ordered " + toString(count) + " sequences from " + groupfile + ".\n");
946 }else { //read in file to fill names
950 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
952 in >> name; m->gobble(in);
953 in >> group; m->gobble(in);
956 //if this name is in the accnos file
959 out << name << '\t' << group << endl;
966 m->mothurOut("\nUsing " + groupfile + " to determine the order. It contains " + toString(count) + " sequences.\n");
971 catch(exception& e) {
972 m->errorOut(e, "SortSeqsCommand", "readGroup");
976 //**********************************************************************************************************************
977 int SortSeqsCommand::readTax(){
979 string thisOutputDir = outputDir;
980 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
981 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
982 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
985 m->openOutputFile(outputFileName, out);
988 m->openInputFile(taxfile, in);
991 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
993 vector<string> seqs; seqs.resize(names.size(), "");
996 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
998 in >> name; m->gobble(in);
999 in >> tax; m->gobble(in);
1002 map<string, int>::iterator it = names.find(name);
1003 if (it != names.end()) { //we found it, so put it in the vector in the right place.
1004 seqs[it->second] = name + '\t' + tax;
1005 }else { //if we cant find it then add it to the end
1006 names[name] = seqs.size();
1007 seqs.push_back((name + '\t' + tax));
1008 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
1015 for (int i = 0; i < seqs.size(); i++) {
1016 if (seqs[i] != "") { out << seqs[i] << endl; count++; }
1020 m->mothurOut("Ordered " + toString(count) + " sequences from " + taxfile + ".\n");
1022 }else { //read in file to fill names
1026 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
1028 in >> name; m->gobble(in);
1029 in >> tax; m->gobble(in);
1032 //if this name is in the accnos file
1033 names[name] = count;
1035 out << name << '\t' << tax << endl;
1042 m->mothurOut("\nUsing " + taxfile + " to determine the order. It contains " + toString(count) + " sequences.\n");
1048 catch(exception& e) {
1049 m->errorOut(e, "SortSeqsCommand", "readTax");
1053 //**********************************************************************************************************************
1054 int SortSeqsCommand::readAccnos(){
1058 m->openInputFile(accnosfile, in);
1064 if (m->control_pressed) { break; }
1066 in >> name; m->gobble(in);
1069 names[name] = count;
1075 m->mothurOut("\nUsing " + accnosfile + " to determine the order. It contains " + toString(count) + " representative sequences.\n");
1079 catch(exception& e) {
1080 m->errorOut(e, "SortSeqsCommand", "readAccnos");
1085 //**********************************************************************************************************************