| 1 | // This file is part of A2Amapper. |
| 2 | // Copyright (c) 2005, 2006 J. Craig Venter Institute |
| 3 | // Author: Brian Walenz |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation; either version 2 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received (LICENSE.txt) a copy of the GNU General Public |
| 16 | // License along with this program; if not, write to the Free Software |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | |
| 19 | // Compute some simple statistics on a set of matches |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <time.h> |
| 24 | #include <math.h> |
| 25 | |
| 26 | #include "atac.H" |
| 27 | #include "util++.H" |
| 28 | #include "bio++.H" |
| 29 | #include "seqCache.H" |
| 30 | |
| 31 | bool noHistogramPlots = true; |
| 32 | |
| 33 | |
| 34 | // Sort uint32 backwards |
| 35 | int |
| 36 | uint32compare(const void *a, const void *b) { |
| 37 | const uint32 A = *((const uint32 *)a); |
| 38 | const uint32 B = *((const uint32 *)b); |
| 39 | if (A < B) return(1); |
| 40 | if (A > B) return(-1); |
| 41 | return(0); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | class histogram { |
| 46 | public: |
| 47 | histogram(uint64 blockSize, uint64 maxSize) { |
| 48 | _b = blockSize; |
| 49 | _m = maxSize; |
| 50 | _l = 0; |
| 51 | _h = new uint32 [maxSize / blockSize + 1]; |
| 52 | for (uint32 i=0; i<maxSize / blockSize + 1; i++) |
| 53 | _h[i] = 0; |
| 54 | _eLen = 0; |
| 55 | _eMax = 10240; |
| 56 | _e = new uint32 [_eMax]; |
| 57 | }; |
| 58 | ~histogram() { |
| 59 | delete [] _h; |
| 60 | delete [] _e; |
| 61 | }; |
| 62 | |
| 63 | void add(uint64 x) { |
| 64 | if (_eLen >= _eMax) { |
| 65 | _eMax *= 2; |
| 66 | uint32 *e = new uint32 [_eMax]; |
| 67 | memcpy(e, _e, sizeof(uint32) * _eLen); |
| 68 | delete [] _e; |
| 69 | _e = e; |
| 70 | } |
| 71 | _e[_eLen++] = x; |
| 72 | |
| 73 | if (x > _m) |
| 74 | _l++; |
| 75 | else |
| 76 | _h[x/_b]++; |
| 77 | }; |
| 78 | |
| 79 | void show(char const *label) { |
| 80 | double average = 0; |
| 81 | double stddev = 0; |
| 82 | |
| 83 | for (uint32 i=0; i<_eLen; i++) |
| 84 | average += _e[i]; |
| 85 | average /= _eLen; |
| 86 | |
| 87 | for (uint32 i=0; i<_eLen; i++) |
| 88 | stddev += (_e[i] - average) * (_e[i] - average); |
| 89 | stddev = sqrt(stddev / _eLen); |
| 90 | |
| 91 | fprintf(stdout, "histogram %s "uint32FMT" items %8.3f average %8.3f std.dev.\n", |
| 92 | label, _eLen, average, stddev); |
| 93 | }; |
| 94 | |
| 95 | void dump(char const *prefix, char const *label) { |
| 96 | if (noHistogramPlots) |
| 97 | return; |
| 98 | char filename[1024]; |
| 99 | sprintf(filename, "%s.%s.histogramdat", prefix, label); |
| 100 | FILE *out = fopen(filename, "w"); |
| 101 | for (uint64 i=0; i<_m / _b; i++) |
| 102 | fprintf(out, uint64FMT" "uint32FMT"\n", i * _b, _h[i]); |
| 103 | fprintf(out, ">"uint64FMT" "uint32FMT"\n", _m, _l); |
| 104 | fclose(out); |
| 105 | } |
| 106 | |
| 107 | void plot(char const *prefix, char const *label) { |
| 108 | |
| 109 | if (noHistogramPlots) |
| 110 | return; |
| 111 | |
| 112 | // Find max's of the data |
| 113 | uint64 maxx = 0; |
| 114 | uint64 maxy = 0; |
| 115 | |
| 116 | for (uint64 i=0; i<_m / _b; i++) { |
| 117 | if (_h[i] > 0) |
| 118 | maxx = i * _b; |
| 119 | if (maxy < _h[i]) |
| 120 | maxy = _h[i]; |
| 121 | } |
| 122 | |
| 123 | if ((maxx == 0) || (maxy == 0)) |
| 124 | return; |
| 125 | |
| 126 | char filename[1024]; |
| 127 | sprintf(filename, "%s.%s.histogram.gnuplot", prefix, label); |
| 128 | FILE *out = fopen(filename, "w"); |
| 129 | fprintf(out, "set terminal postscript color\n"); |
| 130 | fprintf(out, "set output \"%s.%s.histogram.ps\"\n", prefix, label); |
| 131 | fprintf(out, "set xlabel \"length bp\"\n"); |
| 132 | fprintf(out, "set ylabel \"number of matches\"\n"); |
| 133 | fprintf(out, "plot [0:"uint64FMT"][0:"uint64FMT"] \"%s.%s.histogramdat\" using 2 with lines\n", |
| 134 | maxx, maxy, prefix, label); |
| 135 | fprintf(out, "set output \"%s.%s.histogram.closeup.ps\"\n", prefix, label); |
| 136 | fprintf(out, "plot [0:"uint64FMT"][0:"uint64FMT"] \"%s.%s.histogramdat\" using 2 with lines\n", |
| 137 | maxx/10, maxy, prefix, label); |
| 138 | fprintf(out, "quit\n"); |
| 139 | fclose(out); |
| 140 | sprintf(filename, "gnuplot < %s.%s.histogram.gnuplot", prefix, label); |
| 141 | if (system(filename)) |
| 142 | fprintf(stderr, "Failed to execute '%s'\n", filename); |
| 143 | }; |
| 144 | |
| 145 | |
| 146 | private: |
| 147 | uint64 _b; // blockSize |
| 148 | uint64 _m; // maximum element size |
| 149 | uint32 _l; // number of things bigger than _m |
| 150 | uint32 *_h; // the histogram |
| 151 | uint32 _eMax; |
| 152 | uint32 _eLen; |
| 153 | uint32 *_e; // the elements -- for computing the stats; |
| 154 | }; |
| 155 | |
| 156 | |
| 157 | |
| 158 | // Compute the total gapped and ungapped length of the input |
| 159 | // sequences. Uses atacMatchList only to access the underlying fasta |
| 160 | // sequences. |
| 161 | // |
| 162 | void |
| 163 | totalLength(atacFile &AF, seqCache *A, seqCache *B) { |
| 164 | uint64 length1 = 0; |
| 165 | uint64 length2 = 0; |
| 166 | |
| 167 | for (uint32 i=0; i<A->getNumberOfSequences(); i++) |
| 168 | length1 += A->getSequenceLength(i); |
| 169 | for (uint32 i=0; i<B->getNumberOfSequences(); i++) |
| 170 | length2 += B->getSequenceLength(i); |
| 171 | |
| 172 | fprintf(stdout, "totalLength %s "uint64FMT" %s "uint64FMT" # all letters, including N\n", |
| 173 | AF.labelA(), length1, |
| 174 | AF.labelB(), length2); |
| 175 | |
| 176 | length1 = 0; |
| 177 | length2 = 0; |
| 178 | for (uint32 i=0; i<A->getNumberOfSequences(); i++) { |
| 179 | seqInCore *S = A->getSequenceInCore(i); |
| 180 | char *s = S->sequence(); |
| 181 | for (uint32 j=0; j<S->sequenceLength(); j++) |
| 182 | if (letterToBits[s[j]] != 0xff) |
| 183 | length1++; |
| 184 | } |
| 185 | for (uint32 i=0; i<B->getNumberOfSequences(); i++) { |
| 186 | seqInCore *S = B->getSequenceInCore(i); |
| 187 | char *s = S->sequence(); |
| 188 | for (uint32 j=0; j<S->sequenceLength(); j++) |
| 189 | if (letterToBits[s[j]] != 0xff) |
| 190 | length2++; |
| 191 | } |
| 192 | |
| 193 | fprintf(stdout, "totalLength %s "uint64FMT" %s "uint64FMT" # ACGT only\n", |
| 194 | AF.labelA(), length1, |
| 195 | AF.labelB(), length2); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | uint64 |
| 202 | tandemRepeatACGTLength(intervalList<uint64> &il, |
| 203 | uint64 *offset, |
| 204 | seqCache *A) { |
| 205 | |
| 206 | // s -- the sequence |
| 207 | // i -- the interval list index |
| 208 | |
| 209 | il.merge(); |
| 210 | uint64 length = 0; |
| 211 | uint64 unknown[256] = {0}; |
| 212 | for (uint32 i=0, s=0; i<il.numberOfIntervals(); i++) { |
| 213 | while ((offset[s + 1]) <= il.lo(i)) |
| 214 | s++; |
| 215 | |
| 216 | char *S = A->getSequenceInCore(s)->sequence(); |
| 217 | |
| 218 | uint64 lo = il.lo(i) - offset[s]; |
| 219 | uint64 hi = il.hi(i) - offset[s]; |
| 220 | |
| 221 | for (uint64 j=lo; j < hi; j++) |
| 222 | if (letterToBits[S[j]] != 0xff) |
| 223 | length++; |
| 224 | else |
| 225 | unknown[S[j]]++; |
| 226 | } |
| 227 | |
| 228 | //fprintf(stderr, "tandemRepeatACGTLength: "uint64FMT"\n", length); |
| 229 | //for (uint32 i=0; i<256; i++) |
| 230 | // if (unknown[i] > 0) |
| 231 | // fprintf(stderr, "tandemRepeatACGTLength["uint32FMT"] = "uint64FMT" (%c)\n", i, unknown[i], i); |
| 232 | |
| 233 | return(length); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | uint64 * |
| 238 | buildOffset(seqCache *F) { |
| 239 | uint64 *offset = new uint64 [F->getNumberOfSequences() + 1]; |
| 240 | offset[0] = 1000000; |
| 241 | for (uint32 i=0; i<F->getNumberOfSequences(); i++) |
| 242 | offset[i+1] = offset[i] + F->getSequenceLength(i) + 1; |
| 243 | return(offset); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void |
| 248 | tandemRepeatStats(atacFileStream &featuresA, |
| 249 | atacFileStream &featuresB, |
| 250 | atacFile &AF, |
| 251 | seqCache *A, |
| 252 | seqCache *B) { |
| 253 | intervalList<uint64> ifa, ifb; |
| 254 | intervalList<uint64> ima, imb; |
| 255 | intervalList<uint64> mma, mmb; |
| 256 | |
| 257 | atacMatchList &matches = *AF.matches(); |
| 258 | |
| 259 | uint64 *offset1 = buildOffset(A); |
| 260 | uint64 *offset2 = buildOffset(B); |
| 261 | |
| 262 | // ifa, ifb are intervalLists, storing the intervals labeled as |
| 263 | // tandem repeats. They are using the offset[] to encode the |
| 264 | // entire sequence as one consecutive string. |
| 265 | // |
| 266 | atacFeature *f = 0L; |
| 267 | while ((f = featuresA.nextFeature("tr")) != 0L) |
| 268 | ifa.add(offset1[f->iid] + f->pos, f->len); |
| 269 | while ((f = featuresB.nextFeature("tr")) != 0L) |
| 270 | ifb.add(offset2[f->iid] + f->pos, f->len); |
| 271 | |
| 272 | |
| 273 | // ima, imb, like if?, encode the matches in one string. |
| 274 | // |
| 275 | for (uint32 m=0; m<matches.numberOfMatches(); m++) |
| 276 | ima.add(offset1[matches[m]->iid1] + (uint64)matches[m]->pos1, (uint64)matches[m]->len1); |
| 277 | for (uint32 m=0; m<matches.numberOfMatches(); m++) |
| 278 | imb.add(offset2[matches[m]->iid2] + (uint64)matches[m]->pos2, (uint64)matches[m]->len2); |
| 279 | |
| 280 | |
| 281 | fprintf(stdout, "\nTANDEM REPEATS in %s\n", AF.labelA()); |
| 282 | fprintf(stdout, "numberOfItems "uint64FMT"\n", (uint64)ifa.numberOfIntervals()); |
| 283 | fprintf(stdout, "totalLength "uint64FMT" # sum of lengths of all features\n", ifa.sumOfLengths()); |
| 284 | ifa.merge(); |
| 285 | fprintf(stdout, "numberOfItems "uint64FMT" # after merging overlapping regions\n", (uint64)ifa.numberOfIntervals()); |
| 286 | fprintf(stdout, "coveredLength "uint64FMT" # sequence covered by a feature, including N\n", ifa.sumOfLengths()); |
| 287 | fprintf(stdout, "coveredLength "uint64FMT" # sequence covered by a feature, ACGT only\n", tandemRepeatACGTLength(ifa, offset1, A)); |
| 288 | mma.intersect(ifa, ima); |
| 289 | fprintf(stdout, "numberOfItems "uint64FMT" # after merging overlapping regions, only in matches\n", (uint64)mma.numberOfIntervals()); |
| 290 | fprintf(stdout, "inMatches "uint64FMT" # sequence covered by a feature and in a match, including N\n", mma.sumOfLengths()); |
| 291 | fprintf(stdout, "inMatches "uint64FMT" # sequence covered by a feature and in a match, ACGT only\n", tandemRepeatACGTLength(mma, offset1, A)); |
| 292 | |
| 293 | |
| 294 | fprintf(stdout, "\nTANDEM REPEATS in %s\n", AF.labelB()); |
| 295 | fprintf(stdout, "numberOfItems "uint64FMT"\n", (uint64)ifb.numberOfIntervals()); |
| 296 | fprintf(stdout, "totalLength "uint64FMT" # sum of lengths of all features\n", ifb.sumOfLengths()); |
| 297 | ifb.merge(); |
| 298 | fprintf(stdout, "numberOfItems "uint64FMT" # after merging overlapping regions\n", (uint64)ifb.numberOfIntervals()); |
| 299 | fprintf(stdout, "coveredLength "uint64FMT" # sequence covered by a feature, including N\n", ifb.sumOfLengths()); |
| 300 | fprintf(stdout, "coveredLength "uint64FMT" # sequence covered by a feature, ACGT only\n", tandemRepeatACGTLength(ifb, offset2, B)); |
| 301 | mmb.intersect(ifb, imb); |
| 302 | fprintf(stdout, "numberOfItems "uint64FMT" # after merging overlapping regions, only in matches\n", (uint64)mmb.numberOfIntervals()); |
| 303 | fprintf(stdout, "inMatches "uint64FMT" # sequence covered by a feature and in a match, including N\n", mmb.sumOfLengths()); |
| 304 | fprintf(stdout, "inMatches "uint64FMT" # sequence covered by a feature and in a match, ACGT only\n", tandemRepeatACGTLength(mmb, offset2, B)); |
| 305 | |
| 306 | delete [] offset1; |
| 307 | delete [] offset2; |
| 308 | } |
| 309 | |
| 310 | |
| 311 | |
| 312 | void |
| 313 | mappedLengths(atacFile &AF, atacMatchList &matches, seqCache *A, seqCache *B, char *prefix) { |
| 314 | histogram h1(100, 1000000); |
| 315 | histogram h2(100, 1000000); |
| 316 | |
| 317 | // For the coverage to work correctly, we need to either have one |
| 318 | // intervalList per input sequence, or build a table of the chained |
| 319 | // sequence positions. |
| 320 | // |
| 321 | uint64 *offset1 = buildOffset(AF.fastaA()); |
| 322 | uint64 *offset2 = buildOffset(AF.fastaB()); |
| 323 | |
| 324 | intervalList<uint64> intervalA; |
| 325 | intervalList<uint64> intervalB; |
| 326 | |
| 327 | for (uint32 m=0; m<matches.numberOfMatches(); m++) { |
| 328 | intervalA.add(offset1[matches[m]->iid1] + (uint64)matches[m]->pos1, (uint64)matches[m]->len1); |
| 329 | intervalB.add(offset2[matches[m]->iid2] + (uint64)matches[m]->pos2, (uint64)matches[m]->len2); |
| 330 | |
| 331 | h1.add(matches[m]->len1); |
| 332 | h2.add(matches[m]->len2); |
| 333 | } |
| 334 | |
| 335 | fprintf(stdout, "numberOfItems "uint64FMT"\n", (uint64)matches.numberOfMatches()); |
| 336 | |
| 337 | fprintf(stdout, "matchLength %s "uint64FMT" %s "uint64FMT" # Sum of lengths of sequence in matches\n", |
| 338 | AF.labelA(), (uint64)intervalA.sumOfLengths(), |
| 339 | AF.labelB(), (uint64)intervalB.sumOfLengths()); |
| 340 | |
| 341 | h1.show("AmatchLength"); |
| 342 | h2.show("BmatchLength"); |
| 343 | h1.dump(prefix, "AmatchLength"); h1.plot(prefix, "AmatchLength"); |
| 344 | h2.dump(prefix, "BmatchLength"); h2.plot(prefix, "BmatchLength"); |
| 345 | |
| 346 | intervalA.merge(); |
| 347 | intervalB.merge(); |
| 348 | |
| 349 | fprintf(stdout, "coveredLength %s "uint64FMT" %s "uint64FMT" # sequence covered by a match, including N\n", |
| 350 | AF.labelA(), (uint64)intervalA.sumOfLengths(), |
| 351 | AF.labelB(), (uint64)intervalB.sumOfLengths()); |
| 352 | |
| 353 | fprintf(stdout, "coveredLength %s "uint64FMT" %s "uint64FMT" # sequence covered by a match, ACGT only (new)\n", |
| 354 | AF.labelA(), tandemRepeatACGTLength(intervalA, offset1, A), |
| 355 | AF.labelB(), tandemRepeatACGTLength(intervalB, offset2, B)); |
| 356 | |
| 357 | delete [] offset1; |
| 358 | delete [] offset2; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | |
| 363 | // Generate an Nx plot |
| 364 | void |
| 365 | NxOfMapped(atacFile &AF, |
| 366 | atacMatchList &matches, |
| 367 | uint64 genomeSize, |
| 368 | char *prefix) { |
| 369 | |
| 370 | uint32 *n50 = new uint32 [matches.numberOfMatches()]; |
| 371 | |
| 372 | for (uint32 i=0; i<matches.numberOfMatches(); i++) |
| 373 | n50[i] = matches[i]->len1; |
| 374 | |
| 375 | // Compute the total length of the sequence |
| 376 | uint64 totalLength = 0; |
| 377 | switch (genomeSize) { |
| 378 | case 0: |
| 379 | for (uint32 i=0; i<AF.fastaA()->getNumberOfSequences(); i++) |
| 380 | totalLength += AF.fastaA()->getSequenceLength(i); |
| 381 | break; |
| 382 | case 1: |
| 383 | for (uint32 i=0; i<AF.fastaB()->getNumberOfSequences(); i++) |
| 384 | totalLength += AF.fastaB()->getSequenceLength(i); |
| 385 | break; |
| 386 | default: |
| 387 | totalLength = genomeSize; |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | // Sort the n50 list of lengths |
| 392 | qsort(n50, matches.numberOfMatches(), sizeof(uint32), uint32compare); |
| 393 | |
| 394 | // It's slow and obvious and, yes, there is a better way. Dump the |
| 395 | // Nx plot as it's being generated. |
| 396 | // |
| 397 | char filename[1024]; |
| 398 | sprintf(filename, "%s.Nx", prefix); |
| 399 | FILE *out = fopen(filename, "w"); |
| 400 | |
| 401 | for (uint64 n=1; n<100; n++) { |
| 402 | uint64 limit = totalLength / 100 * n; |
| 403 | uint64 iter = 0; |
| 404 | uint64 sum = 0; |
| 405 | |
| 406 | while ((sum < limit) && (iter < matches.numberOfMatches())) |
| 407 | sum += n50[iter++]; |
| 408 | |
| 409 | fprintf(out, uint64FMT" "uint32FMT"\n", n, n50[iter-1]); |
| 410 | } |
| 411 | |
| 412 | fclose(out); |
| 413 | |
| 414 | // Now plot it. |
| 415 | // |
| 416 | if (noHistogramPlots == false) { |
| 417 | sprintf(filename, "%s.Nx.gnuplot", prefix); |
| 418 | out = fopen(filename, "w"); |
| 419 | fprintf(out, "set terminal postscript color\n"); |
| 420 | fprintf(out, "set output \"%s.Nx.ps\"\n", prefix); |
| 421 | fprintf(out, "set xlabel \"N\"\n"); |
| 422 | fprintf(out, "set ylabel \"match length\"\n"); |
| 423 | fprintf(out, "plot \"%s.Nx\" using 2 with lines\n", prefix); |
| 424 | fclose(out); |
| 425 | sprintf(filename, "gnuplot < %s.Nx.gnuplot", prefix); |
| 426 | if (system(filename)) |
| 427 | fprintf(stderr, "Failed to execute '%s'\n", filename); |
| 428 | } |
| 429 | |
| 430 | delete [] n50; |
| 431 | } |
| 432 | |
| 433 | |
| 434 | // Computes the percentage of each chromosome (assumes chromosomes are A) |
| 435 | // that is mapped, with and without N's. |
| 436 | // |
| 437 | void |
| 438 | MappedByChromosome(atacFile &AF, |
| 439 | atacMatchList &matches, |
| 440 | seqCache *A, |
| 441 | seqCache *B, |
| 442 | char *prefix) { |
| 443 | |
| 444 | uint32 maxIID1 = A->getNumberOfSequences(); |
| 445 | intervalList<uint64> *il1full; |
| 446 | intervalList<uint64> *il1acgt; |
| 447 | histogram **hist1full; |
| 448 | histogram **hist1acgt; |
| 449 | |
| 450 | if (A->getNumberOfSequences() > 24) { |
| 451 | fprintf(stderr, "WARNING: too many sequences to be chromosomes, only using the first 24.\n"); |
| 452 | maxIID1 = 24; |
| 453 | } |
| 454 | |
| 455 | // We could cache this when we compute the totalLength() above |
| 456 | uint64 *nonNlength = new uint64 [maxIID1+1]; |
| 457 | for (uint32 i=0; i<maxIID1; i++) { |
| 458 | seqInCore *S = A->getSequenceInCore(i); |
| 459 | char *s = S->sequence(); |
| 460 | nonNlength[i] = 0; |
| 461 | for (uint32 j=0; j<S->sequenceLength(); j++) |
| 462 | if (letterToBits[s[j]] != 0xff) |
| 463 | nonNlength[i]++; |
| 464 | } |
| 465 | |
| 466 | il1full = new intervalList<uint64> [maxIID1 + 1]; |
| 467 | il1acgt = new intervalList<uint64> [maxIID1 + 1]; |
| 468 | |
| 469 | hist1full = new histogram * [maxIID1 + 1]; |
| 470 | hist1acgt = new histogram * [maxIID1 + 1]; |
| 471 | |
| 472 | for (uint32 i=0; i<maxIID1; i++) { |
| 473 | hist1full[i] = new histogram(100, 1000000); |
| 474 | hist1acgt[i] = new histogram(100, 1000000); |
| 475 | } |
| 476 | |
| 477 | for (uint32 m=0; m<matches.numberOfMatches(); m++) { |
| 478 | if (matches[m]->iid1 < maxIID1) { |
| 479 | il1full[matches[m]->iid1].add(matches[m]->pos1, matches[m]->len1); |
| 480 | hist1full[matches[m]->iid1]->add(matches[m]->len1); |
| 481 | |
| 482 | seqInCore *Sa = A->getSequenceInCore(matches[m]->iid1); |
| 483 | char *sa = Sa->sequence() + matches[m]->pos1; |
| 484 | |
| 485 | uint32 length = 0; |
| 486 | |
| 487 | for (uint32 j=0; j<matches[m]->len1; j++) { |
| 488 | bool invalid = (letterToBits[sa[j]] == 0xff); |
| 489 | |
| 490 | if (!invalid) |
| 491 | length++; |
| 492 | |
| 493 | if (length && invalid) { // Last time we were ACGT, this time not. |
| 494 | il1acgt[matches[m]->iid1].add(matches[m]->pos1 + j - length, length); |
| 495 | hist1acgt[matches[m]->iid1]->add(length); |
| 496 | length = 0; |
| 497 | } |
| 498 | } |
| 499 | if (length) { |
| 500 | il1acgt[matches[m]->iid1].add(matches[m]->pos1 + matches[m]->len1 - length, length); |
| 501 | hist1acgt[matches[m]->iid1]->add(length); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | for (uint32 c=0; c<maxIID1; c++) { |
| 507 | fprintf(stdout, "chrCoveredLength["uint32FMTW(2)"] %s "uint64FMT" "uint64FMT" %6.2f%% "uint64FMT" "uint64FMT" %6.2f%% # seqCov, totalSeq for both ALL and ACGTonly\n", |
| 508 | c, AF.labelA(), |
| 509 | il1full[c].sumOfLengths(), (uint64)A->getSequenceLength(c), 100.0 * il1full[c].sumOfLengths() / A->getSequenceLength(c), |
| 510 | il1acgt[c].sumOfLengths(), nonNlength[c], 100.0 * il1acgt[c].sumOfLengths() / nonNlength[c]); |
| 511 | } |
| 512 | |
| 513 | for (uint32 c=0; c<maxIID1; c++) { |
| 514 | char label[1024]; |
| 515 | |
| 516 | sprintf(label, "chr"uint32FMTW(02)"full", c); |
| 517 | hist1full[c]->dump(prefix, label); |
| 518 | hist1full[c]->plot(prefix, label); |
| 519 | |
| 520 | sprintf(label, "chr"uint32FMTW(02)"acgt", c); |
| 521 | hist1acgt[c]->dump(prefix, label); |
| 522 | hist1acgt[c]->plot(prefix, label); |
| 523 | } |
| 524 | |
| 525 | delete [] il1full; |
| 526 | delete [] il1acgt; |
| 527 | for (uint32 i=0; i<maxIID1; i++) { |
| 528 | delete hist1full[i]; |
| 529 | delete hist1acgt[i]; |
| 530 | } |
| 531 | delete [] hist1full; |
| 532 | delete [] hist1acgt; |
| 533 | delete [] nonNlength; |
| 534 | } |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | void |
| 542 | statsInACGT(seqInCore *S, |
| 543 | uint32 beg, |
| 544 | uint32 len, |
| 545 | intervalList<uint64> *IL, |
| 546 | histogram *HI) { |
| 547 | char *s = S->sequence() + beg; |
| 548 | uint32 length = 0; |
| 549 | |
| 550 | for (uint32 j=0; j<len; j++) { |
| 551 | bool invalid = (letterToBits[s[j]] == 0xff); |
| 552 | |
| 553 | if (!invalid) |
| 554 | length++; |
| 555 | |
| 556 | if (length && invalid) { // Last time we were ACGT, this time not. |
| 557 | if (IL) IL->add(beg + j - length, length); |
| 558 | if (HI) HI->add(length); |
| 559 | length = 0; |
| 560 | } |
| 561 | } |
| 562 | if (length) { |
| 563 | if (IL) IL->add(beg + len - length, length); |
| 564 | if (HI) HI->add(length); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | // Computes the amount of ACGT in runs that is unmapped |
| 572 | // |
| 573 | void |
| 574 | unmappedInRuns(atacFile &AF, seqCache *A, seqCache *B, char *prefix) { |
| 575 | |
| 576 | atacMatchList &matches = *AF.matches(); |
| 577 | |
| 578 | // We must sort by the location and not the parentID; when we |
| 579 | // stream through, we check that the pair of matches are in the |
| 580 | // same parent. |
| 581 | // |
| 582 | atacMatchOrder MO(matches); |
| 583 | MO.sortA(); |
| 584 | |
| 585 | intervalList<uint64> il1full, il2full; |
| 586 | intervalList<uint64> il1acgt, il2acgt; |
| 587 | |
| 588 | histogram hist1full(100, 1000000), hist2full(100, 1000000); |
| 589 | histogram hist1acgt(100, 1000000), hist2acgt(100, 1000000); |
| 590 | |
| 591 | for (uint32 i=1; i<MO.numberOfMatches(); i++) { |
| 592 | if (strcmp(MO[i-1]->parentuid, MO[i]->parentuid) == 0) { |
| 593 | uint32 l1, r1, l2, r2; |
| 594 | |
| 595 | if (MO[i]->fwd2 == 1) { |
| 596 | l1 = MO[i-1]->pos1 + MO[i-1]->len1; |
| 597 | r1 = MO[i]->pos1; |
| 598 | l2 = MO[i-1]->pos2 + MO[i-1]->len2; |
| 599 | r2 = MO[i]->pos2; |
| 600 | } else { |
| 601 | l1 = MO[i-1]->pos1 + MO[i-1]->len1; |
| 602 | r1 = MO[i]->pos1; |
| 603 | l2 = MO[i]->pos2 + MO[i]->len2; |
| 604 | r2 = MO[i-1]->pos2; |
| 605 | } |
| 606 | |
| 607 | il1full.add(l1, r1-l1); |
| 608 | il2full.add(l2, r2-l2); |
| 609 | |
| 610 | hist1full.add(r1-l1); |
| 611 | hist2full.add(r2-l2); |
| 612 | |
| 613 | statsInACGT(A->getSequenceInCore(MO[i]->iid1), |
| 614 | l1, |
| 615 | r1-l1, |
| 616 | &il1acgt, |
| 617 | &hist1acgt); |
| 618 | statsInACGT(B->getSequenceInCore(MO[i]->iid2), |
| 619 | l2, |
| 620 | r2-l2, |
| 621 | &il2acgt, |
| 622 | &hist2acgt); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | // Dump the stats |
| 627 | |
| 628 | fprintf(stdout, "runMissingFull %s "uint64FMT" %s "uint64FMT" # sequence in run, not covered, including N\n", |
| 629 | AF.labelA(), (uint64)il1full.sumOfLengths(), |
| 630 | AF.labelB(), (uint64)il2full.sumOfLengths()); |
| 631 | fprintf(stdout, "runMissingFull %s "uint64FMT" %s "uint64FMT" # sequence in run, not covered, ACGT only\n", |
| 632 | AF.labelA(), (uint64)il1acgt.sumOfLengths(), |
| 633 | AF.labelB(), (uint64)il2acgt.sumOfLengths()); |
| 634 | |
| 635 | hist1full.dump(prefix, "ARunMissingFull"); |
| 636 | hist1full.plot(prefix, "ARunMissingFull"); |
| 637 | |
| 638 | hist2full.dump(prefix, "BRunMissingFull"); |
| 639 | hist2full.plot(prefix, "BRunMissingFull"); |
| 640 | |
| 641 | hist1acgt.dump(prefix, "ARunMissingACGT"); |
| 642 | hist1acgt.plot(prefix, "ARunMissingACGT"); |
| 643 | |
| 644 | hist2acgt.dump(prefix, "BRunMissingACGT"); |
| 645 | hist2acgt.plot(prefix, "BRunMissingACGT"); |
| 646 | } |
| 647 | |
| 648 | |
| 649 | |
| 650 | int |
| 651 | main(int argc, char **argv) { |
| 652 | uint64 genomeSize = 0; |
| 653 | char *atacFileName = 0L; |
| 654 | char *prefix = 0L; |
| 655 | char *trFile1 = 0L; |
| 656 | char *trFile2 = 0L; |
| 657 | char prefixFull[1024]; |
| 658 | bool error = false; |
| 659 | |
| 660 | int arg=1; |
| 661 | while (arg < argc) { |
| 662 | if (strcmp(argv[arg], "-g") == 0) { |
| 663 | ++arg; |
| 664 | if (argv[arg][0] == 'A') { |
| 665 | genomeSize = 0; |
| 666 | } else if (argv[arg][0] == 'B') { |
| 667 | genomeSize = 1; |
| 668 | } else { |
| 669 | genomeSize = strtouint64(argv[arg], 0L); |
| 670 | } |
| 671 | } else if (strcmp(argv[arg], "-a") == 0) { |
| 672 | atacFileName = argv[++arg]; |
| 673 | } else if (strcmp(argv[arg], "-p") == 0) { |
| 674 | prefix = argv[++arg]; |
| 675 | } else if (strcmp(argv[arg], "-ta") == 0) { |
| 676 | trFile1 = argv[++arg]; |
| 677 | } else if (strcmp(argv[arg], "-tb") == 0) { |
| 678 | trFile2 = argv[++arg]; |
| 679 | } else { |
| 680 | error = true; |
| 681 | } |
| 682 | arg++; |
| 683 | } |
| 684 | |
| 685 | if (!atacFileName || !prefix || error) { |
| 686 | fprintf(stderr, "usage: %s -a <file.atac> -p <outprefix> [-ta trfile] [-tb trfile] [-g {A | B | g}]\n", argv[0]); |
| 687 | fprintf(stderr, " -a read input from 'file.atac'\n"); |
| 688 | fprintf(stderr, " -p write stats to files prefixed with 'outprefix'\n"); |
| 689 | fprintf(stderr, " -g use a genome size of g for the Nx computation, defaults to\n"); |
| 690 | fprintf(stderr, " the length of the A sequence. Or use the actual length\n"); |
| 691 | fprintf(stderr, " of sequence A or B.\n"); |
| 692 | fprintf(stderr, " -ta read tandem repeats for A from trfile\n"); |
| 693 | fprintf(stderr, " -tb read tandem repeats for B from trfile\n"); |
| 694 | exit(1); |
| 695 | } |
| 696 | |
| 697 | atacFile AF(atacFileName); |
| 698 | atacMatchList &matches = *AF.matches(); |
| 699 | atacMatchList &runs = *AF.runs(); |
| 700 | atacMatchList &clumps = *AF.clumps(); |
| 701 | |
| 702 | // We end up using sequences a lot here, so just bite it and load them in a cache. |
| 703 | // |
| 704 | seqCache *A = new seqCache(AF.assemblyFileA(), 0, true); |
| 705 | seqCache *B = new seqCache(AF.assemblyFileB(), 0, true); |
| 706 | |
| 707 | A->loadAllSequences(); |
| 708 | B->loadAllSequences(); |
| 709 | |
| 710 | fprintf(stdout, "\nSEQUENCE\n"); |
| 711 | totalLength(AF, A, B); |
| 712 | |
| 713 | if (trFile1 && trFile2) { |
| 714 | atacFileStream tr1(trFile1); |
| 715 | atacFileStream tr2(trFile2); |
| 716 | tandemRepeatStats(tr1, tr2, AF, A, B); |
| 717 | } |
| 718 | |
| 719 | // XXX unmappedInRuns only works on runs, and if we have clumps in |
| 720 | // the input it fails. |
| 721 | // |
| 722 | if ((runs.numberOfMatches() > 0) && (clumps.numberOfMatches() == 0)) { |
| 723 | fprintf(stdout, "\nMATCHES IN RUNS\n"); |
| 724 | unmappedInRuns(AF, A, B, prefix); |
| 725 | } |
| 726 | |
| 727 | if (matches.numberOfMatches() > 0) { |
| 728 | fprintf(stdout, "\nMATCHES\n"); |
| 729 | sprintf(prefixFull, "%s-matches", prefix); |
| 730 | mappedLengths(AF, matches, A, B, prefixFull); |
| 731 | NxOfMapped(AF, matches, genomeSize, prefixFull); |
| 732 | MappedByChromosome(AF, matches, A, B, prefixFull); |
| 733 | } |
| 734 | |
| 735 | if (runs.numberOfMatches() > 0) { |
| 736 | fprintf(stdout, "\nRUNS\n"); |
| 737 | sprintf(prefixFull, "%s-runs", prefix); |
| 738 | mappedLengths(AF, runs, A, B, prefixFull); |
| 739 | NxOfMapped(AF, runs, genomeSize, prefixFull); |
| 740 | MappedByChromosome(AF, runs, A, B, prefixFull); |
| 741 | } |
| 742 | |
| 743 | if (clumps.numberOfMatches() > 0) { |
| 744 | fprintf(stdout, "\nCLUMPS\n"); |
| 745 | sprintf(prefixFull, "%s-clumps", prefix); |
| 746 | mappedLengths(AF, clumps, A, B, prefixFull); |
| 747 | NxOfMapped(AF, clumps, genomeSize, prefixFull); |
| 748 | MappedByChromosome(AF, clumps, A, B, prefixFull); |
| 749 | } |
| 750 | |
| 751 | delete A; |
| 752 | delete B; |
| 753 | |
| 754 | return(0); |
| 755 | } |