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