]> git.donarmstrong.com Git - mothur.git/blob - readcluster.cpp
added checks for ^C to quit command instead of program
[mothur.git] / readcluster.cpp
1 /*
2  *  readcluster.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 10/28/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "readcluster.h"
11
12 /***********************************************************************/
13
14 ReadCluster::ReadCluster(string distfile, float c){
15                 globaldata = GlobalData::getInstance();
16                 m = MothurOut::getInstance();
17         distFile = distfile;
18                 cutoff = c;
19 }
20
21 /***********************************************************************/
22
23 int ReadCluster::read(NameAssignment* nameMap){
24         try {
25         
26                 if (format == "phylip") { convertPhylip2Column(nameMap); }
27                 else { list = new ListVector(nameMap->getListVector());  }
28                 
29                 if (m->control_pressed) { return 0; }
30                 
31                 OutPutFile = sortFile(distFile);
32                 
33                 return 0;
34                         
35         }
36         catch(exception& e) {
37                 m->errorOut(e, "ReadCluster", "read");
38                 exit(1);
39         }
40 }
41 /***********************************************************************/
42
43 int ReadCluster::convertPhylip2Column(NameAssignment* nameMap){
44         try {   
45                 //convert phylip file to column file
46                 map<int, string> rowToName;
47                 map<int, string>::iterator it;
48                 
49                 ifstream in;
50                 ofstream out;
51                 string tempFile = distFile + ".column.temp";
52                 
53                 openInputFile(distFile, in);
54                 openOutputFile(tempFile, out);
55                 
56                 float distance;
57                 int square, nseqs;
58                 string name;
59                 vector<string> matrixNames;
60         
61                 in >> nseqs >> name;
62                 rowToName[0] = name;
63                 matrixNames.push_back(name);
64                 
65                 if(nameMap == NULL){
66                         list = new ListVector(nseqs);
67                         list->set(0, name);
68                 }
69                 else{
70                         list = new ListVector(nameMap->getListVector());
71                         if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
72                 }
73         
74                 char d;
75                 while((d=in.get()) != EOF){
76                         
77                         if(isalnum(d)){
78                                 square = 1;
79                                 in.putback(d);
80                                 for(int i=0;i<nseqs;i++){
81                                         in >> distance;
82                                 }
83                                 break;
84                         }
85                         if(d == '\n'){
86                                 square = 0;
87                                 break;
88                         }
89                 }
90         
91                 if(square == 0){
92                                         
93                         for(int i=1;i<nseqs;i++){
94                                 in >> name;
95                                 rowToName[i] = name;
96                                 matrixNames.push_back(name);
97                                 
98                                 //there's A LOT of repeated code throughout this method...
99                                 if(nameMap == NULL){
100                                         list->set(i, name);
101                                         
102                                         for(int j=0;j<i;j++){
103                                         
104                                                 if (m->control_pressed) { in.close(); out.close(); remove(tempFile.c_str()); return 0; }
105                                                 
106                                                 in >> distance;
107                                                 
108                                                 if (distance == -1) { distance = 1000000; }
109                                                 
110                                                 if(distance < cutoff){
111                                                         out << i << '\t' << j << '\t' << distance << endl;
112                                                 }
113                                         }
114                                         
115                                 }
116                                 else{
117                                         if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
118                                         
119                                         for(int j=0;j<i;j++){
120                                                 
121                                                 if (m->control_pressed) { in.close(); out.close(); remove(tempFile.c_str()); return 0; }
122                                                 
123                                                 in >> distance;
124                                                 
125                                                 if (distance == -1) { distance = 1000000; }
126                                                 
127                                                 if(distance < cutoff){
128                                                         out << i << '\t' << j << '\t' << distance << endl;
129                                                 }
130                                         }
131                                 }
132                         }
133                 }
134                 else{
135                         for(int i=1;i<nseqs;i++){
136                                 in >> name;                
137                                 rowToName[i] = name;
138                                 matrixNames.push_back(name);
139                 
140                                 if(nameMap == NULL){
141                                         list->set(i, name);
142                                         for(int j=0;j<nseqs;j++){
143                                                 if (m->control_pressed) { in.close(); out.close(); remove(tempFile.c_str()); return 0; }
144                                                 
145                                                 in >> distance;
146                                         
147                                                 if (distance == -1) { distance = 1000000; }
148                                                 
149                                                 if(distance < cutoff && j < i){
150                                                         out << i << '\t' << j << '\t' << distance << endl;
151                                                 }
152                                         }
153                                 }
154                                 else{
155                                         if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
156                                         
157                                         for(int j=0;j<nseqs;j++){
158                                                 if (m->control_pressed) { in.close(); out.close(); remove(tempFile.c_str()); return 0; }
159                                                 
160                                                 in >> distance;
161                         
162                                                 if (distance == -1) { distance = 1000000; }
163                                                 
164                                                 if(distance < cutoff && j < i){
165                                                         out << i << '\t' << j << '\t' << distance << endl;
166                                                 }
167                                                 
168                                         }
169                                 }
170                         }
171                 }
172                 
173                 list->setLabel("0");
174                 in.close();
175                 out.close();
176                 
177                 if(nameMap == NULL){
178                         nameMap = new NameAssignment();
179                         for(int i=0;i<matrixNames.size();i++){
180                                 nameMap->push_back(matrixNames[i]);
181                         }
182                         globaldata->nameMap = nameMap;
183                 }
184                 
185         
186                 ifstream in2;
187                 ofstream out2;
188                 
189                 string outputFile = getRootName(distFile) + "column.dist";
190                 openInputFile(tempFile, in2);
191                 openOutputFile(outputFile, out2);
192                 
193                 int first, second;
194                 float dist;
195                 
196                 while (in2) {
197                         if (m->control_pressed) { in2.close(); out2.close(); remove(tempFile.c_str()); remove(outputFile.c_str()); return 0; }
198                         
199                         in2 >> first >> second >> dist;
200                         out2 << rowToName[first] << '\t' << rowToName[second] << '\t' << dist << endl;
201                         gobble(in2);
202                 }
203                 in2.close();
204                 out2.close();
205                 
206                 remove(tempFile.c_str());
207                 distFile = outputFile;
208                 
209                 if (m->control_pressed) {  remove(outputFile.c_str());  }
210
211                 return 0;
212         }
213         catch(exception& e) {
214                 m->errorOut(e, "ReadCluster", "convertPhylip2Column");
215                 exit(1);
216         }
217 }
218 /***********************************************************************/
219
220 ReadCluster::~ReadCluster(){}
221 /***********************************************************************/
222