5 * Created by westcott on 1/13/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "formatphylip.h"
11 #include "progress.hpp"
13 /***********************************************************************/
14 FormatPhylipMatrix::FormatPhylipMatrix(string df) : filename(df) {
15 m->openInputFile(filename, fileHandle);
17 /***********************************************************************/
19 int FormatPhylipMatrix::read(NameAssignment* nameMap){
28 fileHandle >> numTest >> name;
30 if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
31 else { convert(numTest, nseqs); }
34 list = new ListVector(nseqs);
38 while((d=fileHandle.get()) != EOF){
40 if(isalnum(d)){ //you are square
42 fileHandle.close(); //reset file
44 //open and get through numSeqs, code below formats rest of file
45 m->openInputFile(filename, fileHandle);
46 fileHandle >> nseqs; m->gobble(fileHandle);
48 distFile = filename + ".rowFormatted";
49 m->openOutputFile(distFile, out);
59 reading = new Progress("Formatting matrix: ", nseqs * nseqs);
61 //lower triangle, so must go to column then formatted row file
66 string tempFile = filename + ".temp";
67 m->openOutputFile(tempFile, outTemp);
69 //convert to square column matrix
70 for(int i=1;i<nseqs;i++){
78 if (m->control_pressed) { outTemp.close(); m->mothurRemove(tempFile); fileHandle.close(); delete reading; return 0; }
80 fileHandle >> distance;
82 if (distance == -1) { distance = 1000000; }
84 if(distance < cutoff){
85 outTemp << i << '\t' << j << '\t' << distance << endl;
86 outTemp << j << '\t' << i << '\t' << distance << endl;
89 reading->update(index);
94 //format from square column to rowFormatted
95 //sort file by first column so the distances for each row are together
96 string outfile = m->getRootName(tempFile) + "sorted.dist.temp";
99 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
100 string command = "sort -n " + tempFile + " -o " + outfile;
101 system(command.c_str());
102 #else //sort using windows sort
103 string command = "sort " + tempFile + " /O " + outfile;
104 system(command.c_str());
107 if (m->control_pressed) { m->mothurRemove(tempFile); m->mothurRemove(outfile); delete reading; return 0; }
109 //output to new file distance for each row and save positions in file where new row begins
111 m->openInputFile(outfile, in);
113 distFile = outfile + ".rowFormatted";
114 m->openOutputFile(distFile, out);
116 rowPos.resize(nseqs, -1);
120 map<int, float> rowMap;
121 map<int, float>::iterator itRow;
123 //get first currentRow
127 string firstString = toString(first);
128 for(int k = 0; k < firstString.length(); k++) { in.putback(firstString[k]); }
131 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(tempFile); m->mothurRemove(distFile); m->mothurRemove(outfile); delete reading; return 0; }
133 in >> first >> second >> dist; m->gobble(in);
135 if (first != currentRow) {
136 //save position in file of each new row
137 rowPos[currentRow] = out.tellp();
139 out << currentRow << '\t' << rowMap.size() << '\t';
141 for (itRow = rowMap.begin(); itRow != rowMap.end(); itRow++) {
142 out << itRow->first << '\t' << itRow->second << '\t';
149 //save row you just read
150 rowMap[second] = dist;
153 reading->update(index);
155 rowMap[second] = dist;
160 //save position in file of each new row
161 rowPos[currentRow] = out.tellp();
163 out << currentRow << '\t' << rowMap.size() << '\t';
165 for (itRow = rowMap.begin(); itRow != rowMap.end(); itRow++) {
166 out << itRow->first << '\t' << itRow->second << '\t';
173 m->mothurRemove(tempFile);
174 m->mothurRemove(outfile);
176 if (m->control_pressed) { m->mothurRemove(distFile); delete reading; return 0; }
179 else{ //square matrix convert directly to formatted row file
181 map<int, float> rowMap;
182 map<int, float>::iterator itRow;
183 rowPos.resize(nseqs, -1);
185 for(int i=0;i<nseqs;i++){
190 for(int j=0;j<nseqs;j++){
191 if (m->control_pressed) { fileHandle.close(); out.close(); m->mothurRemove(distFile); delete reading; return 0; }
193 fileHandle >> distance;
195 if (distance == -1) { distance = 1000000; }
197 if((distance < cutoff) && (j != i)){
198 rowMap[j] = distance;
201 reading->update(index);
204 m->gobble(fileHandle);
206 //save position in file of each new row
207 rowPos[i] = out.tellp();
210 out << i << '\t' << rowMap.size() << '\t';
211 for (itRow = rowMap.begin(); itRow != rowMap.end(); itRow++) {
212 out << itRow->first << '\t' << itRow->second << '\t';
216 //clear map for new row's info
225 if (m->control_pressed) { m->mothurRemove(distFile); return 0; }
233 catch(exception& e) {
234 m->errorOut(e, "FormatPhylipMatrix", "read");
238 /***********************************************************************/
239 FormatPhylipMatrix::~FormatPhylipMatrix(){}
240 /***********************************************************************/