| 1 | // This file is part of A2Amapper. |
| 2 | // Copyright (c) 2005 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 | #include "overlap.H" |
| 20 | |
| 21 | |
| 22 | int |
| 23 | main(int argc, char **argv) { |
| 24 | |
| 25 | if (argc != 4) { |
| 26 | fprintf(stderr, "usage: %s <matches-1> <matches-2> <out-prefix>\n", argv[0]); |
| 27 | exit(1); |
| 28 | } |
| 29 | atacFile *AF1 = new atacFile(argv[1]); |
| 30 | atacFile *AF2 = new atacFile(argv[2]); |
| 31 | |
| 32 | atacMatchList *M1 = AF1->matches(); |
| 33 | atacMatchList *M2 = AF2->matches(); |
| 34 | |
| 35 | char *OP = argv[3]; |
| 36 | |
| 37 | |
| 38 | // We want to annotate the two assembies with: |
| 39 | // a) mapped by both, the same |
| 40 | // b) mapped by both, differently |
| 41 | // c) mapped by the first, unmapped by the second |
| 42 | // d) mapped by the second, unmapped by the first |
| 43 | // e) unmapped by both |
| 44 | // |
| 45 | // If unmapped, we could further annotate with the reason it was |
| 46 | // unmapped -- not found, or found multiple times. |
| 47 | // |
| 48 | // Our annotation datastructure is a tree of spans. Each span is a |
| 49 | // sequence, and an interval on that sequence. We assume that the |
| 50 | // tree contains the spans for the whole sequence, that is, that we |
| 51 | // never need to increase a span, just split. |
| 52 | // |
| 53 | spanTree *S1 = new spanTree(); |
| 54 | spanTree *S2 = new spanTree(); |
| 55 | |
| 56 | // Initialize the tree of spans by inserting a single span for each |
| 57 | // sequence in the file. |
| 58 | // |
| 59 | for (uint32 i=0; i<AF1->fastaA()->getNumberOfSequences(); i++) |
| 60 | S1->addNewSpan(i, AF1->fastaA()->getSequenceLength(i)); |
| 61 | for (uint32 i=0; i<AF1->fastaB()->getNumberOfSequences(); i++) |
| 62 | S2->addNewSpan(i, AF1->fastaB()->getSequenceLength(i)); |
| 63 | |
| 64 | // Add every match to the spanTrees. |
| 65 | |
| 66 | for (uint32 i=0; i<M1->numberOfMatches(); i++) { |
| 67 | S1->addMatch(M1->getMatch(i), 0, 0); |
| 68 | S2->addMatch(M1->getMatch(i), 1, 0); |
| 69 | } |
| 70 | for (uint32 i=0; i<M2->numberOfMatches(); i++) { |
| 71 | S1->addMatch(M2->getMatch(i), 0, 1); |
| 72 | S2->addMatch(M2->getMatch(i), 1, 1); |
| 73 | } |
| 74 | |
| 75 | // Dump each spanTree: For each span, we need to check that |
| 76 | // it has matches? |
| 77 | // only one match, or only matches from one mapping? |
| 78 | // matches from both mappings? need to check that |
| 79 | // the span in the other tree also has the same matches |
| 80 | // |
| 81 | // Doesn't handle weird stuff like this span (on sequence 1) |
| 82 | // mapping onto seq2 correctly, but the span in seq2 having an |
| 83 | // extra match to somewhere else in seq1. |
| 84 | // |
| 85 | // we want to find the single span in the other spanTree that |
| 86 | // corresponds to this span. once we do that, we can verify that |
| 87 | // all the matches are the same. |
| 88 | // |
| 89 | // because we are gapless matches, we can, for each match, |
| 90 | // compute the exact location this span should occur on the other |
| 91 | // sequence. then, do a lookup() to get that span, or just |
| 92 | // verify that everybody is the same location. |
| 93 | |
| 94 | char outname[1024]; |
| 95 | FILE *outfile; |
| 96 | |
| 97 | overlapStats statsA; |
| 98 | uint32 ALmax = (uint32)dict_count(S1->_tree); |
| 99 | uint32 ALlen = 0; |
| 100 | annoList *AL = new annoList [ ALmax ]; |
| 101 | |
| 102 | sprintf(outname, "%s.map1annotation", OP); |
| 103 | errno = 0; |
| 104 | outfile = fopen(outname, "w"); |
| 105 | if (errno) |
| 106 | fprintf(stderr, "Failed to open '%s': %s\n", outname, strerror(errno)); |
| 107 | process1(outfile, S1, M1, M2, statsA, AL, ALlen, ALmax); |
| 108 | fclose(outfile); |
| 109 | |
| 110 | overlapStats statsB; |
| 111 | uint32 BLmax = (uint32)dict_count(S1->_tree); |
| 112 | uint32 BLlen = 0; |
| 113 | annoList *BL = new annoList [ ALmax ]; |
| 114 | |
| 115 | sprintf(outname, "%s.map2annotation", OP); |
| 116 | errno = 0; |
| 117 | outfile = fopen(outname, "w"); |
| 118 | if (errno) |
| 119 | fprintf(stderr, "Failed to open '%s': %s\n", outname, strerror(errno)); |
| 120 | process2(outfile, S2, M1, M2, statsB, BL, BLlen, BLmax); |
| 121 | fclose(outfile); |
| 122 | |
| 123 | fprintf(stderr, "unmapped: A:"uint32FMTW(10)" B:"uint32FMTW(10)"\n", statsA.unmapped.getSum(), statsB.unmapped.getSum()); |
| 124 | fprintf(stderr, "unique mapping 1: A:"uint32FMTW(10)" B:"uint32FMTW(10)"\n", statsA.map1unique.getSum(), statsB.map1unique.getSum()); |
| 125 | fprintf(stderr, "unique mapping 2: A:"uint32FMTW(10)" B:"uint32FMTW(10)"\n", statsA.map2unique.getSum(), statsB.map2unique.getSum()); |
| 126 | fprintf(stderr, "different: A:"uint32FMTW(10)" B:"uint32FMTW(10)"\n", statsA.different.getSum(), statsB.different.getSum()); |
| 127 | fprintf(stderr, "wild diff: A:"uint32FMTW(10)" B:"uint32FMTW(10)"\n", statsA.wilddiff.getSum(), statsB.wilddiff.getSum()); |
| 128 | fprintf(stderr, "same: A:"uint32FMTW(10)" B:"uint32FMTW(10)"\n", statsA.same.getSum(), statsB.same.getSum()); |
| 129 | fprintf(stderr, "inconsistent: A:"uint32FMTW(10)" B:"uint32FMTW(10)"\n", statsA.inconsistent.getSum(), statsB.inconsistent.getSum()); |
| 130 | |
| 131 | // Dump the histograms for each of the labelings |
| 132 | // |
| 133 | sprintf(outname, "%s.asm1histogram", OP); |
| 134 | statsA.writeHistogram(outname); |
| 135 | sprintf(outname, "%s.asm2histogram", OP); |
| 136 | statsB.writeHistogram(outname); |
| 137 | |
| 138 | // Draw some pretty pictures |
| 139 | // |
| 140 | sprintf(outname, "%s.histogram.gnuplot", OP); |
| 141 | errno = 0; |
| 142 | outfile = fopen(outname, "w"); |
| 143 | if (errno) |
| 144 | fprintf(stderr, "failed to open '%s': %s\n", outname, strerror(errno)), exit(1); |
| 145 | fprintf(outfile, "set terminal postscript color\n"); |
| 146 | fprintf(outfile, "set output \"%s.unmapped.histogram.ps\"\n", OP); |
| 147 | fprintf(outfile, "set ylabel \"number of regions\"\n"); |
| 148 | fprintf(outfile, "set xlabel \"length of region\"\n"); |
| 149 | fprintf(outfile, "plot [0:10000][0:400] \\\n"); |
| 150 | fprintf(outfile, " \"%s.asm1histogram.unmapped\" using 2 title \"assembly 1 unmapped\" with lines, \\\n", OP); |
| 151 | fprintf(outfile, " \"%s.asm2histogram.unmapped\" using 2 title \"assembly 2 unmapped\" with lines\n", OP); |
| 152 | fprintf(outfile, "set output \"%s.same.histogram.ps\"\n", OP); |
| 153 | fprintf(outfile, "plot [0:20000][0:2000] \\\n"); |
| 154 | fprintf(outfile, " \"%s.asm1histogram.same\" using 2 title \"assembly 1 same\" with lines, \\\n", OP); |
| 155 | fprintf(outfile, " \"%s.asm2histogram.same\" using 2 title \"assembly 2 same\" with lines\n", OP); |
| 156 | fprintf(outfile, "set output \"%s.histogram.ps\"\n", OP); |
| 157 | fprintf(outfile, "plot [0:2000][0:100] \\\n"); |
| 158 | fprintf(outfile, " \"%s.asm1histogram.different\" using 2 title \"assembly 1 different\" with lines, \\\n", OP); |
| 159 | fprintf(outfile, " \"%s.asm2histogram.different\" using 2 title \"assembly 2 different\" with lines, \\\n", OP); |
| 160 | fprintf(outfile, " \"%s.asm1histogram.wilddiff\" using 2 title \"assembly 1 wildly diff\" with lines, \\\n", OP); |
| 161 | fprintf(outfile, " \"%s.asm2histogram.wilddiff\" using 2 title \"assembly 2 wildly diff\" with lines\n", OP); |
| 162 | fprintf(outfile, "set output \"%s.unique.histogram.ps\"\n", OP); |
| 163 | fprintf(outfile, "plot [0:2000][0:100] \\\n"); |
| 164 | fprintf(outfile, " \"%s.asm1histogram.map1unique\" using 2 title \"map 1, assembly 1 unique\" with lines, \\\n", OP); |
| 165 | fprintf(outfile, " \"%s.asm1histogram.map2unique\" using 2 title \"map 2, assembly 1 unique\" with lines, \\\n", OP); |
| 166 | fprintf(outfile, " \"%s.asm2histogram.map1unique\" using 2 title \"map 1, assembly 2 unique\" with lines, \\\n", OP); |
| 167 | fprintf(outfile, " \"%s.asm2histogram.map2unique\" using 2 title \"map 2, assembly 2 unique\" with lines\n", OP); |
| 168 | fclose(outfile); |
| 169 | |
| 170 | sprintf(outname, "gnuplot < %s.histogram.gnuplot", OP); |
| 171 | if (system(outname)) |
| 172 | fprintf(stderr, "Failed to '%s'\n", outname); |
| 173 | |
| 174 | #if 0 |
| 175 | findIsolatedUnique(AL, ALlen); |
| 176 | findExtended(AL, ALlen); |
| 177 | #endif |
| 178 | |
| 179 | // Deleting the spanTrees takes a long time, so we don't bother with any cleanup. |
| 180 | return(0); |
| 181 | } |