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