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;
80 set<int> userLines = globaldata->lines;
83 while((list != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
85 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(list->getLabel()) == 1){
87 error = process(list, count);
88 if (error == 1) { return 0; }
90 processedLabels.insert(list->getLabel());
91 userLabels.erase(list->getLabel());
92 userLines.erase(count);
95 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
97 error = process(lastList, count);
98 if (error == 1) { return 0; }
100 processedLabels.insert(lastList->getLabel());
101 userLabels.erase(lastList->getLabel());
105 if (count != 1) { delete lastList; }
108 list = input->getListVector();
113 //output error messages about any remaining user labels
114 set<string>::iterator it;
115 bool needToRun = false;
116 for (it = userLabels.begin(); it != userLabels.end(); it++) {
117 cout << "Your file does not include the label "<< *it;
118 if (processedLabels.count(lastList->getLabel()) != 1) {
119 cout << ". I will use " << lastList->getLabel() << "." << endl;
122 cout << ". Please refer to " << lastList->getLabel() << "." << endl;
126 //run last line if you need to
127 if (needToRun == true) {
128 error = process(lastList, count);
129 if (error == 1) { return 0; }
135 catch(exception& e) {
136 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
140 cout << "An unknown error has occurred in the BinSeqCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
145 //**********************************************************************************************************************
146 void BinSeqCommand::readNamesFile() {
148 vector<string> dupNames;
149 openInputFile(namesfile, inNames);
151 string name, names, sequence;
154 inNames >> name; //read from first column A
155 inNames >> names; //read from second column A,B,C,D
159 //parse names into vector
160 splitAtComma(names, dupNames);
162 //store names in fasta map
163 sequence = fasta->getSequence(name);
164 for (int i = 0; i < dupNames.size(); i++) {
165 fasta->push_back(dupNames[i], sequence);
173 catch(exception& e) {
174 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
178 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182 //**********************************************************************************************************************
183 //return 1 if error, 0 otherwise
184 int BinSeqCommand::process(ListVector* list, int count) {
186 string binnames, name, sequence;
187 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
188 openOutputFile(outputFileName, out);
190 cout << list->getLabel() << '\t' << count << endl;
192 //for each bin in the list vector
193 for (int i = 0; i < list->size(); i++) {
195 binnames = list->get(i);
196 while (binnames.find_first_of(',') != -1) {
197 name = binnames.substr(0,binnames.find_first_of(','));
198 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
200 //do work for that name
201 sequence = fasta->getSequence(name);
202 if (sequence != "not found") {
203 //if you don't have groups
204 if (groupfile == "") {
205 name = name + "|" + toString(i+1);
206 out << ">" << name << endl;
207 out << sequence << endl;
208 }else {//if you do have groups
209 string group = groupMap->getGroup(name);
210 if (group == "not found") {
211 cout << name << " is missing from your group file. Please correct. " << endl;
212 remove(outputFileName.c_str());
215 name = name + "|" + group + "|" + toString(i+1);
216 out << ">" << name << endl;
217 out << sequence << endl;
221 cout << name << " is missing from your fasta or name file. Please correct. " << endl;
222 remove(outputFileName.c_str());
229 sequence = fasta->getSequence(binnames);
230 if (sequence != "not found") {
231 //if you don't have groups
232 if (groupfile == "") {
233 binnames = binnames + "|" + toString(i+1);
234 out << ">" << binnames << endl;
235 out << sequence << endl;
236 }else {//if you do have groups
237 string group = groupMap->getGroup(binnames);
238 if (group == "not found") {
239 cout << binnames << " is missing from your group file. Please correct. " << endl;
240 remove(outputFileName.c_str());
243 binnames = binnames + "|" + group + "|" + toString(i+1);
244 out << ">" << binnames << endl;
245 out << sequence << endl;
249 cout << binnames << " is missing from your fasta or name file. Please correct. " << endl;
250 remove(outputFileName.c_str());
259 catch(exception& e) {
260 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
264 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
268 //**********************************************************************************************************************