2 * binsequencecommand.cpp
5 * Created by Sarah Westcott on 4/3/09.
6 * Copyright 2009 Schloss Lab UMASS Amhers. All rights reserved.
10 #include "binsequencecommand.h"
12 //**********************************************************************************************************************
13 BinSeqCommand::BinSeqCommand(){
15 globaldata = GlobalData::getInstance();
16 fastafile = globaldata->getFastaFile();
17 namesfile = globaldata->getNameFile();
18 groupfile = globaldata->getGroupFile();
19 openInputFile(fastafile, in);
21 if (groupfile != "") {
22 //read in group map info.
23 groupMap = new GroupMap(groupfile);
27 fasta = new FastaMap();
30 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
34 cout << "An unknown error has occurred in the BinSeqCommand class function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
39 //**********************************************************************************************************************
41 BinSeqCommand::~BinSeqCommand(){
46 if (groupfile != "") {
51 //**********************************************************************************************************************
53 int BinSeqCommand::execute(){
59 fasta->readFastaFile(in);
61 //set format to list so input can get listvector
62 globaldata->setFormat("list");
64 //if user gave a namesfile then use it
65 if (namesfile != "") {
70 read = new ReadOTUFile(globaldata->getListFile());
71 read->read(&*globaldata);
73 input = globaldata->ginput;
74 list = globaldata->gListVector;
75 ListVector* lastList = list;
77 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
78 set<string> processedLabels;
79 set<string> userLabels = globaldata->labels;
82 while((list != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
84 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(list->getLabel()) == 1){
86 error = process(list, count);
87 if (error == 1) { return 0; }
89 processedLabels.insert(list->getLabel());
90 userLabels.erase(list->getLabel());
94 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
96 error = process(lastList, count);
97 if (error == 1) { return 0; }
99 processedLabels.insert(lastList->getLabel());
100 userLabels.erase(lastList->getLabel());
104 if (count != 1) { delete lastList; }
107 list = input->getListVector();
112 //output error messages about any remaining user labels
113 set<string>::iterator it;
114 bool needToRun = false;
115 for (it = userLabels.begin(); it != userLabels.end(); it++) {
116 cout << "Your file does not include the label "<< *it;
117 if (processedLabels.count(lastList->getLabel()) != 1) {
118 cout << ". I will use " << lastList->getLabel() << "." << endl;
121 cout << ". Please refer to " << lastList->getLabel() << "." << endl;
125 //run last line if you need to
126 if (needToRun == true) {
127 error = process(lastList, count);
128 if (error == 1) { return 0; }
134 catch(exception& e) {
135 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
139 cout << "An unknown error has occurred in the BinSeqCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
144 //**********************************************************************************************************************
145 void BinSeqCommand::readNamesFile() {
147 vector<string> dupNames;
148 openInputFile(namesfile, inNames);
150 string name, names, sequence;
153 inNames >> name; //read from first column A
154 inNames >> names; //read from second column A,B,C,D
158 //parse names into vector
159 splitAtComma(names, dupNames);
161 //store names in fasta map
162 sequence = fasta->getSequence(name);
163 for (int i = 0; i < dupNames.size(); i++) {
164 fasta->push_back(dupNames[i], sequence);
172 catch(exception& e) {
173 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
181 //**********************************************************************************************************************
182 //return 1 if error, 0 otherwise
183 int BinSeqCommand::process(ListVector* list, int count) {
185 string binnames, name, sequence;
186 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
187 openOutputFile(outputFileName, out);
189 cout << list->getLabel() << '\t' << count << endl;
191 //for each bin in the list vector
192 for (int i = 0; i < list->size(); i++) {
194 binnames = list->get(i);
195 while (binnames.find_first_of(',') != -1) {
196 name = binnames.substr(0,binnames.find_first_of(','));
197 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
199 //do work for that name
200 sequence = fasta->getSequence(name);
201 if (sequence != "not found") {
202 //if you don't have groups
203 if (groupfile == "") {
204 name = name + "|" + toString(i+1);
205 out << ">" << name << endl;
206 out << sequence << endl;
207 }else {//if you do have groups
208 string group = groupMap->getGroup(name);
209 if (group == "not found") {
210 cout << name << " is missing from your group file. Please correct. " << endl;
211 remove(outputFileName.c_str());
214 name = name + "|" + group + "|" + toString(i+1);
215 out << ">" << name << endl;
216 out << sequence << endl;
220 cout << name << " is missing from your fasta or name file. Please correct. " << endl;
221 remove(outputFileName.c_str());
228 sequence = fasta->getSequence(binnames);
229 if (sequence != "not found") {
230 //if you don't have groups
231 if (groupfile == "") {
232 binnames = binnames + "|" + toString(i+1);
233 out << ">" << binnames << endl;
234 out << sequence << endl;
235 }else {//if you do have groups
236 string group = groupMap->getGroup(binnames);
237 if (group == "not found") {
238 cout << binnames << " is missing from your group file. Please correct. " << endl;
239 remove(outputFileName.c_str());
242 binnames = binnames + "|" + group + "|" + toString(i+1);
243 out << ">" << binnames << endl;
244 out << sequence << endl;
248 cout << binnames << " is missing from your fasta or name file. Please correct. " << endl;
249 remove(outputFileName.c_str());
258 catch(exception& e) {
259 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
263 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
267 //**********************************************************************************************************************