#include "fullmatrix.h"
/**************************************************************************/
+
//This constructor reads a distance matrix file and stores the data in the matrix.
FullMatrix::FullMatrix(ifstream& filehandle) {
try{
group = groupmap->getGroup(name);
if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
- index[0] = group;
+ index.resize(numSeqs);
+ index[0].groupName = group;
+ index[0].seqName = name;
//determine if matrix is square or lower triangle
//if it is square read the distances for the first sequence
char d;
+ bool square;
while((d=filehandle.get()) != EOF){
//is d a number meaning its square
if(isalnum(d)){
square = true;
filehandle.putback(d);
+
for(int i=0;i<numSeqs;i++){
filehandle >> matrix[0][i];
}
if (square == true) { readSquareMatrix(filehandle); }
else { readLTMatrix(filehandle); }
+ //sort sequences so they are gathered in groups for processing
+ sortGroups(0, numSeqs-1);
+ groups.push_back(index[0].groupName);
+ sizes.push_back(1);
+ int groupCount = 0;
- printMatrix(cout);
- //sort sequences so they are gathered in groups for processing
- sortGroups();
- cout << "after sort" << endl;
- printMatrix(cout);
+ for(int i=1;i<numSeqs;i++){
+ if(index[i].groupName == index[i-1].groupName){ sizes[groupCount]++; }
+ else{
+ sizes.push_back(1);
+ groups.push_back(index[i].groupName);
+ groupCount++;
+ }
+ }
}
catch(exception& e) {
try {
Progress* reading;
- reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
+ reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
int count = 0;
- float distance;
+
string group, name;
for(int i=1;i<numSeqs;i++){
filehandle >> name;
group = groupmap->getGroup(name);
- index[i] = group;
+ index[i].groupName = group;
+ index[i].seqName = name;
if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
for(int j=0;j<numSeqs;j++){
- filehandle >> distance;
-
- matrix[i][j] = distance;
+ filehandle >> matrix[i][j];
+
count++;
reading->update(count);
}
void FullMatrix::readLTMatrix(ifstream& filehandle) {
try {
Progress* reading;
- reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
+ reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
int count = 0;
float distance;
+
string group, name;
for(int i=1;i<numSeqs;i++){
filehandle >> name;
group = groupmap->getGroup(name);
- index[i] = group;
+ index[i].groupName = group;
+ index[i].seqName = name;
if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
count++;
reading->update(count);
}
+
}
reading->finish();
delete reading;
}
/**************************************************************************/
-void FullMatrix::sortGroups(){
- try{
- //sort each row by group and when you do, swap rows too.
- for (int i = 0; i < numSeqs; i++) {
- quicksort(0, numSeqs-1, i);
- }
- }
- catch(exception& e) {
- cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
- exit(1);
- }
- catch(...) {
- cout << "An unknown error has occurred in the FullMatrix class function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
- exit(1);
- }
-}
-/**************************************************************************/
-//this is a version of quicksort taken from http://www.c.happycodings.com/Sorting_Searching/code13.html
-/* sort everything inbetween `low' <-> `high' */
-void FullMatrix::quicksort(int low, int high, int row) {
- try {
+void FullMatrix::sortGroups(int low, int high){
+ try{
+
int i = low;
int j = high;
- int y = 0;
+ float y = 0;
+ string name;
/* compare value */
//what group does this row belong to
- string z = index[(low + high) / 2];
+ string z = index[(low + high) / 2].groupName;
/* partition */
do {
/* find member above ... */
- while(index[i] < z) i++;
+ while(index[i].groupName < z) i++;
/* find element below ... */
- while(index[j] > z) j--;
+ while(index[j].groupName > z) j--;
if(i <= j) {
- /* swap two elements in row*/
- y = matrix[row][i];
- matrix[row][i] = matrix[row][j];
- matrix[row][j] = y;
+ /* swap rows*/
+ for (int h = 0; h < numSeqs; h++) {
+ y = matrix[i][h];
+ matrix[i][h] = matrix[j][h];
+ matrix[j][h] = y;
+ }
- /* swap two elements in column*/
- y = matrix[i][row];
- matrix[i][row] = matrix[j][row];
- matrix[j][row] = y;
+ /* swap columns*/
+ for (int b = 0; b < numSeqs; b++) {
+ y = matrix[b][i];
+ matrix[b][i] = matrix[b][j];
+ matrix[b][j] = y;
+ }
//swap map elements
- z = index[i];
- index[i] = index[j];
- index[j] = z;
+ z = index[i].groupName;
+ index[i].groupName = index[j].groupName;
+ index[j].groupName = z;
+
+ name = index[i].seqName;
+ index[i].seqName = index[j].seqName;
+ index[j].seqName = name;
+
i++;
j--;
-//cout << "swapping elements " << i << " " << j << endl;
-//printMatrix(cout); cout << endl;
}
} while(i <= j);
/* recurse */
if(low < j)
- quicksort(low, j, row);
+ sortGroups(low, j);
if(i < high)
- quicksort(i, high, row);
+ sortGroups(i, high);
+
+
}
catch(exception& e) {
- cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function quicksort. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+ cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
exit(1);
}
catch(...) {
- cout << "An unknown error has occurred in the FullMatrix class function quicksort. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+ cout << "An unknown error has occurred in the FullMatrix class function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
exit(1);
}
+
}
/**************************************************************************/
-int FullMatrix::getNumSeqs(){ return numSeqs; }
+
+float FullMatrix::get(int i, int j){ return matrix[i][j]; }
+
+/**************************************************************************/
+
+vector<string> FullMatrix::getGroups(){ return groups; }
+
+/**************************************************************************/
+
+vector<int> FullMatrix::getSizes(){ return sizes; }
+
+/**************************************************************************/
+
+int FullMatrix::getNumGroups(){ return groups.size(); }
+
+/**************************************************************************/
+
+int FullMatrix::getNumSeqs(){ return numSeqs; }
+
/**************************************************************************/
-//print out matrix
+
void FullMatrix::printMatrix(ostream& out) {
try{
for (int i = 0; i < numSeqs; i++) {
- out << "row " << i << " group = " << index[i] << endl;
+ out << "row " << i << " group = " << index[i].groupName << " name = " << index[i].seqName << endl;
for (int j = 0; j < numSeqs; j++) {
out << matrix[i][j] << " ";
}
}
}
+
/**************************************************************************/