]> git.donarmstrong.com Git - mothur.git/blob - readphylip.cpp
added checks for ^C to quit command instead of program
[mothur.git] / readphylip.cpp
1 /*
2  *  readphylip.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 "readphylip.h"
11 #include "progress.hpp"
12
13 /***********************************************************************/
14
15 ReadPhylipMatrix::ReadPhylipMatrix(string distFile){
16         
17         successOpen = openInputFile(distFile, fileHandle);
18         
19 }
20
21 /***********************************************************************/
22
23 int ReadPhylipMatrix::read(NameAssignment* nameMap){
24         try {
25         
26                         float distance;
27                         int square, nseqs;
28                         string name;
29                         vector<string> matrixNames;
30         
31                         fileHandle >> nseqs >> name;
32
33                         matrixNames.push_back(name);
34
35                         if(nameMap == NULL){
36                                 list = new ListVector(nseqs);
37                                 list->set(0, name);
38                         }
39                         else{
40                                 list = new ListVector(nameMap->getListVector());
41                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
42                         }
43         
44                         char d;
45                         while((d=fileHandle.get()) != EOF){
46                 
47                                 if(isalnum(d)){
48                                         square = 1;
49                                         fileHandle.putback(d);
50                                         for(int i=0;i<nseqs;i++){
51                                                 fileHandle >> distance;
52                                         }
53                                         break;
54                                 }
55                                 if(d == '\n'){
56                                         square = 0;
57                                         break;
58                                 }
59                         }
60         
61                         Progress* reading;
62       
63                         if(square == 0){
64
65                                 reading = new Progress("Reading matrix:     ", nseqs * (nseqs - 1) / 2);
66                 
67                                 int        index = 0;
68                
69                                 for(int i=1;i<nseqs;i++){
70                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
71                                                                                 
72                                         fileHandle >> name;
73                                         matrixNames.push_back(name);
74                                                 
75         
76                                         //there's A LOT of repeated code throughout this method...
77                                         if(nameMap == NULL){
78                                                 list->set(i, name);
79                                         
80                                                 for(int j=0;j<i;j++){
81                                                                                                 
82                                                                                                                 if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
83                                                                                                                 
84                                                         fileHandle >> distance;
85                                                                                         
86                                                 
87                                                         if (distance == -1) { distance = 1000000; }
88                                                 
89                                                         if(distance < cutoff){
90                                                                 PCell value(i, j, distance);
91                                                                 D->addCell(value);
92                                                         }
93                                                         index++;
94                                                         reading->update(index);
95                                                 }
96                                 
97                                         }
98                                         else{
99                                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
100                                 
101                                                 for(int j=0;j<i;j++){
102                                                         fileHandle >> distance;
103                                                                                                                 
104                                                                                                                 if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
105                                 
106                                                         if (distance == -1) { distance = 1000000; }
107                                                         
108                                                         if(distance < cutoff){
109                                                                 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
110                                                                 D->addCell(value);
111                                                         }
112                                                         index++;
113                                                         reading->update(index);
114                                                 }
115                                         }
116                                 }
117                         }
118                         else{
119
120                                 reading = new Progress("Reading matrix:     ", nseqs * nseqs);
121                         
122                                 int index = nseqs;
123                 
124                                 for(int i=1;i<nseqs;i++){
125                                         fileHandle >> name;                
126                                         matrixNames.push_back(name);
127                                                                                 
128                                                                                 
129         
130                                         if(nameMap == NULL){
131                                                 list->set(i, name);
132                                                 for(int j=0;j<nseqs;j++){
133                                                         fileHandle >> distance;
134                                                                                                                 
135                                                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
136                                                                                                                 
137                                                         if (distance == -1) { distance = 1000000; }
138                                                         
139                                                         if(distance < cutoff && j < i){
140                                                                 PCell value(i, j, distance);
141                                                                 D->addCell(value);
142                                                         }
143                                                         index++;
144                                                         reading->update(index);
145                                                 }
146                                         
147                                         }
148                                         else{
149                                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
150                                 
151                                                 for(int j=0;j<nseqs;j++){
152                                                         fileHandle >> distance;
153                                                                                                                 
154                                                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
155                                                                                                                 
156                                                         if (distance == -1) { distance = 1000000; }
157                                                         
158                                                         if(distance < cutoff && j < i){
159                                                                 PCell value(nameMap->get(matrixNames[i]), nameMap->get(matrixNames[j]), distance);
160                                                                 D->addCell(value);
161                                                         }
162                                                         index++;
163                                                         reading->update(index);
164                                                 }
165                                         }
166                                 }
167                         }
168                                                 
169                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
170                                                 
171                         reading->finish();
172                         delete reading;
173
174                         list->setLabel("0");
175                         fileHandle.close();
176
177                      /*   if(nameMap != NULL){
178                                 for(int i=0;i<matrixNames.size();i++){
179                                         nameMap->erase(matrixNames[i]);
180                                 }
181                                 if(nameMap->size() > 0){
182                                         //should probably tell them what is missing if we missed something
183                                         m->mothurOut("missed something\t" + toString(nameMap->size())); m->mothurOutEndLine();
184                                 }
185                         } */
186                                                 
187                                                 return 1;
188
189                 }
190         catch(exception& e) {
191                m->errorOut(e, "ReadPhylipMatrix", "read");
192                 exit(1);
193         }
194         }
195
196 /***********************************************************************/
197
198 ReadPhylipMatrix::~ReadPhylipMatrix(){
199        // delete D;
200        // delete list;
201 }