5 * Created by Sarah Westcott on 4/6/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "getoturepcommand.h"
12 //**********************************************************************************************************************
13 GetOTURepCommand::GetOTURepCommand(){
15 globaldata = GlobalData::getInstance();
17 if(globaldata->gSparseMatrix != NULL) { matrix = new SparseMatrix(*globaldata->gSparseMatrix); }
19 //listOfNames bin 0 = first name read in distance matrix, listOfNames bin 1 = second name read in distance matrix
20 if(globaldata->gListVector != NULL) {
21 listOfNames = new ListVector(*globaldata->gListVector);
25 //map names to rows in sparsematrix
26 for (int i = 0; i < listOfNames->size(); i++) {
28 binnames = listOfNames->get(i);
29 splitAtComma(binnames, names);
31 for (int j = 0; j < names.size(); j++) {
32 nameToIndex[names[j]] = i;
35 }else { cout << "error, no listvector." << endl; }
38 fastafile = globaldata->getFastaFile();
39 namesfile = globaldata->getNameFile();
40 openInputFile(fastafile, in);
42 fasta = new FastaMap();
46 cout << "Standard Error: " << e.what() << " has occurred in the GetOTURepCommand class Function GetOTURepCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
50 cout << "An unknown error has occurred in the GetOTURepCommand class function GetOTURepCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
54 //**********************************************************************************************************************
56 GetOTURepCommand::~GetOTURepCommand(){
64 //**********************************************************************************************************************
66 int GetOTURepCommand::execute(){
69 string nameRep, name, sequence;
72 fasta->readFastaFile(in);
74 //set format to list so input can get listvector
75 globaldata->setFormat("list");
77 //if user gave a namesfile then use it
78 if (namesfile != "") {
83 read = new ReadOTUFile(globaldata->getListFile());
84 read->read(&*globaldata);
86 input = globaldata->ginput;
87 list = globaldata->gListVector;
91 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(list->getLabel()) == 1){
94 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".rep.fasta";
95 openOutputFile(outputFileName, out);
97 cout << list->getLabel() << '\t' << count << endl;
99 //for each bin in the list vector
100 for (int i = 0; i < list->size(); i++) {
101 nameRep = FindRep(i);
103 //print out name and sequence for that bin
104 sequence = fasta->getSequence(nameRep);
106 if (sequence != "not found") {
107 nameRep = nameRep + "|" + toString(i+1);
108 out << ">" << nameRep << endl;
109 out << sequence << endl;
111 cout << nameRep << " is missing from your fasta or name file. Please correct. " << endl;
112 remove(outputFileName.c_str());
120 list = input->getListVector();
127 catch(exception& e) {
128 cout << "Standard Error: " << e.what() << " has occurred in the GetOTURepCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
132 cout << "An unknown error has occurred in the GetOTURepCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
138 //**********************************************************************************************************************
139 void GetOTURepCommand::readNamesFile() {
141 vector<string> dupNames;
142 openInputFile(namesfile, inNames);
144 string name, names, sequence;
147 inNames >> name; //read from first column A
148 inNames >> names; //read from second column A,B,C,D
152 //parse names into vector
153 splitAtComma(names, dupNames);
155 //store names in fasta map
156 sequence = fasta->getSequence(name);
157 for (int i = 0; i < dupNames.size(); i++) {
158 fasta->push_back(dupNames[i], sequence);
166 catch(exception& e) {
167 cout << "Standard Error: " << e.what() << " has occurred in the GetOTURepCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
171 cout << "An unknown error has occurred in the GetOTURepCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
175 //**********************************************************************************************************************
176 string GetOTURepCommand::FindRep(int bin) {
178 vector<string> names;
179 map<string, float> sums;
180 map<string, float>::iterator it4;
181 map<int, string> binMap; //subset of namesToIndex - just member of this bin
186 binnames = list->get(bin);
188 //parse names into vector
189 splitAtComma(binnames, names);
191 //if only 1 sequence in bin then that's the rep
192 if (names.size() == 1) { return names[0]; }
195 for (int i = 0; i < names.size(); i++) {
196 for (it3 = nameToIndex.begin(); it3 != nameToIndex.end(); it3++) {
198 if (it3->first == names[i]) {
199 binMap[it3->second] = it3->first;
201 //initialize sums map
202 sums[it3->first] = 0.0;
208 //go through each cell in the sparsematrix
209 for(MatData currentCell = matrix->begin(); currentCell != matrix->end(); currentCell++){
210 //is this a distance between 2 members of this bin
211 it = binMap.find(currentCell->row);
212 it2 = binMap.find(currentCell->column);
214 //sum the distance of the sequences in the bin to eachother
215 if ((it != binMap.end()) && (it2 != binMap.end())) {
216 //this is a cell that repesents the distance between to of this bins members
217 sums[it->second] += currentCell->dist;
218 sums[it2->second] += currentCell->dist;
222 //smallest sum is the representative
223 for (it4 = sums.begin(); it4 != sums.end(); it4++) {
224 if (it4->second < min) {
226 minName = it4->first;
235 catch(exception& e) {
236 cout << "Standard Error: " << e.what() << " has occurred in the GetOTURepCommand class Function FindRep. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
240 cout << "An unknown error has occurred in the GetOTURepCommand class function FindRep. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";