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