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 /***********************************************************************/
45 int ReadCluster::read(CountTable*& ct){
48 if (format == "phylip") { convertPhylip2Column(ct); }
49 else { list = new ListVector(ct->getListVector()); }
51 if (m->control_pressed) { return 0; }
53 if (sortWanted) { OutPutFile = m->sortFile(distFile, outputDir); }
54 else { OutPutFile = distFile; } //for use by clusters splitMatrix to convert a phylip matrix to column
60 m->errorOut(e, "ReadCluster", "read");
64 /***********************************************************************/
66 int ReadCluster::convertPhylip2Column(NameAssignment*& nameMap){
68 //convert phylip file to column file
69 map<int, string> rowToName;
70 map<int, string>::iterator it;
74 string tempFile = distFile + ".column.temp";
76 m->openInputFile(distFile, in); m->gobble(in);
77 m->openOutputFile(tempFile, out);
82 vector<string> matrixNames;
85 in >> numTest >> name;
87 if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
88 else { convert(numTest, nseqs); }
91 matrixNames.push_back(name);
94 list = new ListVector(nseqs);
98 list = new ListVector(nameMap->getListVector());
99 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
103 while((d=in.get()) != EOF){
108 for(int i=0;i<nseqs;i++){
121 for(int i=1;i<nseqs;i++){
124 matrixNames.push_back(name);
126 //there's A LOT of repeated code throughout this method...
130 for(int j=0;j<i;j++){
132 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(tempFile); return 0; }
136 if (distance == -1) { distance = 1000000; }
138 if(distance < cutoff){
139 out << i << '\t' << j << '\t' << distance << endl;
145 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
147 for(int j=0;j<i;j++){
149 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(tempFile); return 0; }
153 if (distance == -1) { distance = 1000000; }
155 if(distance < cutoff){
156 out << i << '\t' << j << '\t' << distance << endl;
163 for(int i=1;i<nseqs;i++){
166 matrixNames.push_back(name);
170 for(int j=0;j<nseqs;j++){
171 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(tempFile); return 0; }
175 if (distance == -1) { distance = 1000000; }
177 if(distance < cutoff && j < i){
178 out << i << '\t' << j << '\t' << distance << endl;
183 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
185 for(int j=0;j<nseqs;j++){
186 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(tempFile); return 0; }
190 if (distance == -1) { distance = 1000000; }
192 if(distance < cutoff && j < i){
193 out << i << '\t' << j << '\t' << distance << endl;
206 nameMap = new NameAssignment();
207 for(int i=0;i<matrixNames.size();i++){
208 nameMap->push_back(matrixNames[i]);
216 string outputFile = m->getRootName(distFile) + "column.dist";
217 m->openInputFile(tempFile, in2);
218 m->openOutputFile(outputFile, out2);
224 if (m->control_pressed) { in2.close(); out2.close(); m->mothurRemove(tempFile); m->mothurRemove(outputFile); return 0; }
226 in2 >> first >> second >> dist;
227 out2 << rowToName[first] << '\t' << rowToName[second] << '\t' << dist << endl;
233 m->mothurRemove(tempFile);
234 distFile = outputFile;
236 if (m->control_pressed) { m->mothurRemove(outputFile); }
240 catch(exception& e) {
241 m->errorOut(e, "ReadCluster", "convertPhylip2Column");
245 /***********************************************************************/
247 int ReadCluster::convertPhylip2Column(CountTable*& ct){
249 //convert phylip file to column file
250 map<int, string> rowToName;
251 map<int, string>::iterator it;
255 string tempFile = distFile + ".column.temp";
257 m->openInputFile(distFile, in); m->gobble(in);
258 m->openOutputFile(tempFile, out);
263 vector<string> matrixNames;
266 in >> numTest >> name;
268 if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
269 else { convert(numTest, nseqs); }
272 matrixNames.push_back(name);
275 list = new ListVector(nseqs);
278 else{ list = new ListVector(ct->getListVector()); }
281 while((d=in.get()) != EOF){
286 for(int i=0;i<nseqs;i++){
299 for(int i=1;i<nseqs;i++){
302 matrixNames.push_back(name);
304 //there's A LOT of repeated code throughout this method...
308 for(int j=0;j<i;j++){
310 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(tempFile); return 0; }
314 if (distance == -1) { distance = 1000000; }
316 if(distance < cutoff){
317 out << i << '\t' << j << '\t' << distance << endl;
324 for(int j=0;j<i;j++){
326 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(tempFile); return 0; }
330 if (distance == -1) { distance = 1000000; }
332 if(distance < cutoff){
333 out << i << '\t' << j << '\t' << distance << endl;
340 for(int i=1;i<nseqs;i++){
343 matrixNames.push_back(name);
347 for(int j=0;j<nseqs;j++){
348 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(tempFile); return 0; }
352 if (distance == -1) { distance = 1000000; }
354 if(distance < cutoff && j < i){
355 out << i << '\t' << j << '\t' << distance << endl;
360 for(int j=0;j<nseqs;j++){
361 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(tempFile); return 0; }
365 if (distance == -1) { distance = 1000000; }
367 if(distance < cutoff && j < i){
368 out << i << '\t' << j << '\t' << distance << endl;
381 ct = new CountTable();
382 for(int i=0;i<matrixNames.size();i++){
383 ct->push_back(matrixNames[i]);
391 string outputFile = m->getRootName(distFile) + "column.dist";
392 m->openInputFile(tempFile, in2);
393 m->openOutputFile(outputFile, out2);
399 if (m->control_pressed) { in2.close(); out2.close(); m->mothurRemove(tempFile); m->mothurRemove(outputFile); return 0; }
401 in2 >> first >> second >> dist;
402 out2 << rowToName[first] << '\t' << rowToName[second] << '\t' << dist << endl;
408 m->mothurRemove(tempFile);
409 distFile = outputFile;
411 if (m->control_pressed) { m->mothurRemove(outputFile); }
415 catch(exception& e) {
416 m->errorOut(e, "ReadCluster", "convertPhylip2Column");
420 /***********************************************************************/
422 ReadCluster::~ReadCluster(){}
423 /***********************************************************************/