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 groupfile = globaldata->getGroupFile();
42 if (groupfile != "") {
43 //read in group map info.
44 groupMap = new GroupMap(groupfile);
48 openInputFile(fastafile, in);
50 fasta = new FastaMap();
54 cout << "Standard Error: " << e.what() << " has occurred in the GetOTURepCommand class Function GetOTURepCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
58 cout << "An unknown error has occurred in the GetOTURepCommand class function GetOTURepCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
62 //**********************************************************************************************************************
64 GetOTURepCommand::~GetOTURepCommand(){
70 if (groupfile != "") {
75 //**********************************************************************************************************************
77 int GetOTURepCommand::execute(){
80 string nameRep, name, sequence;
83 fasta->readFastaFile(in);
85 //set format to list so input can get listvector
86 globaldata->setFormat("list");
88 //if user gave a namesfile then use it
89 if (namesfile != "") {
94 read = new ReadOTUFile(globaldata->getListFile());
95 read->read(&*globaldata);
97 input = globaldata->ginput;
98 list = globaldata->gListVector;
102 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(list->getLabel()) == 1){
105 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".rep.fasta";
106 openOutputFile(outputFileName, out);
108 cout << list->getLabel() << '\t' << count << endl;
110 //for each bin in the list vector
111 for (int i = 0; i < list->size(); i++) {
113 nameRep = FindRep(i, groups);
115 //print out name and sequence for that bin
116 sequence = fasta->getSequence(nameRep);
118 if (sequence != "not found") {
119 if (groupfile == "") {
120 nameRep = nameRep + "|" + toString(i+1);
121 out << ">" << nameRep << endl;
122 out << sequence << endl;
124 nameRep = nameRep + "|" + groups + "|" + toString(i+1);
125 out << ">" << nameRep << endl;
126 out << sequence << endl;
129 cout << nameRep << " is missing from your fasta or name file. Please correct. " << endl;
130 remove(outputFileName.c_str());
138 list = input->getListVector();
145 catch(exception& e) {
146 cout << "Standard Error: " << e.what() << " has occurred in the GetOTURepCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
150 cout << "An unknown error has occurred in the GetOTURepCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
156 //**********************************************************************************************************************
157 void GetOTURepCommand::readNamesFile() {
159 vector<string> dupNames;
160 openInputFile(namesfile, inNames);
162 string name, names, sequence;
165 inNames >> name; //read from first column A
166 inNames >> names; //read from second column A,B,C,D
170 //parse names into vector
171 splitAtComma(names, dupNames);
173 //store names in fasta map
174 sequence = fasta->getSequence(name);
175 for (int i = 0; i < dupNames.size(); i++) {
176 fasta->push_back(dupNames[i], sequence);
184 catch(exception& e) {
185 cout << "Standard Error: " << e.what() << " has occurred in the GetOTURepCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
189 cout << "An unknown error has occurred in the GetOTURepCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
193 //**********************************************************************************************************************
194 string GetOTURepCommand::FindRep(int bin, string& group) {
196 vector<string> names;
197 map<string, float> sums;
198 map<string, float>::iterator it4;
199 map<int, string> binMap; //subset of namesToIndex - just member of this bin
203 map<string, string> groups;
204 map<string, string>::iterator groupIt;
206 binnames = list->get(bin);
208 //parse names into vector
209 splitAtComma(binnames, names);
211 //if you have a groupfile
212 if(groupfile != "") {
213 //find the groups that are in this bin
214 for (int i = 0; i < names.size(); i++) {
215 string groupName = groupMap->getGroup(names[i]);
216 if (groupName == "not found") {
217 cout << names[i] << " is missing from your group file. Please correct. " << endl;
220 groups[groupName] = groupName;
224 //turn the groups into a string
225 for(groupIt = groups.begin(); groupIt != groups.end(); groupIt++) { group += groupIt->first + "-"; }
228 group = group.substr(0, group.length()-1);
231 //if only 1 sequence in bin then that's the rep
232 if (names.size() == 1) { return names[0]; }
235 for (int i = 0; i < names.size(); i++) {
236 for (it3 = nameToIndex.begin(); it3 != nameToIndex.end(); it3++) {
238 if (it3->first == names[i]) {
239 binMap[it3->second] = it3->first;
241 //initialize sums map
242 sums[it3->first] = 0.0;
248 //go through each cell in the sparsematrix
249 for(MatData currentCell = matrix->begin(); currentCell != matrix->end(); currentCell++){
250 //is this a distance between 2 members of this bin
251 it = binMap.find(currentCell->row);
252 it2 = binMap.find(currentCell->column);
254 //sum the distance of the sequences in the bin to eachother
255 if ((it != binMap.end()) && (it2 != binMap.end())) {
256 //this is a cell that repesents the distance between to of this bins members
257 sums[it->second] += currentCell->dist;
258 sums[it2->second] += currentCell->dist;
262 //smallest sum is the representative
263 for (it4 = sums.begin(); it4 != sums.end(); it4++) {
264 if (it4->second < min) {
266 minName = it4->first;
275 catch(exception& e) {
276 cout << "Standard Error: " << e.what() << " has occurred in the GetOTURepCommand class Function FindRep. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
280 cout << "An unknown error has occurred in the GetOTURepCommand class function FindRep. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";