]> git.donarmstrong.com Git - mothur.git/blob - readphylip.cpp
added modify names parameter to set.dir
[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                         DMatrix->resize(nseqs);
76         
77                         if(square == 0){
78
79                                 reading = new Progress("Reading matrix:     ", nseqs * (nseqs - 1) / 2);
80                 
81                                 int        index = 0;
82                
83                                 for(int i=1;i<nseqs;i++){
84                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
85                                                                                 
86                                         fileHandle >> name;
87                                         matrixNames.push_back(name);
88                                                 
89         
90                                         //there's A LOT of repeated code throughout this method...
91                                         if(nameMap == NULL){
92                                                 list->set(i, name);
93                                         
94                                                 for(int j=0;j<i;j++){
95                                                                                                 
96                                                                                                                 if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
97                                                                                                                 
98                                                         fileHandle >> distance;
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                                                             PDistCell value(i, distance);
105                                                             DMatrix->addCell(j, 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                                                             PDistCell value(nameMap->get(matrixNames[i]), distance);
125                                                             DMatrix->addCell(nameMap->get(matrixNames[j]), 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                                                             PDistCell value(i, distance);
157                                                             DMatrix->addCell(j, 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                                                             PDistCell value(nameMap->get(matrixNames[i]), distance);
177                                                             DMatrix->addCell(nameMap->get(matrixNames[j]), 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                                                                 
195                                                 return 1;
196
197                 }
198         catch(exception& e) {
199                m->errorOut(e, "ReadPhylipMatrix", "read");
200                 exit(1);
201         }
202         }
203 /***********************************************************************/
204
205 int ReadPhylipMatrix::read(CountTable* countTable){
206     try {
207         
208         float distance;
209         int square, nseqs; 
210         string name;
211         vector<string> matrixNames;
212         
213         string numTest;
214         fileHandle >> numTest >> name;
215         
216         if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
217         else { convert(numTest, nseqs); }
218         
219         matrixNames.push_back(name);
220         
221         if(countTable == NULL){
222             list = new ListVector(nseqs);
223             list->set(0, name);
224         }
225         else{  list = new ListVector(countTable->getListVector()); }
226         
227         if (m->control_pressed) { return 0; }
228         
229         char d;
230         while((d=fileHandle.get()) != EOF){
231             
232             if(isalnum(d)){
233                 square = 1;
234                 fileHandle.putback(d);
235                 for(int i=0;i<nseqs;i++){
236                     fileHandle >> distance;
237                 }
238                 break;
239             }
240             if(d == '\n'){
241                 square = 0;
242                 break;
243             }
244         }
245         
246         Progress* reading;
247         DMatrix->resize(nseqs);
248         
249         if(square == 0){
250             
251             reading = new Progress("Reading matrix:     ", nseqs * (nseqs - 1) / 2);
252             
253             int        index = 0;
254             
255             for(int i=1;i<nseqs;i++){
256                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
257                 
258                 fileHandle >> name;
259                 matrixNames.push_back(name);
260                 
261                 
262                 //there's A LOT of repeated code throughout this method...
263                 if(countTable == NULL){
264                     list->set(i, name);
265                     
266                     for(int j=0;j<i;j++){
267                         
268                         if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
269                         
270                         fileHandle >> distance;
271                         
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.
274                         
275                         if(distance < cutoff){
276                             PDistCell value(i, distance);
277                             DMatrix->addCell(j, value);
278                         }
279                         index++;
280                         reading->update(index);
281                     }
282                     
283                 }
284                 else{
285                     for(int j=0;j<i;j++){
286                         fileHandle >> distance;
287                         
288                         if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
289                         
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.
292                         
293                         if(distance < cutoff){
294                             int iIndex = countTable->get(matrixNames[i]);
295                             int jIndex = countTable->get(matrixNames[j]);
296                             
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);
301                             }else {
302                                 PDistCell value(iIndex, distance);
303                                 DMatrix->addCell(jIndex, value);
304
305                             }
306                         }
307                         index++;
308                         reading->update(index);
309                     }
310                 }
311             }
312         }
313         else{
314             
315             reading = new Progress("Reading matrix:     ", nseqs * nseqs);
316             
317             int index = nseqs;
318             
319             for(int i=1;i<nseqs;i++){
320                 fileHandle >> name;                
321                 matrixNames.push_back(name);
322                 
323                 
324                 
325                 if(countTable == NULL){
326                     list->set(i, name);
327                     for(int j=0;j<nseqs;j++){
328                         fileHandle >> distance;
329                         
330                         if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
331                         
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.
334                         
335                         if(distance < cutoff && j < i){
336                             PDistCell value(i, distance);
337                             DMatrix->addCell(j, value);
338                         }
339                         index++;
340                         reading->update(index);
341                     }
342                     
343                 }
344                 else{
345                     for(int j=0;j<nseqs;j++){
346                         fileHandle >> distance;
347                         
348                         if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
349                         
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.                                                        
352                         
353                         if(distance < cutoff && j < i){
354                             int iIndex = countTable->get(matrixNames[i]);
355                             int jIndex = countTable->get(matrixNames[j]);
356                             
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);
361                             }else {
362                                 PDistCell value(iIndex, distance);
363                                 DMatrix->addCell(jIndex, value);
364                                 
365                             }
366                         }
367                         index++;
368                         reading->update(index);
369                     }
370                 }
371             }
372         }
373         
374         if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
375         
376         reading->finish();
377         delete reading;
378         
379         list->setLabel("0");
380         fileHandle.close();
381         
382         
383         return 1;
384         
385     }
386     catch(exception& e) {
387         m->errorOut(e, "ReadPhylipMatrix", "read");
388         exit(1);
389     }
390 }
391 /***********************************************************************/
392 ReadPhylipMatrix::~ReadPhylipMatrix(){}
393 /***********************************************************************/
394