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){
15 globaldata = GlobalData::getInstance();
16 m = MothurOut::getInstance();
21 /***********************************************************************/
23 void ReadCluster::read(NameAssignment* nameMap){
26 if (format == "phylip") { convertPhylip2Column(nameMap); }
27 else { list = new ListVector(nameMap->getListVector()); }
29 OutPutFile = sortFile(distFile);
33 m->errorOut(e, "ReadCluster", "read");
37 /***********************************************************************/
39 void ReadCluster::convertPhylip2Column(NameAssignment* nameMap){
41 //convert phylip file to column file
42 map<int, string> rowToName;
43 map<int, string>::iterator it;
47 string tempFile = distFile + ".column.temp";
49 openInputFile(distFile, in);
50 openOutputFile(tempFile, out);
55 vector<string> matrixNames;
59 matrixNames.push_back(name);
62 list = new ListVector(nseqs);
66 list = new ListVector(nameMap->getListVector());
67 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
71 while((d=in.get()) != EOF){
76 for(int i=0;i<nseqs;i++){
89 for(int i=1;i<nseqs;i++){
92 matrixNames.push_back(name);
94 //there's A LOT of repeated code throughout this method...
101 if (distance == -1) { distance = 1000000; }
103 if(distance < cutoff){
104 out << i << '\t' << j << '\t' << distance << endl;
110 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
112 for(int j=0;j<i;j++){
115 if (distance == -1) { distance = 1000000; }
117 if(distance < cutoff){
118 out << i << '\t' << j << '\t' << distance << endl;
125 for(int i=1;i<nseqs;i++){
128 matrixNames.push_back(name);
132 for(int j=0;j<nseqs;j++){
135 if (distance == -1) { distance = 1000000; }
137 if(distance < cutoff && j < i){
138 out << i << '\t' << j << '\t' << distance << endl;
143 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
145 for(int j=0;j<nseqs;j++){
148 if (distance == -1) { distance = 1000000; }
150 if(distance < cutoff && j < i){
151 out << i << '\t' << j << '\t' << distance << endl;
164 nameMap = new NameAssignment();
165 for(int i=0;i<matrixNames.size();i++){
166 nameMap->push_back(matrixNames[i]);
168 globaldata->nameMap = nameMap;
175 string outputFile = getRootName(distFile) + "column.dist";
176 openInputFile(tempFile, in2);
177 openOutputFile(outputFile, out2);
183 in2 >> first >> second >> dist;
184 out2 << rowToName[first] << '\t' << rowToName[second] << '\t' << dist << endl;
190 remove(tempFile.c_str());
191 distFile = outputFile;
193 catch(exception& e) {
194 m->errorOut(e, "ReadCluster", "convertPhylip2Column");
198 /***********************************************************************/
200 ReadCluster::~ReadCluster(){}
201 /***********************************************************************/