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 = openInputFile(distFile, fileHandle);
21 /***********************************************************************/
23 int ReadPhylipMatrix::read(NameAssignment* nameMap){
29 vector<string> matrixNames;
31 fileHandle >> nseqs >> name;
33 matrixNames.push_back(name);
36 list = new ListVector(nseqs);
40 list = new ListVector(nameMap->getListVector());
41 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
45 while((d=fileHandle.get()) != EOF){
49 fileHandle.putback(d);
50 for(int i=0;i<nseqs;i++){
51 fileHandle >> distance;
65 reading = new Progress("Reading matrix: ", nseqs * (nseqs - 1) / 2);
69 for(int i=1;i<nseqs;i++){
70 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
73 matrixNames.push_back(name);
76 //there's A LOT of repeated code throughout this method...
82 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
84 fileHandle >> distance;
87 if (distance == -1) { distance = 1000000; }
88 else if (globaldata->sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
90 if(distance < cutoff){
91 PCell value(i, j, distance);
95 reading->update(index);
100 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
102 for(int j=0;j<i;j++){
103 fileHandle >> distance;
105 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
107 if (distance == -1) { distance = 1000000; }
108 else if (globaldata->sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
110 if(distance < cutoff){
111 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
115 reading->update(index);
122 reading = new Progress("Reading matrix: ", nseqs * nseqs);
126 for(int i=1;i<nseqs;i++){
128 matrixNames.push_back(name);
134 for(int j=0;j<nseqs;j++){
135 fileHandle >> distance;
137 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
139 if (distance == -1) { distance = 1000000; }
140 else if (globaldata->sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
142 if(distance < cutoff && j < i){
143 PCell value(i, j, distance);
147 reading->update(index);
152 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
154 for(int j=0;j<nseqs;j++){
155 fileHandle >> distance;
157 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
159 if (distance == -1) { distance = 1000000; }
160 else if (globaldata->sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
162 if(distance < cutoff && j < i){
163 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
167 reading->update(index);
173 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
181 /* if(nameMap != NULL){
182 for(int i=0;i<matrixNames.size();i++){
183 nameMap->erase(matrixNames[i]);
185 if(nameMap->size() > 0){
186 //should probably tell them what is missing if we missed something
187 m->mothurOut("missed something\t" + toString(nameMap->size())); m->mothurOutEndLine();
194 catch(exception& e) {
195 m->errorOut(e, "ReadPhylipMatrix", "read");
200 /***********************************************************************/
202 ReadPhylipMatrix::~ReadPhylipMatrix(){