]> git.donarmstrong.com Git - mothur.git/blob - readcolumn.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[mothur.git] / readcolumn.cpp
1 /*
2  *  readcolumn.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 "readcolumn.h"
11 #include "progress.hpp"
12
13 /***********************************************************************/
14
15 ReadColumnMatrix::ReadColumnMatrix(string df) : distFile(df){
16         
17         successOpen = m->openInputFile(distFile, fileHandle);
18         sim = false;
19         
20 }
21 /***********************************************************************/
22
23 ReadColumnMatrix::ReadColumnMatrix(string df, bool s) : distFile(df){
24         
25         successOpen = m->openInputFile(distFile, fileHandle);
26         sim = s;
27 }
28
29 /***********************************************************************/
30
31 int ReadColumnMatrix::read(NameAssignment* nameMap){
32         try {           
33
34                 string firstName, secondName;
35                 float distance;
36                 int nseqs = nameMap->size();
37         DMatrix->resize(nseqs);
38                 list = new ListVector(nameMap->getListVector());
39         
40                 Progress* reading = new Progress("Reading matrix:     ", nseqs * nseqs);
41
42                 int lt = 1;
43                 int refRow = 0; //we'll keep track of one cell - Cell(refRow,refCol) - and see if it's transpose
44                 int refCol = 0; //shows up later - Cell(refCol,refRow).  If it does, then its a square matrix
45
46                 //need to see if this is a square or a triangular matrix...
47         
48                 while(fileHandle && lt == 1){  //let's assume it's a triangular matrix...
49
50                 
51                         fileHandle >> firstName >> secondName >> distance;      // get the row and column names and distance
52                         
53                         if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
54         
55                         map<string,int>::iterator itA = nameMap->find(firstName);
56                         map<string,int>::iterator itB = nameMap->find(secondName);
57
58                         if(itA == nameMap->end()){  m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1);  }
59                         if(itB == nameMap->end()){  m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1);  }
60
61                         if (distance == -1) { distance = 1000000; }
62                         else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
63                         
64                         if(distance < cutoff && itA != itB){
65                                 if(itA->second > itB->second){
66                     PDistCell value(itA->second, distance);
67                     
68                     
69                                         if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
70                                                 refRow = itA->second;
71                                                 refCol = itB->second;
72                                                 DMatrix->addCell(itB->second, value);
73                                         }
74                                         else if(refRow == itA->second && refCol == itB->second){
75                                                 lt = 0;
76                                         }
77                                         else{
78                                                 DMatrix->addCell(itB->second, value);
79                                         }
80                                 }
81                                 else if(itA->second < itB->second){
82                                         PDistCell value(itB->second, distance);
83                         
84                                         if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
85                                                 refRow = itA->second;
86                                                 refCol = itB->second;
87                                                 DMatrix->addCell(itA->second, value);
88                                         }
89                                         else if(refRow == itB->second && refCol == itA->second){
90                                                 lt = 0;
91                                         }
92                                         else{
93                                                 DMatrix->addCell(itA->second, value);
94                                         }
95                                 }
96                                 reading->update(itA->second * nseqs);
97                         }
98                         m->gobble(fileHandle);
99                 }
100
101                 if(lt == 0){  // oops, it was square
102         
103                         fileHandle.close();  //let's start over
104                         DMatrix->clear();  //let's start over
105                    
106                         m->openInputFile(distFile, fileHandle);  //let's start over
107
108                         while(fileHandle){
109                                 fileHandle >> firstName >> secondName >> distance;
110                                 
111                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
112                 
113                                 map<string,int>::iterator itA = nameMap->find(firstName);
114                                 map<string,int>::iterator itB = nameMap->find(secondName);
115                                 
116                                 if(itA == nameMap->end()){  m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1);  }
117                                 if(itB == nameMap->end()){  m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1);  }
118                                 
119                                 if (distance == -1) { distance = 1000000; }
120                                 else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
121                                 
122                                 if(distance < cutoff && itA->second > itB->second){
123                     PDistCell value(itA->second, distance);
124                                         DMatrix->addCell(itB->second, value);
125                                         reading->update(itA->second * nseqs);
126                                 }
127                 
128                                 m->gobble(fileHandle);
129                         }
130                 }
131                 
132                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
133                 
134                 reading->finish();
135                 fileHandle.close();
136
137                 list->setLabel("0");
138                 
139                 return 1;
140
141         }
142         catch(exception& e) {
143                 m->errorOut(e, "ReadColumnMatrix", "read");
144                 exit(1);
145         }
146 }
147 /***********************************************************************/
148
149 int ReadColumnMatrix::read(CountTable* countTable){
150         try {           
151         
152                 string firstName, secondName;
153                 float distance;
154                 int nseqs = countTable->size();
155         
156         DMatrix->resize(nseqs);
157                 list = new ListVector(countTable->getListVector());
158         
159                 Progress* reading = new Progress("Reading matrix:     ", nseqs * nseqs);
160         
161                 int lt = 1;
162                 int refRow = 0; //we'll keep track of one cell - Cell(refRow,refCol) - and see if it's transpose
163                 int refCol = 0; //shows up later - Cell(refCol,refRow).  If it does, then its a square matrix
164         
165                 //need to see if this is a square or a triangular matrix...
166                
167                 while(fileHandle && lt == 1){  //let's assume it's a triangular matrix...
168             
169             
170                         fileHandle >> firstName >> secondName >> distance;      // get the row and column names and distance
171                         
172                         if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
173             
174                         int itA = countTable->get(firstName);
175                         int itB = countTable->get(secondName);
176             
177             if (m->control_pressed) { exit(1); }
178             
179                         if (distance == -1) { distance = 1000000; }
180                         else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
181                         
182                         if(distance < cutoff && itA != itB){
183                                 if(itA > itB){
184                     PDistCell value(itA, distance);
185                     
186                     
187                                         if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
188                                                 refRow = itA;
189                                                 refCol = itB;
190                                                 DMatrix->addCell(itB, value);
191                                         }
192                                         else if(refRow == itA && refCol == itB){
193                                                 lt = 0;
194                                         }
195                                         else{
196                                                 DMatrix->addCell(itB, value);
197                                         }
198                                 }
199                                 else if(itA < itB){
200                                         PDistCell value(itB, distance);
201                     
202                                         if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
203                                                 refRow = itA;
204                                                 refCol = itB;
205                                                 DMatrix->addCell(itA, value);
206                                         }
207                                         else if(refRow == itB && refCol == itA){
208                                                 lt = 0;
209                                         }
210                                         else{
211                                                 DMatrix->addCell(itA, value);
212                                         }
213                                 }
214                                 reading->update(itA * nseqs);
215                         }
216                         m->gobble(fileHandle);
217                 }
218         
219                 if(lt == 0){  // oops, it was square
220             
221                         fileHandle.close();  //let's start over
222                         DMatrix->clear();  //let's start over
223             
224                         m->openInputFile(distFile, fileHandle);  //let's start over
225             
226                         while(fileHandle){
227                                 fileHandle >> firstName >> secondName >> distance;
228                                 
229                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
230                 
231                                 int itA = countTable->get(firstName);
232                 int itB = countTable->get(secondName);
233                 
234                 
235                 if (m->control_pressed) { exit(1); }
236                                 
237                                 if (distance == -1) { distance = 1000000; }
238                                 else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
239                                 
240                                 if(distance < cutoff && itA > itB){
241                     PDistCell value(itA, distance);
242                                         DMatrix->addCell(itB, value);
243                                         reading->update(itA * nseqs);
244                                 }
245                 
246                                 m->gobble(fileHandle);
247                         }
248                 }
249                 
250                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
251                 
252                 reading->finish();
253                 fileHandle.close();
254         
255                 list->setLabel("0");
256                 
257                 return 1;
258         
259         }
260         catch(exception& e) {
261                 m->errorOut(e, "ReadColumnMatrix", "read");
262                 exit(1);
263         }
264 }
265
266 /***********************************************************************/
267 ReadColumnMatrix::~ReadColumnMatrix(){}
268 /***********************************************************************/
269