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){
70 //make new folder for bin info
71 //string foldername = "/" + getRootName(globaldata->getListFile()) + list->getLabel() + ".bins/";
72 // mkdir(foldername.c_str());
74 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
75 openOutputFile(outputFileName, out);
77 cout << list->getLabel() << '\t' << count << endl;
79 //for each bin in the list vector
80 for (int i = 0; i < list->size(); i++) {
83 //string outputFileName = foldername + getRootName(globaldata->getListFile()) + "bin" + toString(i+1) + ".fasta";
84 //openOutputFile(outputFileName, out);
86 binnames = list->get(i);
87 while (binnames.find_first_of(',') != -1) {
88 name = binnames.substr(0,binnames.find_first_of(','));
89 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
91 //do work for that name
92 sequence = fasta->getSequence(name);
93 if (sequence != "not found") {
94 name = name + "|" + toString(i+1);
95 out << ">" << name << endl;
96 out << sequence << endl;
98 cout << name << " is missing from your fasta or name file. Please correct. " << endl;
99 remove(outputFileName.c_str());
106 sequence = fasta->getSequence(binnames);
107 if (sequence != "not found") {
108 name = binnames + '|' + toString(i+1);
109 out << ">" << name << endl;
110 out << sequence << endl;
112 cout << binnames << " is missing from your fasta or name file. Please correct. " << endl;
113 remove(outputFileName.c_str());
121 list = input->getListVector();
127 catch(exception& e) {
128 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
132 cout << "An unknown error has occurred in the BinSeqCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
137 //**********************************************************************************************************************
138 void BinSeqCommand::readNamesFile() {
140 vector<string> dupNames;
141 openInputFile(namesfile, inNames);
143 string name, names, sequence;
146 inNames >> name; //read from first column A
147 inNames >> names; //read from second column A,B,C,D
151 //parse names into vector
152 splitAtComma(names, dupNames);
154 //store names in fasta map
155 sequence = fasta->getSequence(name);
156 for (int i = 0; i < dupNames.size(); i++) {
157 fasta->push_back(dupNames[i], sequence);
165 catch(exception& e) {
166 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
170 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174 //**********************************************************************************************************************