]> git.donarmstrong.com Git - mothur.git/blob - readphylipvector.cpp
reworked amova / homova / anosim
[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                 f.close();
98                 
99                 return names;
100         }
101         catch(exception& e) {
102                 m->errorOut(e, "ReadPhylipVector", "read");
103                 exit(1);
104         }
105 }
106 /***********************************************************************/
107 vector<string> ReadPhylipVector::read(vector<seqDist>& matrix) {
108         try {
109                 vector<string> names;
110                 
111                 ifstream in;
112                 m->openInputFile(distFile, in);
113                 
114                 //check whether matrix is square
115                 char d;
116                 int square = 1;
117                 int numSeqs;
118                 string name;
119                 
120                 in >> numSeqs >> name; 
121                 
122                 while((d=in.get()) != EOF){
123                         
124                         //is d a number meaning its square
125                         if(isalnum(d)){ 
126                                 square = 1; 
127                                 break; 
128                         }
129                         
130                         //is d a line return meaning its lower triangle
131                         if(d == '\n'){
132                                 square = 2;
133                                 break;
134                         }
135                 }
136                 in.close();
137                 
138                 
139                 //reopen and read now that you know whether you are square
140                 ifstream f;
141                 m->openInputFile(distFile, f);
142                 
143                 int rank;
144                 float temp;
145                 f >> rank;
146                 
147                 names.resize(rank);
148                 if(square == 1){
149                         for(int i=0;i<rank;i++) {
150                                 f >> names[i];
151                                 for(int j=0;j<rank;j++) {
152                                         if (m->control_pressed) { return names; }
153                                         
154                                         f >> temp;
155                                         
156                                         if (j < i) { //only save lt
157                                                 seqDist dist(i, j, temp);
158                                                 matrix.push_back(dist);
159                                         }
160                                 }
161                         }
162                 }
163                 else if(square == 2){
164                         f >> names[0];
165                         for(int i=1;i<rank;i++){
166                                 f >> names[i];
167                                 for(int j=0;j<i;j++){
168                                         if (m->control_pressed) { return names; }
169                                         f >> temp;
170                                         seqDist dist(i, j, temp);
171                                         matrix.push_back(dist);
172                                 }
173                         }
174                 }
175                 f.close();
176                 
177                 return names;
178         }
179         catch(exception& e) {
180                 m->errorOut(e, "ReadPhylipVector", "read");
181                 exit(1);
182         }
183 }
184 /***********************************************************************/
185
186