]> git.donarmstrong.com Git - mothur.git/blob - fullmatrix.cpp
6ea2318d740003f8cf46ec7e63815c175e1893ca
[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                 //sort sequences so they are gathered in groups for processing
60                 sortGroups(0, numSeqs-1);
61                         
62         }
63         catch(exception& e) {
64                 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
65                 exit(1);
66         }
67         catch(...) {
68                 cout << "An unknown error has occurred in the FullMatrix class function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
69                 exit(1);
70         }
71 }
72 /**************************************************************************/
73 void FullMatrix::readSquareMatrix(ifstream& filehandle) {
74         try {
75         
76                 Progress* reading;
77                 reading = new Progress("Reading matrix:    ", numSeqs * numSeqs);
78                 
79                 int count = 0;
80                 float distance;
81                 string group, name;
82                 
83                 for(int i=1;i<numSeqs;i++){
84                         filehandle >> name;             
85                         
86                         group = groupmap->getGroup(name);
87                         index[i].groupname = group;
88                         index[i].seqName = name;
89                         
90                         if(group == "not found") {      cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
91                                 
92                         for(int j=0;j<numSeqs;j++){
93                                 filehandle >> distance;
94                                         
95                                 matrix[i][j] = distance;
96                                 count++;
97                                 reading->update(count);
98                         }
99                 }
100                 reading->finish();
101                 delete reading;
102         }
103         catch(exception& e) {
104                 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105                 exit(1);
106         }
107         catch(...) {
108                 cout << "An unknown error has occurred in the FullMatrix class function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
109                 exit(1);
110         }
111
112
113 /**************************************************************************/
114 void FullMatrix::readLTMatrix(ifstream& filehandle) {
115         try {
116                 Progress* reading;
117                 reading = new Progress("Reading matrix:    ", numSeqs * (numSeqs - 1) / 2);
118                 
119                 int count = 0;
120                 float distance;
121                 string group, name;
122                 
123                 for(int i=1;i<numSeqs;i++){
124                         filehandle >> name;             
125                                                 
126                         group = groupmap->getGroup(name);
127                         index[i].groupname = group;
128                         index[i].seqName = name;
129         
130                         if(group == "not found") {      cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl;  exit(1); }
131                                 
132                         for(int j=0;j<i;j++){
133                                 filehandle >> distance;
134                                         
135                                 matrix[i][j] = distance;  matrix[j][i] = distance;
136                                 count++;
137                                 reading->update(count);
138                         }
139                 }
140                 reading->finish();
141                 delete reading;
142         }
143         catch(exception& e) {
144                 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
145                 exit(1);
146         }
147         catch(...) {
148                 cout << "An unknown error has occurred in the FullMatrix class function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
149                 exit(1);
150         }
151
152 }
153
154 /**************************************************************************/
155 void FullMatrix::sortGroups(int low, int high){
156         try{
157         
158                 int i = low;
159                 int j = high;
160                 int y = 0;
161                 string name;
162                 
163                 /* compare value */
164                 //what group does this row belong to
165                 string z = index[(low + high) / 2].groupname;
166
167                 /* partition */
168                 do {
169                         /* find member above ... */
170                         while(index[i].groupname < z) i++;
171
172                         /* find element below ... */
173                         while(index[j].groupname > z) j--;
174                         
175                         if(i <= j) {
176                                 /* swap rows*/
177                                 for (int h = 0; h < numSeqs; h++) {
178                                         y = matrix[i][h];
179                                         matrix[i][h] = matrix[j][h]; 
180                                         matrix[j][h] = y;
181                                 }
182                                 
183                                 /* swap columns*/
184                                 for (int b = 0; b < numSeqs; b++) {
185                                         y = matrix[b][i];
186                                         matrix[b][i] = matrix[b][j]; 
187                                         matrix[b][j] = y;
188                                 }
189                                 
190                                 //swap map elements
191                                 z = index[i].groupname;
192                                 index[i].groupname = index[j].groupname;
193                                 index[j].groupname = z;
194                                 
195                                 name = index[i].seqName;
196                                 index[i].seqName = index[j].seqName;
197                                 index[j].seqName = name;
198
199                                 
200                                 i++; 
201                                 j--;
202                         }
203                 } while(i <= j);
204
205                 /* recurse */
206                 if(low < j) 
207                 sortGroups(low, j);
208
209                 if(i < high) 
210                 sortGroups(i, high); 
211
212         
213         }
214         catch(exception& e) {
215                 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
216                 exit(1);
217         }
218         catch(...) {
219                 cout << "An unknown error has occurred in the FullMatrix class function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
220                 exit(1);
221         }
222
223 }
224
225 /**************************************************************************/    
226 int FullMatrix::getNumSeqs(){ return numSeqs; }
227 /**************************************************************************/
228 //print out matrix
229 void FullMatrix::printMatrix(ostream& out) {
230         try{
231                 for (int i = 0; i < numSeqs; i++) {
232                         out << "row " << i << " group = " << index[i].groupname << " name = " << index[i].seqName << endl;
233                         for (int j = 0; j < numSeqs; j++) {
234                                 out << matrix[i][j] << " ";
235                         }
236                         out << endl;
237                 }
238         }
239         catch(exception& e) {
240                 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
241                 exit(1);
242         }
243         catch(...) {
244                 cout << "An unknown error has occurred in the FullMatrix class function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
245                 exit(1);
246         }
247
248 }
249 /**************************************************************************/
250