]> git.donarmstrong.com Git - mothur.git/blob - readphylipvector.cpp
fixed trim.seqs bug with qtrim parameter and added num=1 special case to database...
[mothur.git] / readphylipvector.cpp
1 /*
2  *  readphylipvector.cpp
3  *  mothur
4  *
5  *  Created by westcott on 1/11/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "readphylipvector.h"
11
12 /***********************************************************************/
13 ReadPhylipVector::ReadPhylipVector(string d) {
14         try {
15                 m = MothurOut::getInstance();
16                 distFile = d;
17         }
18         catch(exception& e) {
19                 m->errorOut(e, "ReadPhylipVector", "ReadPhylipVector");
20                 exit(1);
21         }
22 }
23 /***********************************************************************/
24 vector<string> ReadPhylipVector::read(vector< vector<double> >& matrix) {
25         try {
26                 vector<string> names;
27                 
28                 ifstream in;
29                 m->openInputFile(distFile, in);
30                 
31                 //check whether matrix is square
32                 char d;
33                 int square = 1;
34                 int numSeqs;
35                 string name;
36                 
37                 in >> numSeqs >> name; 
38                 
39                 while((d=in.get()) != EOF){
40                         
41                         //is d a number meaning its square
42                         if(isalnum(d)){ 
43                                 square = 1; 
44                                 break; 
45                         }
46                         
47                         //is d a line return meaning its lower triangle
48                         if(d == '\n'){
49                                 square = 2;
50                                 break;
51                         }
52                 }
53                 in.close();
54                 
55                 
56                 //reopen and read now that you know whether you are square
57                 ifstream f;
58                 m->openInputFile(distFile, f);
59                 
60                 int rank;
61                 f >> rank;
62                 
63                 names.resize(rank);
64                 matrix.resize(rank);
65                 if(square == 1){
66                         for(int i=0;i<rank;i++)
67                                 matrix[i].resize(rank);
68                         for(int i=0;i<rank;i++) {
69                                 f >> names[i];
70                                 for(int j=0;j<rank;j++) {
71                                         if (m->control_pressed) { return names; }
72                                         
73                                         f >> matrix[i][j];
74                                         if (matrix[i][j] == -0.0000)
75                                                 matrix[i][j] = 0.0000;
76                                 }
77                         }
78                 }
79                 else if(square == 2){
80                         for(int i=0;i<rank;i++){
81                                 matrix[i].resize(rank);
82                         }
83                         matrix[0][0] = 0.0000;
84                         f >> names[0];
85                         for(int i=1;i<rank;i++){
86                                 f >> names[i];
87                                 matrix[i][i]=0.0000;
88                                 for(int j=0;j<i;j++){
89                                         if (m->control_pressed) { return names; }
90                                         f >> matrix[i][j];
91                                         if (matrix[i][j] == -0.0000)
92                                                 matrix[i][j] = 0.0000;
93                                         matrix[j][i]=matrix[i][j];
94                                 }
95                         }
96                 }
97                 
98                 return names;
99         }
100         catch(exception& e) {
101                 m->errorOut(e, "ReadPhylipVector", "read");
102                 exit(1);
103         }
104 }
105 /***********************************************************************/
106 vector<string> ReadPhylipVector::read(vector<seqDist>& matrix) {
107         try {
108                 vector<string> names;
109                 
110                 ifstream in;
111                 m->openInputFile(distFile, in);
112                 
113                 //check whether matrix is square
114                 char d;
115                 int square = 1;
116                 int numSeqs;
117                 string name;
118                 
119                 in >> numSeqs >> name; 
120                 
121                 while((d=in.get()) != EOF){
122                         
123                         //is d a number meaning its square
124                         if(isalnum(d)){ 
125                                 square = 1; 
126                                 break; 
127                         }
128                         
129                         //is d a line return meaning its lower triangle
130                         if(d == '\n'){
131                                 square = 2;
132                                 break;
133                         }
134                 }
135                 in.close();
136                 
137                 
138                 //reopen and read now that you know whether you are square
139                 ifstream f;
140                 m->openInputFile(distFile, f);
141                 
142                 int rank;
143                 float temp;
144                 f >> rank;
145                 
146                 names.resize(rank);
147                 if(square == 1){
148                         for(int i=0;i<rank;i++) {
149                                 f >> names[i];
150                                 for(int j=0;j<rank;j++) {
151                                         if (m->control_pressed) { return names; }
152                                         
153                                         f >> temp;
154                                         
155                                         if (j < i) { //only save lt
156                                                 seqDist dist(i, j, temp);
157                                                 matrix.push_back(dist);
158                                         }
159                                 }
160                         }
161                 }
162                 else if(square == 2){
163                         f >> names[0];
164                         for(int i=1;i<rank;i++){
165                                 f >> names[i];
166                                 for(int j=0;j<i;j++){
167                                         if (m->control_pressed) { return names; }
168                                         f >> temp;
169                                         seqDist dist(i, j, temp);
170                                         matrix.push_back(dist);
171                                 }
172                         }
173                 }
174                 
175                 return names;
176         }
177         catch(exception& e) {
178                 m->errorOut(e, "ReadPhylipVector", "read");
179                 exit(1);
180         }
181 }
182 /***********************************************************************/
183
184