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