]> git.donarmstrong.com Git - mothur.git/blob - readphylip.cpp
fixing bugs while testing
[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 = m->openInputFile(distFile, fileHandle);
18                 sim=false;
19         
20 }
21 /***********************************************************************/
22
23 ReadPhylipMatrix::ReadPhylipMatrix(string distFile, bool s){
24         
25         successOpen = m->openInputFile(distFile, fileHandle);
26         sim=s;
27 }
28
29
30 /***********************************************************************/
31
32 int ReadPhylipMatrix::read(NameAssignment* nameMap){
33         try {
34         
35                         float distance;
36                         int square, nseqs;
37                         string name;
38                         vector<string> matrixNames;
39                                                 
40                                                 string numTest;
41                                                 fileHandle >> numTest >> name;
42                         
43                                                 if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
44                                                 else { convert(numTest, nseqs); }
45                         
46                         matrixNames.push_back(name);
47
48                         if(nameMap == NULL){
49                                 list = new ListVector(nseqs);
50                                 list->set(0, name);
51                         }
52                         else{
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(); }
55                         }
56         
57                         char d;
58                         while((d=fileHandle.get()) != EOF){
59                 
60                                 if(isalnum(d)){
61                                         square = 1;
62                                         fileHandle.putback(d);
63                                         for(int i=0;i<nseqs;i++){
64                                                 fileHandle >> distance;
65                                         }
66                                         break;
67                                 }
68                                 if(d == '\n'){
69                                         square = 0;
70                                         break;
71                                 }
72                         }
73         
74                         Progress* reading;
75       
76                         if(square == 0){
77
78                                 reading = new Progress("Reading matrix:     ", nseqs * (nseqs - 1) / 2);
79                 
80                                 int        index = 0;
81                
82                                 for(int i=1;i<nseqs;i++){
83                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
84                                                                                 
85                                         fileHandle >> name;
86                                         matrixNames.push_back(name);
87                                                 
88         
89                                         //there's A LOT of repeated code throughout this method...
90                                         if(nameMap == NULL){
91                                                 list->set(i, name);
92                                         
93                                                 for(int j=0;j<i;j++){
94                                                                                                 
95                                                                                                                 if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
96                                                                                                                 
97                                                         fileHandle >> distance;
98                                                                                         
99                                                 
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.
102                                                 
103                                                         if(distance < cutoff){
104                                                                 PCell value(i, j, distance);
105                                                                 D->addCell(value);
106                                                         }
107                                                         index++;
108                                                         reading->update(index);
109                                                 }
110                                 
111                                         }
112                                         else{
113                                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
114                                 
115                                                 for(int j=0;j<i;j++){
116                                                         fileHandle >> distance;
117                                                                                                                 
118                                                                                                                 if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
119                                 
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.
122                                                         
123                                                         if(distance < cutoff){
124                                                                 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
125                                                                 D->addCell(value);
126                                                         }
127                                                         index++;
128                                                         reading->update(index);
129                                                 }
130                                         }
131                                 }
132                         }
133                         else{
134
135                                 reading = new Progress("Reading matrix:     ", nseqs * nseqs);
136                         
137                                 int index = nseqs;
138                 
139                                 for(int i=1;i<nseqs;i++){
140                                         fileHandle >> name;                
141                                         matrixNames.push_back(name);
142                                                                                 
143                                                                                 
144         
145                                         if(nameMap == NULL){
146                                                 list->set(i, name);
147                                                 for(int j=0;j<nseqs;j++){
148                                                         fileHandle >> distance;
149                                                                                                                 
150                                                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
151                                                                                                                 
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.
154                                                         
155                                                         if(distance < cutoff && j < i){
156                                                                 PCell value(i, j, distance);
157                                                                 D->addCell(value);
158                                                         }
159                                                         index++;
160                                                         reading->update(index);
161                                                 }
162                                         
163                                         }
164                                         else{
165                                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
166                                 
167                                                 for(int j=0;j<nseqs;j++){
168                                                         fileHandle >> distance;
169                                                                                                                 
170                                                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
171                                                                                                                 
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.                                                        
174                                                         
175                                                                                                                 if(distance < cutoff && j < i){
176                                                                 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
177                                                                 D->addCell(value);
178                                                         }
179                                                         index++;
180                                                         reading->update(index);
181                                                 }
182                                         }
183                                 }
184                         }
185                                                 
186                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
187                                                 
188                         reading->finish();
189                         delete reading;
190
191                         list->setLabel("0");
192                         fileHandle.close();
193
194                      /*   if(nameMap != NULL){
195                                 for(int i=0;i<matrixNames.size();i++){
196                                         nameMap->erase(matrixNames[i]);
197                                 }
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();
201                                 }
202                         } */
203                                                 
204                                                 return 1;
205
206                 }
207         catch(exception& e) {
208                m->errorOut(e, "ReadPhylipMatrix", "read");
209                 exit(1);
210         }
211         }
212
213 /***********************************************************************/
214
215 ReadPhylipMatrix::~ReadPhylipMatrix(){
216        // delete D;
217        // delete list;
218 }