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");
55 //**********************************************************************************************************************
56 string SortSeqsCommand::getOutputFileNameTag(string type, string inputName=""){
58 string outputFileName = "";
59 map<string, vector<string> >::iterator it;
61 //is this a type this command creates
62 it = outputTypes.find(type);
63 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
65 if (type == "fasta") { outputFileName = "sorted" + m->getExtension(inputName); }
66 else if (type == "taxonomy") { outputFileName = "sorted" + m->getExtension(inputName); }
67 else if (type == "name") { outputFileName = "sorted" + m->getExtension(inputName); }
68 else if (type == "group") { outputFileName = "sorted" + m->getExtension(inputName); }
69 else if (type == "flow") { outputFileName = "sorted" + m->getExtension(inputName); }
70 else if (type == "qfile") { outputFileName = "sorted" + m->getExtension(inputName); }
71 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
73 return outputFileName;
76 m->errorOut(e, "SortSeqsCommand", "getOutputFileNameTag");
81 //**********************************************************************************************************************
82 SortSeqsCommand::SortSeqsCommand(){
84 abort = true; calledHelp = true;
86 vector<string> tempOutNames;
87 outputTypes["fasta"] = tempOutNames;
88 outputTypes["taxonomy"] = tempOutNames;
89 outputTypes["name"] = tempOutNames;
90 outputTypes["group"] = tempOutNames;
91 outputTypes["qfile"] = tempOutNames;
92 outputTypes["flow"] = tempOutNames;
95 m->errorOut(e, "SortSeqsCommand", "SortSeqsCommand");
99 //**********************************************************************************************************************
100 SortSeqsCommand::SortSeqsCommand(string option) {
102 abort = false; calledHelp = false;
104 //allow user to run help
105 if(option == "help") { help(); abort = true; calledHelp = true; }
106 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
109 vector<string> myArray = setParameters();
111 OptionParser parser(option);
112 map<string,string> parameters = parser.getParameters();
114 ValidParameters validParameter;
115 map<string,string>::iterator it;
117 //check to make sure all parameters are valid for command
118 for (it = parameters.begin(); it != parameters.end(); it++) {
119 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
122 //initialize outputTypes
123 vector<string> tempOutNames;
124 outputTypes["fasta"] = tempOutNames;
125 outputTypes["taxonomy"] = tempOutNames;
126 outputTypes["name"] = tempOutNames;
127 outputTypes["group"] = tempOutNames;
128 outputTypes["qfile"] = tempOutNames;
129 outputTypes["flow"] = tempOutNames;
131 //if the user changes the output directory command factory will send this info to us in the output parameter
132 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
134 //if the user changes the input directory command factory will send this info to us in the output parameter
135 string inputDir = validParameter.validFile(parameters, "inputdir", false);
136 if (inputDir == "not found"){ inputDir = ""; }
139 it = parameters.find("fasta");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["fasta"] = inputDir + it->second; }
147 it = parameters.find("name");
148 //user has given a template file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["name"] = inputDir + it->second; }
155 it = parameters.find("group");
156 //user has given a template file
157 if(it != parameters.end()){
158 path = m->hasPath(it->second);
159 //if the user has not given a path then, add inputdir. else leave path alone.
160 if (path == "") { parameters["group"] = inputDir + it->second; }
163 it = parameters.find("taxonomy");
164 //user has given a template file
165 if(it != parameters.end()){
166 path = m->hasPath(it->second);
167 //if the user has not given a path then, add inputdir. else leave path alone.
168 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
171 it = parameters.find("qfile");
172 //user has given a template file
173 if(it != parameters.end()){
174 path = m->hasPath(it->second);
175 //if the user has not given a path then, add inputdir. else leave path alone.
176 if (path == "") { parameters["qfile"] = inputDir + it->second; }
179 it = parameters.find("accnos");
180 //user has given a template file
181 if(it != parameters.end()){
182 path = m->hasPath(it->second);
183 //if the user has not given a path then, add inputdir. else leave path alone.
184 if (path == "") { parameters["accnos"] = inputDir + it->second; }
187 it = parameters.find("flow");
188 //user has given a template file
189 if(it != parameters.end()){
190 path = m->hasPath(it->second);
191 //if the user has not given a path then, add inputdir. else leave path alone.
192 if (path == "") { parameters["flow"] = inputDir + it->second; }
197 //check for parameters
198 accnosfile = validParameter.validFile(parameters, "accnos", true);
199 if (accnosfile == "not open") { accnosfile = ""; abort = true; }
200 else if (accnosfile == "not found") { accnosfile = ""; }
201 else { m->setAccnosFile(accnosfile); }
203 fastafile = validParameter.validFile(parameters, "fasta", true);
204 if (fastafile == "not open") { fastafile = ""; abort = true; }
205 else if (fastafile == "not found") { fastafile = ""; }
206 else { m->setFastaFile(fastafile); }
208 flowfile = validParameter.validFile(parameters, "flow", true);
209 if (flowfile == "not open") { flowfile = ""; abort = true; }
210 else if (flowfile == "not found") { flowfile = ""; }
211 else { m->setFlowFile(flowfile); }
213 namefile = validParameter.validFile(parameters, "name", true);
214 if (namefile == "not open") { namefile = ""; abort = true; }
215 else if (namefile == "not found") { namefile = ""; }
216 else { m->setNameFile(namefile); }
218 groupfile = validParameter.validFile(parameters, "group", true);
219 if (groupfile == "not open") { abort = true; }
220 else if (groupfile == "not found") { groupfile = ""; }
221 else { m->setGroupFile(groupfile); }
223 taxfile = validParameter.validFile(parameters, "taxonomy", true);
224 if (taxfile == "not open") { abort = true; }
225 else if (taxfile == "not found") { taxfile = ""; }
226 else { m->setTaxonomyFile(taxfile); }
228 qualfile = validParameter.validFile(parameters, "qfile", true);
229 if (qualfile == "not open") { abort = true; }
230 else if (qualfile == "not found") { qualfile = ""; }
231 else { m->setQualFile(qualfile); }
233 string temp = validParameter.validFile(parameters, "large", false); if (temp == "not found") { temp = "f"; }
234 large = m->isTrue(temp);
236 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; }
238 if ((fastafile != "") && (namefile == "")) {
239 vector<string> files; files.push_back(fastafile);
240 parser.getNameFile(files);
245 catch(exception& e) {
246 m->errorOut(e, "SortSeqsCommand", "SortSeqsCommand");
250 //**********************************************************************************************************************
252 int SortSeqsCommand::execute(){
255 if (abort == true) { if (calledHelp) { return 0; } return 2; }
257 //read through the correct file and output lines you want to keep
258 if (accnosfile != "") {
260 m->readAccnos(accnosfile, temp);
261 for (int i = 0; i < temp.size(); i++) { names[temp[i]] = i; }
262 m->mothurOut("\nUsing " + accnosfile + " to determine the order. It contains " + toString(temp.size()) + " representative sequences.\n");
265 if (fastafile != "") { readFasta(); }
266 if (flowfile != "") { readFlow(); }
267 if (qualfile != "") { readQual(); }
268 if (namefile != "") { readName(); }
269 if (groupfile != "") { readGroup(); }
270 if (taxfile != "") { readTax(); }
272 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
274 if (outputNames.size() != 0) {
275 m->mothurOutEndLine();
276 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
277 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
278 m->mothurOutEndLine();
280 //set fasta file as new current fastafile
282 itTypes = outputTypes.find("fasta");
283 if (itTypes != outputTypes.end()) {
284 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
287 itTypes = outputTypes.find("name");
288 if (itTypes != outputTypes.end()) {
289 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
292 itTypes = outputTypes.find("group");
293 if (itTypes != outputTypes.end()) {
294 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
298 itTypes = outputTypes.find("taxonomy");
299 if (itTypes != outputTypes.end()) {
300 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
303 itTypes = outputTypes.find("qfile");
304 if (itTypes != outputTypes.end()) {
305 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
308 itTypes = outputTypes.find("flow");
309 if (itTypes != outputTypes.end()) {
310 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFlowFile(current); }
317 catch(exception& e) {
318 m->errorOut(e, "SortSeqsCommand", "execute");
323 //**********************************************************************************************************************
324 int SortSeqsCommand::readFasta(){
326 string thisOutputDir = outputDir;
327 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
328 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("fasta", fastafile);
329 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
332 m->openOutputFile(outputFileName, out);
335 m->openInputFile(fastafile, in);
338 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
340 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.
341 //read through the file looking for 1000 seqs at a time. Once we find them output them and start looking for the next 1000.
342 //this way we only store 1000 seqs in memory at a time.
344 int numNames = names.size();
345 int numNamesInFile = 0;
347 //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
349 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
351 Sequence currSeq(in);
352 name = currSeq.getName();
356 map<string, int>::iterator it = names.find(name);
357 if (it == names.end()) {
358 names[name] = numNames; numNames++;
359 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
367 int numLeft = names.size();
368 if (numNamesInFile < numLeft) { numLeft = numNamesInFile; }
370 int size = 1000; //assume that user can hold 1000 seqs in memory
371 if (numLeft < size) { size = numLeft; }
374 vector<Sequence> seqs; seqs.resize(size);
375 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
377 while (numLeft > 0) {
380 m->openInputFile(fastafile, in2);
382 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
385 int needToFind = size;
386 if (numLeft < size) { needToFind = numLeft; }
389 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
391 //stop reading if we already found the seqs we are looking for
392 if (found >= needToFind) { break; }
394 Sequence currSeq(in2);
395 name = currSeq.getName();
398 map<string, int>::iterator it = names.find(name);
399 if (it != names.end()) { //we found it, so put it in the vector in the right place.
400 //is it in the set of seqs we are looking for this time around
401 int thisSeqsPlace = it->second;
402 thisSeqsPlace -= (times * size);
403 if ((thisSeqsPlace < size) && (thisSeqsPlace >= 0)) {
404 seqs[thisSeqsPlace] = currSeq;
407 }else { m->mothurOut("[ERROR]: in logic of readFasta function.\n"); m->control_pressed = true; }
414 m->openOutputFileAppend(outputFileName, out2);
416 int output = seqs.size();
417 if (numLeft < seqs.size()) { output = numLeft; }
419 for (int i = 0; i < output; i++) {
420 if (seqs[i].getName() != "") { seqs[i].printSequence(out2); }
428 m->mothurOut("Ordered " + toString(numNamesInFile) + " sequences from " + fastafile + ".\n");
431 vector<Sequence> seqs; seqs.resize(names.size());
432 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
435 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
437 Sequence currSeq(in);
438 name = currSeq.getName();
441 map<string, int>::iterator it = names.find(name);
442 if (it != names.end()) { //we found it, so put it in the vector in the right place.
443 seqs[it->second] = currSeq;
444 }else { //if we cant find it then add it to the end
445 names[name] = seqs.size();
446 seqs.push_back(currSeq);
447 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
455 for (int i = 0; i < seqs.size(); i++) {
456 if (seqs[i].getName() != "") {
457 seqs[i].printSequence(out); count++;
462 m->mothurOut("Ordered " + toString(count) + " sequences from " + fastafile + ".\n");
465 }else { //read in file to fill names
469 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
471 Sequence currSeq(in);
472 name = currSeq.getName();
475 //if this name is in the accnos file
478 currSeq.printSequence(out);
485 m->mothurOut("\nUsing " + fastafile + " to determine the order. It contains " + toString(count) + " sequences.\n");
491 catch(exception& e) {
492 m->errorOut(e, "SortSeqsCommand", "readFasta");
496 //**********************************************************************************************************************
497 int SortSeqsCommand::readFlow(){
499 string thisOutputDir = outputDir;
500 if (outputDir == "") { thisOutputDir += m->hasPath(flowfile); }
501 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(flowfile)) + getOutputFileNameTag("flow", flowfile);
502 outputTypes["flow"].push_back(outputFileName); outputNames.push_back(outputFileName);
505 m->openOutputFile(outputFileName, out);
508 m->openInputFile(flowfile, in);
512 in >> numFlows; m->gobble(in);
514 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
516 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.
517 //read through the file looking for 1000 seqs at a time. Once we find them output them and start looking for the next 1000.
518 //this way we only store 1000 seqs in memory at a time.
520 int numNames = names.size();
521 int numNamesInFile = 0;
523 //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
525 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
528 string rest = m->getline(in);
532 map<string, int>::iterator it = names.find(name);
533 if (it == names.end()) {
534 names[name] = numNames; numNames++;
535 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
543 int numLeft = names.size();
544 if (numNamesInFile < numLeft) { numLeft = numNamesInFile; }
546 int size = 1000; //assume that user can hold 1000 seqs in memory
547 if (numLeft < size) { size = numLeft; }
550 vector<string> seqs; seqs.resize(size, "");
552 while (numLeft > 0) {
555 m->openInputFile(flowfile, in2); in2 >> numFlows; m->gobble(in2);
557 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
560 int needToFind = size;
561 if (numLeft < size) { needToFind = numLeft; }
564 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
566 //stop reading if we already found the seqs we are looking for
567 if (found >= needToFind) { break; }
570 string rest = m->getline(in2);
573 map<string, int>::iterator it = names.find(name);
574 if (it != names.end()) { //we found it, so put it in the vector in the right place.
575 //is it in the set of seqs we are looking for this time around
576 int thisSeqsPlace = it->second;
577 thisSeqsPlace -= (times * size);
578 if ((thisSeqsPlace < size) && (thisSeqsPlace >= 0)) {
579 seqs[thisSeqsPlace] = (name +'\t' + rest);
582 }else { m->mothurOut("[ERROR]: in logic of readFlow function.\n"); m->control_pressed = true; }
589 m->openOutputFileAppend(outputFileName, out2);
591 int output = seqs.size();
592 if (numLeft < seqs.size()) { output = numLeft; }
594 for (int i = 0; i < output; i++) {
596 out2 << seqs[i] << endl;
605 m->mothurOut("Ordered " + toString(numNamesInFile) + " flows from " + flowfile + ".\n");
608 vector<string> seqs; seqs.resize(names.size(), "");
611 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
614 string rest = m->getline(in);
617 map<string, int>::iterator it = names.find(name);
618 if (it != names.end()) { //we found it, so put it in the vector in the right place.
619 seqs[it->second] = (name + '\t' + rest);
620 }else { //if we cant find it then add it to the end
621 names[name] = seqs.size();
622 seqs.push_back((name + '\t' + rest));
623 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
631 for (int i = 0; i < seqs.size(); i++) {
633 out << seqs[i] << endl;
639 m->mothurOut("Ordered " + toString(count) + " flows from " + flowfile + ".\n");
642 }else { //read in file to fill names
646 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
649 string rest = m->getline(in);
652 //if this name is in the accnos file
655 out << name << '\t' << rest << endl;
662 m->mothurOut("\nUsing " + flowfile + " to determine the order. It contains " + toString(count) + " flows.\n");
668 catch(exception& e) {
669 m->errorOut(e, "SortSeqsCommand", "readFlow");
674 //**********************************************************************************************************************
675 int SortSeqsCommand::readQual(){
677 string thisOutputDir = outputDir;
678 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
679 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + getOutputFileNameTag("qfile", qualfile);
680 outputTypes["qfile"].push_back(outputFileName); outputNames.push_back(outputFileName);
683 m->openOutputFile(outputFileName, out);
686 m->openInputFile(qualfile, in);
689 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
691 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.
692 //read through the file looking for 1000 seqs at a time. Once we find them output them and start looking for the next 1000.
693 //this way we only store 1000 seqs in memory at a time.
695 int numNames = names.size();
696 int numNamesInFile = 0;
698 //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
700 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
702 QualityScores currQual;
703 currQual = QualityScores(in);
704 name = currQual.getName();
708 map<string, int>::iterator it = names.find(name);
709 if (it == names.end()) {
710 names[name] = numNames; numNames++;
711 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
719 int numLeft = names.size();
720 if (numNamesInFile < numLeft) { numLeft = numNamesInFile; }
722 int size = 1000; //assume that user can hold 1000 seqs in memory
723 if (numLeft < size) { size = numLeft; }
727 vector<QualityScores> seqs; seqs.resize(size);
728 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
730 while (numLeft > 0) {
733 m->openInputFile(qualfile, in2);
735 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
738 int needToFind = size;
739 if (numLeft < size) { needToFind = numLeft; }
742 if (m->control_pressed) { in2.close(); m->mothurRemove(outputFileName); return 0; }
744 //stop reading if we already found the seqs we are looking for
745 if (found >= needToFind) { break; }
747 QualityScores currQual;
748 currQual = QualityScores(in2);
749 name = currQual.getName();
752 map<string, int>::iterator it = names.find(name);
753 if (it != names.end()) { //we found it, so put it in the vector in the right place.
754 //is it in the set of seqs we are looking for this time around
755 int thisSeqsPlace = it->second;
756 thisSeqsPlace -= (times * size);
757 if ((thisSeqsPlace < size) && (thisSeqsPlace >= 0)) {
758 seqs[thisSeqsPlace] = currQual;
761 }else { m->mothurOut("[ERROR]: in logic of readQual function.\n"); m->control_pressed = true; }
768 m->openOutputFileAppend(outputFileName, out2);
770 int output = seqs.size();
771 if (numLeft < seqs.size()) { output = numLeft; }
773 for (int i = 0; i < output; i++) {
774 if (seqs[i].getName() != "") {
775 seqs[i].printQScores(out2);
784 m->mothurOut("Ordered " + toString(numNamesInFile) + " sequences from " + qualfile + ".\n");
788 vector<QualityScores> seqs; seqs.resize(names.size());
789 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
792 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
794 QualityScores currQual;
795 currQual = QualityScores(in);
796 name = currQual.getName();
799 map<string, int>::iterator it = names.find(name);
800 if (it != names.end()) { //we found it, so put it in the vector in the right place.
801 seqs[it->second] = currQual;
802 }else { //if we cant find it then add it to the end
803 names[name] = seqs.size();
804 seqs.push_back(currQual);
805 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
813 for (int i = 0; i < seqs.size(); i++) {
814 if (seqs[i].getName() != "") { seqs[i].printQScores(out); count++; }
818 m->mothurOut("Ordered " + toString(count) + " sequences from " + qualfile + ".\n");
821 }else { //read in file to fill names
825 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
827 QualityScores currQual;
828 currQual = QualityScores(in);
832 if (currQual.getName() != "") {
833 //if this name is in the accnos file
834 names[currQual.getName()] = count;
836 currQual.printQScores(out);
843 m->mothurOut("\nUsing " + qualfile + " to determine the order. It contains " + toString(count) + " sequences.\n");
849 catch(exception& e) {
850 m->errorOut(e, "SortSeqsCommand", "readQual");
854 //**********************************************************************************************************************
855 int SortSeqsCommand::readName(){
857 string thisOutputDir = outputDir;
858 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
859 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + getOutputFileNameTag("name", namefile);
860 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
863 m->openOutputFile(outputFileName, out);
866 m->openInputFile(namefile, in);
867 string name, firstCol, secondCol;
869 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
871 vector<string> seqs; seqs.resize(names.size(), "");
874 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
876 in >> firstCol; m->gobble(in);
877 in >> secondCol; m->gobble(in);
879 if (firstCol != "") {
880 map<string, int>::iterator it = names.find(firstCol);
881 if (it != names.end()) { //we found it, so put it in the vector in the right place.
882 seqs[it->second] = firstCol + '\t' + secondCol;
883 }else { //if we cant find it then add it to the end
884 names[firstCol] = seqs.size();
885 seqs.push_back((firstCol + '\t' + secondCol));
886 m->mothurOut(firstCol + " was not in the contained the file which determined the order, adding it to the end.\n");
893 for (int i = 0; i < seqs.size(); i++) {
894 if (seqs[i] != "") { out << seqs[i] << endl; count++; }
898 m->mothurOut("Ordered " + toString(count) + " sequences from " + namefile + ".\n");
900 }else { //read in file to fill names
904 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
906 in >> firstCol; m->gobble(in);
907 in >> secondCol; m->gobble(in);
909 if (firstCol != "") {
910 //if this name is in the accnos file
911 names[firstCol] = count;
913 out << firstCol << '\t' << secondCol << endl;
920 m->mothurOut("\nUsing " + namefile + " to determine the order. It contains " + toString(count) + " representative sequences.\n");
925 catch(exception& e) {
926 m->errorOut(e, "SortSeqsCommand", "readName");
931 //**********************************************************************************************************************
932 int SortSeqsCommand::readGroup(){
934 string thisOutputDir = outputDir;
935 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
936 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + getOutputFileNameTag("group", groupfile);
937 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
940 m->openOutputFile(outputFileName, out);
943 m->openInputFile(groupfile, in);
946 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
948 vector<string> seqs; seqs.resize(names.size(), "");
951 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
953 in >> name; m->gobble(in);
954 in >> group; m->gobble(in);
957 map<string, int>::iterator it = names.find(name);
958 if (it != names.end()) { //we found it, so put it in the vector in the right place.
959 seqs[it->second] = name + '\t' + group;
960 }else { //if we cant find it then add it to the end
961 names[name] = seqs.size();
962 seqs.push_back((name + '\t' + group));
963 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
970 for (int i = 0; i < seqs.size(); i++) {
971 if (seqs[i] != "") { out << seqs[i] << endl; count++; }
975 m->mothurOut("Ordered " + toString(count) + " sequences from " + groupfile + ".\n");
977 }else { //read in file to fill names
981 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
983 in >> name; m->gobble(in);
984 in >> group; m->gobble(in);
987 //if this name is in the accnos file
990 out << name << '\t' << group << endl;
997 m->mothurOut("\nUsing " + groupfile + " to determine the order. It contains " + toString(count) + " sequences.\n");
1002 catch(exception& e) {
1003 m->errorOut(e, "SortSeqsCommand", "readGroup");
1007 //**********************************************************************************************************************
1008 int SortSeqsCommand::readTax(){
1010 string thisOutputDir = outputDir;
1011 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
1012 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + getOutputFileNameTag("taxonomy", taxfile);
1013 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
1016 m->openOutputFile(outputFileName, out);
1019 m->openInputFile(taxfile, in);
1022 if (names.size() != 0) {//this is not the first file we are reading so we need to use the order we already have
1024 vector<string> seqs; seqs.resize(names.size(), "");
1027 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
1029 in >> name; m->gobble(in);
1030 in >> tax; m->gobble(in);
1033 map<string, int>::iterator it = names.find(name);
1034 if (it != names.end()) { //we found it, so put it in the vector in the right place.
1035 seqs[it->second] = name + '\t' + tax;
1036 }else { //if we cant find it then add it to the end
1037 names[name] = seqs.size();
1038 seqs.push_back((name + '\t' + tax));
1039 m->mothurOut(name + " was not in the contained the file which determined the order, adding it to the end.\n");
1046 for (int i = 0; i < seqs.size(); i++) {
1047 if (seqs[i] != "") { out << seqs[i] << endl; count++; }
1051 m->mothurOut("Ordered " + toString(count) + " sequences from " + taxfile + ".\n");
1053 }else { //read in file to fill names
1057 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
1059 in >> name; m->gobble(in);
1060 in >> tax; m->gobble(in);
1063 //if this name is in the accnos file
1064 names[name] = count;
1066 out << name << '\t' << tax << endl;
1073 m->mothurOut("\nUsing " + taxfile + " to determine the order. It contains " + toString(count) + " sequences.\n");
1079 catch(exception& e) {
1080 m->errorOut(e, "SortSeqsCommand", "readTax");
1084 //**********************************************************************************************************************