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::setParameters(){
15 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
16 CommandParameter pname("name", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pname);
17 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
18 CommandParameter plist("list", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(plist);
19 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
20 CommandParameter pcutoff("cutoff", "Number", "", "0", "", "", "",false,true); parameters.push_back(pcutoff);
21 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
22 CommandParameter paccnos("accnos", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(paccnos);
23 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
24 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
26 vector<string> myArray;
27 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
31 m->errorOut(e, "SplitAbundCommand", "setParameters");
35 //**********************************************************************************************************************
36 string SplitAbundCommand::getHelpString(){
38 string helpString = "";
39 helpString += "The split.abund command reads a fasta file and a list or a names file splits the sequences into rare and abundant groups. \n";
40 helpString += "The split.abund command parameters are fasta, list, name, cutoff, group, label, groups, cutoff and accnos.\n";
41 helpString += "The fasta and a list or name parameter are required, and you must provide a cutoff value.\n";
42 helpString += "The cutoff parameter is used to qualify what is abundant and rare.\n";
43 helpString += "The group parameter allows you to parse a group file into rare and abundant groups.\n";
44 helpString += "The label parameter is used to read specific labels in your listfile you want to use.\n";
45 helpString += "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";
46 helpString += "The groups parameter allows you to parse the files into rare and abundant files by group. \n";
47 helpString += "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";
48 helpString += "If you want .abund and .rare files for all groups, set groups=all. \n";
49 helpString += "The split.abund command should be used in the following format: split.abund(fasta=yourFasta, list=yourListFile, group=yourGroupFile, label=yourLabels, cutoff=yourCutoff).\n";
50 helpString += "Example: split.abund(fasta=abrecovery.fasta, list=abrecovery.fn.list, group=abrecovery.groups, label=0.03, cutoff=2).\n";
51 helpString += "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n";
55 m->errorOut(e, "SplitAbundCommand", "getHelpString");
60 //**********************************************************************************************************************
61 SplitAbundCommand::SplitAbundCommand(){
63 abort = true; calledHelp = true;
65 vector<string> tempOutNames;
66 outputTypes["list"] = tempOutNames;
67 outputTypes["name"] = tempOutNames;
68 outputTypes["accnos"] = tempOutNames;
69 outputTypes["group"] = tempOutNames;
70 outputTypes["fasta"] = tempOutNames;
73 m->errorOut(e, "SplitAbundCommand", "SplitAbundCommand");
77 //**********************************************************************************************************************
78 SplitAbundCommand::SplitAbundCommand(string option) {
80 abort = false; calledHelp = false;
83 //allow user to run help
84 if(option == "help") { help(); abort = true; calledHelp = true; }
85 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
87 vector<string> myArray = setParameters();
89 OptionParser parser(option);
90 map<string, string> parameters = parser.getParameters();
92 ValidParameters validParameter;
93 map<string, string>::iterator it;
95 //check to make sure all parameters are valid for command
96 for (it = parameters.begin(); it != parameters.end(); it++) {
97 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
100 //initialize outputTypes
101 vector<string> tempOutNames;
102 outputTypes["list"] = tempOutNames;
103 outputTypes["name"] = tempOutNames;
104 outputTypes["accnos"] = tempOutNames;
105 outputTypes["group"] = tempOutNames;
106 outputTypes["fasta"] = tempOutNames;
108 //if the user changes the input directory command factory will send this info to us in the output parameter
109 string inputDir = validParameter.validFile(parameters, "inputdir", false);
110 if (inputDir == "not found"){ inputDir = ""; }
113 it = parameters.find("list");
114 //user has given a template file
115 if(it != parameters.end()){
116 path = m->hasPath(it->second);
117 //if the user has not given a path then, add inputdir. else leave path alone.
118 if (path == "") { parameters["list"] = inputDir + it->second; }
121 it = parameters.find("group");
122 //user has given a template file
123 if(it != parameters.end()){
124 path = m->hasPath(it->second);
125 //if the user has not given a path then, add inputdir. else leave path alone.
126 if (path == "") { parameters["group"] = inputDir + it->second; }
129 it = parameters.find("fasta");
130 //user has given a template file
131 if(it != parameters.end()){
132 path = m->hasPath(it->second);
133 //if the user has not given a path then, add inputdir. else leave path alone.
134 if (path == "") { parameters["fasta"] = inputDir + it->second; }
137 it = parameters.find("name");
138 //user has given a template file
139 if(it != parameters.end()){
140 path = m->hasPath(it->second);
141 //if the user has not given a path then, add inputdir. else leave path alone.
142 if (path == "") { parameters["name"] = inputDir + it->second; }
148 //if the user changes the output directory command factory will send this info to us in the output parameter
149 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
151 //check for required parameters
152 listfile = validParameter.validFile(parameters, "list", true);
153 if (listfile == "not open") { abort = true; }
154 else if (listfile == "not found") { listfile = ""; }
155 else{ inputFile = listfile; m->setListFile(listfile); }
157 namefile = validParameter.validFile(parameters, "name", true);
158 if (namefile == "not open") { abort = true; }
159 else if (namefile == "not found") { namefile = ""; }
160 else{ inputFile = namefile; m->setNameFile(namefile); }
162 fastafile = validParameter.validFile(parameters, "fasta", true);
163 if (fastafile == "not open") { abort = true; }
164 else if (fastafile == "not found") {
165 fastafile = m->getFastaFile();
166 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
167 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
168 }else { m->setFastaFile(fastafile); }
170 groupfile = validParameter.validFile(parameters, "group", true);
171 if (groupfile == "not open") { groupfile = ""; abort = true; }
172 else if (groupfile == "not found") { groupfile = ""; }
174 groupMap = new GroupMap(groupfile);
176 int error = groupMap->readMap();
177 if (error == 1) { abort = true; }
178 m->setGroupFile(groupfile);
181 groups = validParameter.validFile(parameters, "groups", false);
182 if (groups == "not found") { groups = ""; }
183 else if (groups == "all") {
184 if (groupfile != "") { Groups = groupMap->getNamesOfGroups(); }
185 else { m->mothurOut("You cannot select groups without a valid groupfile, I will disregard your groups selection. "); m->mothurOutEndLine(); groups = ""; }
187 m->splitAtDash(groups, Groups);
190 if ((groupfile == "") && (groups != "")) { m->mothurOut("You cannot select groups without a valid groupfile, I will disregard your groups selection. "); m->mothurOutEndLine(); groups = ""; Groups.clear(); }
192 //do you have all files needed
193 if ((listfile == "") && (namefile == "")) {
194 namefile = m->getNameFile();
195 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
197 listfile = m->getListFile();
198 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
199 else { m->mothurOut("You have no current list or namefile and the list or name parameter is required."); m->mothurOutEndLine(); abort = true; }
203 //check for optional parameter and set defaults
204 // ...at some point should added some additional type checking...
205 label = validParameter.validFile(parameters, "label", false);
206 if (label == "not found") { label = ""; allLines = 1; }
208 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
209 else { allLines = 1; }
212 string temp = validParameter.validFile(parameters, "accnos", false); if (temp == "not found") { temp = "F"; }
213 accnos = m->isTrue(temp);
215 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "0"; }
216 m->mothurConvert(temp, cutoff);
218 if (cutoff == 0) { m->mothurOut("You must provide a cutoff to qualify what is abundant for the split.abund command. "); m->mothurOutEndLine(); abort = true; }
222 catch(exception& e) {
223 m->errorOut(e, "SplitAbundCommand", "SplitAbundCommand");
227 //**********************************************************************************************************************
228 SplitAbundCommand::~SplitAbundCommand(){
229 if (groupfile != "") { delete groupMap; }
231 //**********************************************************************************************************************
232 int SplitAbundCommand::execute(){
235 if (abort == true) { if (calledHelp) { return 0; } return 2; }
237 if (listfile != "") { //you are using a listfile to determine abundance
238 if (outputDir == "") { outputDir = m->hasPath(listfile); }
240 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
241 set<string> processedLabels;
242 set<string> userLabels = labels;
244 input = new InputData(listfile, "list");
245 list = input->getListVector();
246 string lastLabel = list->getLabel();
248 //do you have a namefile or do we need to similate one?
249 if (namefile != "") { readNamesFile(); }
250 else { createNameMap(list); }
252 if (m->control_pressed) { delete input; delete list; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
254 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
256 if (m->control_pressed) { delete input; delete list; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
258 if(allLines == 1 || labels.count(list->getLabel()) == 1){
260 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
263 processedLabels.insert(list->getLabel());
264 userLabels.erase(list->getLabel());
267 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
268 string saveLabel = list->getLabel();
271 list = input->getListVector(lastLabel); //get new list vector to process
273 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
276 processedLabels.insert(list->getLabel());
277 userLabels.erase(list->getLabel());
279 //restore real lastlabel to save below
280 list->setLabel(saveLabel);
284 lastLabel = list->getLabel();
287 list = input->getListVector(); //get new list vector to process
290 if (m->control_pressed) { delete input; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
292 //output error messages about any remaining user labels
293 set<string>::iterator it;
294 bool needToRun = false;
295 for (it = userLabels.begin(); it != userLabels.end(); it++) {
296 m->mothurOut("Your file does not include the label " + *it);
297 if (processedLabels.count(lastLabel) != 1) {
298 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
301 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
306 if (m->control_pressed) { delete input; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
308 //run last label if you need to
309 if (needToRun == true) {
310 if (list != NULL) { delete list; }
311 list = input->getListVector(lastLabel); //get new list vector to process
313 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
321 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
323 }else { //you are using the namefile to determine abundance
324 if (outputDir == "") { outputDir = m->hasPath(namefile); }
330 if (groupfile != "") { parseGroup(tag); }
331 if (accnos) { writeAccnos(tag); }
332 if (fastafile != "") { parseFasta(tag); }
335 //set fasta file as new current fastafile
337 itTypes = outputTypes.find("fasta");
338 if (itTypes != outputTypes.end()) {
339 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
342 itTypes = outputTypes.find("name");
343 if (itTypes != outputTypes.end()) {
344 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
347 itTypes = outputTypes.find("group");
348 if (itTypes != outputTypes.end()) {
349 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
352 itTypes = outputTypes.find("list");
353 if (itTypes != outputTypes.end()) {
354 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
357 itTypes = outputTypes.find("accnos");
358 if (itTypes != outputTypes.end()) {
359 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
362 m->mothurOutEndLine();
363 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
364 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
365 m->mothurOutEndLine();
369 catch(exception& e) {
370 m->errorOut(e, "SplitAbundCommand", "execute");
374 /**********************************************************************************************************************/
375 int SplitAbundCommand::splitList(ListVector* thisList) {
380 //get rareNames and abundNames
381 for (int i = 0; i < thisList->getNumBins(); i++) {
382 if (m->control_pressed) { return 0; }
384 string bin = thisList->get(i);
386 vector<string> names;
387 m->splitAtComma(bin, names); //parses bin into individual sequence names
388 int size = names.size();
390 if (size <= cutoff) {
391 for (int j = 0; j < names.size(); j++) { rareNames.insert(names[j]); }
393 for (int j = 0; j < names.size(); j++) { abundNames.insert(names[j]); }
398 string tag = thisList->getLabel() + ".";
400 writeList(thisList, tag);
402 if (groupfile != "") { parseGroup(tag); }
403 if (accnos) { writeAccnos(tag); }
404 if (fastafile != "") { parseFasta(tag); }
409 catch(exception& e) {
410 m->errorOut(e, "SplitAbundCommand", "splitList");
414 /**********************************************************************************************************************/
415 int SplitAbundCommand::writeList(ListVector* thisList, string tag) {
418 map<string, ofstream*> filehandles;
420 if (Groups.size() == 0) {
421 SAbundVector* sabund = new SAbundVector();
422 *sabund = thisList->getSAbundVector();
424 //find out how many bins are rare and how many are abundant so you can process the list vector one bin at a time
425 // and don't have to store the bins until you are done with the whole vector, this save alot of space.
427 for (int i = 0; i <= sabund->getMaxRank(); i++) {
428 if (i > cutoff) { break; }
429 numRareBins += sabund->get(i);
431 int numAbundBins = thisList->getNumBins() - numRareBins;
437 string rare = outputDir + m->getRootName(m->getSimpleName(listfile)) + tag + "rare.list";
438 m->openOutputFile(rare, rout);
439 outputNames.push_back(rare); outputTypes["list"].push_back(rare);
441 string abund = outputDir + m->getRootName(m->getSimpleName(listfile)) + tag + "abund.list";
442 m->openOutputFile(abund, aout);
443 outputNames.push_back(abund); outputTypes["list"].push_back(abund);
445 if (rareNames.size() != 0) { rout << thisList->getLabel() << '\t' << numRareBins << '\t'; }
446 if (abundNames.size() != 0) { aout << thisList->getLabel() << '\t' << numAbundBins << '\t'; }
448 for (int i = 0; i < thisList->getNumBins(); i++) {
449 if (m->control_pressed) { break; }
451 string bin = list->get(i);
453 int size = m->getNumNames(bin);
455 if (size <= cutoff) { rout << bin << '\t'; }
456 else { aout << bin << '\t'; }
459 if (rareNames.size() != 0) { rout << endl; }
460 if (abundNames.size() != 0) { aout << endl; }
465 }else{ //parse names by abundance and group
466 string fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
469 //map<string, bool> wroteFile;
470 map<string, ofstream*> filehandles;
471 map<string, ofstream*>::iterator it3;
473 for (int i=0; i<Groups.size(); i++) {
475 filehandles[Groups[i]+".rare"] = temp;
476 temp2 = new ofstream;
477 filehandles[Groups[i]+".abund"] = temp2;
479 m->openOutputFile(fileroot + Groups[i] + tag + ".rare.list", *(filehandles[Groups[i]+".rare"]));
480 m->openOutputFile(fileroot + Groups[i] + tag + ".abund.list", *(filehandles[Groups[i]+".abund"]));
481 outputNames.push_back(fileroot + Groups[i] + tag + ".rare.list"); outputTypes["list"].push_back(fileroot + Groups[i] + tag + ".rare.list");
482 outputNames.push_back(fileroot + Groups[i] + tag + ".abund.list"); outputTypes["list"].push_back(fileroot + Groups[i] + tag + ".abund.list");
485 map<string, string> groupVector;
486 map<string, string>::iterator itGroup;
487 map<string, int> groupNumBins;
489 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
490 groupNumBins[it3->first] = 0;
491 groupVector[it3->first] = "";
494 for (int i = 0; i < thisList->getNumBins(); i++) {
495 if (m->control_pressed) { break; }
497 map<string, string> groupBins;
498 string bin = list->get(i);
500 vector<string> names;
501 m->splitAtComma(bin, names); //parses bin into individual sequence names
503 //parse bin into list of sequences in each group
504 for (int j = 0; j < names.size(); j++) {
506 if (rareNames.count(names[j]) != 0) { //you are a rare name
508 }else{ //you are a abund name
509 rareAbund = ".abund";
512 string group = groupMap->getGroup(names[j]);
514 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
515 itGroup = groupBins.find(group+rareAbund);
516 if(itGroup == groupBins.end()) {
517 groupBins[group+rareAbund] = names[j]; //add first name
518 groupNumBins[group+rareAbund]++;
519 }else{ //add another name
520 groupBins[group+rareAbund] += "," + names[j];
522 }else if(group == "not found") {
523 m->mothurOut(names[j] + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
528 for (itGroup = groupBins.begin(); itGroup != groupBins.end(); itGroup++) {
529 groupVector[itGroup->first] += itGroup->second + '\t';
534 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
535 (*(filehandles[it3->first])) << thisList->getLabel() << '\t' << groupNumBins[it3->first] << '\t' << groupVector[it3->first] << endl; // label numBins listvector for that group
536 (*(filehandles[it3->first])).close();
544 catch(exception& e) {
545 m->errorOut(e, "SplitAbundCommand", "writeList");
549 /**********************************************************************************************************************/
550 int SplitAbundCommand::splitNames() { //namefile
558 m->openInputFile(namefile, in);
561 if (m->control_pressed) { break; }
563 string firstCol, secondCol;
564 in >> firstCol >> secondCol; m->gobble(in);
566 nameMap[firstCol] = secondCol;
568 int size = m->getNumNames(secondCol);
570 if (size <= cutoff) {
571 rareNames.insert(firstCol);
573 abundNames.insert(firstCol);
581 catch(exception& e) {
582 m->errorOut(e, "SplitAbundCommand", "splitNames");
586 /**********************************************************************************************************************/
587 int SplitAbundCommand::readNamesFile() {
591 m->openInputFile(namefile, in);
594 if (m->control_pressed) { break; }
596 string firstCol, secondCol;
597 in >> firstCol >> secondCol; m->gobble(in);
599 nameMap[firstCol] = secondCol;
606 catch(exception& e) {
607 m->errorOut(e, "SplitAbundCommand", "readNamesFile");
611 /**********************************************************************************************************************/
612 int SplitAbundCommand::createNameMap(ListVector* thisList) {
615 if (thisList != NULL) {
616 for (int i = 0; i < thisList->getNumBins(); i++) {
617 if (m->control_pressed) { return 0; }
619 string bin = thisList->get(i);
621 vector<string> names;
622 m->splitAtComma(bin, names); //parses bin into individual sequence names
624 for (int j = 0; j < names.size(); j++) { nameMap[names[j]] = names[j]; }
630 catch(exception& e) {
631 m->errorOut(e, "SplitAbundCommand", "createNameMap");
635 /**********************************************************************************************************************/
636 int SplitAbundCommand::writeNames() { //namefile
639 map<string, ofstream*> filehandles;
641 if (Groups.size() == 0) {
645 string rare = outputDir + m->getRootName(m->getSimpleName(namefile)) + "rare.names";
646 m->openOutputFile(rare, rout);
647 outputNames.push_back(rare); outputTypes["name"].push_back(rare);
649 string abund = outputDir + m->getRootName(m->getSimpleName(namefile)) + "abund.names";
650 m->openOutputFile(abund, aout);
651 outputNames.push_back(abund); outputTypes["name"].push_back(abund);
653 if (rareNames.size() != 0) {
654 for (set<string>::iterator itRare = rareNames.begin(); itRare != rareNames.end(); itRare++) {
655 rout << (*itRare) << '\t' << nameMap[(*itRare)] << endl;
660 if (abundNames.size() != 0) {
661 for (set<string>::iterator itAbund = abundNames.begin(); itAbund != abundNames.end(); itAbund++) {
662 aout << (*itAbund) << '\t' << nameMap[(*itAbund)] << endl;
667 }else{ //parse names by abundance and group
668 string fileroot = outputDir + m->getRootName(m->getSimpleName(namefile));
671 map<string, ofstream*> filehandles;
672 map<string, ofstream*>::iterator it3;
674 for (int i=0; i<Groups.size(); i++) {
676 filehandles[Groups[i]+".rare"] = temp;
677 temp2 = new ofstream;
678 filehandles[Groups[i]+".abund"] = temp2;
680 m->openOutputFile(fileroot + Groups[i] + ".rare.names", *(filehandles[Groups[i]+".rare"]));
681 m->openOutputFile(fileroot + Groups[i] + ".abund.names", *(filehandles[Groups[i]+".abund"]));
684 for (map<string, string>::iterator itName = nameMap.begin(); itName != nameMap.end(); itName++) {
685 vector<string> names;
686 m->splitAtComma(itName->second, names); //parses bin into individual sequence names
689 if (rareNames.count(itName->first) != 0) { //you are a rare name
691 }else{ //you are a abund name
692 rareAbund = ".abund";
695 map<string, string> outputStrings;
696 map<string, string>::iterator itout;
697 for (int i = 0; i < names.size(); i++) {
699 string group = groupMap->getGroup(names[i]);
701 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
702 itout = outputStrings.find(group+rareAbund);
703 if (itout == outputStrings.end()) {
704 outputStrings[group+rareAbund] = names[i] + '\t' + names[i];
705 }else { outputStrings[group+rareAbund] += "," + names[i]; }
706 }else if(group == "not found") {
707 m->mothurOut(names[i] + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
711 for (itout = outputStrings.begin(); itout != outputStrings.end(); itout++) { *(filehandles[itout->first]) << itout->second << endl; }
715 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
716 (*(filehandles[it3->first])).close();
717 outputNames.push_back(fileroot + it3->first + ".names"); outputTypes["name"].push_back(fileroot + it3->first + ".names");
725 catch(exception& e) {
726 m->errorOut(e, "SplitAbundCommand", "writeNames");
730 /**********************************************************************************************************************/
731 //just write the unique names - if a namesfile is given
732 int SplitAbundCommand::writeAccnos(string tag) {
735 map<string, ofstream*> filehandles;
737 if (Groups.size() == 0) {
742 string rare = outputDir + m->getRootName(m->getSimpleName(inputFile)) + tag + "rare.accnos";
743 m->openOutputFile(rare, rout);
744 outputNames.push_back(rare); outputTypes["accnos"].push_back(rare);
746 for (set<string>::iterator itRare = rareNames.begin(); itRare != rareNames.end(); itRare++) {
747 rout << (*itRare) << endl;
751 string abund = outputDir + m->getRootName(m->getSimpleName(inputFile)) + tag + "abund.accnos";
752 m->openOutputFile(abund, aout);
753 outputNames.push_back(abund); outputTypes["accnos"].push_back(abund);
755 for (set<string>::iterator itAbund = abundNames.begin(); itAbund != abundNames.end(); itAbund++) {
756 aout << (*itAbund) << endl;
760 }else{ //parse names by abundance and group
761 string fileroot = outputDir + m->getRootName(m->getSimpleName(inputFile));
764 map<string, ofstream*> filehandles;
765 map<string, ofstream*>::iterator it3;
767 for (int i=0; i<Groups.size(); i++) {
769 filehandles[Groups[i]+".rare"] = temp;
770 temp2 = new ofstream;
771 filehandles[Groups[i]+".abund"] = temp2;
773 m->openOutputFile(fileroot + tag + Groups[i] + ".rare.accnos", *(filehandles[Groups[i]+".rare"]));
774 m->openOutputFile(fileroot + tag + Groups[i] + ".abund.accnos", *(filehandles[Groups[i]+".abund"]));
778 for (set<string>::iterator itRare = rareNames.begin(); itRare != rareNames.end(); itRare++) {
779 string group = groupMap->getGroup(*itRare);
781 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
782 *(filehandles[group+".rare"]) << *itRare << endl;
787 for (set<string>::iterator itAbund = abundNames.begin(); itAbund != abundNames.end(); itAbund++) {
788 string group = groupMap->getGroup(*itAbund);
790 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
791 *(filehandles[group+".abund"]) << *itAbund << endl;
796 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
797 (*(filehandles[it3->first])).close();
798 outputNames.push_back(fileroot + tag + it3->first + ".accnos"); outputTypes["accnos"].push_back(fileroot + tag + it3->first + ".accnos");
806 catch(exception& e) {
807 m->errorOut(e, "SplitAbundCommand", "writeAccnos");
811 /**********************************************************************************************************************/
812 int SplitAbundCommand::parseGroup(string tag) { //namefile
815 map<string, ofstream*> filehandles;
817 if (Groups.size() == 0) {
821 string rare = outputDir + m->getRootName(m->getSimpleName(groupfile)) + tag + "rare.groups";
822 m->openOutputFile(rare, rout);
823 outputNames.push_back(rare); outputTypes["group"].push_back(rare);
825 string abund = outputDir + m->getRootName(m->getSimpleName(groupfile)) + tag + "abund.groups";
826 m->openOutputFile(abund, aout);
827 outputNames.push_back(abund); outputTypes["group"].push_back(abund);
829 for (map<string, string>::iterator itName = nameMap.begin(); itName != nameMap.end(); itName++) {
830 vector<string> names;
831 m->splitAtComma(itName->second, names); //parses bin into individual sequence names
833 for (int i = 0; i < names.size(); i++) {
835 string group = groupMap->getGroup(names[i]);
837 if (group == "not found") {
838 m->mothurOut(names[i] + " is not in your groupfile, ignoring, please correct."); m->mothurOutEndLine();
840 if (rareNames.count(itName->first) != 0) { //you are a rare name
841 rout << names[i] << '\t' << group << endl;
842 }else{ //you are a abund name
843 aout << names[i] << '\t' << group << endl;
852 }else{ //parse names by abundance and group
853 string fileroot = outputDir + m->getRootName(m->getSimpleName(groupfile));
856 map<string, ofstream*> filehandles;
857 map<string, ofstream*>::iterator it3;
859 for (int i=0; i<Groups.size(); i++) {
861 filehandles[Groups[i]+".rare"] = temp;
862 temp2 = new ofstream;
863 filehandles[Groups[i]+".abund"] = temp2;
865 m->openOutputFile(fileroot + tag + Groups[i] + ".rare.groups", *(filehandles[Groups[i]+".rare"]));
866 m->openOutputFile(fileroot + tag + Groups[i] + ".abund.groups", *(filehandles[Groups[i]+".abund"]));
869 for (map<string, string>::iterator itName = nameMap.begin(); itName != nameMap.end(); itName++) {
870 vector<string> names;
871 m->splitAtComma(itName->second, names); //parses bin into individual sequence names
874 if (rareNames.count(itName->first) != 0) { //you are a rare name
876 }else{ //you are a abund name
877 rareAbund = ".abund";
880 for (int i = 0; i < names.size(); i++) {
882 string group = groupMap->getGroup(names[i]);
884 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
885 *(filehandles[group+rareAbund]) << names[i] << '\t' << group << endl;
890 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
891 (*(filehandles[it3->first])).close();
892 outputNames.push_back(fileroot + tag + it3->first + ".groups"); outputTypes["group"].push_back(fileroot + tag + it3->first + ".groups");
900 catch(exception& e) {
901 m->errorOut(e, "SplitAbundCommand", "parseGroups");
905 /**********************************************************************************************************************/
906 int SplitAbundCommand::parseFasta(string tag) { //namefile
909 map<string, ofstream*> filehandles;
911 if (Groups.size() == 0) {
915 string rare = outputDir + m->getRootName(m->getSimpleName(fastafile)) + tag + "rare.fasta";
916 m->openOutputFile(rare, rout);
917 outputNames.push_back(rare); outputTypes["fasta"].push_back(rare);
919 string abund = outputDir + m->getRootName(m->getSimpleName(fastafile)) + tag + "abund.fasta";
920 m->openOutputFile(abund, aout);
921 outputNames.push_back(abund); outputTypes["fasta"].push_back(abund);
925 m->openInputFile(fastafile, in);
928 if (m->control_pressed) { break; }
930 Sequence seq(in); m->gobble(in);
932 if (seq.getName() != "") {
934 map<string, string>::iterator itNames;
936 itNames = nameMap.find(seq.getName());
938 if (itNames == nameMap.end()) {
939 m->mothurOut(seq.getName() + " is not in your namesfile, ignoring."); m->mothurOutEndLine();
941 if (rareNames.count(seq.getName()) != 0) { //you are a rare name
942 seq.printSequence(rout);
943 }else{ //you are a abund name
944 seq.printSequence(aout);
953 }else{ //parse names by abundance and group
954 string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
957 map<string, ofstream*> filehandles;
958 map<string, ofstream*>::iterator it3;
960 for (int i=0; i<Groups.size(); i++) {
962 filehandles[Groups[i]+".rare"] = temp;
963 temp2 = new ofstream;
964 filehandles[Groups[i]+".abund"] = temp2;
966 m->openOutputFile(fileroot + tag + Groups[i] + ".rare.fasta", *(filehandles[Groups[i]+".rare"]));
967 m->openOutputFile(fileroot + tag + Groups[i] + ".abund.fasta", *(filehandles[Groups[i]+".abund"]));
972 m->openInputFile(fastafile, in);
975 if (m->control_pressed) { break; }
977 Sequence seq(in); m->gobble(in);
979 if (seq.getName() != "") {
980 map<string, string>::iterator itNames = nameMap.find(seq.getName());
982 if (itNames == nameMap.end()) {
983 m->mothurOut(seq.getName() + " is not in your namesfile, ignoring."); m->mothurOutEndLine();
985 vector<string> names;
986 m->splitAtComma(itNames->second, names); //parses bin into individual sequence names
989 if (rareNames.count(itNames->first) != 0) { //you are a rare name
991 }else{ //you are a abund name
992 rareAbund = ".abund";
995 for (int i = 0; i < names.size(); i++) {
997 string group = groupMap->getGroup(seq.getName());
999 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
1000 seq.printSequence(*(filehandles[group+rareAbund]));
1001 }else if(group == "not found") {
1002 m->mothurOut(seq.getName() + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
1010 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
1011 (*(filehandles[it3->first])).close();
1012 outputNames.push_back(fileroot + tag + it3->first + ".fasta"); outputTypes["fasta"].push_back(fileroot + tag + it3->first + ".fasta");
1020 catch(exception& e) {
1021 m->errorOut(e, "SplitAbundCommand", "parseFasta");
1025 /**********************************************************************************************************************/