]> git.donarmstrong.com Git - mothur.git/blob - readphylip.cpp
created mothurOut class to handle logfiles
[mothur.git] / readphylip.cpp
1 /*
2  *  readphylip.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 4/21/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "readphylip.h"
11 #include "progress.hpp"
12
13 /***********************************************************************/
14
15 ReadPhylipMatrix::ReadPhylipMatrix(string distFile){
16         
17         successOpen = openInputFile(distFile, fileHandle);
18         
19 }
20
21 /***********************************************************************/
22
23 void ReadPhylipMatrix::read(NameAssignment* nameMap){
24         try {
25         
26                         float distance;
27                         int square, nseqs;
28                         string name;
29                         vector<string> matrixNames;
30         
31                         fileHandle >> nseqs >> name;
32
33                         matrixNames.push_back(name);
34
35                         if(nameMap == NULL){
36                                 list = new ListVector(nseqs);
37                                 list->set(0, name);
38                         }
39                         else{
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(); }
42                         }
43         
44                         char d;
45                         while((d=fileHandle.get()) != EOF){
46                 
47                                 if(isalnum(d)){
48                                         square = 1;
49                                         fileHandle.putback(d);
50                                         for(int i=0;i<nseqs;i++){
51                                                 fileHandle >> distance;
52                                         }
53                                         break;
54                                 }
55                                 if(d == '\n'){
56                                         square = 0;
57                                         break;
58                                 }
59                         }
60         
61                         Progress* reading;
62       
63                         if(square == 0){
64
65                                 reading = new Progress("Reading matrix:     ", nseqs * (nseqs - 1) / 2);
66                 
67                                 int        index = 0;
68                
69                                 for(int i=1;i<nseqs;i++){
70                                         fileHandle >> name;
71                                         matrixNames.push_back(name);
72         
73                                         //there's A LOT of repeated code throughout this method...
74                                         if(nameMap == NULL){
75                                                 list->set(i, name);
76                                         
77                                                 for(int j=0;j<i;j++){
78                                                         fileHandle >> distance;
79                                                 
80                                                         if (distance == -1) { distance = 1000000; }
81                                                 
82                                                         if(distance < cutoff){
83                                                                 PCell value(i, j, distance);
84                                                                 D->addCell(value);
85                                                         }
86                                                         index++;
87                                                         reading->update(index);
88                                                 }
89                                 
90                                         }
91                                         else{
92                                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
93                                 
94                                                 for(int j=0;j<i;j++){
95                                                         fileHandle >> distance;
96                                 
97                                                         if (distance == -1) { distance = 1000000; }
98                                                         
99                                                         if(distance < cutoff){
100                                                                 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
101                                                                 D->addCell(value);
102                                                         }
103                                                         index++;
104                                                         reading->update(index);
105                                                 }
106                                         }
107                                 }
108                         }
109                         else{
110
111                                 reading = new Progress("Reading matrix:     ", nseqs * nseqs);
112                         
113                                 int index = nseqs;
114                 
115                                 for(int i=1;i<nseqs;i++){
116                                         fileHandle >> name;                
117                                         matrixNames.push_back(name);
118         
119                                         if(nameMap == NULL){
120                                                 list->set(i, name);
121                                                 for(int j=0;j<nseqs;j++){
122                                                         fileHandle >> distance;
123                                         
124                                                         if (distance == -1) { distance = 1000000; }
125                                                         
126                                                         if(distance < cutoff && j < i){
127                                                                 PCell value(i, j, distance);
128                                                                 D->addCell(value);
129                                                         }
130                                                         index++;
131                                                         reading->update(index);
132                                                 }
133                                         
134                                         }
135                                         else{
136                                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
137                                 
138                                                 for(int j=0;j<nseqs;j++){
139                                                         fileHandle >> distance;
140                         
141                                                         if (distance == -1) { distance = 1000000; }
142                                                         
143                                                         if(distance < cutoff && j < i){
144                                                                 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
145                                                                 D->addCell(value);
146                                                         }
147                                                         index++;
148                                                         reading->update(index);
149                                                 }
150                                         }
151                                 }
152                         }
153                         reading->finish();
154                         delete reading;
155
156                         list->setLabel("0");
157                         fileHandle.close();
158
159                      /*   if(nameMap != NULL){
160                                 for(int i=0;i<matrixNames.size();i++){
161                                         nameMap->erase(matrixNames[i]);
162                                 }
163                                 if(nameMap->size() > 0){
164                                         //should probably tell them what is missing if we missed something
165                                         m->mothurOut("missed something\t" + toString(nameMap->size())); m->mothurOutEndLine();
166                                 }
167                         } */
168
169                 }
170         catch(exception& e) {
171                m->errorOut(e, "ReadPhylipMatrix", "read");
172                 exit(1);
173         }
174         }
175
176 /***********************************************************************/
177
178 ReadPhylipMatrix::~ReadPhylipMatrix(){
179        // delete D;
180        // delete list;
181 }