5 * Created by Sarah Westcott on 3/6/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "fullmatrix.h"
12 /**************************************************************************/
13 //This constructor reads a distance matrix file and stores the data in the matrix.
14 FullMatrix::FullMatrix(ifstream& filehandle) {
16 globaldata = GlobalData::getInstance();
17 groupmap = globaldata->gGroupmap;
20 filehandle >> numSeqs >> name;
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);
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;
33 //determine if matrix is square or lower triangle
34 //if it is square read the distances for the first sequence
36 while((d=filehandle.get()) != EOF){
38 //is d a number meaning its square
41 filehandle.putback(d);
43 for(int i=0;i<numSeqs;i++){
44 filehandle >> matrix[0][i];
49 //is d a line return meaning its lower triangle
57 if (square == true) { readSquareMatrix(filehandle); }
58 else { readLTMatrix(filehandle); }
60 //sort sequences so they are gathered in groups for processing
61 sortGroups(0, numSeqs-1);
65 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
69 cout << "An unknown error has occurred in the FullMatrix class function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
73 /**************************************************************************/
74 void FullMatrix::readSquareMatrix(ifstream& filehandle) {
78 reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
85 for(int i=1;i<numSeqs;i++){
88 group = groupmap->getGroup(name);
89 index[i].groupname = group;
90 index[i].seqName = name;
92 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
94 for(int j=0;j<numSeqs;j++){
95 filehandle >> distance;
97 matrix[i][j] = distance;
99 reading->update(count);
105 catch(exception& e) {
106 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
110 cout << "An unknown error has occurred in the FullMatrix class function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
115 /**************************************************************************/
116 void FullMatrix::readLTMatrix(ifstream& filehandle) {
119 reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
126 for(int i=1;i<numSeqs;i++){
129 group = groupmap->getGroup(name);
130 index[i].groupname = group;
131 index[i].seqName = name;
133 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
135 for(int j=0;j<i;j++){
136 filehandle >> distance;
138 matrix[i][j] = distance; matrix[j][i] = distance;
140 reading->update(count);
147 catch(exception& e) {
148 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
152 cout << "An unknown error has occurred in the FullMatrix class function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
158 /**************************************************************************/
159 void FullMatrix::sortGroups(int low, int high){
168 //what group does this row belong to
169 string z = index[(low + high) / 2].groupname;
173 /* find member above ... */
174 while(index[i].groupname < z) i++;
176 /* find element below ... */
177 while(index[j].groupname > z) j--;
181 for (int h = 0; h < numSeqs; h++) {
183 matrix[i][h] = matrix[j][h];
188 for (int b = 0; b < numSeqs; b++) {
190 matrix[b][i] = matrix[b][j];
195 z = index[i].groupname;
196 index[i].groupname = index[j].groupname;
197 index[j].groupname = z;
199 name = index[i].seqName;
200 index[i].seqName = index[j].seqName;
201 index[j].seqName = name;
218 catch(exception& e) {
219 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
223 cout << "An unknown error has occurred in the FullMatrix class function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
229 /**************************************************************************/
230 int FullMatrix::getNumSeqs(){ return numSeqs; }
231 /**************************************************************************/
233 void FullMatrix::printMatrix(ostream& out) {
235 for (int i = 0; i < numSeqs; i++) {
236 out << "row " << i << " group = " << index[i].groupname << " name = " << index[i].seqName << endl;
237 for (int j = 0; j < numSeqs; j++) {
238 out << matrix[i][j] << " ";
243 catch(exception& e) {
244 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
248 cout << "An unknown error has occurred in the FullMatrix class function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
254 /**************************************************************************/
255 void FullMatrix::getMinsForRowsVectors(){
257 numGroups = globaldata->gGroupmap->namesOfGroups.size();
259 //sort globaldata->gGroupmap.namesOfGroups so that it will match the matrix
260 sort(globaldata->gGroupmap->namesOfGroups.begin(), globaldata->gGroupmap->namesOfGroups.end());
262 /*************************************************/
263 //find where in matrix each group starts and stops
264 /*************************************************/
265 vector<int> bounds; //bounds[1] = starting row in matrix from group B, bounds[2] = starting row in matrix from group C, bounds[3] = no need to find upper bound of C because its numSeqs.
266 bounds.resize(numGroups);
269 bounds[numGroups] = numSeqs-1;
270 //for each group find bounds of subgroup/comparison
271 for (int i = 1; i < numGroups; i++) {
272 getBounds(bounds[i], globaldata->gGroupmap->namesOfGroups[i]);
275 /************************************************************/
276 //fill the minsForRows vectors for each group the user wants
277 /************************************************************/
278 int countx = bounds[1]; //where second group starts
279 int county = bounds[1];
281 //go through the entire matrix
282 for (int x = 0; x < numSeqs; x++) {
283 for (int y = 0; y < numSeqs; y++) {
284 //if have not changed groups
285 if ((x < countx) && (y < county)) {
295 catch(exception& e) {
296 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function getMinsForRowsVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
300 cout << "An unknown error has occurred in the FullMatrix class function getMinsForRowsVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
306 /**************************************************************************/
307 void FullMatrix::getBounds(int& higher, string group) {
309 bool gotLower = false;
311 //for each group find bounds of subgroup/comparison
312 for (it = index.begin(); it != index.end(); it++) {
313 if (it->second.groupname == group) {
314 if (gotLower != true) { gotLower = true; }
315 }else if ((gotLower == true) && (it->second.groupname != group)) { higher = it->first; break; }
319 catch(exception& e) {
320 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function getBounds. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
324 cout << "An unknown error has occurred in the FullMatrix class function getBounds. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";