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