| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <unistd.h> |
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
#include "bio.h" |
| 7 |
#include "sim4.H" |
| 8 |
|
| 9 |
// Reports the amount of sequence covered by ALL matches for that |
| 10 |
// sequence. Example: if sequence iid 4 has two matches, one |
| 11 |
// covering the first 30% and the second covering the last 30%, this |
| 12 |
// will report that sequence iid 4 is covered 60%. |
| 13 |
// |
| 14 |
// Takes no options, reads from stdin, writes to stdout. |
| 15 |
|
| 16 |
int |
| 17 |
main(int argc, char **argv) { |
| 18 |
uint32 covMax = 0; |
| 19 |
intervalList<uint64> **cov = 0L; |
| 20 |
uint32 *len = 0L; |
| 21 |
|
| 22 |
uint32 lastIID = 0; |
| 23 |
|
| 24 |
bool isRaw = false; |
| 25 |
bool isBlast = false; |
| 26 |
|
| 27 |
char *fastaname = 0L; |
| 28 |
char *covname = 0L; |
| 29 |
|
| 30 |
seqCache *F = 0L; |
| 31 |
|
| 32 |
FILE *C = stdout; |
| 33 |
|
| 34 |
int arg=1; |
| 35 |
int err=0; |
| 36 |
while (arg < argc) { |
| 37 |
if (strcmp(argv[arg], "-mask") == 0) { |
| 38 |
fastaname = argv[++arg]; |
| 39 |
|
| 40 |
} else if (strcmp(argv[arg], "-cov") == 0) { |
| 41 |
covname = argv[++arg]; |
| 42 |
|
| 43 |
} else if (strcmp(argv[arg], "-raw") == 0) { |
| 44 |
isRaw = true; |
| 45 |
|
| 46 |
} else if (strcmp(argv[arg], "-blast") == 0) { |
| 47 |
isBlast = true; |
| 48 |
|
| 49 |
} else { |
| 50 |
fprintf(stderr, "unknown arg: '%s'\n", argv[arg]); |
| 51 |
err++; |
| 52 |
} |
| 53 |
arg++; |
| 54 |
} |
| 55 |
if ((err) || (isatty(fileno(stdin)))) { |
| 56 |
fprintf(stderr, "usage: %s [-mask in.fasta] [-cov dat] [-raw | -blast] < sim4db-results\n", argv[0]); |
| 57 |
fprintf(stderr, " -mask Read sequences from in.fasta, lower-case mask\n"); |
| 58 |
fprintf(stderr, " any base with an alignment, write to out.fasta\n"); |
| 59 |
fprintf(stderr, " -cov Write coverage statistics to 'dat' instead of stdout\n"); |
| 60 |
fprintf(stderr, " -raw If present, assume the 'sim4db-results' are\n"); |
| 61 |
fprintf(stderr, " a space-separated list of 'iid begin end', one per line\n"); |
| 62 |
fprintf(stderr, " -blast Same idea as raw, expects 'UID.IID' for query id,\n"); |
| 63 |
fprintf(stderr, " blast format (-m) 9.\n"); |
| 64 |
fprintf(stderr, "\n"); |
| 65 |
fprintf(stderr, "Output on stdout is the masked sequence if -mask is specified,\n"); |
| 66 |
fprintf(stderr, "otherwise, it is the coverage statistics.\n"); |
| 67 |
fprintf(stderr, "\n"); |
| 68 |
fprintf(stderr, "-mask is almost a required option - we need it to get the length.\n"); |
| 69 |
fprintf(stderr, "of sequences with no mapping (100%% uncovered) and to get the\n"); |
| 70 |
fprintf(stderr, "number of sequences.\n"); |
| 71 |
fprintf(stderr, "\n"); |
| 72 |
if (isatty(fileno(stdin))) |
| 73 |
fprintf(stderr, "error: I cannot read polishes from the terminal!\n\n"); |
| 74 |
} |
| 75 |
|
| 76 |
if (fastaname) { |
| 77 |
C = 0L; |
| 78 |
F = new seqCache(fastaname); |
| 79 |
} |
| 80 |
|
| 81 |
if (covname) { |
| 82 |
errno = 0; |
| 83 |
C = fopen(covname, "w"); |
| 84 |
if (errno) |
| 85 |
fprintf(stderr, "Failed to open '%s' for write: %s\n", covname, strerror(errno)), exit(1); |
| 86 |
} |
| 87 |
|
| 88 |
covMax = 1024 * 1024; |
| 89 |
if (F) |
| 90 |
covMax = F->getNumberOfSequences(); |
| 91 |
cov = new intervalList<uint64> * [covMax]; |
| 92 |
len = new uint32 [covMax]; |
| 93 |
|
| 94 |
fprintf(stderr, "Found "uint32FMT" sequences in the input file.\n", covMax); |
| 95 |
|
| 96 |
for (uint32 i=0; i<covMax; i++) { |
| 97 |
cov[i] = 0L; |
| 98 |
len[i] = 0; |
| 99 |
} |
| 100 |
|
| 101 |
if (isRaw || isBlast) { |
| 102 |
char inLine[1024]; |
| 103 |
splitToWords S; |
| 104 |
|
| 105 |
while (!feof(stdin)) { |
| 106 |
fgets(inLine, 1024, stdin); |
| 107 |
S.split(inLine); |
| 108 |
|
| 109 |
uint32 iid=0, beg=0, end=0; |
| 110 |
|
| 111 |
if (isRaw) { |
| 112 |
iid = strtouint32(S[0], 0L); |
| 113 |
beg = strtouint32(S[1], 0L) - 1; // Convert to space-based |
| 114 |
end = strtouint32(S[2], 0L); |
| 115 |
} |
| 116 |
if (isBlast) { |
| 117 |
char *iii = S[0]; |
| 118 |
while ((*iii != '.') && (*iii)) |
| 119 |
iii++; |
| 120 |
iii++; |
| 121 |
if (*iii == 0) |
| 122 |
fprintf(stderr, "UID.IID error: '%s'\n", S[0]); |
| 123 |
|
| 124 |
iid = strtouint32(iii, 0L); |
| 125 |
beg = strtouint32(S[6], 0L) - 1; // Convert to space-based |
| 126 |
end = strtouint32(S[7], 0L); |
| 127 |
} |
| 128 |
|
| 129 |
if (iid >= covMax) { |
| 130 |
fprintf(stderr, "ERROR: Found iid "uint32FMT", but only allocated "uint32FMT" places!\n", |
| 131 |
iid, covMax); |
| 132 |
exit(1); |
| 133 |
} |
| 134 |
if (cov[iid] == 0L) { |
| 135 |
cov[iid] = new intervalList<uint64>; |
| 136 |
len[iid] = 0; |
| 137 |
} |
| 138 |
if (iid >= lastIID) { |
| 139 |
lastIID = iid + 1; |
| 140 |
} |
| 141 |
cov[iid]->add(beg, end-beg); |
| 142 |
} |
| 143 |
|
| 144 |
} else { |
| 145 |
sim4polishReader *R = new sim4polishReader("-"); |
| 146 |
sim4polish *p = 0L; |
| 147 |
|
| 148 |
while (R->nextAlignment(p)) { |
| 149 |
if (p->_estID > covMax) |
| 150 |
fprintf(stderr, "DIE! You have more sequences in your polishes than in your source!\n"), exit(1); |
| 151 |
|
| 152 |
if (p->_estID >= covMax) { |
| 153 |
fprintf(stderr, "ERROR: Found iid "uint32FMT", but only allocated "uint32FMT" places!\n", |
| 154 |
p->_estID, covMax); |
| 155 |
exit(1); |
| 156 |
} |
| 157 |
if (cov[p->_estID] == 0L) { |
| 158 |
cov[p->_estID] = new intervalList<uint64>; |
| 159 |
len[p->_estID] = p->_estLen; |
| 160 |
} |
| 161 |
if (p->_estID >= lastIID) { |
| 162 |
lastIID = p->_estID + 1; |
| 163 |
} |
| 164 |
|
| 165 |
for (uint32 e=0; e<p->_numExons; e++) { |
| 166 |
p->_exons[e]._estFrom--; // Convert to space-based |
| 167 |
|
| 168 |
if (p->_matchOrientation == SIM4_MATCH_FORWARD) |
| 169 |
cov[p->_estID]->add(p->_exons[e]._estFrom, |
| 170 |
p->_exons[e]._estTo - p->_exons[e]._estFrom); |
| 171 |
else |
| 172 |
cov[p->_estID]->add(p->_estLen - p->_exons[e]._estTo, |
| 173 |
p->_exons[e]._estTo - p->_exons[e]._estFrom); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
// Scan the list of intervalLists, compute the amount covered, print. |
| 180 |
// |
| 181 |
for (uint32 iid=0; iid<lastIID; iid++) { |
| 182 |
|
| 183 |
// Argh! If there are no intervals, we need to report the whole |
| 184 |
// sequence is uncovered! |
| 185 |
|
| 186 |
uint32 numRegions = 0; |
| 187 |
uint32 sumLengths = 0; |
| 188 |
uint32 l, h; |
| 189 |
|
| 190 |
// Save the number of regions and the sum of their lengths, |
| 191 |
// then merge regions |
| 192 |
// |
| 193 |
if (cov[iid]) { |
| 194 |
numRegions = cov[iid]->numberOfIntervals(); |
| 195 |
sumLengths = cov[iid]->sumOfLengths(); |
| 196 |
cov[iid]->merge(); |
| 197 |
} |
| 198 |
|
| 199 |
if (F) { |
| 200 |
seqInCore *S = F->getSequenceInCore(iid); |
| 201 |
|
| 202 |
if (len[iid] == 0) |
| 203 |
len[iid] = S->sequenceLength(); |
| 204 |
|
| 205 |
assert(len[iid] == S->sequenceLength()); |
| 206 |
|
| 207 |
char *seq = new char [len[iid] + 1]; |
| 208 |
strcpy(seq, S->sequence()); |
| 209 |
|
| 210 |
for (uint32 p=0; p<len[iid]; p++) |
| 211 |
seq[p] = toUpper[seq[p]]; |
| 212 |
|
| 213 |
if (cov[iid]) { |
| 214 |
for (uint32 c=0; c<cov[iid]->numberOfIntervals(); c++) { |
| 215 |
l = cov[iid]->lo(c); |
| 216 |
h = cov[iid]->hi(c); |
| 217 |
|
| 218 |
if (h > len[iid]) { |
| 219 |
fprintf(stderr, "ERROR: range "uint32FMT"-"uint32FMT" out of bounds (seqLen = "uint32FMT")\n", |
| 220 |
l, h, len[iid]); |
| 221 |
assert(h <= len[iid]); |
| 222 |
} |
| 223 |
|
| 224 |
for (uint32 p=l; p<h; p++) |
| 225 |
//seq[p] = toLower[seq[p]]; |
| 226 |
seq[p] = 'N'; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
fprintf(stdout, "%s\n%s\n", S->header(), seq); |
| 231 |
|
| 232 |
delete [] seq; |
| 233 |
delete S; |
| 234 |
} |
| 235 |
|
| 236 |
if (C) { |
| 237 |
double percentCovered = 0.00; |
| 238 |
|
| 239 |
if (cov[iid]) |
| 240 |
percentCovered = cov[iid]->sumOfLengths() / (double)len[iid]; |
| 241 |
|
| 242 |
fprintf(C, uint32FMT"\t"uint32FMT"\t%5.3f\t"uint32FMT"\t"uint32FMT"\n", |
| 243 |
iid, |
| 244 |
len[iid], |
| 245 |
percentCovered, |
| 246 |
numRegions, |
| 247 |
sumLengths); |
| 248 |
} |
| 249 |
} |
| 250 |
} |