Debian Codegraph @

sim4dbutils/vennPolishes.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4
5 #include "bio++.H"
6 #include "sim4.H"
7
8
9 const char *usage =
10 "usage: %s [options] <polishes-file-1> <polishes-file-2> ...\n"
11 "\n"
12 " Given n sets of sim4 polishes (of the same set of cDNA to the same\n"
13 " set of genomic, but this isn't enforced) this code will generate a\n"
14 " Venn diagram of how the sequences map.\n"
15 "\n"
16 " -n <num-seqs> there are <num-seqs> in the input set\n"
17 " -i <min-ident> filter matches to be >= <min-ident> identity\n"
18 " default = 95\n"
19 " -c <min-cover> filter matches to be >= <min-cover> coverage\n"
20 " default = 50\n"
21 " -d <class-id> dump the sequence IIDs in <class-id> to stdout\n"
22 "\n"
23 " -plot write a plot-able datafile of the venn diagram\n"
24 " for percent identity <min-idenit> to 100 (inclusive)\n"
25 " and <min-cover> coverage.\n";
26
27
28 // Yes, yes. Tell me all about how bad globals are.
29 uint32 minI = 95;
30 uint32 minC = 50;
31 uint32 foundMax = 100000;
32 uint32 dumpIID = ~uint32ZERO;
33 int numArgs = 0;
34 bool plot = false;
35 uint32 numFiles = 0;
36 uint32 **found = 0L;
37 uint32 indexMax = 0;
38 uint32 *counts = 0L;
39 uint32 *sizes = 0L;
40
41
42 void
43 doVenn(uint32 minI, uint32 minC) {
44
45 // Count how many elements are in each set
46 for (uint32 i=0; i<numFiles; i++) {
47 sizes[i] = 0;
48 for (uint32 j=0; j<foundMax; j++)
49 if (found[i][j] >= minI)
50 sizes[i]++;
51 }
52
53
54 for (uint32 i=0; i<indexMax; i++)
55 counts[i] = 0;
56
57
58 // For each guy in the datasets
59 //
60 for (uint32 thisguy=0; thisguy < foundMax; thisguy++) {
61
62 // Compute which class he is in. 'class' is a reserved word,
63 // so we use 'membership' instead.
64 //
65 uint32 membership = 0;
66
67 for (uint32 dataset=0; dataset < numFiles; dataset++) {
68 if (found[dataset][thisguy] >= minI)
69 membership |= 1 << dataset;
70 }
71
72 if (membership == dumpIID)
73 fprintf(stdout, uint32FMT"\n", thisguy);
74
75 counts[membership]++;
76 }
77 }
78
79
80
81 int
82 main(int argc, char **argv) {
83
84 if ((argc < 5)) {
85 fprintf(stderr, usage, argv[0]);
86 exit(1);
87 }
88
89 int arg=1;
90 while (arg < argc) {
91 if (strcmp(argv[arg], "-n") == 0) {
92 foundMax = atoi(argv[++arg]);
93 } else if (strcmp(argv[arg], "-i") == 0) {
94 minI = atoi(argv[++arg]);
95 } else if (strcmp(argv[arg], "-c") == 0) {
96 minC = atoi(argv[++arg]);
97 } else if (strcmp(argv[arg], "-d") == 0) {
98 dumpIID = atoi(argv[++arg]);
99 } else if (strcmp(argv[arg], "-plot") == 0) {
100 plot = true;
101 } else {
102 // Assume we got all the options, and we are at a file.
103 //
104 numArgs = arg;
105 arg = argc;
106 }
107
108 arg++;
109 }
110
111 numFiles = argc - numArgs;
112 found = new uint32 * [numFiles];
113
114 if (numFiles > 16) {
115 fprintf(stderr, "WARNING: You gave me "uint32FMT" files! That's pretty big. I don't know\n", numFiles);
116 fprintf(stderr, " if I'm up to it. Fasten seat belts and hang on!\n");
117 }
118
119 for (int arg=numArgs; arg<argc; arg++) {
120 fprintf(stderr, "Reading '%s'\n", argv[arg]);
121
122 found[arg-numArgs] = new uint32 [foundMax];
123
124 for (uint32 i=0; i<foundMax; i++)
125 found[arg-numArgs][i] = 0;
126
127 sim4polishReader *R = new sim4polishReader("-");
128 sim4polish *p = 0L;
129
130 while (R->nextAlignment(p)) {
131 if ((p->_percentIdentity >= minI) &&
132 (p->_querySeqIdentity >= minC)) {
133
134 if (p->_estID >= foundMax) {
135 fprintf(stderr, "Please increase foundMax, or make me reallocate storage.\n");
136 exit(1);
137 }
138
139 if (found[arg-numArgs][p->_estID] < p->_percentIdentity)
140 found[arg-numArgs][p->_estID] = p->_percentIdentity;
141 }
142 }
143 }
144
145
146 // There are 2^n categories for n files.
147 //
148 // If A and B, then there is
149 //
150 // A B
151 // 0 0 - neither (we can't compute this)
152 // 0 1 - only B
153 // 1 0 - only A
154 // 1 1 - both A and B
155 //
156 // So, we make an array of size 2^n that holds the ocunts of each
157 // class. It's indexed by a bit vector.
158 //
159 indexMax = 1 << numFiles;
160 counts = new uint32 [indexMax];
161 sizes = new uint32 [numFiles];
162
163 if (dumpIID != ~uint32ZERO) {
164 doVenn(minI, minC);
165 } else if (plot) {
166 for (uint32 id=minI; id <= 100; id++) {
167 doVenn(id, minC);
168
169 fprintf(stdout, uint32FMTW(3)" ", id);
170 for (uint32 i=0; i<numFiles; i++)
171 fprintf(stdout, uint32FMTW(8)" ", sizes[i]);
172 for (uint32 index=0; index < indexMax; index++) {
173 for (uint32 dataset=0; dataset < numFiles; dataset++)
174 fprintf(stdout, "%c", (index & (1 << dataset)) ? 'A' + (char)dataset : '-');
175 fprintf(stdout, " "uint32FMTW(8)" ", counts[index]);
176 }
177 fprintf(stdout, "\n");
178 }
179 } else {
180 doVenn(minI, minC);
181
182 for (uint32 i=0; i<numFiles; i++)
183 fprintf(stdout, "%c = ("uint32FMTW(8)" total) %s\n", 'A' + (char)i, sizes[i], argv[i+numArgs]);
184
185 for (uint32 index=0; index < indexMax; index++) {
186 fprintf(stdout, uint32FMTW(4)" [", index);
187 for (uint32 dataset=0; dataset < numFiles; dataset++)
188 fprintf(stdout, "%c", (index & (1 << dataset)) ? 'A' + (char)dataset : '-');
189 fprintf(stdout, "] "uint32FMT"\n", counts[index]);
190 }
191 }
192 }