| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <errno.h> |
| 5 |
|
| 6 |
#include "sim4.H" |
| 7 |
#include "util++.H" |
| 8 |
|
| 9 |
int |
| 10 |
main(int argc, char **argv) { |
| 11 |
char *outPrefix = 0L; |
| 12 |
char datName[FILENAME_MAX]; |
| 13 |
char gnuName[FILENAME_MAX]; |
| 14 |
char pngName[FILENAME_MAX]; |
| 15 |
char gnuCmd[FILENAME_MAX]; |
| 16 |
char *inName = 0L; |
| 17 |
|
| 18 |
int arg = 1; |
| 19 |
int err = 0; |
| 20 |
while (arg < argc) { |
| 21 |
if (strncmp(argv[arg], "-o", 2) == 0) { |
| 22 |
outPrefix = argv[++arg]; |
| 23 |
|
| 24 |
} else if (strncmp(argv[arg], "-i", 2) == 0) { |
| 25 |
inName = argv[++arg]; |
| 26 |
|
| 27 |
} else { |
| 28 |
fprintf(stderr, "Unknown arg '%s'\n", argv[arg]); |
| 29 |
err++; |
| 30 |
} |
| 31 |
arg++; |
| 32 |
} |
| 33 |
if ((inName == 0L) || (outPrefix == 0L) || (err != 0)) { |
| 34 |
fprintf(stderr, "usage: %s -i sim4db -o outputPrefix\n", argv[0]); |
| 35 |
fprintf(stderr, "\n"); |
| 36 |
fprintf(stderr, " Creates outputPrefix.dat containing the number of errors at each\n"); |
| 37 |
fprintf(stderr, " base position, and outputPrefix.png the visual representation.\n"); |
| 38 |
fprintf(stderr, "\n"); |
| 39 |
fprintf(stderr, " Suggested usage:\n"); |
| 40 |
fprintf(stderr, "\n"); |
| 41 |
fprintf(stderr, " snapper2\n"); |
| 42 |
fprintf(stderr, " -queries Q.fasta\n"); |
| 43 |
fprintf(stderr, " -genomic G.fasta\n"); |
| 44 |
fprintf(stderr, " -positions G.posDB\n"); |
| 45 |
fprintf(stderr, " -aligns\n"); |
| 46 |
fprintf(stderr, " -minmatchidentity 94\n"); |
| 47 |
fprintf(stderr, " -minmatchcoverage 90\n"); |
| 48 |
fprintf(stderr, " -mersize 18\n"); |
| 49 |
fprintf(stderr, " -ignore 500\n"); |
| 50 |
fprintf(stderr, " -numthreads 16\n"); |
| 51 |
fprintf(stderr, " -verbose\n"); |
| 52 |
fprintf(stderr, " -output Q.sim4db\n"); |
| 53 |
fprintf(stderr, "\n"); |
| 54 |
fprintf(stderr, " pickBestPolish < Q.sim4db > Q.best.sim4db\n"); |
| 55 |
fprintf(stderr, "\n"); |
| 56 |
fprintf(stderr, " reportAlignmentDifferences\n"); |
| 57 |
fprintf(stderr, " -i Q.best.sim4db\n"); |
| 58 |
fprintf(stderr, " -o Q\n"); |
| 59 |
fprintf(stderr, "\n"); |
| 60 |
fprintf(stderr, "\n"); |
| 61 |
exit(1); |
| 62 |
} |
| 63 |
|
| 64 |
fprintf(stderr, "Reading input from '%s'\n", inName); |
| 65 |
fprintf(stderr, "Writing output to '%s'\n", outPrefix); |
| 66 |
|
| 67 |
// Open output files early, in case they fail. |
| 68 |
|
| 69 |
errno = 0; |
| 70 |
|
| 71 |
sprintf(datName, "%s.dat", outPrefix); |
| 72 |
sprintf(gnuName, "%s.gnuplot", outPrefix); |
| 73 |
sprintf(pngName, "%s.png", outPrefix); |
| 74 |
|
| 75 |
FILE *DAT = fopen(datName, "w"); |
| 76 |
if (errno) |
| 77 |
fprintf(stderr, "Failed to open '%s' for writing data: %s\n", datName, strerror(errno)), exit(1); |
| 78 |
|
| 79 |
FILE *GNU = fopen(gnuName, "w"); |
| 80 |
if (errno) |
| 81 |
fprintf(stderr, "Failed to open '%s' for writing gnuplot command: %s\n", gnuName, strerror(errno)), exit(1); |
| 82 |
|
| 83 |
// Read matches. |
| 84 |
|
| 85 |
uint32 lMax = 1048576; |
| 86 |
uint32 lLen = 0; |
| 87 |
|
| 88 |
uint32 *nTot = new uint32 [lMax]; |
| 89 |
uint32 *nIde = new uint32 [lMax]; |
| 90 |
uint32 *nMis = new uint32 [lMax]; |
| 91 |
uint32 *nIns = new uint32 [lMax]; |
| 92 |
uint32 *nDel = new uint32 [lMax]; |
| 93 |
|
| 94 |
memset(nTot, 0, sizeof(uint32) * lMax); |
| 95 |
memset(nIde, 0, sizeof(uint32) * lMax); |
| 96 |
memset(nMis, 0, sizeof(uint32) * lMax); |
| 97 |
memset(nIns, 0, sizeof(uint32) * lMax); |
| 98 |
memset(nDel, 0, sizeof(uint32) * lMax); |
| 99 |
|
| 100 |
sim4polishReader *R = new sim4polishReader(inName); |
| 101 |
sim4polish *p = 0L; |
| 102 |
|
| 103 |
while (R->nextAlignment(p)) { |
| 104 |
bool fwd = (p->_matchOrientation == SIM4_MATCH_FORWARD); |
| 105 |
|
| 106 |
for (uint32 exon=0; exon<p->_numExons; exon++) { |
| 107 |
sim4polishExon *e = p->_exons + exon; |
| 108 |
|
| 109 |
// Fail if there are no alignments. |
| 110 |
|
| 111 |
if ((e->_estAlignment == 0L) || |
| 112 |
(e->_genAlignment == 0L)) |
| 113 |
fprintf(stderr, "FAIL: Input has no alignment strings (-aligns option in snapper2).\n"), exit(1); |
| 114 |
|
| 115 |
// Parse the alignment to find ungapped blocks |
| 116 |
|
| 117 |
uint32 aPos = 0; // Position in the alignment |
| 118 |
uint32 qPos = e->_estFrom - 1; // Actual position in the query sequence |
| 119 |
uint32 gPos = e->_genFrom - 1; // Actual position in the genome sequence |
| 120 |
|
| 121 |
if (fwd == false) |
| 122 |
qPos = p->_estLen - e->_estFrom + 1; |
| 123 |
|
| 124 |
|
| 125 |
bool notDone = true; // There should be a way to get rid of this stupid variable.... |
| 126 |
while (notDone) { |
| 127 |
notDone = ((e->_estAlignment[aPos] != 0) && |
| 128 |
(e->_genAlignment[aPos] != 0)); |
| 129 |
|
| 130 |
// If we find the end of a gapless block, emit a match |
| 131 |
|
| 132 |
if (e->_estAlignment[aPos] == e->_genAlignment[aPos]) |
| 133 |
nIde[qPos]++; |
| 134 |
|
| 135 |
else if (e->_estAlignment[aPos] == '-') |
| 136 |
nDel[qPos]++; |
| 137 |
|
| 138 |
else if (e->_genAlignment[aPos] == '-') |
| 139 |
nIns[qPos]++; |
| 140 |
|
| 141 |
else |
| 142 |
nMis[qPos]++; |
| 143 |
|
| 144 |
nTot[qPos]++; |
| 145 |
|
| 146 |
assert(qPos < lMax); |
| 147 |
|
| 148 |
if (lLen < qPos) |
| 149 |
lLen = qPos; |
| 150 |
|
| 151 |
//fprintf(stdout, "%s "uint32FMT" %c ->_ %s "uint32FMT" %c\n", |
| 152 |
// p->_estDefLine, qPos, e->_estAlignment[aPos], |
| 153 |
// p->_genDefLine, gPos, e->_genAlignment[aPos]); |
| 154 |
|
| 155 |
if (e->_estAlignment[aPos] != '-') |
| 156 |
if (fwd) qPos++; |
| 157 |
else qPos--; |
| 158 |
if (e->_genAlignment[aPos] != '-') |
| 159 |
gPos++; |
| 160 |
|
| 161 |
aPos++; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
// Index |
| 168 |
// nTot |
| 169 |
// nIde, percent |
| 170 |
// nDel, percent |
| 171 |
// nIns, percent |
| 172 |
// nMis, percent |
| 173 |
|
| 174 |
fprintf(DAT, "#idx\tnTot\tnIde\tfrac\tnDel\tfrac\tnIns\tfrac\tnMis\tfrac\tnErr\tfrac\n"); |
| 175 |
for (uint32 i=0; i<=lLen; i++) |
| 176 |
fprintf(DAT, "%u\t%u\t%u\t%6.4f\t%u\t%6.4f\t%u\t%6.4f\t%u\t%6.4f\t%u\t%6.4f\n", |
| 177 |
i, |
| 178 |
nTot[i], |
| 179 |
nIde[i], (double)nIde[i] / nTot[i], |
| 180 |
nDel[i], (double)nDel[i] / nTot[i], |
| 181 |
nIns[i], (double)nIns[i] / nTot[i], |
| 182 |
nMis[i], (double)nMis[i] / nTot[i], |
| 183 |
nTot[i] - nIde[i], (double)(nTot[i] - nIde[i]) / nTot[i]); |
| 184 |
|
| 185 |
fprintf(GNU, "set terminal png\n"); |
| 186 |
fprintf(GNU, "set output \"%s\"\n", pngName); |
| 187 |
fprintf(GNU, "set title \"Fraction error per base for '%s'\"\n", inName); |
| 188 |
fprintf(GNU, "set xlabel \"Base position\"\n"); |
| 189 |
fprintf(GNU, "set ylabel \"Fraction error\"\n"); |
| 190 |
fprintf(GNU, "plot [][0:0.04] \\\n"); |
| 191 |
fprintf(GNU, " \"%s\" using 1:4 with lines title \"nTot\", \\\n", datName); |
| 192 |
fprintf(GNU, " \"%s\" using 1:6 with lines title \"nDel\", \\\n", datName); |
| 193 |
fprintf(GNU, " \"%s\" using 1:8 with lines title \"nIns\", \\\n", datName); |
| 194 |
fprintf(GNU, " \"%s\" using 1:10 with lines title \"nMis\", \\\n", datName); |
| 195 |
fprintf(GNU, " \"%s\" using 1:12 with lines title \"nErr\"\n", datName); |
| 196 |
|
| 197 |
fclose(DAT); |
| 198 |
fclose(GNU); |
| 199 |
|
| 200 |
sprintf(gnuCmd, "gnuplot < %s", gnuName); |
| 201 |
system(gnuCmd); |
| 202 |
|
| 203 |
return(0); |
| 204 |
} |
| 205 |
|