5 * Created by westcott on 10/28/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "readcluster.h"
12 /***********************************************************************/
14 ReadCluster::ReadCluster(string distfile, float c, string o, bool s){
15 m = MothurOut::getInstance();
23 /***********************************************************************/
25 int ReadCluster::read(NameAssignment*& nameMap){
28 if (format == "phylip") { convertPhylip2Column(nameMap); }
29 else { list = new ListVector(nameMap->getListVector()); }
31 if (m->control_pressed) { return 0; }
33 if (sortWanted) { OutPutFile = m->sortFile(distFile, outputDir); }
34 else { OutPutFile = distFile; } //for use by clusters splitMatrix to convert a phylip matrix to column
40 m->errorOut(e, "ReadCluster", "read");
44 /***********************************************************************/
46 int ReadCluster::convertPhylip2Column(NameAssignment*& nameMap){
48 //convert phylip file to column file
49 map<int, string> rowToName;
50 map<int, string>::iterator it;
54 string tempFile = distFile + ".column.temp";
56 m->openInputFile(distFile, in); m->gobble(in);
57 m->openOutputFile(tempFile, out);
62 vector<string> matrixNames;
65 in >> numTest >> name;
67 if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
68 else { convert(numTest, nseqs); }
71 matrixNames.push_back(name);
74 list = new ListVector(nseqs);
78 list = new ListVector(nameMap->getListVector());
79 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
83 while((d=in.get()) != EOF){
88 for(int i=0;i<nseqs;i++){
101 for(int i=1;i<nseqs;i++){
104 matrixNames.push_back(name);
106 //there's A LOT of repeated code throughout this method...
110 for(int j=0;j<i;j++){
112 if (m->control_pressed) { in.close(); out.close(); remove(tempFile.c_str()); return 0; }
116 if (distance == -1) { distance = 1000000; }
118 if(distance < cutoff){
119 out << i << '\t' << j << '\t' << distance << endl;
125 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
127 for(int j=0;j<i;j++){
129 if (m->control_pressed) { in.close(); out.close(); remove(tempFile.c_str()); return 0; }
133 if (distance == -1) { distance = 1000000; }
135 if(distance < cutoff){
136 out << i << '\t' << j << '\t' << distance << endl;
143 for(int i=1;i<nseqs;i++){
146 matrixNames.push_back(name);
150 for(int j=0;j<nseqs;j++){
151 if (m->control_pressed) { in.close(); out.close(); remove(tempFile.c_str()); return 0; }
155 if (distance == -1) { distance = 1000000; }
157 if(distance < cutoff && j < i){
158 out << i << '\t' << j << '\t' << distance << endl;
163 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
165 for(int j=0;j<nseqs;j++){
166 if (m->control_pressed) { in.close(); out.close(); remove(tempFile.c_str()); return 0; }
170 if (distance == -1) { distance = 1000000; }
172 if(distance < cutoff && j < i){
173 out << i << '\t' << j << '\t' << distance << endl;
186 nameMap = new NameAssignment();
187 for(int i=0;i<matrixNames.size();i++){
188 nameMap->push_back(matrixNames[i]);
196 string outputFile = m->getRootName(distFile) + "column.dist";
197 m->openInputFile(tempFile, in2);
198 m->openOutputFile(outputFile, out2);
204 if (m->control_pressed) { in2.close(); out2.close(); remove(tempFile.c_str()); remove(outputFile.c_str()); return 0; }
206 in2 >> first >> second >> dist;
207 out2 << rowToName[first] << '\t' << rowToName[second] << '\t' << dist << endl;
213 remove(tempFile.c_str());
214 distFile = outputFile;
216 if (m->control_pressed) { remove(outputFile.c_str()); }
220 catch(exception& e) {
221 m->errorOut(e, "ReadCluster", "convertPhylip2Column");
225 /***********************************************************************/
227 ReadCluster::~ReadCluster(){}
228 /***********************************************************************/