]> git.donarmstrong.com Git - mothur.git/blob - readcolumn.cpp
added sparseDistanceMatrix class. Modified cluster commands to use the new sparse...
[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         DMatrix->resize(nseqs);
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                     PDistCell value(itA->second, distance);
67                     
68                     
69                                         if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
70                                                 refRow = itA->second;
71                                                 refCol = itB->second;
72                                                 DMatrix->addCell(itB->second, value);
73                                         }
74                                         else if(refRow == itA->second && refCol == itB->second){
75                                                 lt = 0;
76                                         }
77                                         else{
78                                                 DMatrix->addCell(itB->second, value);
79                                         }
80                                 }
81                                 else if(itA->second < itB->second){
82                                         PDistCell value(itB->second, distance);
83                         
84                                         if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
85                                                 refRow = itA->second;
86                                                 refCol = itB->second;
87                                                 DMatrix->addCell(itA->second, value);
88                                         }
89                                         else if(refRow == itB->second && refCol == itA->second){
90                                                 lt = 0;
91                                         }
92                                         else{
93                                                 DMatrix->addCell(itA->second, value);
94                                         }
95                                 }
96                                 reading->update(itA->second * nseqs);
97                         }
98                         m->gobble(fileHandle);
99                 }
100
101                 if(lt == 0){  // oops, it was square
102         
103                         fileHandle.close();  //let's start over
104                         DMatrix->clear();  //let's start over
105                    
106                         m->openInputFile(distFile, fileHandle);  //let's start over
107
108                         while(fileHandle){
109                                 fileHandle >> firstName >> secondName >> distance;
110                                 
111                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
112                 
113                                 map<string,int>::iterator itA = nameMap->find(firstName);
114                                 map<string,int>::iterator itB = nameMap->find(secondName);
115                                 
116                                 if(itA == nameMap->end()){  m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1);  }
117                                 if(itB == nameMap->end()){  m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1);  }
118                                 
119                                 if (distance == -1) { distance = 1000000; }
120                                 else if (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                     PDistCell value(itA->second, distance);
124                                         DMatrix->addCell(itB->second, value);
125                                         reading->update(itA->second * nseqs);
126                                 }
127                 
128                                 m->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 ReadColumnMatrix::~ReadColumnMatrix(){}
150 /***********************************************************************/
151