]> git.donarmstrong.com Git - mothur.git/blob - fullmatrix.cpp
fixed bug in read.tree
[mothur.git] / fullmatrix.cpp
1 /*
2  *  fullmatrix.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 3/6/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "fullmatrix.h"
11
12 /**************************************************************************/
13 //This constructor reads a distance matrix file and stores the data in the matrix.
14 FullMatrix::FullMatrix(ifstream& filehandle) {
15         try{
16                 globaldata = GlobalData::getInstance();
17                 groupmap = globaldata->gGroupmap;
18                 
19                 float minSoFar = 0.0;
20                 string name, group;
21                 filehandle >> numSeqs >> name;
22                 
23                 //make the matrix filled with zeros
24                 matrix.resize(numSeqs); 
25                 for(int i = 0; i < numSeqs; i++) {
26                         matrix[i].resize(numSeqs, 0);
27                 }
28                 
29                 group = groupmap->getGroup(name);
30                 if(group == "not found") {      cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
31                 index[0].groupname = group; 
32                 index[0].seqName = name;
33                 
34                 //determine if matrix is square or lower triangle
35                 //if it is square read the distances for the first sequence
36                 char d;
37                 while((d=filehandle.get()) != EOF){
38                         
39                         //is d a number meaning its square
40                         if(isalnum(d)){ 
41                                 square = true;
42                                 filehandle.putback(d);
43                                 
44                                 if (numSeqs >= 2) {
45                                         //save first distance that is not distance to itself as minimum
46                                         filehandle >> matrix[0][0] >> minSoFar;
47                                         matrix[0][1] = minSoFar;
48                                 }
49                                 
50                                 for(int i=2;i<numSeqs;i++){
51                                         filehandle >> matrix[0][i];
52                                         if (matrix[0][i] < minSoFar) { minSoFar = matrix[0][i]; }
53                                 }
54                                 index[0].minDist = minSoFar;
55                                 break;
56                         }
57                         
58                         //is d a line return meaning its lower triangle
59                         if(d == '\n'){
60                                 square = false;
61                                 break;
62                         }
63                 }
64                 
65                 //read rest of matrix
66                 if (square == true) { readSquareMatrix(filehandle); }
67                 else { readLTMatrix(filehandle); }
68                 
69                 //sort sequences so they are gathered in groups for processing
70                 sortGroups(0, numSeqs-1);
71                         
72         }
73         catch(exception& e) {
74                 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
75                 exit(1);
76         }
77         catch(...) {
78                 cout << "An unknown error has occurred in the FullMatrix class function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79                 exit(1);
80         }
81 }
82 /**************************************************************************/
83 void FullMatrix::readSquareMatrix(ifstream& filehandle) {
84         try {
85         
86                 Progress* reading;
87                 reading = new Progress("Reading matrix:    ", numSeqs * numSeqs);
88                 
89                 int count = 0;
90                 float distance, minSoFar;
91                 minSoFar = 0.0;
92                 string group, name;
93                 
94                 for(int i=1;i<numSeqs;i++){
95                         filehandle >> name;             
96                         
97                         group = groupmap->getGroup(name);
98                         index[i].groupname = group;
99                         index[i].seqName = name;
100                         
101                         filehandle >> minSoFar;
102                         matrix[i][0] = minSoFar;
103                         
104                         if(group == "not found") {      cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
105                                 
106                         for(int j=1;j<numSeqs;j++){
107                                 filehandle >> distance;
108                                 
109                                 if ((distance < minSoFar) && (i != j)) { minSoFar = distance; }
110                                         
111                                 matrix[i][j] = distance;
112                                 count++;
113                                 reading->update(count);
114                         }
115                         
116                         //save minimum value for each row
117                         index[i].minDist = minSoFar;
118                 }
119                 reading->finish();
120                 delete reading;
121         }
122         catch(exception& e) {
123                 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
124                 exit(1);
125         }
126         catch(...) {
127                 cout << "An unknown error has occurred in the FullMatrix class function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
128                 exit(1);
129         }
130
131
132 /**************************************************************************/
133 void FullMatrix::readLTMatrix(ifstream& filehandle) {
134         try {
135                 Progress* reading;
136                 reading = new Progress("Reading matrix:    ", numSeqs * (numSeqs - 1) / 2);
137                 
138                 int count = 0;
139                 float distance, minSoFar;
140                 minSoFar = 0.0;
141
142                 string group, name;
143                 
144                 for(int i=1;i<numSeqs;i++){
145                         filehandle >> name;             
146                                                 
147                         group = groupmap->getGroup(name);
148                         index[i].groupname = group;
149                         index[i].seqName = name;
150                         
151                         filehandle >> minSoFar;
152                         matrix[i][0] = minSoFar;
153         
154                         if(group == "not found") {      cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl;  exit(1); }
155                                 
156                         for(int j=1;j<i;j++){
157                                 filehandle >> distance;
158                                 
159                                 if (distance < minSoFar)  { minSoFar = distance; }
160                                         
161                                 matrix[i][j] = distance;  matrix[j][i] = distance;
162                                 count++;
163                                 reading->update(count);
164                         }
165                         
166                         //save minimum value for each row
167                         index[i].minDist = minSoFar;
168                 }
169                 reading->finish();
170                 delete reading;
171         }
172         catch(exception& e) {
173                 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174                 exit(1);
175         }
176         catch(...) {
177                 cout << "An unknown error has occurred in the FullMatrix class function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
178                 exit(1);
179         }
180
181 }
182
183 /**************************************************************************/
184 void FullMatrix::sortGroups(int low, int high){
185         try{
186         
187                 int i = low;
188                 int j = high;
189                 int y = 0;
190                 string name;
191                 
192                 /* compare value */
193                 //what group does this row belong to
194                 string z = index[(low + high) / 2].groupname;
195
196                 /* partition */
197                 do {
198                         /* find member above ... */
199                         while(index[i].groupname < z) i++;
200
201                         /* find element below ... */
202                         while(index[j].groupname > z) j--;
203                         
204                         if(i <= j) {
205                                 /* swap rows*/
206                                 for (int h = 0; h < numSeqs; h++) {
207                                         y = matrix[i][h];
208                                         matrix[i][h] = matrix[j][h]; 
209                                         matrix[j][h] = y;
210                                 }
211                                 
212                                 /* swap columns*/
213                                 for (int b = 0; b < numSeqs; b++) {
214                                         y = matrix[b][i];
215                                         matrix[b][i] = matrix[b][j]; 
216                                         matrix[b][j] = y;
217                                 }
218                                 
219                                 //swap map elements
220                                 z = index[i].groupname;
221                                 index[i].groupname = index[j].groupname;
222                                 index[j].groupname = z;
223                                 
224                                 name = index[i].seqName;
225                                 index[i].seqName = index[j].seqName;
226                                 index[j].seqName = name;
227
228                                 
229                                 i++; 
230                                 j--;
231                         }
232                 } while(i <= j);
233
234                 /* recurse */
235                 if(low < j) 
236                 sortGroups(low, j);
237
238                 if(i < high) 
239                 sortGroups(i, high); 
240
241         
242         }
243         catch(exception& e) {
244                 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
245                 exit(1);
246         }
247         catch(...) {
248                 cout << "An unknown error has occurred in the FullMatrix class function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
249                 exit(1);
250         }
251
252 }
253
254 /**************************************************************************/    
255 int FullMatrix::getNumSeqs(){ return numSeqs; }
256 /**************************************************************************/
257 //print out matrix
258 void FullMatrix::printMatrix(ostream& out) {
259         try{
260                 for (int i = 0; i < numSeqs; i++) {
261                         out << "row " << i << " group = " << index[i].groupname << " name = " << index[i].seqName << endl;
262                         for (int j = 0; j < numSeqs; j++) {
263                                 out << matrix[i][j] << " ";
264                         }
265                         out << endl;
266                 }
267         }
268         catch(exception& e) {
269                 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
270                 exit(1);
271         }
272         catch(...) {
273                 cout << "An unknown error has occurred in the FullMatrix class function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
274                 exit(1);
275         }
276
277 }
278 /**************************************************************************/
279