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