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 openInputFile(fastafile, in);
20 fasta = new FastaMap();
23 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
27 cout << "An unknown error has occurred in the BinSeqCommand class function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
32 //**********************************************************************************************************************
34 BinSeqCommand::~BinSeqCommand(){
41 //**********************************************************************************************************************
43 int BinSeqCommand::execute(){
46 string binnames, name, sequence;
49 fasta->readFastaFile(in);
51 //set format to list so input can get listvector
52 globaldata->setFormat("list");
54 //if user gave a namesfile then use it
55 if (namesfile != "") {
60 read = new ReadPhilFile(globaldata->getListFile());
61 read->read(&*globaldata);
63 input = globaldata->ginput;
64 list = globaldata->gListVector;
68 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(list->getLabel()) == 1){
71 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
72 openOutputFile(outputFileName, out);
74 cout << list->getLabel() << '\t' << count << endl;
76 //for each bin in the list vector
77 for (int i = 0; i < list->size(); i++) {
78 binnames = list->get(i);
79 while (binnames.find_first_of(',') != -1) {
80 name = binnames.substr(0,binnames.find_first_of(','));
81 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
83 //do work for that name
84 sequence = fasta->getSequence(name);
85 if (sequence != "not found") {
86 name = name + "bin" + toString(i+1);
87 out << ">" << name << endl;
88 out << sequence << endl;
90 cout << name << " is missing from your fasta or name file. Please correct. " << endl;
91 remove(outputFileName.c_str());
98 sequence = fasta->getSequence(binnames);
99 if (sequence != "not found") {
100 name = binnames + "bin" + toString(i+1);
101 out << ">" << name << endl;
102 out << sequence << endl;
104 cout << binnames << " is missing from your fasta or name file. Please correct. " << endl;
105 remove(outputFileName.c_str());
111 list = input->getListVector();
117 catch(exception& e) {
118 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
122 cout << "An unknown error has occurred in the BinSeqCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
127 //**********************************************************************************************************************
128 void BinSeqCommand::readNamesFile() {
130 vector<string> dupNames;
131 openInputFile(namesfile, inNames);
133 string name, names, sequence;
136 inNames >> name; //read from first column A
137 inNames >> names; //read from second column A,B,C,D
141 //parse names into vector
142 splitAtComma(names, dupNames);
144 //store names in fasta map
145 sequence = fasta->getSequence(name);
146 for (int i = 0; i < dupNames.size(); i++) {
147 fasta->push_back(dupNames[i], sequence);
155 catch(exception& e) {
156 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
160 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
164 //**********************************************************************************************************************