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();
20 /***********************************************************************/
22 void ReadCluster::read(NameAssignment* nameMap){
25 if (format == "phylip") { convertPhylip2Column(nameMap); }
26 else { list = new ListVector(nameMap->getListVector()); }
28 OutPutFile = sortFile(distFile);
32 errorOut(e, "ReadCluster", "read");
36 /***********************************************************************/
38 void ReadCluster::convertPhylip2Column(NameAssignment* nameMap){
40 //convert phylip file to column file
41 map<int, string> rowToName;
42 map<int, string>::iterator it;
46 string tempFile = distFile + ".column.temp";
48 openInputFile(distFile, in);
49 openOutputFile(tempFile, out);
54 vector<string> matrixNames;
58 matrixNames.push_back(name);
61 list = new ListVector(nseqs);
65 list = new ListVector(nameMap->getListVector());
66 if(nameMap->count(name)==0){ mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); mothurOutEndLine(); }
70 while((d=in.get()) != EOF){
75 for(int i=0;i<nseqs;i++){
88 for(int i=1;i<nseqs;i++){
91 matrixNames.push_back(name);
93 //there's A LOT of repeated code throughout this method...
100 if (distance == -1) { distance = 1000000; }
102 if(distance < cutoff){
103 out << i << '\t' << j << '\t' << distance << endl;
109 if(nameMap->count(name)==0){ mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); mothurOutEndLine(); }
111 for(int j=0;j<i;j++){
114 if (distance == -1) { distance = 1000000; }
116 if(distance < cutoff){
117 out << i << '\t' << j << '\t' << distance << endl;
124 for(int i=1;i<nseqs;i++){
127 matrixNames.push_back(name);
131 for(int j=0;j<nseqs;j++){
134 if (distance == -1) { distance = 1000000; }
136 if(distance < cutoff && j < i){
137 out << i << '\t' << j << '\t' << distance << endl;
142 if(nameMap->count(name)==0){ mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); mothurOutEndLine(); }
144 for(int j=0;j<nseqs;j++){
147 if (distance == -1) { distance = 1000000; }
149 if(distance < cutoff && j < i){
150 out << i << '\t' << j << '\t' << distance << endl;
163 nameMap = new NameAssignment();
164 for(int i=0;i<matrixNames.size();i++){
165 nameMap->push_back(matrixNames[i]);
167 globaldata->nameMap = nameMap;
174 string outputFile = getRootName(distFile) + "column.dist";
175 openInputFile(tempFile, in2);
176 openOutputFile(outputFile, out2);
182 in2 >> first >> second >> dist;
183 out2 << rowToName[first] << '\t' << rowToName[second] << '\t' << dist << endl;
189 remove(tempFile.c_str());
190 distFile = outputFile;
192 catch(exception& e) {
193 errorOut(e, "ReadCluster", "convertPhylip2Column");
197 /***********************************************************************/
199 ReadCluster::~ReadCluster(){}
200 /***********************************************************************/