File: readphylipvector.cpp

package info (click to toggle)
mothur 1.33.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,248 kB
  • ctags: 12,231
  • sloc: cpp: 152,046; fortran: 665; makefile: 74; sh: 34
file content (190 lines) | stat: -rw-r--r-- 3,877 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 *  readphylipvector.cpp
 *  mothur
 *
 *  Created by westcott on 1/11/11.
 *  Copyright 2011 Schloss Lab. All rights reserved.
 *
 */

#include "readphylipvector.h"

/***********************************************************************/
ReadPhylipVector::ReadPhylipVector(string d) {
	try {
		m = MothurOut::getInstance();
		distFile = d;
	}
	catch(exception& e) {
		m->errorOut(e, "ReadPhylipVector", "ReadPhylipVector");
		exit(1);
	}
}
/***********************************************************************/
vector<string> ReadPhylipVector::read(vector< vector<double> >& matrix) {
	try {
		vector<string> names;
		
		ifstream in;
		m->openInputFile(distFile, in);
		
		//check whether matrix is square
		char d;
		int square = 1;
		int numSeqs;
		string name;
		
		string numTest;
		in >> numTest >> name;
		
		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); }
		else { convert(numTest, numSeqs); }
		
		while((d=in.get()) != EOF){
			
			//is d a number meaning its square
			if(isalnum(d)){ 
				square = 1; 
				break; 
			}
			
			//is d a line return meaning its lower triangle
			if(d == '\n'){
				square = 2;
				break;
			}
		}
		in.close();
		
		
		//reopen and read now that you know whether you are square
		ifstream f;
		m->openInputFile(distFile, f);
		
		int rank;
		f >> rank;
		
		names.resize(rank);
		matrix.resize(rank);
		if(square == 1){
			for(int i=0;i<rank;i++)
				matrix[i].resize(rank);
			for(int i=0;i<rank;i++) {
				f >> names[i];
				for(int j=0;j<rank;j++) {
					if (m->control_pressed) { return names; }
					
					f >> matrix[i][j];
					if (matrix[i][j] == -0.0000)
						matrix[i][j] = 0.0000;
				}
			}
		}
		else if(square == 2){
			for(int i=0;i<rank;i++){
				matrix[i].resize(rank);
			}
			matrix[0][0] = 0.0000;
			f >> names[0];
			for(int i=1;i<rank;i++){
				f >> names[i];
				matrix[i][i]=0.0000;
				for(int j=0;j<i;j++){
					if (m->control_pressed) { return names; }
					f >> matrix[i][j];
					if (matrix[i][j] == -0.0000)
						matrix[i][j] = 0.0000;
					matrix[j][i]=matrix[i][j];
				}
			}
		}
		f.close();
		
		return names;
	}
	catch(exception& e) {
		m->errorOut(e, "ReadPhylipVector", "read");
		exit(1);
	}
}
/***********************************************************************/
vector<string> ReadPhylipVector::read(vector<seqDist>& matrix) {
	try {
		vector<string> names;
		
		ifstream in;
		m->openInputFile(distFile, in);
		
		//check whether matrix is square
		char d;
		int square = 1;
		int numSeqs;
		string name;
		
		in >> numSeqs >> name; 
		
		while((d=in.get()) != EOF){
			
			//is d a number meaning its square
			if(isalnum(d)){ 
				square = 1; 
				break; 
			}
			
			//is d a line return meaning its lower triangle
			if(d == '\n'){
				square = 2;
				break;
			}
		}
		in.close();
		
		
		//reopen and read now that you know whether you are square
		ifstream f;
		m->openInputFile(distFile, f);
		
		int rank;
		float temp;
		f >> rank;
		
		names.resize(rank);
		if(square == 1){
			for(int i=0;i<rank;i++) {
				f >> names[i];
				for(int j=0;j<rank;j++) {
					if (m->control_pressed) { return names; }
					
					f >> temp;
					
					if (j < i) { //only save lt
						seqDist dist(i, j, temp);
						matrix.push_back(dist);
					}
				}
			}
		}
		else if(square == 2){
			f >> names[0];
			for(int i=1;i<rank;i++){
				f >> names[i];
				for(int j=0;j<i;j++){
					if (m->control_pressed) { return names; }
					f >> temp;
					seqDist dist(i, j, temp);
					matrix.push_back(dist);
				}
			}
		}
		f.close();
		
		return names;
	}
	catch(exception& e) {
		m->errorOut(e, "ReadPhylipVector", "read");
		exit(1);
	}
}
/***********************************************************************/