-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseTribaseFiles.h
237 lines (216 loc) · 5.97 KB
/
parseTribaseFiles.h
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "mutation.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const int array[];
#ifdef __cplusplus
}
#endif
using namespace std;
// trim from start
static inline string <rim(string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))));
return s;
}
// trim from end
static inline std::string &rtrim(string &s) {
s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline string &trim(string &s) {
return ltrim(rtrim(s));
}
int getBaseDigit(char baseChar){
switch(baseChar){
case 'A':
return 0;
case 'C':
return 1;
case 'G':
return 2;
case 'T':
return 3;
default:
cout << "invalid base" << endl;
return -1;
}
}
char convertDigitToBase(int digit){
switch(digit){
case 0:
return 'A';
case 1:
return 'C';
case 2:
return 'G';
case 3:
return 'T';
default:
cout << "invalid base" << endl;
return -1;
}
}
int getBaseNum(const char* baseChar){
int baseLength = strlen(baseChar);
if(baseLength == 0) return -1;
else if(baseLength == 1) return getBaseDigit(baseChar[0]);
else{
int num = pow(10,baseLength);
for(int i = 0; i < baseLength; i++){
int digit = getBaseDigit(baseChar[i]);
num += digit*pow(10, baseLength - i - 1);
}
return num;
}
}
int getStrandNum(const char* strand){
switch(strand[0]){
case '+':
return 1;
case '-':
return -1;
default:
return 0;
}
}
struct Parser{
private:
vector<Individ> individs_;
vector<string> inFiles;
public:
vector<Individ> getIndivids(){
return individs_;
}
string getIthFileName(int i){
return inFiles[i];
}
Parser(string inputFiles){
//Load the file list in the input txt file
ifstream fileList(inputFiles);
if(!fileList){
cout<<"Error opening file list"<<endl;
system("pause");
return;
}
string line;
while(getline(fileList, line)){
inFiles.push_back(line);
cout << "file in:" << line << endl;
}
}
vector<Individ> readIndividsFromTribase(){
for(int fileInd = 0; fileInd < inFiles.size(); fileInd++){
ifstream inputFile(inFiles[fileInd]);
if(!inputFile.is_open()){
cout << "file cannot be opened" << endl;
return individs_;
}
int state = 0;
Individ current;
string lineTmp;
while(getline(inputFile, lineTmp)){
trim(lineTmp);
if(lineTmp.length() == 0) continue;
if(state == 0){
state = 1;
getline(inputFile, lineTmp);
}
if(state == 1){
string chromStr;
int pos;
string baseInStr;
string baseOutStr;
string tribaseStr;
string dummy;
string strandStr;
stringstream lineStream;
lineStream << lineTmp;
lineStream >> chromStr >> pos >> baseInStr >> baseOutStr >> tribaseStr >> dummy >> strandStr;
//cout << chromStr << " " << pos << " " << baseInStr << " " << baseOutStr << " "<< tribaseStr << " " << strandStr << endl;
int chrom;
if(chromStr == "X") chrom = 23;
if(chromStr == "Y") chrom = 24;
else chrom = atoi(chromStr.c_str());
int baseIn = getBaseNum(baseInStr.c_str());
int baseOut = getBaseNum(baseOutStr.c_str());
int tribase = getBaseNum(tribaseStr.c_str());
int strand = getStrandNum(strandStr.c_str());
Mutation mut(baseIn, baseOut, pos, chrom, strand, tribase);
current.addMut(mut);
}
//cout << "nMuts" << current.getNMuts() << endl;
}
individs_.push_back(current);
}
individs_.size();
return individs_;
}
vector<Individ> readIndividsSnvMnvVcf(){
for(int fileInd = 0; fileInd < inFiles.size(); fileInd++){
ifstream inputFile(inFiles[fileInd]);
int state = 0;
Individ current;
vector<Mutation> muts;
string lineTmp;
while(getline(inputFile, lineTmp)){
//cout << lineTmp << endl;
trim(lineTmp);
if(lineTmp.length() == 0) continue;
if(string::npos != lineTmp.find("#CHROM")){
state = 1;
getline(inputFile, lineTmp);
}
if(state == 1){
string chromStr;
int pos;
string baseInStr;
string baseOutStr;
string strandStr;
string dummy1;
string dummy2;
string dummy3;
string dummy4;
stringstream lineStream;
lineStream << lineTmp;
lineStream >> chromStr >> pos >> dummy1 >> baseInStr >> baseOutStr >> dummy2 >> dummy3 >> dummy4;
int chrom;
if(chromStr == "X") chrom = 23;
else if(chromStr == "Y") chrom = 24;
else chrom = atoi(chromStr.c_str());
if(chrom == 0){
cout << "chrom=" << chrom << endl;
cout << chromStr << " " << pos << " " << dummy1 << " " << " " << baseInStr << " " << baseOutStr << " " << dummy2 << " " << dummy3 << " " << dummy4 << endl;
}
int baseIn = getBaseNum(baseInStr.c_str());
int baseOut = getBaseNum(baseOutStr.c_str());
Mutation mut(baseIn, baseOut, pos, chrom);
muts.push_back(mut);
//current.addMut(mut);
//cout << "nMut for loop=" << current.getNMuts() << endl;
}
}
std::sort(muts.begin(), muts.end(), less_than_key());
cout << 2 << endl;
current.setMuts(muts);
muts.clear();
//cout << "nMuts after loop=" << current.getNMuts() << endl;
individs_.push_back(current);
//cout << "size individs = " << individs_.size() << endl;
}
return individs_;
}
};