Debian Codegraph @

sim4dbutils/coveragehack.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "util++.H"
4 #include "bio++.H"
5 #include "sim4.H"
6
7 // g++ -o coveragehack coveragehack.C -I../libutil -I../libbio -I../libsim4 -L../libutil -L../libbio -L../libsim4 -lsim4 -lbio -lutil
8
9 // Flag that tells which side of the alignment our contaminated assembly is on.
10 // 1 (R) -- if atac the contaminant is on the left, the assembly is on the right
11 //
12 uint32 orientation = 1;
13
14 //
15 // WARNING! This is stale code. It does not compile. The fasta interface has changed.
16 //
17
18 void
19 readATAC(intervalList **coverage, char *path) {
20 char line[1024] = {0};
21 splitToWords S(line);
22
23 errno = 0;
24 FILE *F = fopen(path, "r");
25 if (errno)
26 fprintf(stderr, "Failed to open '%s': %s\n", path, strerror(errno)), exit(1);
27
28 while (!feof(F)) {
29 fgets(line, 1024, F);
30
31 if ((line[0] == 'M') && (line[2] == 'u')) {
32 S.split(line);
33
34 uint32 taglength = 0;
35 while (S[8][taglength] != ':')
36 taglength++;
37 uint32 idx = atoi(S[8] + taglength + 1);
38 uint32 beg = atoi(S[9]);
39 uint32 len = atoi(S[10]);
40
41 if (orientation == 2) {
42 while (S[4][taglength] != ':')
43 taglength++;
44 idx = atoi(S[4] + taglength + 1);
45 beg = atoi(S[5]);
46 len = atoi(S[6]);
47 }
48
49 if (coverage[idx] == 0L)
50 coverage[idx] = new intervalList();
51
52 coverage[idx]->add(beg, len);
53 }
54 }
55
56 fclose(F);
57 }
58
59
60 void
61 readSIM4(intervalList **coverage, int which, char *path) {
62
63 errno = 0;
64 FILE *F = fopen(path, "r");
65 if (errno)
66 fprintf(stderr, "Failed to open '%s': %s\n", path, strerror(errno)), exit(1);
67
68 while (!feof(F)) {
69 sim4polish *p = new sim4polish(F);
70
71 if (p) {
72
73 switch (which) {
74 case 1:
75 // The query are contaminant reads, the genomic is the assembly
76 if ((p->_percentIdentity >= 94) && (p->_querySeqIdentity >= 80)) {
77 uint32 idx = p->_genID;
78
79 if (coverage[idx] == 0L)
80 coverage[idx] = new intervalList();
81
82 coverage[idx]->add(p->_exons[0]._genFrom,
83 p->_exons[0]._genTo - p->_exons[0]._genFrom + 1);
84 }
85 break;
86 case 2:
87 // The query are assembly scaffolds, the genomic is the contaminant assembly (one or a few contigs)
88 //
89 uint32 idx = p->_estID;
90
91 if (coverage[idx] == 0L)
92 coverage[idx] = new intervalList();
93
94 if (p->_matchOrientation == SIM4_MATCH_FORWARD) {
95 coverage[idx]->add(p->_exons[0]._estFrom,
96 p->_exons[0]._estTo - p->_exons[0]._estFrom + 1);
97 } else {
98 coverage[idx]->add(p->_estLen - p->_exons[0]._estTo + 1,
99 p->_exons[0]._estTo - p->_exons[0]._estFrom + 1);
100 }
101 break;
102 }
103
104 delete p;
105 }
106 }
107 fclose(F);
108 }
109
110
111 #define MAXSCAFFOLD 200000
112
113 int
114 main(int argc, char **argv) {
115 intervalList **coverage = new intervalList* [MAXSCAFFOLD];
116 intervalList **gaps = new intervalList* [MAXSCAFFOLD];
117 FastAWrapper *W = 0L;
118 uint32 minCov = 80;
119
120 bool includeGapsAsContamination = true;
121
122 for (uint32 i=0; i<MAXSCAFFOLD; i++)
123 coverage[i] = 0L;
124
125 int arg=1;
126 while (arg < argc) {
127 if (strcmp(argv[arg], "-a") == 0) {
128 readATAC(coverage, argv[++arg]);
129 } else if (strcmp(argv[arg], "-s") == 0) {
130 // SNAPPER of query fragments onto the contaminant.
131 readSIM4(coverage, 1, argv[++arg]);
132 } else if (strcmp(argv[arg], "-S") == 0) {
133 // SNAPPER of query contaminant onto scaffolds
134 readSIM4(coverage, 2, argv[++arg]);
135 } else if (strcmp(argv[arg], "-f") == 0) {
136 W = new seqCache(argv[++arg]);
137 } else if (strcmp(argv[arg], "-c") == 0) {
138 minCov = strtouint32(argv[++arg], 0L);
139 } else if (strcmp(argv[arg], "-r") == 0) {
140 orientation = 1;
141 } else if (strcmp(argv[arg], "-l") == 0) {
142 orientation = 2;
143 } else if (strcmp(argv[arg], "-g") == 0) {
144 includeGapsAsContamination = false;
145 } else {
146 fprintf(stderr, "%s: unknown arg %s\n", argv[0], argv[arg]);
147 }
148 arg++;
149 }
150
151 if (W == 0L) {
152 fprintf(stderr, "usage: %s [-a atacmapping] [-s sim4db] [-g] -f seq.fasta\n", argv[0]);
153 fprintf(stderr, " -g don't count gaps in scaffolds as contamination.\n");
154 exit(1);
155 }
156
157 uint32 sumOfLengths = 0;
158 uint32 sequences = 0;
159
160 for (uint32 i=0; i<MAXSCAFFOLD; i++) {
161 if (coverage[i]) {
162
163 W->find(i);
164 FastASequenceInCore *S = W->getSequence();
165
166 intervalList gaps;
167
168 // Compute how much of the scaffold is gap.
169
170 uint32 gapBeg = W->sequenceLength(i);
171 char *seq = S->sequence();
172
173 for (uint32 beg=0, len=W->sequenceLength(i); beg<len; beg++) {
174 if ((seq[beg] == 'N') || (seq[beg] == 'n')) {
175 if (gapBeg > beg)
176 gapBeg = beg;
177 } else {
178 if (gapBeg < beg) {
179 gaps.add(gapBeg, beg-gapBeg);
180 gapBeg = W->sequenceLength(i);
181 }
182 }
183 }
184
185 // Geez! I suppose we could have just directly counted ACGT above!
186
187 gaps.merge();
188 coverage[i]->merge();
189
190 uint32 coveredLength = coverage[i]->sumOfLengths();
191 uint32 gapLength = gaps.sumOfLengths();
192 uint32 totalLength = W->sequenceLength(i) - gapLength;
193
194 if (100 * coveredLength > minCov * totalLength) {
195
196 sumOfLengths += coveredLength;
197 sequences++;
198
199 double cov = 100.0 * coveredLength / (double)totalLength;
200
201 fprintf(stderr, "sequence ["uint32FMT"] %s covered "uint32FMT" out of "uint32FMT" (%7.3f)\n",
202 i,
203 S->header(),
204 coveredLength,
205 totalLength,
206 cov);
207
208 delete S;
209 }
210
211 // Dump a special scaffold
212 if (i == 4796) {
213 for (uint32 z=0; z<coverage[i]->numberOfIntervals(); z++) {
214 fprintf(stderr, "interval[%3d] %6d - %6d\n", z, coverage[i]->lo(z), coverage[i]->hi(z));
215 }
216
217 }
218
219 }
220 }
221
222 fprintf(stderr, "Found "uint32FMT" bases in "uint32FMT" scaffolds.\n", sumOfLengths, sequences);
223 }
224