]> git.donarmstrong.com Git - mothur.git/blob - readcolumn.cpp
This is v.1.4.0
[mothur.git] / readcolumn.cpp
1 /*
2  *  readcolumn.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 4/21/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "readcolumn.h"
11 #include "progress.hpp"
12
13 /***********************************************************************/
14
15 ReadColumnMatrix::ReadColumnMatrix(string df) : distFile(df){
16         
17         successOpen = openInputFile(distFile, fileHandle);
18         
19 }
20
21 /***********************************************************************/
22
23 void ReadColumnMatrix::read(NameAssignment* nameMap){
24         try {           
25
26                 string firstName, secondName;
27                 float distance;
28                 int nseqs = nameMap->size();
29
30                 list = new ListVector(nameMap->getListVector());
31         
32                 Progress* reading = new Progress("Reading matrix:     ", nseqs * nseqs);
33
34                 int lt = 1;
35                 int refRow = 0; //we'll keep track of one cell - Cell(refRow,refCol) - and see if it's transpose
36                 int refCol = 0; //shows up later - Cell(refCol,refRow).  If it does, then its a square matrix
37
38                 //need to see if this is a square or a triangular matrix...
39         
40                 while(fileHandle && lt == 1){  //let's assume it's a triangular matrix...
41                 
42                         fileHandle >> firstName >> secondName >> distance;      // get the row and column names and distance
43         
44                         map<string,int>::iterator itA = nameMap->find(firstName);
45                         map<string,int>::iterator itB = nameMap->find(secondName);
46                         
47                         if(itA == nameMap->end()){
48                                 cerr << "AAError: Sequence '" << firstName << "' was not found in the names file, please correct\n";
49                         }
50                         if(itB == nameMap->end()){
51                                 cerr << "ABError: Sequence '" << secondName << "' was not found in the names file, please correct\n";
52                         }
53
54                         if (distance == -1) { distance = 1000000; }
55                         
56                         if(distance < cutoff && itA != itB){
57                                 if(itA->second > itB->second){
58                                         PCell value(itA->second, itB->second, distance);
59                         
60                                         if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
61                                                 refRow = itA->second;
62                                                 refCol = itB->second;
63                                                 D->addCell(value);
64                                         }
65                                         else if(refRow == itA->second && refCol == itB->second){
66                                                 lt = 0;
67                                         }
68                                         else{
69                                                 D->addCell(value);
70                                         }
71                                 }
72                                 else if(itA->second < itB->second){
73                                         PCell value(itB->second, itA->second, distance);
74                         
75                                         if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
76                                                 refRow = itA->second;
77                                                 refCol = itB->second;
78                                                 D->addCell(value);
79                                         }
80                                         else if(refRow == itB->second && refCol == itA->second){
81                                                 lt = 0;
82                                         }
83                                         else{
84                                                 D->addCell(value);
85                                         }
86                                 }
87                                 reading->update(itA->second * nseqs);
88                         }
89                         gobble(fileHandle);
90                 }
91
92                 if(lt == 0){  // oops, it was square
93                         fileHandle.close();  //let's start over
94                         D->clear();  //let's start over
95                    
96                         openInputFile(distFile, fileHandle);  //let's start over
97
98                         while(fileHandle){
99                                 fileHandle >> firstName >> secondName >> distance;
100                 
101                                 map<string,int>::iterator itA = nameMap->find(firstName);
102                                 map<string,int>::iterator itB = nameMap->find(secondName);
103                                 
104                                 if(itA == nameMap->end()){
105                                         cerr << "BError: Sequence '" << firstName << "' was not found in the names file, please correct\n";
106                                 }
107                                 if(itB == nameMap->end()){
108                                         cerr << "BError: Sequence '" << secondName << "' was not found in the names file, please correct\n";
109                                 }
110                                 
111                                 if (distance == -1) { distance = 1000000; }
112                                 
113                                 if(distance < cutoff && itA->second > itB->second){
114                                         PCell value(itA->second, itB->second, distance);
115                                         D->addCell(value);
116                                         reading->update(itA->second * nseqs);
117                                 }
118                 
119                                 gobble(fileHandle);
120                         }
121                 }
122
123                 reading->finish();
124                 fileHandle.close();
125
126                 list->setLabel("0");
127
128         }
129         catch(exception& e) {
130                 cout << "Standard Error: " << e.what() << " has occurred in the ReadColumnMatrix class Function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
131                 exit(1);
132         }
133         catch(...) {
134                 cout << "An unknown error has occurred in the ReadColumnMatrix class function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
135                 exit(1);
136         }
137
138 }
139
140 /***********************************************************************/
141
142 ReadColumnMatrix::~ReadColumnMatrix(){
143         //delete D;
144         //delete list;
145 }
146
147