forked from mothur/mothur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chopseqscommand.h
269 lines (205 loc) · 9.95 KB
/
chopseqscommand.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#ifndef CHOPSEQSCOMMAND_H
#define CHOPSEQSCOMMAND_H
/*
* chopseqscommand.h
* Mothur
*
* Created by westcott on 5/10/10.
* Copyright 2010 Schloss Lab. All rights reserved.
*
*/
#include "command.hpp"
#include "sequence.hpp"
class ChopSeqsCommand : public Command {
public:
ChopSeqsCommand(string);
ChopSeqsCommand();
~ChopSeqsCommand(){};
vector<string> setParameters();
string getCommandName() { return "chop.seqs"; }
string getCommandCategory() { return "Sequence Processing"; }
string getHelpString();
string getOutputPattern(string);
string getCitation() { return "http://www.mothur.org/wiki/Chops.seqs"; }
string getDescription() { return "trim sequence length"; }
int execute();
void help() { m->mothurOut(getHelpString()); }
private:
struct linePair {
unsigned long long start;
unsigned long long end;
linePair(unsigned long long i, unsigned long long j) : start(i), end(j) {}
};
string fastafile, outputDir, keep, namefile, groupfile, countfile;
bool abort, countGaps, Short;
int numbases, processors;
vector<string> outputNames;
string getChopped(Sequence);
bool driver (linePair, string, string, string);
bool createProcesses(vector<linePair>, string, string, string);
};
/**************************************************************************************************/
//custom data structure for threads to use.
// This is passed by void pointer so it can be any data type
// that can be passed using a single void pointer (LPVOID).
struct chopData {
string filename;
string outFasta, outAccnos, keep;
unsigned long long start;
unsigned long long end;
int numbases, count;
bool countGaps, Short, wroteAccnos;
MothurOut* m;
string namefile;
map<string, int> nameMap;
chopData(){}
chopData(string f, string ff, string a, MothurOut* mout, unsigned long long st, unsigned long long en, string k, bool cGaps, int nbases, bool S) {
filename = f;
outFasta = ff;
outAccnos = a;
m = mout;
start = st;
end = en;
keep = k;
countGaps = cGaps;
numbases = nbases;
Short = S;
wroteAccnos = false;
}
};
/**************************************************************************************************/
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
#else
static DWORD WINAPI MyChopThreadFunction(LPVOID lpParam){
chopData* pDataArray;
pDataArray = (chopData*)lpParam;
try {
ofstream out;
pDataArray->m->openOutputFile(pDataArray->outFasta, out);
ofstream outAcc;
pDataArray->m->openOutputFile(pDataArray->outAccnos, outAcc);
ifstream in;
pDataArray->m->openInputFile(pDataArray->filename, in);
if ((pDataArray->start == 0) || (pDataArray->start == 1)) {
in.seekg(0);
}else { //this accounts for the difference in line endings.
in.seekg(pDataArray->start-1); pDataArray->m->gobble(in);
}
bool done = false;
bool wroteAccnos = false;
pDataArray->count = 0;
for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
if (pDataArray->m->control_pressed) { in.close(); out.close(); outAcc.close(); pDataArray->m->mothurRemove(pDataArray->outFasta); pDataArray->m->mothurRemove(pDataArray->outAccnos); return 0; }
Sequence seq(in); pDataArray->m->gobble(in);
if (seq.getName() != "") {
//string newSeqString = getChopped(seq);
///////////////////////////////////////////////////////////////////////
string temp = seq.getAligned();
string tempUnaligned = seq.getUnaligned();
if (pDataArray->countGaps) {
//if needed trim sequence
if (pDataArray->keep == "front") {//you want to keep the beginning
int tempLength = temp.length();
if (tempLength > pDataArray->numbases) { //you have enough bases to remove some
int stopSpot = 0;
int numBasesCounted = 0;
for (int i = 0; i < temp.length(); i++) {
//eliminate N's
if (toupper(temp[i]) == 'N') { temp[i] = '.'; }
numBasesCounted++;
if (numBasesCounted >= pDataArray->numbases) { stopSpot = i; break; }
}
if (stopSpot == 0) { temp = ""; }
else { temp = temp.substr(0, stopSpot+1); }
}else {
if (!pDataArray->Short) { temp = ""; } //sequence too short
}
}else { //you are keeping the back
int tempLength = temp.length();
if (tempLength > pDataArray->numbases) { //you have enough bases to remove some
int stopSpot = 0;
int numBasesCounted = 0;
for (int i = (temp.length()-1); i >= 0; i--) {
//eliminate N's
if (toupper(temp[i]) == 'N') { temp[i] = '.'; }
numBasesCounted++;
if (numBasesCounted >= pDataArray->numbases) { stopSpot = i; break; }
}
if (stopSpot == 0) { temp = ""; }
else { temp = temp.substr(stopSpot+1); }
}else {
if (!pDataArray->Short) { temp = ""; } //sequence too short
}
}
}else{
//if needed trim sequence
if (pDataArray->keep == "front") {//you want to keep the beginning
int tempLength = tempUnaligned.length();
if (tempLength > pDataArray->numbases) { //you have enough bases to remove some
int stopSpot = 0;
int numBasesCounted = 0;
for (int i = 0; i < temp.length(); i++) {
//eliminate N's
if (toupper(temp[i]) == 'N') {
temp[i] = '.';
tempLength--;
if (tempLength < pDataArray->numbases) { stopSpot = 0; break; }
}
if(isalpha(temp[i])) { numBasesCounted++; }
if (numBasesCounted >= pDataArray->numbases) { stopSpot = i; break; }
}
if (stopSpot == 0) { temp = ""; }
else { temp = temp.substr(0, stopSpot+1); }
}else {
if (!pDataArray->Short) { temp = ""; } //sequence too short
}
}else { //you are keeping the back
int tempLength = tempUnaligned.length();
if (tempLength > pDataArray->numbases) { //you have enough bases to remove some
int stopSpot = 0;
int numBasesCounted = 0;
for (int i = (temp.length()-1); i >= 0; i--) {
//eliminate N's
if (toupper(temp[i]) == 'N') {
temp[i] = '.';
tempLength--;
if (tempLength < pDataArray->numbases) { stopSpot = 0; break; }
}
if(isalpha(temp[i])) { numBasesCounted++; }
if (numBasesCounted >= pDataArray->numbases) { stopSpot = i; break; }
}
if (stopSpot == 0) { temp = ""; }
else { temp = temp.substr(stopSpot); }
}else {
if (!pDataArray->Short) { temp = ""; } //sequence too short
}
}
}
string newSeqString = temp;
///////////////////////////////////////////////////////////////////////
//output trimmed sequence
if (newSeqString != "") {
out << ">" << seq.getName() << endl << newSeqString << endl;
}else{
outAcc << seq.getName() << endl;
pDataArray->wroteAccnos = true;
}
pDataArray->count++;
}
//report progress
if((pDataArray->count) % 1000 == 0){ pDataArray->m->mothurOut(toString(pDataArray->count)); pDataArray->m->mothurOutEndLine(); }
}
//report progress
if((pDataArray->count) % 1000 != 0){ pDataArray->m->mothurOut(toString(pDataArray->count)); pDataArray->m->mothurOutEndLine(); }
in.close();
out.close();
outAcc.close();
return 0;
}
catch(exception& e) {
pDataArray->m->errorOut(e, "ChopsSeqsCommand", "MyChopThreadFunction");
exit(1);
}
}
#endif
#endif