5 * Created by Sarah Westcott on 4/21/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "readphylip.h"
11 #include "progress.hpp"
13 /***********************************************************************/
15 ReadPhylipMatrix::ReadPhylipMatrix(string distFile){
17 successOpen = m->openInputFile(distFile, fileHandle);
21 /***********************************************************************/
23 ReadPhylipMatrix::ReadPhylipMatrix(string distFile, bool s){
25 successOpen = m->openInputFile(distFile, fileHandle);
30 /***********************************************************************/
32 int ReadPhylipMatrix::read(NameAssignment* nameMap){
38 vector<string> matrixNames;
40 fileHandle >> nseqs >> name;
42 matrixNames.push_back(name);
45 list = new ListVector(nseqs);
49 list = new ListVector(nameMap->getListVector());
50 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
54 while((d=fileHandle.get()) != EOF){
58 fileHandle.putback(d);
59 for(int i=0;i<nseqs;i++){
60 fileHandle >> distance;
74 reading = new Progress("Reading matrix: ", nseqs * (nseqs - 1) / 2);
78 for(int i=1;i<nseqs;i++){
79 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
82 matrixNames.push_back(name);
85 //there's A LOT of repeated code throughout this method...
91 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
93 fileHandle >> distance;
96 if (distance == -1) { distance = 1000000; }
97 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
99 if(distance < cutoff){
100 PCell value(i, j, distance);
104 reading->update(index);
109 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
111 for(int j=0;j<i;j++){
112 fileHandle >> distance;
114 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
116 if (distance == -1) { distance = 1000000; }
117 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
119 if(distance < cutoff){
120 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
124 reading->update(index);
131 reading = new Progress("Reading matrix: ", nseqs * nseqs);
135 for(int i=1;i<nseqs;i++){
137 matrixNames.push_back(name);
143 for(int j=0;j<nseqs;j++){
144 fileHandle >> distance;
146 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
148 if (distance == -1) { distance = 1000000; }
149 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
151 if(distance < cutoff && j < i){
152 PCell value(i, j, distance);
156 reading->update(index);
161 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
163 for(int j=0;j<nseqs;j++){
164 fileHandle >> distance;
166 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
168 if (distance == -1) { distance = 1000000; }
169 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
171 if(distance < cutoff && j < i){
172 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
176 reading->update(index);
182 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
190 /* if(nameMap != NULL){
191 for(int i=0;i<matrixNames.size();i++){
192 nameMap->erase(matrixNames[i]);
194 if(nameMap->size() > 0){
195 //should probably tell them what is missing if we missed something
196 m->mothurOut("missed something\t" + toString(nameMap->size())); m->mothurOutEndLine();
203 catch(exception& e) {
204 m->errorOut(e, "ReadPhylipMatrix", "read");
209 /***********************************************************************/
211 ReadPhylipMatrix::~ReadPhylipMatrix(){