2 * splitabundcommand.cpp
5 * Created by westcott on 5/17/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "splitabundcommand.h"
12 //**********************************************************************************************************************
13 vector<string> SplitAbundCommand::getValidParameters(){
15 string Array[] = {"name","group","list","label","accnos","groups","fasta","cutoff","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "SplitAbundCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 SplitAbundCommand::SplitAbundCommand(){
27 //initialize outputTypes
28 vector<string> tempOutNames;
29 outputTypes["list"] = tempOutNames;
30 outputTypes["name"] = tempOutNames;
31 outputTypes["accnos"] = tempOutNames;
32 outputTypes["group"] = tempOutNames;
33 outputTypes["fasta"] = tempOutNames;
36 m->errorOut(e, "SplitAbundCommand", "SplitAbundCommand");
40 //**********************************************************************************************************************
41 vector<string> SplitAbundCommand::getRequiredParameters(){
43 string Array[] = {"fasta","list","name","or"};
44 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
48 m->errorOut(e, "SplitAbundCommand", "getRequiredParameters");
52 //**********************************************************************************************************************
53 vector<string> SplitAbundCommand::getRequiredFiles(){
55 vector<string> myArray;
59 m->errorOut(e, "SplitAbundCommand", "getRequiredFiles");
63 //**********************************************************************************************************************
64 SplitAbundCommand::SplitAbundCommand(string option) {
69 //allow user to run help
70 if(option == "help") { help(); abort = true; }
73 //valid paramters for this command
74 string Array[] = {"name","group","list","label","accnos","groups","fasta","cutoff","outputdir","inputdir"}; //
75 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
77 OptionParser parser(option);
78 map<string, string> parameters = parser.getParameters();
80 ValidParameters validParameter;
81 map<string, string>::iterator it;
83 //check to make sure all parameters are valid for command
84 for (it = parameters.begin(); it != parameters.end(); it++) {
85 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
88 //initialize outputTypes
89 vector<string> tempOutNames;
90 outputTypes["list"] = tempOutNames;
91 outputTypes["name"] = tempOutNames;
92 outputTypes["accnos"] = tempOutNames;
93 outputTypes["group"] = tempOutNames;
94 outputTypes["fasta"] = tempOutNames;
96 //if the user changes the input directory command factory will send this info to us in the output parameter
97 string inputDir = validParameter.validFile(parameters, "inputdir", false);
98 if (inputDir == "not found"){ inputDir = ""; }
101 it = parameters.find("list");
102 //user has given a template file
103 if(it != parameters.end()){
104 path = m->hasPath(it->second);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { parameters["list"] = inputDir + it->second; }
109 it = parameters.find("group");
110 //user has given a template file
111 if(it != parameters.end()){
112 path = m->hasPath(it->second);
113 //if the user has not given a path then, add inputdir. else leave path alone.
114 if (path == "") { parameters["group"] = inputDir + it->second; }
117 it = parameters.find("fasta");
118 //user has given a template file
119 if(it != parameters.end()){
120 path = m->hasPath(it->second);
121 //if the user has not given a path then, add inputdir. else leave path alone.
122 if (path == "") { parameters["fasta"] = inputDir + it->second; }
125 it = parameters.find("name");
126 //user has given a template file
127 if(it != parameters.end()){
128 path = m->hasPath(it->second);
129 //if the user has not given a path then, add inputdir. else leave path alone.
130 if (path == "") { parameters["name"] = inputDir + it->second; }
136 //if the user changes the output directory command factory will send this info to us in the output parameter
137 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
139 //check for required parameters
140 listfile = validParameter.validFile(parameters, "list", true);
141 if (listfile == "not open") { abort = true; }
142 else if (listfile == "not found") { listfile = ""; }
143 else{ inputFile = listfile; }
145 namefile = validParameter.validFile(parameters, "name", true);
146 if (namefile == "not open") { abort = true; }
147 else if (namefile == "not found") { namefile = ""; }
148 else{ inputFile = namefile; }
150 fastafile = validParameter.validFile(parameters, "fasta", true);
151 if (fastafile == "not open") { abort = true; }
152 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the split.abund command. "); m->mothurOutEndLine(); abort = true; }
154 groupfile = validParameter.validFile(parameters, "group", true);
155 if (groupfile == "not open") { groupfile = ""; abort = true; }
156 else if (groupfile == "not found") { groupfile = ""; }
158 groupMap = new GroupMap(groupfile);
160 int error = groupMap->readMap();
161 if (error == 1) { abort = true; }
165 groups = validParameter.validFile(parameters, "groups", false);
166 if (groups == "not found") { groups = ""; }
167 else if (groups == "all") {
168 if (groupfile != "") { Groups = groupMap->namesOfGroups; }
169 else { m->mothurOut("You cannot select groups without a valid groupfile, I will disregard your groups selection. "); m->mothurOutEndLine(); groups = ""; }
171 m->splitAtDash(groups, Groups);
174 if ((groupfile == "") && (groups != "")) { m->mothurOut("You cannot select groups without a valid groupfile, I will disregard your groups selection. "); m->mothurOutEndLine(); groups = ""; Groups.clear(); }
176 //do you have all files needed
177 if ((listfile == "") && (namefile == "")) { m->mothurOut("You must either a listfile or a namefile for the split.abund command. "); m->mothurOutEndLine(); abort = true; }
179 //check for optional parameter and set defaults
180 // ...at some point should added some additional type checking...
181 label = validParameter.validFile(parameters, "label", false);
182 if (label == "not found") { label = ""; allLines = 1; }
184 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
185 else { allLines = 1; }
188 string temp = validParameter.validFile(parameters, "accnos", false); if (temp == "not found") { temp = "F"; }
189 accnos = m->isTrue(temp);
191 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "0"; }
192 convert(temp, cutoff);
194 if (cutoff == 0) { m->mothurOut("You must provide a cutoff to qualify what is abundant for the split.abund command. "); m->mothurOutEndLine(); abort = true; }
199 catch(exception& e) {
200 m->errorOut(e, "SplitAbundCommand", "SplitAbundCommand");
204 //**********************************************************************************************************************
205 void SplitAbundCommand::help(){
207 m->mothurOut("The split.abund command reads a fasta file and a list or a names file splits the sequences into rare and abundant groups. \n");
208 m->mothurOut("The split.abund command parameters are fasta, list, name, cutoff, group, label, groups, cutoff and accnos.\n");
209 m->mothurOut("The fasta and a list or name parameter are required, and you must provide a cutoff value.\n");
210 m->mothurOut("The cutoff parameter is used to qualify what is abundant and rare.\n");
211 m->mothurOut("The group parameter allows you to parse a group file into rare and abundant groups.\n");
212 m->mothurOut("The label parameter is used to read specific labels in your listfile you want to use.\n");
213 m->mothurOut("The accnos parameter allows you to output a .rare.accnos and .abund.accnos files to use with the get.seqs and remove.seqs commands.\n");
214 m->mothurOut("The groups parameter allows you to parse the files into rare and abundant files by group. \n");
215 m->mothurOut("For example if you set groups=A-B-C, you will get a .A.abund, .A.rare, .B.abund, .B.rare, .C.abund, .C.rare files. \n");
216 m->mothurOut("If you want .abund and .rare files for all groups, set groups=all. \n");
217 m->mothurOut("The split.abund command should be used in the following format: split.abund(fasta=yourFasta, list=yourListFile, group=yourGroupFile, label=yourLabels, cutoff=yourCutoff).\n");
218 m->mothurOut("Example: split.abund(fasta=abrecovery.fasta, list=abrecovery.fn.list, group=abrecovery.groups, label=0.03, cutoff=2).\n");
219 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
222 catch(exception& e) {
223 m->errorOut(e, "SplitAbundCommand", "help");
227 //**********************************************************************************************************************
228 SplitAbundCommand::~SplitAbundCommand(){
229 if (groupfile != "") { delete groupMap; }
231 //**********************************************************************************************************************
232 int SplitAbundCommand::execute(){
235 if (abort == true) { return 0; }
237 if (listfile != "") { //you are using a listfile to determine abundance
238 if (outputDir == "") { outputDir = m->hasPath(listfile); }
240 //remove old files so you can append later....
241 string fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
242 if (Groups.size() == 0) {
243 remove((fileroot + "rare.list").c_str());
244 remove((fileroot + "abund.list").c_str());
246 outputNames.push_back((fileroot + "rare.list"));
247 outputNames.push_back((fileroot + "abund.list"));
248 outputTypes["list"].push_back((fileroot + "rare.list"));
249 outputTypes["list"].push_back((fileroot + "abund.list"));
251 for (int i=0; i<Groups.size(); i++) {
252 remove((fileroot + Groups[i] + ".rare.list").c_str());
253 remove((fileroot + Groups[i] + ".abund.list").c_str());
255 outputNames.push_back((fileroot + Groups[i] + ".rare.list"));
256 outputNames.push_back((fileroot + Groups[i] + ".abund.list"));
257 outputTypes["list"].push_back((fileroot + Groups[i] + ".rare.list"));
258 outputTypes["list"].push_back((fileroot + Groups[i] + ".abund.list"));
262 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
263 set<string> processedLabels;
264 set<string> userLabels = labels;
266 input = new InputData(listfile, "list");
267 list = input->getListVector();
268 string lastLabel = list->getLabel();
270 //do you have a namefile or do we need to similate one?
271 if (namefile != "") { readNamesFile(); }
272 else { createNameMap(list); }
274 if (m->control_pressed) { delete input; delete list; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
276 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
278 if (m->control_pressed) { delete input; delete list; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
280 if(allLines == 1 || labels.count(list->getLabel()) == 1){
282 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
285 processedLabels.insert(list->getLabel());
286 userLabels.erase(list->getLabel());
289 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
290 string saveLabel = list->getLabel();
293 list = input->getListVector(lastLabel); //get new list vector to process
295 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
298 processedLabels.insert(list->getLabel());
299 userLabels.erase(list->getLabel());
301 //restore real lastlabel to save below
302 list->setLabel(saveLabel);
306 lastLabel = list->getLabel();
309 list = input->getListVector(); //get new list vector to process
312 if (m->control_pressed) { delete input; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
314 //output error messages about any remaining user labels
315 set<string>::iterator it;
316 bool needToRun = false;
317 for (it = userLabels.begin(); it != userLabels.end(); it++) {
318 m->mothurOut("Your file does not include the label " + *it);
319 if (processedLabels.count(lastLabel) != 1) {
320 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
323 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
328 if (m->control_pressed) { delete input; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
330 //run last label if you need to
331 if (needToRun == true) {
332 if (list != NULL) { delete list; }
333 list = input->getListVector(lastLabel); //get new list vector to process
335 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
343 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
345 }else { //you are using the namefile to determine abundance
346 if (outputDir == "") { outputDir = m->hasPath(namefile); }
352 if (groupfile != "") { parseGroup(tag); }
353 if (accnos) { writeAccnos(tag); }
354 if (fastafile != "") { parseFasta(tag); }
357 m->mothurOutEndLine();
358 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
359 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
360 m->mothurOutEndLine();
364 catch(exception& e) {
365 m->errorOut(e, "SplitAbundCommand", "execute");
369 /**********************************************************************************************************************/
370 int SplitAbundCommand::splitList(ListVector* thisList) {
375 //get rareNames and abundNames
376 for (int i = 0; i < thisList->getNumBins(); i++) {
377 if (m->control_pressed) { return 0; }
379 string bin = thisList->get(i);
381 vector<string> names;
382 m->splitAtComma(bin, names); //parses bin into individual sequence names
383 int size = names.size();
385 if (size <= cutoff) {
386 for (int j = 0; j < names.size(); j++) { rareNames.insert(names[j]); }
388 for (int j = 0; j < names.size(); j++) { abundNames.insert(names[j]); }
394 string tag = thisList->getLabel() + ".";
395 if (groupfile != "") { parseGroup(tag); }
396 if (accnos) { writeAccnos(tag); }
397 if (fastafile != "") { parseFasta(tag); }
402 catch(exception& e) {
403 m->errorOut(e, "SplitAbundCommand", "splitList");
407 /**********************************************************************************************************************/
408 int SplitAbundCommand::writeList(ListVector* thisList) {
411 map<string, ofstream*> filehandles;
413 if (Groups.size() == 0) {
414 SAbundVector* sabund = new SAbundVector();
415 *sabund = thisList->getSAbundVector();
417 //find out how many bins are rare and how many are abundant so you can process the list vector one bin at a time
418 // and don't have to store the bins until you are done with the whole vector, this save alot of space.
420 for (int i = 0; i <= sabund->getMaxRank(); i++) {
421 if (i > cutoff) { break; }
422 numRareBins += sabund->get(i);
424 int numAbundBins = thisList->getNumBins() - numRareBins;
430 string rare = outputDir + m->getRootName(m->getSimpleName(listfile)) + "rare.list";
431 m->openOutputFileAppend(rare, rout);
432 //outputNames.push_back(rare);
434 string abund = outputDir + m->getRootName(m->getSimpleName(listfile)) + "abund.list";
435 m->openOutputFileAppend(abund, aout);
436 //outputNames.push_back(abund);
438 if (rareNames.size() != 0) { rout << thisList->getLabel() << '\t' << numRareBins << '\t'; }
439 if (abundNames.size() != 0) { aout << thisList->getLabel() << '\t' << numAbundBins << '\t'; }
441 for (int i = 0; i < thisList->getNumBins(); i++) {
442 if (m->control_pressed) { break; }
444 string bin = list->get(i);
446 int size = m->getNumNames(bin);
448 if (size <= cutoff) { rout << bin << '\t'; }
449 else { aout << bin << '\t'; }
452 if (rareNames.size() != 0) { rout << endl; }
453 if (abundNames.size() != 0) { aout << endl; }
458 }else{ //parse names by abundance and group
459 string fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
462 //map<string, bool> wroteFile;
463 map<string, ofstream*> filehandles;
464 map<string, ofstream*>::iterator it3;
466 for (int i=0; i<Groups.size(); i++) {
468 filehandles[Groups[i]+".rare"] = temp;
469 temp2 = new ofstream;
470 filehandles[Groups[i]+".abund"] = temp2;
472 m->openOutputFileAppend(fileroot + Groups[i] + ".rare.list", *(filehandles[Groups[i]+".rare"]));
473 m->openOutputFileAppend(fileroot + Groups[i] + ".abund.list", *(filehandles[Groups[i]+".abund"]));
476 map<string, string> groupVector;
477 map<string, string>::iterator itGroup;
478 map<string, int> groupNumBins;
480 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
481 groupNumBins[it3->first] = 0;
482 groupVector[it3->first] = "";
485 for (int i = 0; i < thisList->getNumBins(); i++) {
486 if (m->control_pressed) { break; }
488 map<string, string> groupBins;
489 string bin = list->get(i);
491 vector<string> names;
492 m->splitAtComma(bin, names); //parses bin into individual sequence names
494 //parse bin into list of sequences in each group
495 for (int j = 0; j < names.size(); j++) {
497 if (rareNames.count(names[j]) != 0) { //you are a rare name
499 }else{ //you are a abund name
500 rareAbund = ".abund";
503 string group = groupMap->getGroup(names[j]);
505 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
506 itGroup = groupBins.find(group+rareAbund);
507 if(itGroup == groupBins.end()) {
508 groupBins[group+rareAbund] = names[j]; //add first name
509 groupNumBins[group+rareAbund]++;
510 }else{ //add another name
511 groupBins[group+rareAbund] += "," + names[j];
513 }else if(group == "not found") {
514 m->mothurOut(names[j] + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
519 for (itGroup = groupBins.begin(); itGroup != groupBins.end(); itGroup++) {
520 groupVector[itGroup->first] += itGroup->second + '\t';
525 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
526 (*(filehandles[it3->first])) << thisList->getLabel() << '\t' << groupNumBins[it3->first] << '\t' << groupVector[it3->first] << endl; // label numBins listvector for that group
527 (*(filehandles[it3->first])).close();
535 catch(exception& e) {
536 m->errorOut(e, "SplitAbundCommand", "writeList");
540 /**********************************************************************************************************************/
541 int SplitAbundCommand::splitNames() { //namefile
549 m->openInputFile(namefile, in);
552 if (m->control_pressed) { break; }
554 string firstCol, secondCol;
555 in >> firstCol >> secondCol; m->gobble(in);
557 nameMap[firstCol] = secondCol;
559 int size = m->getNumNames(secondCol);
561 if (size <= cutoff) {
562 rareNames.insert(firstCol);
564 abundNames.insert(firstCol);
572 catch(exception& e) {
573 m->errorOut(e, "SplitAbundCommand", "splitNames");
577 /**********************************************************************************************************************/
578 int SplitAbundCommand::readNamesFile() {
582 m->openInputFile(namefile, in);
585 if (m->control_pressed) { break; }
587 string firstCol, secondCol;
588 in >> firstCol >> secondCol; m->gobble(in);
590 nameMap[firstCol] = secondCol;
597 catch(exception& e) {
598 m->errorOut(e, "SplitAbundCommand", "readNamesFile");
602 /**********************************************************************************************************************/
603 int SplitAbundCommand::createNameMap(ListVector* thisList) {
606 if (thisList != NULL) {
607 for (int i = 0; i < thisList->getNumBins(); i++) {
608 if (m->control_pressed) { return 0; }
610 string bin = thisList->get(i);
612 vector<string> names;
613 m->splitAtComma(bin, names); //parses bin into individual sequence names
615 for (int j = 0; j < names.size(); j++) { nameMap[names[j]] = names[j]; }
621 catch(exception& e) {
622 m->errorOut(e, "SplitAbundCommand", "createNameMap");
626 /**********************************************************************************************************************/
627 int SplitAbundCommand::writeNames() { //namefile
630 map<string, ofstream*> filehandles;
632 if (Groups.size() == 0) {
636 string rare = outputDir + m->getRootName(m->getSimpleName(namefile)) + "rare.names";
637 m->openOutputFile(rare, rout);
638 outputNames.push_back(rare); outputTypes["name"].push_back(rare);
640 string abund = outputDir + m->getRootName(m->getSimpleName(namefile)) + "abund.names";
641 m->openOutputFile(abund, aout);
642 outputNames.push_back(abund); outputTypes["name"].push_back(abund);
644 if (rareNames.size() != 0) {
645 for (set<string>::iterator itRare = rareNames.begin(); itRare != rareNames.end(); itRare++) {
646 rout << (*itRare) << '\t' << nameMap[(*itRare)] << endl;
651 if (abundNames.size() != 0) {
652 for (set<string>::iterator itAbund = abundNames.begin(); itAbund != abundNames.end(); itAbund++) {
653 aout << (*itAbund) << '\t' << nameMap[(*itAbund)] << endl;
658 }else{ //parse names by abundance and group
659 string fileroot = outputDir + m->getRootName(m->getSimpleName(namefile));
662 map<string, ofstream*> filehandles;
663 map<string, ofstream*>::iterator it3;
665 for (int i=0; i<Groups.size(); i++) {
667 filehandles[Groups[i]+".rare"] = temp;
668 temp2 = new ofstream;
669 filehandles[Groups[i]+".abund"] = temp2;
671 m->openOutputFile(fileroot + Groups[i] + ".rare.names", *(filehandles[Groups[i]+".rare"]));
672 m->openOutputFile(fileroot + Groups[i] + ".abund.names", *(filehandles[Groups[i]+".abund"]));
675 for (map<string, string>::iterator itName = nameMap.begin(); itName != nameMap.end(); itName++) {
676 vector<string> names;
677 m->splitAtComma(itName->second, names); //parses bin into individual sequence names
680 if (rareNames.count(itName->first) != 0) { //you are a rare name
682 }else{ //you are a abund name
683 rareAbund = ".abund";
686 map<string, string> outputStrings;
687 map<string, string>::iterator itout;
688 for (int i = 0; i < names.size(); i++) {
690 string group = groupMap->getGroup(names[i]);
692 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
693 itout = outputStrings.find(group+rareAbund);
694 if (itout == outputStrings.end()) {
695 outputStrings[group+rareAbund] = names[i] + '\t' + names[i];
696 }else { outputStrings[group+rareAbund] += "," + names[i]; }
697 }else if(group == "not found") {
698 m->mothurOut(names[i] + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
702 for (itout = outputStrings.begin(); itout != outputStrings.end(); itout++) { *(filehandles[itout->first]) << itout->second << endl; }
706 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
707 (*(filehandles[it3->first])).close();
708 outputNames.push_back(fileroot + it3->first + ".names"); outputTypes["name"].push_back(fileroot + it3->first + ".names");
716 catch(exception& e) {
717 m->errorOut(e, "SplitAbundCommand", "writeNames");
721 /**********************************************************************************************************************/
722 //just write the unique names - if a namesfile is given
723 int SplitAbundCommand::writeAccnos(string tag) {
726 map<string, ofstream*> filehandles;
728 if (Groups.size() == 0) {
733 string rare = outputDir + m->getRootName(m->getSimpleName(inputFile)) + tag + "rare.accnos";
734 m->openOutputFile(rare, rout);
735 outputNames.push_back(rare); outputTypes["accnos"].push_back(rare);
737 for (set<string>::iterator itRare = rareNames.begin(); itRare != rareNames.end(); itRare++) {
738 rout << (*itRare) << endl;
742 string abund = outputDir + m->getRootName(m->getSimpleName(inputFile)) + tag + "abund.accnos";
743 m->openOutputFile(abund, aout);
744 outputNames.push_back(abund); outputTypes["accnos"].push_back(abund);
746 for (set<string>::iterator itAbund = abundNames.begin(); itAbund != abundNames.end(); itAbund++) {
747 aout << (*itAbund) << endl;
751 }else{ //parse names by abundance and group
752 string fileroot = outputDir + m->getRootName(m->getSimpleName(inputFile));
755 map<string, ofstream*> filehandles;
756 map<string, ofstream*>::iterator it3;
758 for (int i=0; i<Groups.size(); i++) {
760 filehandles[Groups[i]+".rare"] = temp;
761 temp2 = new ofstream;
762 filehandles[Groups[i]+".abund"] = temp2;
764 m->openOutputFile(fileroot + tag + Groups[i] + ".rare.accnos", *(filehandles[Groups[i]+".rare"]));
765 m->openOutputFile(fileroot + tag + Groups[i] + ".abund.accnos", *(filehandles[Groups[i]+".abund"]));
769 for (set<string>::iterator itRare = rareNames.begin(); itRare != rareNames.end(); itRare++) {
770 string group = groupMap->getGroup(*itRare);
772 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
773 *(filehandles[group+".rare"]) << *itRare << endl;
778 for (set<string>::iterator itAbund = abundNames.begin(); itAbund != abundNames.end(); itAbund++) {
779 string group = groupMap->getGroup(*itAbund);
781 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
782 *(filehandles[group+".abund"]) << *itAbund << endl;
787 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
788 (*(filehandles[it3->first])).close();
789 outputNames.push_back(fileroot + tag + it3->first + ".accnos"); outputTypes["accnos"].push_back(fileroot + tag + it3->first + ".accnos");
797 catch(exception& e) {
798 m->errorOut(e, "SplitAbundCommand", "writeAccnos");
802 /**********************************************************************************************************************/
803 int SplitAbundCommand::parseGroup(string tag) { //namefile
806 map<string, ofstream*> filehandles;
808 if (Groups.size() == 0) {
812 string rare = outputDir + m->getRootName(m->getSimpleName(groupfile)) + tag + "rare.groups";
813 m->openOutputFile(rare, rout);
814 outputNames.push_back(rare); outputTypes["group"].push_back(rare);
816 string abund = outputDir + m->getRootName(m->getSimpleName(groupfile)) + tag + "abund.groups";
817 m->openOutputFile(abund, aout);
818 outputNames.push_back(abund); outputTypes["group"].push_back(abund);
820 for (map<string, string>::iterator itName = nameMap.begin(); itName != nameMap.end(); itName++) {
821 vector<string> names;
822 m->splitAtComma(itName->second, names); //parses bin into individual sequence names
824 for (int i = 0; i < names.size(); i++) {
826 string group = groupMap->getGroup(names[i]);
828 if (group == "not found") {
829 m->mothurOut(names[i] + " is not in your groupfile, ignoring, please correct."); m->mothurOutEndLine();
831 if (rareNames.count(itName->first) != 0) { //you are a rare name
832 rout << names[i] << '\t' << group << endl;
833 }else{ //you are a abund name
834 aout << names[i] << '\t' << group << endl;
843 }else{ //parse names by abundance and group
844 string fileroot = outputDir + m->getRootName(m->getSimpleName(groupfile));
847 map<string, ofstream*> filehandles;
848 map<string, ofstream*>::iterator it3;
850 for (int i=0; i<Groups.size(); i++) {
852 filehandles[Groups[i]+".rare"] = temp;
853 temp2 = new ofstream;
854 filehandles[Groups[i]+".abund"] = temp2;
856 m->openOutputFile(fileroot + tag + Groups[i] + ".rare.groups", *(filehandles[Groups[i]+".rare"]));
857 m->openOutputFile(fileroot + tag + Groups[i] + ".abund.groups", *(filehandles[Groups[i]+".abund"]));
860 for (map<string, string>::iterator itName = nameMap.begin(); itName != nameMap.end(); itName++) {
861 vector<string> names;
862 m->splitAtComma(itName->second, names); //parses bin into individual sequence names
865 if (rareNames.count(itName->first) != 0) { //you are a rare name
867 }else{ //you are a abund name
868 rareAbund = ".abund";
871 for (int i = 0; i < names.size(); i++) {
873 string group = groupMap->getGroup(names[i]);
875 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
876 *(filehandles[group+rareAbund]) << names[i] << '\t' << group << endl;
881 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
882 (*(filehandles[it3->first])).close();
883 outputNames.push_back(fileroot + tag + it3->first + ".groups"); outputTypes["group"].push_back(fileroot + tag + it3->first + ".groups");
891 catch(exception& e) {
892 m->errorOut(e, "SplitAbundCommand", "parseGroups");
896 /**********************************************************************************************************************/
897 int SplitAbundCommand::parseFasta(string tag) { //namefile
900 map<string, ofstream*> filehandles;
902 if (Groups.size() == 0) {
906 string rare = outputDir + m->getRootName(m->getSimpleName(fastafile)) + tag + "rare.fasta";
907 m->openOutputFile(rare, rout);
908 outputNames.push_back(rare); outputTypes["fasta"].push_back(rare);
910 string abund = outputDir + m->getRootName(m->getSimpleName(fastafile)) + tag + "abund.fasta";
911 m->openOutputFile(abund, aout);
912 outputNames.push_back(abund); outputTypes["fasta"].push_back(abund);
916 m->openInputFile(fastafile, in);
919 if (m->control_pressed) { break; }
921 Sequence seq(in); m->gobble(in);
923 if (seq.getName() != "") {
925 map<string, string>::iterator itNames;
927 itNames = nameMap.find(seq.getName());
929 if (itNames == nameMap.end()) {
930 m->mothurOut(seq.getName() + " is not in your namesfile, ignoring."); m->mothurOutEndLine();
932 if (rareNames.count(seq.getName()) != 0) { //you are a rare name
933 seq.printSequence(rout);
934 }else{ //you are a abund name
935 seq.printSequence(aout);
944 }else{ //parse names by abundance and group
945 string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
948 map<string, ofstream*> filehandles;
949 map<string, ofstream*>::iterator it3;
951 for (int i=0; i<Groups.size(); i++) {
953 filehandles[Groups[i]+".rare"] = temp;
954 temp2 = new ofstream;
955 filehandles[Groups[i]+".abund"] = temp2;
957 m->openOutputFile(fileroot + tag + Groups[i] + ".rare.fasta", *(filehandles[Groups[i]+".rare"]));
958 m->openOutputFile(fileroot + tag + Groups[i] + ".abund.fasta", *(filehandles[Groups[i]+".abund"]));
963 m->openInputFile(fastafile, in);
966 if (m->control_pressed) { break; }
968 Sequence seq(in); m->gobble(in);
970 if (seq.getName() != "") {
971 map<string, string>::iterator itNames = nameMap.find(seq.getName());
973 if (itNames == nameMap.end()) {
974 m->mothurOut(seq.getName() + " is not in your namesfile, ignoring."); m->mothurOutEndLine();
976 vector<string> names;
977 m->splitAtComma(itNames->second, names); //parses bin into individual sequence names
980 if (rareNames.count(itNames->first) != 0) { //you are a rare name
982 }else{ //you are a abund name
983 rareAbund = ".abund";
986 for (int i = 0; i < names.size(); i++) {
988 string group = groupMap->getGroup(seq.getName());
990 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
991 seq.printSequence(*(filehandles[group+rareAbund]));
992 }else if(group == "not found") {
993 m->mothurOut(seq.getName() + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
1001 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
1002 (*(filehandles[it3->first])).close();
1003 outputNames.push_back(fileroot + tag + it3->first + ".fasta"); outputTypes["fasta"].push_back(fileroot + tag + it3->first + ".fasta");
1011 catch(exception& e) {
1012 m->errorOut(e, "SplitAbundCommand", "parseFasta");
1016 /**********************************************************************************************************************/