]> git.donarmstrong.com Git - mothur.git/blob - readcolumn.cpp
Revert to previous commit
[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 = m->openInputFile(distFile, fileHandle);
18         sim = false;
19         
20 }
21 /***********************************************************************/
22
23 ReadColumnMatrix::ReadColumnMatrix(string df, bool s) : distFile(df){
24         
25         successOpen = m->openInputFile(distFile, fileHandle);
26         sim = s;
27 }
28
29 /***********************************************************************/
30
31 int ReadColumnMatrix::read(NameAssignment* nameMap){
32         try {           
33
34                 string firstName, secondName;
35                 float distance;
36                 int nseqs = nameMap->size();
37
38                 list = new ListVector(nameMap->getListVector());
39         
40                 Progress* reading = new Progress("Reading matrix:     ", nseqs * nseqs);
41
42                 int lt = 1;
43                 int refRow = 0; //we'll keep track of one cell - Cell(refRow,refCol) - and see if it's transpose
44                 int refCol = 0; //shows up later - Cell(refCol,refRow).  If it does, then its a square matrix
45
46                 //need to see if this is a square or a triangular matrix...
47         
48                 while(fileHandle && lt == 1){  //let's assume it's a triangular matrix...
49
50                 
51                         fileHandle >> firstName >> secondName >> distance;      // get the row and column names and distance
52                         
53                         if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
54         
55                         map<string,int>::iterator itA = nameMap->find(firstName);
56                         map<string,int>::iterator itB = nameMap->find(secondName);
57                                 
58                         if(itA == nameMap->end()){  m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1);  }
59                         if(itB == nameMap->end()){  m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1);  }
60
61                         if (distance == -1) { distance = 1000000; }
62                         else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
63                         
64                         if(distance < cutoff && itA != itB){
65                                 if(itA->second > itB->second){
66                                         PCell value(itA->second, itB->second, distance);
67                         
68                                         if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
69                                                 refRow = itA->second;
70                                                 refCol = itB->second;
71                                                 D->addCell(value);
72                                         }
73                                         else if(refRow == itA->second && refCol == itB->second){
74                                                 lt = 0;
75                                         }
76                                         else{
77                                                 D->addCell(value);
78                                         }
79                                 }
80                                 else if(itA->second < itB->second){
81                                         PCell value(itB->second, itA->second, distance);
82                         
83                                         if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
84                                                 refRow = itA->second;
85                                                 refCol = itB->second;
86                                                 D->addCell(value);
87                                         }
88                                         else if(refRow == itB->second && refCol == itA->second){
89                                                 lt = 0;
90                                         }
91                                         else{
92                                                 D->addCell(value);
93                                         }
94                                 }
95                                 reading->update(itA->second * nseqs);
96                         }
97                         m->gobble(fileHandle);
98                 }
99
100                 if(lt == 0){  // oops, it was square
101         
102                         fileHandle.close();  //let's start over
103                         D->clear();  //let's start over
104                    
105                         m->openInputFile(distFile, fileHandle);  //let's start over
106
107                         while(fileHandle){
108                                 fileHandle >> firstName >> secondName >> distance;
109                                 
110                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
111                 
112                                 map<string,int>::iterator itA = nameMap->find(firstName);
113                                 map<string,int>::iterator itB = nameMap->find(secondName);
114                                 
115                                 if(itA == nameMap->end()){  m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1);  }
116                                 if(itB == nameMap->end()){  m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1);  }
117                                 
118                                 if (distance == -1) { distance = 1000000; }
119                                 else if (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                                 m->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