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;
41 fileHandle >> numTest >> name;
43 if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
44 else { convert(numTest, nseqs); }
46 matrixNames.push_back(name);
49 list = new ListVector(nseqs);
53 list = new ListVector(nameMap->getListVector());
54 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
58 while((d=fileHandle.get()) != EOF){
62 fileHandle.putback(d);
63 for(int i=0;i<nseqs;i++){
64 fileHandle >> distance;
78 reading = new Progress("Reading matrix: ", nseqs * (nseqs - 1) / 2);
82 for(int i=1;i<nseqs;i++){
83 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
86 matrixNames.push_back(name);
89 //there's A LOT of repeated code throughout this method...
95 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
97 fileHandle >> distance;
100 if (distance == -1) { distance = 1000000; }
101 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
103 if(distance < cutoff){
104 PCell value(i, j, distance);
108 reading->update(index);
113 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
115 for(int j=0;j<i;j++){
116 fileHandle >> distance;
118 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
120 if (distance == -1) { distance = 1000000; }
121 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
123 if(distance < cutoff){
124 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
128 reading->update(index);
135 reading = new Progress("Reading matrix: ", nseqs * nseqs);
139 for(int i=1;i<nseqs;i++){
141 matrixNames.push_back(name);
147 for(int j=0;j<nseqs;j++){
148 fileHandle >> distance;
150 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
152 if (distance == -1) { distance = 1000000; }
153 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
155 if(distance < cutoff && j < i){
156 PCell value(i, j, distance);
160 reading->update(index);
165 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
167 for(int j=0;j<nseqs;j++){
168 fileHandle >> distance;
170 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
172 if (distance == -1) { distance = 1000000; }
173 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
175 if(distance < cutoff && j < i){
176 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
180 reading->update(index);
186 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
194 /* if(nameMap != NULL){
195 for(int i=0;i<matrixNames.size();i++){
196 nameMap->erase(matrixNames[i]);
198 if(nameMap->size() > 0){
199 //should probably tell them what is missing if we missed something
200 m->mothurOut("missed something\t" + toString(nameMap->size())); m->mothurOutEndLine();
207 catch(exception& e) {
208 m->errorOut(e, "ReadPhylipMatrix", "read");
213 /***********************************************************************/
215 ReadPhylipMatrix::~ReadPhylipMatrix(){