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;
75 DMatrix->resize(nseqs);
79 reading = new Progress("Reading matrix: ", nseqs * (nseqs - 1) / 2);
83 for(int i=1;i<nseqs;i++){
84 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
87 matrixNames.push_back(name);
90 //there's A LOT of repeated code throughout this method...
96 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
98 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 PDistCell value(i, distance);
105 DMatrix->addCell(j, value);
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 PDistCell value(nameMap->get(matrixNames[i]), distance);
125 DMatrix->addCell(nameMap->get(matrixNames[j]), value);
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 PDistCell value(i, distance);
157 DMatrix->addCell(j, value);
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 PDistCell value(nameMap->get(matrixNames[i]), distance);
177 DMatrix->addCell(nameMap->get(matrixNames[j]), value);
180 reading->update(index);
186 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
198 catch(exception& e) {
199 m->errorOut(e, "ReadPhylipMatrix", "read");
203 /***********************************************************************/
205 int ReadPhylipMatrix::read(CountTable* countTable){
211 vector<string> matrixNames;
214 fileHandle >> numTest >> name;
216 if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
217 else { convert(numTest, nseqs); }
219 matrixNames.push_back(name);
221 if(countTable == NULL){
222 list = new ListVector(nseqs);
225 else{ list = new ListVector(countTable->getListVector()); }
227 if (m->control_pressed) { return 0; }
230 while((d=fileHandle.get()) != EOF){
234 fileHandle.putback(d);
235 for(int i=0;i<nseqs;i++){
236 fileHandle >> distance;
247 DMatrix->resize(nseqs);
251 reading = new Progress("Reading matrix: ", nseqs * (nseqs - 1) / 2);
255 for(int i=1;i<nseqs;i++){
256 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
259 matrixNames.push_back(name);
262 //there's A LOT of repeated code throughout this method...
263 if(countTable == NULL){
266 for(int j=0;j<i;j++){
268 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
270 fileHandle >> distance;
272 if (distance == -1) { distance = 1000000; }
273 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
275 if(distance < cutoff){
276 PDistCell value(i, distance);
277 DMatrix->addCell(j, value);
280 reading->update(index);
285 for(int j=0;j<i;j++){
286 fileHandle >> distance;
288 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
290 if (distance == -1) { distance = 1000000; }
291 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
293 if(distance < cutoff){
294 int iIndex = countTable->get(matrixNames[i]);
295 int jIndex = countTable->get(matrixNames[j]);
297 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
298 if (iIndex < jIndex) {
299 PDistCell value(jIndex, distance);
300 DMatrix->addCell(iIndex, value);
302 PDistCell value(iIndex, distance);
303 DMatrix->addCell(jIndex, value);
308 reading->update(index);
315 reading = new Progress("Reading matrix: ", nseqs * nseqs);
319 for(int i=1;i<nseqs;i++){
321 matrixNames.push_back(name);
325 if(countTable == NULL){
327 for(int j=0;j<nseqs;j++){
328 fileHandle >> distance;
330 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
332 if (distance == -1) { distance = 1000000; }
333 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
335 if(distance < cutoff && j < i){
336 PDistCell value(i, distance);
337 DMatrix->addCell(j, value);
340 reading->update(index);
345 for(int j=0;j<nseqs;j++){
346 fileHandle >> distance;
348 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
350 if (distance == -1) { distance = 1000000; }
351 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
353 if(distance < cutoff && j < i){
354 int iIndex = countTable->get(matrixNames[i]);
355 int jIndex = countTable->get(matrixNames[j]);
357 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
358 if (iIndex < jIndex) {
359 PDistCell value(jIndex, distance);
360 DMatrix->addCell(iIndex, value);
362 PDistCell value(iIndex, distance);
363 DMatrix->addCell(jIndex, value);
368 reading->update(index);
374 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
386 catch(exception& e) {
387 m->errorOut(e, "ReadPhylipMatrix", "read");
391 /***********************************************************************/
392 ReadPhylipMatrix::~ReadPhylipMatrix(){}
393 /***********************************************************************/