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(){
56 string binnames, name, sequence;
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;
78 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(list->getLabel()) == 1){
80 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
81 openOutputFile(outputFileName, out);
83 cout << list->getLabel() << '\t' << count << endl;
85 //for each bin in the list vector
86 for (int i = 0; i < list->size(); i++) {
88 binnames = list->get(i);
89 while (binnames.find_first_of(',') != -1) {
90 name = binnames.substr(0,binnames.find_first_of(','));
91 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
93 //do work for that name
94 sequence = fasta->getSequence(name);
95 if (sequence != "not found") {
96 //if you don't have groups
97 if (groupfile == "") {
98 name = name + "|" + toString(i+1);
99 out << ">" << name << endl;
100 out << sequence << endl;
101 }else {//if you do have groups
102 string group = groupMap->getGroup(name);
103 if (group == "not found") {
104 cout << name << " is missing from your group file. Please correct. " << endl;
105 remove(outputFileName.c_str());
108 name = name + "|" + group + "|" + toString(i+1);
109 out << ">" << name << endl;
110 out << sequence << endl;
114 cout << name << " is missing from your fasta or name file. Please correct. " << endl;
115 remove(outputFileName.c_str());
122 sequence = fasta->getSequence(binnames);
123 if (sequence != "not found") {
124 //if you don't have groups
125 if (groupfile == "") {
126 binnames = binnames + "|" + toString(i+1);
127 out << ">" << binnames << endl;
128 out << sequence << endl;
129 }else {//if you do have groups
130 string group = groupMap->getGroup(binnames);
131 if (group == "not found") {
132 cout << binnames << " is missing from your group file. Please correct. " << endl;
133 remove(outputFileName.c_str());
136 binnames = binnames + "|" + group + "|" + toString(i+1);
137 out << ">" << binnames << endl;
138 out << sequence << endl;
142 cout << binnames << " is missing from your fasta or name file. Please correct. " << endl;
143 remove(outputFileName.c_str());
152 list = input->getListVector();
158 catch(exception& e) {
159 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
163 cout << "An unknown error has occurred in the BinSeqCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
168 //**********************************************************************************************************************
169 void BinSeqCommand::readNamesFile() {
171 vector<string> dupNames;
172 openInputFile(namesfile, inNames);
174 string name, names, sequence;
177 inNames >> name; //read from first column A
178 inNames >> names; //read from second column A,B,C,D
182 //parse names into vector
183 splitAtComma(names, dupNames);
185 //store names in fasta map
186 sequence = fasta->getSequence(name);
187 for (int i = 0; i < dupNames.size(); i++) {
188 fasta->push_back(dupNames[i], sequence);
196 catch(exception& e) {
197 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
201 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
205 //**********************************************************************************************************************