]> git.donarmstrong.com Git - mothur.git/blob - readphylip.cpp
moved utilities out of mothur.h and into mothurOut class.
[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         
19 }
20
21 /***********************************************************************/
22
23 int 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                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
71                                                                                 
72                                         fileHandle >> name;
73                                         matrixNames.push_back(name);
74                                                 
75         
76                                         //there's A LOT of repeated code throughout this method...
77                                         if(nameMap == NULL){
78                                                 list->set(i, name);
79                                         
80                                                 for(int j=0;j<i;j++){
81                                                                                                 
82                                                                                                                 if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
83                                                                                                                 
84                                                         fileHandle >> distance;
85                                                                                         
86                                                 
87                                                         if (distance == -1) { distance = 1000000; }
88                                                                                                                 else if (globaldata->sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
89                                                 
90                                                         if(distance < cutoff){
91                                                                 PCell value(i, j, distance);
92                                                                 D->addCell(value);
93                                                         }
94                                                         index++;
95                                                         reading->update(index);
96                                                 }
97                                 
98                                         }
99                                         else{
100                                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
101                                 
102                                                 for(int j=0;j<i;j++){
103                                                         fileHandle >> distance;
104                                                                                                                 
105                                                                                                                 if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
106                                 
107                                                         if (distance == -1) { distance = 1000000; }
108                                                                                                                 else if (globaldata->sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
109                                                         
110                                                         if(distance < cutoff){
111                                                                 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
112                                                                 D->addCell(value);
113                                                         }
114                                                         index++;
115                                                         reading->update(index);
116                                                 }
117                                         }
118                                 }
119                         }
120                         else{
121
122                                 reading = new Progress("Reading matrix:     ", nseqs * nseqs);
123                         
124                                 int index = nseqs;
125                 
126                                 for(int i=1;i<nseqs;i++){
127                                         fileHandle >> name;                
128                                         matrixNames.push_back(name);
129                                                                                 
130                                                                                 
131         
132                                         if(nameMap == NULL){
133                                                 list->set(i, name);
134                                                 for(int j=0;j<nseqs;j++){
135                                                         fileHandle >> distance;
136                                                                                                                 
137                                                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
138                                                                                                                 
139                                                         if (distance == -1) { distance = 1000000; }
140                                                                                                                 else if (globaldata->sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
141                                                         
142                                                         if(distance < cutoff && j < i){
143                                                                 PCell value(i, j, distance);
144                                                                 D->addCell(value);
145                                                         }
146                                                         index++;
147                                                         reading->update(index);
148                                                 }
149                                         
150                                         }
151                                         else{
152                                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
153                                 
154                                                 for(int j=0;j<nseqs;j++){
155                                                         fileHandle >> distance;
156                                                                                                                 
157                                                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
158                                                                                                                 
159                                                        if (distance == -1) { distance = 1000000; }
160                                                                                                                 else if (globaldata->sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.                                                        
161                                                         
162                                                                                                                 if(distance < cutoff && j < i){
163                                                                 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
164                                                                 D->addCell(value);
165                                                         }
166                                                         index++;
167                                                         reading->update(index);
168                                                 }
169                                         }
170                                 }
171                         }
172                                                 
173                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
174                                                 
175                         reading->finish();
176                         delete reading;
177
178                         list->setLabel("0");
179                         fileHandle.close();
180
181                      /*   if(nameMap != NULL){
182                                 for(int i=0;i<matrixNames.size();i++){
183                                         nameMap->erase(matrixNames[i]);
184                                 }
185                                 if(nameMap->size() > 0){
186                                         //should probably tell them what is missing if we missed something
187                                         m->mothurOut("missed something\t" + toString(nameMap->size())); m->mothurOutEndLine();
188                                 }
189                         } */
190                                                 
191                                                 return 1;
192
193                 }
194         catch(exception& e) {
195                m->errorOut(e, "ReadPhylipMatrix", "read");
196                 exit(1);
197         }
198         }
199
200 /***********************************************************************/
201
202 ReadPhylipMatrix::~ReadPhylipMatrix(){
203        // delete D;
204        // delete list;
205 }