]> git.donarmstrong.com Git - mothur.git/blob - readcolumn.cpp
some changes while testing 1.9
[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 int 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                 
43                         fileHandle >> firstName >> secondName >> distance;      // get the row and column names and distance
44                         
45                         if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
46         
47                         map<string,int>::iterator itA = nameMap->find(firstName);
48                         map<string,int>::iterator itB = nameMap->find(secondName);
49                                 
50                         if(itA == nameMap->end()){
51                                 cerr << "AAError: Sequence '" << firstName << "' was not found in the names file, please correct\n"; exit(1);
52                         }
53                         if(itB == nameMap->end()){
54                                 cerr << "ABError: Sequence '" << secondName << "' was not found in the names file, please correct\n"; exit(1);
55                         }
56 //if (((itA->second == 8) && (itB->second == 1588)) || ((itA->second == 1588) && (itB->second == 8))) { cout << "found it" << endl; }
57
58                         if (distance == -1) { distance = 1000000; }
59                         else if (globaldata->sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
60                         
61                         if(distance < cutoff && itA != itB){
62                                 if(itA->second > itB->second){
63                                         PCell value(itA->second, itB->second, distance);
64                         
65                                         if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
66                                                 refRow = itA->second;
67                                                 refCol = itB->second;
68                                                 D->addCell(value);
69                                         }
70                                         else if(refRow == itA->second && refCol == itB->second){
71                                                 lt = 0;
72                                         }
73                                         else{
74                                                 D->addCell(value);
75                                         }
76                                 }
77                                 else if(itA->second < itB->second){
78                                         PCell value(itB->second, itA->second, distance);
79                         
80                                         if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
81                                                 refRow = itA->second;
82                                                 refCol = itB->second;
83                                                 D->addCell(value);
84                                         }
85                                         else if(refRow == itB->second && refCol == itA->second){
86                                                 lt = 0;
87                                         }
88                                         else{
89                                                 D->addCell(value);
90                                         }
91                                 }
92                                 reading->update(itA->second * nseqs);
93                         }
94                         gobble(fileHandle);
95                 }
96
97                 if(lt == 0){  // oops, it was square
98         
99                         fileHandle.close();  //let's start over
100                         D->clear();  //let's start over
101                    
102                         openInputFile(distFile, fileHandle);  //let's start over
103
104                         while(fileHandle){
105                                 fileHandle >> firstName >> secondName >> distance;
106                                 
107                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
108                 
109                                 map<string,int>::iterator itA = nameMap->find(firstName);
110                                 map<string,int>::iterator itB = nameMap->find(secondName);
111                                 
112                                 if(itA == nameMap->end()){
113                                         cerr << "BError: Sequence '" << firstName << "' was not found in the names file, please correct\n";
114                                 }
115                                 if(itB == nameMap->end()){
116                                         cerr << "BError: Sequence '" << secondName << "' was not found in the names file, please correct\n";
117                                 }
118                                 
119                                 if (distance == -1) { distance = 1000000; }
120                                 else if (globaldata->sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
121                                 
122                                 if(distance < cutoff && itA->second > itB->second){
123                                         PCell value(itA->second, itB->second, distance);
124                                         D->addCell(value);
125                                         reading->update(itA->second * nseqs);
126                                 }
127                 
128                                 gobble(fileHandle);
129                         }
130                 }
131                 
132                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
133                 
134                 reading->finish();
135                 fileHandle.close();
136
137                 list->setLabel("0");
138                 
139                 return 1;
140
141         }
142         catch(exception& e) {
143                 m->errorOut(e, "ReadColumnMatrix", "read");
144                 exit(1);
145         }
146 }
147
148 /***********************************************************************/
149
150 ReadColumnMatrix::~ReadColumnMatrix(){
151         //delete D;
152         //delete list;
153 }
154
155