-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortdiamond.cpp
245 lines (223 loc) · 5.22 KB
/
sortdiamond.cpp
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
238
239
240
241
242
243
244
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
const int n_intarr = 5;
bool use_bitscore = true;
// Struct to store the data fields
struct SeqData {
string sseqid;
int qstart;
int qend;
double bit_or_e;
string qseq;
};
// Function to generate reverse complement of a DNA sequence
string revcomp(const string &seq) {
string revseq;
for (auto it = seq.rbegin(); it != seq.rend(); ++it) {
switch (*it) {
case 'A':
revseq += 'T';
break;
case 'T':
revseq += 'A';
break;
case 'C':
revseq += 'G';
break;
case 'G':
revseq += 'C';
break;
case 'R':
revseq += 'Y';
break;
case 'Y':
revseq += 'R';
break;
case 'S':
revseq += 'S';
break;
case 'W':
revseq += 'W';
break;
case 'K':
revseq += 'M';
break;
case 'M':
revseq += 'K';
break;
case 'B':
revseq += 'V';
break;
case 'D':
revseq += 'H';
break;
case 'H':
revseq += 'D';
break;
case 'V':
revseq += 'B';
break;
case 'N':
revseq += 'N';
break;
default:
break;
}
}
return revseq;
}
void readInputFile(const string &filename, vector<SeqData> &data_vector,
const int *intnums, const int intmax) {
ifstream infile(filename);
if (!infile) {
cerr << "Error opening input file: " << filename << endl;
return;
}
string line;
while (getline(infile, line)) {
istringstream iss(line);
vector<string> fields(intmax + 1);
// string fields[intmax + 1];
int i = 0;
while (iss >> fields[i] && i < (intmax + 1)) {
i++;
}
// Extract fields
SeqData data;
data.sseqid = fields[intnums[0]];
data.qstart = stoi(fields[intnums[1]]);
data.qend = stoi(fields[intnums[2]]);
data.bit_or_e = stod(fields[intnums[3]]);
data.qseq = fields[intnums[4]];
// Store the data
data_vector.push_back(data);
}
infile.close();
}
void processCompare(const vector<SeqData> &data_vector,
map<string, SeqData> &best_map) {
for (const auto &data : data_vector) {
double bitE = data.bit_or_e;
const string &sseqid = data.sseqid;
if (use_bitscore) {
// bit score
// Check if the seqid already exists in the map
if (best_map.find(sseqid) != best_map.end()) {
// If the new bitscore is greater, update the
// map
if (bitE > best_map[sseqid].bit_or_e) {
best_map[sseqid] = data;
}
} else {
// If the seqid does not exist, insert the new
// seqid-bitE pair
best_map[sseqid] = data;
}
} else {
// evalue
// Check if the seqid already exists in the map
if (best_map.find(sseqid) != best_map.end()) {
// If the new evalue is greater, update the
// map
if (bitE < best_map[sseqid].bit_or_e) {
best_map[sseqid] = data;
}
} else {
// If the seqid does not exist, insert the new
// seqid-bitE pair
best_map[sseqid] = data;
}
}
}
}
void processRevert(const map<string, SeqData> &best_map,
vector<pair<string, string>> &result) {
for (const auto &entry : best_map) {
const SeqData &data = entry.second;
// Check if qstart is larger than qend
string qseq = data.qseq;
if (data.qstart > data.qend) {
qseq = revcomp(qseq);
}
result.push_back(make_pair(">" + data.sseqid, qseq));
}
sort(result.begin(), result.end());
}
void writeOutputFile(const string &filename,
const vector<pair<string, string>> &result) {
ofstream outfile(filename);
if (!outfile) {
cerr << "Error opening output file: " << filename << endl;
return;
}
for (const auto &entry : result) {
outfile << entry.first << "\n" << entry.second << "\n";
}
outfile.close();
}
void splitInts(const std::string &str, int intnums[n_intarr]) {
istringstream iss(str);
int numi = 0;
string tmpstr;
while (std::getline(iss, tmpstr, ',') && numi < n_intarr) {
intnums[numi] = std::stoi(tmpstr);
numi++;
}
}
int maxInts(int intnums[n_intarr]) {
int intmax = intnums[0];
for (int i = 1; i < n_intarr; i++) {
if (intnums[i] > intmax) {
intmax = intnums[i];
}
}
return intmax;
}
int main(int argc, char *argv[]) {
int intnums[n_intarr] = {1, 6, 7, 11, 17};
int intmax = 17;
if (argc == 4 || argc == 5) {
splitInts(argv[3], intnums);
intmax = maxInts(intnums);
if (argc == 5) {
string tmpstri = argv[4];
if (tmpstri == "bitscore") {
use_bitscore = true;
} else if (tmpstri == "evalue") {
use_bitscore = false;
} else {
cout << "Unknown argument: " << argv[4] << endl;
}
}
} else if (argc != 3) {
cerr << "sortDiamond\nAuthor: Guoyi "
"Zhang\nLicense:GPL-2.0-only\nUsage: "
<< argv[0]
<< " <input_file> <output_file> "
"<sseq,qstart,qend,bitscore/evalue,qseq> "
"<bitscore(default)/evalue>\nthe column number starts "
"at 0"
<< endl;
return 1;
}
if (argc <= 5 && argc >= 3) {
string in_name = argv[1];
string ot_name = argv[2];
// get useful information from reading input file
vector<SeqData> data_vector;
readInputFile(in_name, data_vector, intnums, intmax);
// calculate best bitscore or evalue
map<string, SeqData> best_map;
processCompare(data_vector, best_map);
// write the result
vector<pair<string, string>> result;
processRevert(best_map, result);
writeOutputFile(ot_name, result);
}
return 0;
}