-
Notifications
You must be signed in to change notification settings - Fork 0
/
delstop.d
177 lines (153 loc) · 5.81 KB
/
delstop.d
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
import std.file;
import std.stdio;
import std.string;
import std.algorithm;
import std.array;
import std.conv;
// contains taxa and pos
struct TaxaInfo {
string name;
int[] positions;
}
// include TaxaInfo and foundSpecialChar
struct FastaData {
TaxaInfo[] taxaList; // every taxa correspeonds to TaxaInfo
bool foundSpecialChar;
}
void backup(string filePath) {
string backupPath = filePath ~ ".bak";
copy(filePath, backupPath);
}
string replaceSpecialChars(string sequence, const(int)[] positions) {
char[] mutableSequence = sequence.dup; // change to editable
foreach (pos; positions) {
mutableSequence[pos - 1] = '-'; // replace `!` and `*` with `-`
}
sequence = mutableSequence.idup; // change to not editable
return sequence;
}
int[] findSpecialPositions(const string sequence) {
int[] positions;
foreach (i, char c; sequence) {
if (c == '*' || c == '!') {
positions ~= cast(int) (i + 1); // record `int` pos
}
}
return positions;
}
FastaData processFastaAA(string filePath) {
FastaData fastaData; // create struct
string[] lines = File(filePath).byLine().map!(line=>line.to!string).array; // read file
string currentTaxa;
string currentSequence;
string tmpContent;
for (int i = 0; i < lines.length; i++) {
if (lines[i].startsWith(">")) {
if (!currentTaxa.empty) {
// keep last sequence
TaxaInfo taxaInfo;
taxaInfo.name = currentTaxa;
taxaInfo.positions = findSpecialPositions(currentSequence);
fastaData.taxaList ~= taxaInfo; // add TaxaInfo to taxaList
if (taxaInfo.positions.length > 0) {
fastaData.foundSpecialChar = true; // check any special char
}
currentSequence = replaceSpecialChars(currentSequence, taxaInfo.positions); // replace
tmpContent ~= ">" ~ currentTaxa ~ "\n" ~ currentSequence ~ "\n";
currentSequence = ""; // clean prepare next
}
currentTaxa = lines[i][1..$]; // get taxa
} else {
currentSequence ~= lines[i]; // get seq
}
}
// the final one
if (!currentTaxa.empty) {
TaxaInfo taxaInfo;
taxaInfo.name = currentTaxa;
taxaInfo.positions = findSpecialPositions(currentSequence);
fastaData.taxaList ~= taxaInfo;
if (taxaInfo.positions.length > 0) {
fastaData.foundSpecialChar = true;
}
currentSequence = replaceSpecialChars(currentSequence, taxaInfo.positions);
}
if(fastaData.foundSpecialChar){
backup(filePath);
// write back
std.file.write(filePath, tmpContent);
}
return fastaData;
}
void processFastaNT(string filePath, const FastaData fastaData) {
string[] lines = File(filePath).byLine().map!(line=>line.to!string).array; // get fasta_nt
for (int i = 0; i < lines.length; i++) {
if (lines[i].startsWith(">")) {
string currentTaxa = lines[i][1..$];
auto taxaIndex = fastaData.taxaList.countUntil!(t => t.name == currentTaxa); // use countUntil find taxaName
if (taxaIndex != fastaData.taxaList.length) { // if find taxa
int[] positions = fastaData.taxaList[taxaIndex].positions.dup;
int lineIndex = i + 1;
string sequence;
while (lineIndex < lines.length && !lines[lineIndex].startsWith(">")) {
sequence ~= lines[lineIndex]; // get full
lineIndex++;
}
// replace
char[] mutableSequence = sequence.dup;
foreach (pos; positions) {
int startPos = 3 * pos - 2; // 3n-2
int endPos = 3 * pos; // 3n
if (startPos - 1 < mutableSequence.length) {
for (int j = startPos - 1; j < endPos && j < mutableSequence.length; j++) {
mutableSequence[j] = '-'; // 3n-2 to 3n '-'
}
}
}
sequence = mutableSequence.idup;
// write to fasta_nt
int sequencePos = 0;
lineIndex = i + 1;
while (lineIndex < lines.length && !lines[lineIndex].startsWith(">")) {
int len = cast(int) lines[lineIndex].length; // convert to int
lines[lineIndex] = sequence[sequencePos..sequencePos + len];
sequencePos += len;
lineIndex++;
}
}
}
}
// write back
std.file.write(filePath, lines.join("\n"));
}
void processFasta(string fasta_aa, string fasta_nt, string enableDelete){
// get pos from fasta_aa & modify
FastaData fastaData = processFastaAA(fasta_aa);
if (fastaData.foundSpecialChar) {
backup(fasta_nt); // backup fasta_nt
processFastaNT(fasta_nt, fastaData); // modify fasta_nt
}
if (enableDelete == "--delete"){ // delete the nt's gap to meet the requirement of trimal
if(!fastaData.foundSpecialChar){
backup(fasta_nt);
}
string content = readText(fasta_nt);
content = replace(content, "-", "");
std.file.write(fasta_nt, content);
}
}
void main(string[] args) {
if (args.length != 3 && args.length != 4) {
writeln("\t\t\tDelete StopCondon generated by Macse\n\t\t\t\tAuthor: Guoyi Zhang\n\t\tUsage: " ~ args[0] ~ " <fasta_aa> <fasta_nt> --delete\n\t\tNote: fasta_aa and fasta_nt should be macse output files\n\t\t--delete should be used when downstream software is tirmal");
return;
}
string fasta_aa = args[1];
string fasta_nt = args[2];
string enableDelete;
if(args.length>3){
enableDelete = args[3];
} else {
enableDelete = "";
}
processFasta(fasta_aa, fasta_nt, enableDelete);
}