]> git.donarmstrong.com Git - mothur.git/blob - readphylip.cpp
removed read.dist, read.otu, read.tree and globaldata. added current to defaults...
[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                         fileHandle >> nseqs >> name;
41
42                         matrixNames.push_back(name);
43
44                         if(nameMap == NULL){
45                                 list = new ListVector(nseqs);
46                                 list->set(0, name);
47                         }
48                         else{
49                                 list = new ListVector(nameMap->getListVector());
50                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
51                         }
52         
53                         char d;
54                         while((d=fileHandle.get()) != EOF){
55                 
56                                 if(isalnum(d)){
57                                         square = 1;
58                                         fileHandle.putback(d);
59                                         for(int i=0;i<nseqs;i++){
60                                                 fileHandle >> distance;
61                                         }
62                                         break;
63                                 }
64                                 if(d == '\n'){
65                                         square = 0;
66                                         break;
67                                 }
68                         }
69         
70                         Progress* reading;
71       
72                         if(square == 0){
73
74                                 reading = new Progress("Reading matrix:     ", nseqs * (nseqs - 1) / 2);
75                 
76                                 int        index = 0;
77                
78                                 for(int i=1;i<nseqs;i++){
79                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
80                                                                                 
81                                         fileHandle >> name;
82                                         matrixNames.push_back(name);
83                                                 
84         
85                                         //there's A LOT of repeated code throughout this method...
86                                         if(nameMap == NULL){
87                                                 list->set(i, name);
88                                         
89                                                 for(int j=0;j<i;j++){
90                                                                                                 
91                                                                                                                 if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
92                                                                                                                 
93                                                         fileHandle >> distance;
94                                                                                         
95                                                 
96                                                         if (distance == -1) { distance = 1000000; }
97                                                                                                                 else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
98                                                 
99                                                         if(distance < cutoff){
100                                                                 PCell value(i, j, distance);
101                                                                 D->addCell(value);
102                                                         }
103                                                         index++;
104                                                         reading->update(index);
105                                                 }
106                                 
107                                         }
108                                         else{
109                                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
110                                 
111                                                 for(int j=0;j<i;j++){
112                                                         fileHandle >> distance;
113                                                                                                                 
114                                                                                                                 if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
115                                 
116                                                         if (distance == -1) { distance = 1000000; }
117                                                                                                                 else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
118                                                         
119                                                         if(distance < cutoff){
120                                                                 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
121                                                                 D->addCell(value);
122                                                         }
123                                                         index++;
124                                                         reading->update(index);
125                                                 }
126                                         }
127                                 }
128                         }
129                         else{
130
131                                 reading = new Progress("Reading matrix:     ", nseqs * nseqs);
132                         
133                                 int index = nseqs;
134                 
135                                 for(int i=1;i<nseqs;i++){
136                                         fileHandle >> name;                
137                                         matrixNames.push_back(name);
138                                                                                 
139                                                                                 
140         
141                                         if(nameMap == NULL){
142                                                 list->set(i, name);
143                                                 for(int j=0;j<nseqs;j++){
144                                                         fileHandle >> distance;
145                                                                                                                 
146                                                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
147                                                                                                                 
148                                                         if (distance == -1) { distance = 1000000; }
149                                                                                                                 else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
150                                                         
151                                                         if(distance < cutoff && j < i){
152                                                                 PCell value(i, j, distance);
153                                                                 D->addCell(value);
154                                                         }
155                                                         index++;
156                                                         reading->update(index);
157                                                 }
158                                         
159                                         }
160                                         else{
161                                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
162                                 
163                                                 for(int j=0;j<nseqs;j++){
164                                                         fileHandle >> distance;
165                                                                                                                 
166                                                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
167                                                                                                                 
168                                                        if (distance == -1) { distance = 1000000; }
169                                                                                                                 else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.                                                        
170                                                         
171                                                                                                                 if(distance < cutoff && j < i){
172                                                                 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
173                                                                 D->addCell(value);
174                                                         }
175                                                         index++;
176                                                         reading->update(index);
177                                                 }
178                                         }
179                                 }
180                         }
181                                                 
182                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
183                                                 
184                         reading->finish();
185                         delete reading;
186
187                         list->setLabel("0");
188                         fileHandle.close();
189
190                      /*   if(nameMap != NULL){
191                                 for(int i=0;i<matrixNames.size();i++){
192                                         nameMap->erase(matrixNames[i]);
193                                 }
194                                 if(nameMap->size() > 0){
195                                         //should probably tell them what is missing if we missed something
196                                         m->mothurOut("missed something\t" + toString(nameMap->size())); m->mothurOutEndLine();
197                                 }
198                         } */
199                                                 
200                                                 return 1;
201
202                 }
203         catch(exception& e) {
204                m->errorOut(e, "ReadPhylipMatrix", "read");
205                 exit(1);
206         }
207         }
208
209 /***********************************************************************/
210
211 ReadPhylipMatrix::~ReadPhylipMatrix(){
212        // delete D;
213        // delete list;
214 }