]> git.donarmstrong.com Git - mothur.git/blob - readphylipvector.cpp
added forward and reverse barcodes to trim.seqs to process illumina seqs
[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                 string numTest;
38                 in >> numTest >> name;
39                 
40                 if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ". I suspect you entered a column formatted file as a phylip file, quitting."); m->mothurOutEndLine(); exit(1); }
41                 else { convert(numTest, numSeqs); }
42                 
43                 while((d=in.get()) != EOF){
44                         
45                         //is d a number meaning its square
46                         if(isalnum(d)){ 
47                                 square = 1; 
48                                 break; 
49                         }
50                         
51                         //is d a line return meaning its lower triangle
52                         if(d == '\n'){
53                                 square = 2;
54                                 break;
55                         }
56                 }
57                 in.close();
58                 
59                 
60                 //reopen and read now that you know whether you are square
61                 ifstream f;
62                 m->openInputFile(distFile, f);
63                 
64                 int rank;
65                 f >> rank;
66                 
67                 names.resize(rank);
68                 matrix.resize(rank);
69                 if(square == 1){
70                         for(int i=0;i<rank;i++)
71                                 matrix[i].resize(rank);
72                         for(int i=0;i<rank;i++) {
73                                 f >> names[i];
74                                 for(int j=0;j<rank;j++) {
75                                         if (m->control_pressed) { return names; }
76                                         
77                                         f >> matrix[i][j];
78                                         if (matrix[i][j] == -0.0000)
79                                                 matrix[i][j] = 0.0000;
80                                 }
81                         }
82                 }
83                 else if(square == 2){
84                         for(int i=0;i<rank;i++){
85                                 matrix[i].resize(rank);
86                         }
87                         matrix[0][0] = 0.0000;
88                         f >> names[0];
89                         for(int i=1;i<rank;i++){
90                                 f >> names[i];
91                                 matrix[i][i]=0.0000;
92                                 for(int j=0;j<i;j++){
93                                         if (m->control_pressed) { return names; }
94                                         f >> matrix[i][j];
95                                         if (matrix[i][j] == -0.0000)
96                                                 matrix[i][j] = 0.0000;
97                                         matrix[j][i]=matrix[i][j];
98                                 }
99                         }
100                 }
101                 f.close();
102                 
103                 return names;
104         }
105         catch(exception& e) {
106                 m->errorOut(e, "ReadPhylipVector", "read");
107                 exit(1);
108         }
109 }
110 /***********************************************************************/
111 vector<string> ReadPhylipVector::read(vector<seqDist>& matrix) {
112         try {
113                 vector<string> names;
114                 
115                 ifstream in;
116                 m->openInputFile(distFile, in);
117                 
118                 //check whether matrix is square
119                 char d;
120                 int square = 1;
121                 int numSeqs;
122                 string name;
123                 
124                 in >> numSeqs >> name; 
125                 
126                 while((d=in.get()) != EOF){
127                         
128                         //is d a number meaning its square
129                         if(isalnum(d)){ 
130                                 square = 1; 
131                                 break; 
132                         }
133                         
134                         //is d a line return meaning its lower triangle
135                         if(d == '\n'){
136                                 square = 2;
137                                 break;
138                         }
139                 }
140                 in.close();
141                 
142                 
143                 //reopen and read now that you know whether you are square
144                 ifstream f;
145                 m->openInputFile(distFile, f);
146                 
147                 int rank;
148                 float temp;
149                 f >> rank;
150                 
151                 names.resize(rank);
152                 if(square == 1){
153                         for(int i=0;i<rank;i++) {
154                                 f >> names[i];
155                                 for(int j=0;j<rank;j++) {
156                                         if (m->control_pressed) { return names; }
157                                         
158                                         f >> temp;
159                                         
160                                         if (j < i) { //only save lt
161                                                 seqDist dist(i, j, temp);
162                                                 matrix.push_back(dist);
163                                         }
164                                 }
165                         }
166                 }
167                 else if(square == 2){
168                         f >> names[0];
169                         for(int i=1;i<rank;i++){
170                                 f >> names[i];
171                                 for(int j=0;j<i;j++){
172                                         if (m->control_pressed) { return names; }
173                                         f >> temp;
174                                         seqDist dist(i, j, temp);
175                                         matrix.push_back(dist);
176                                 }
177                         }
178                 }
179                 f.close();
180                 
181                 return names;
182         }
183         catch(exception& e) {
184                 m->errorOut(e, "ReadPhylipVector", "read");
185                 exit(1);
186         }
187 }
188 /***********************************************************************/
189
190