Debian Codegraph @

sim4dbutils/fixPolishesIID.C

raw
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 #include <map>
10 #include <string>
11
12 using namespace std;
13
14 // Updates the IID's in a set of polishes. If a file of deflines (or
15 // fasta file) is supplied, the IIDs will match those, otherwise,
16 // they remain the same.
17
18 void
19 addToDict(map<string, uint64> &d, char *n) {
20
21 if (n == 0L)
22 return;
23
24 seqCache *F = new seqCache(n);
25 seqInCore *S = F->getSequenceInCore();
26
27 while (S) {
28 string s = S->header();
29
30 d[s] = S->getIID();
31
32 delete S;
33 S = F->getSequenceInCore();
34 }
35
36 delete F;
37 }
38
39
40
41 int
42 main(int argc, char **argv) {
43 char *cDeflines = 0L;
44 char *gDeflines = 0L;
45
46 sim4polishStyle style = sim4polishStyleDefault;
47
48 int arg=1;
49 while (arg < argc) {
50 if (strcmp(argv[arg], "-c") == 0) {
51 cDeflines = argv[++arg];
52
53 } else if (strcmp(argv[arg], "-g") == 0) {
54 gDeflines = argv[++arg];
55
56 } else if (strcmp(argv[arg], "-gff3") == 0) {
57 style = sim4polishGFF3;
58
59 } else {
60 fprintf(stderr, "Unknown arg: %s\n", argv[arg]);
61 }
62 arg++;
63 }
64 if (isatty(fileno(stdin))) {
65 fprintf(stderr, "usage: %s [-c c.fasta] [-g g.fasta] [-gff3] < polishes > polishes\n", argv[0]);
66 fprintf(stderr, " -c c.fasta Read cDNA deflines from c.fasta\n");
67 fprintf(stderr, " -g g.fasta Read genomic deflines from g.fasta\n");
68 fprintf(stderr, " -gff3 Write output as GFF3\n");
69 fprintf(stderr, "\n");
70 fprintf(stderr, " Rewrites the input polishes, updating the sequence index to match\n");
71 fprintf(stderr, " that of the associated fasta file. One or both of -c and -g may be used.\n");
72 fprintf(stderr, " Polishes that refer to a sequence not present in the input fasta file are\n");
73 fprintf(stderr, " not output.\n");
74 exit(1);
75 }
76
77 // We parse args, then build the dictionaries, so we can do
78 // any quick error detection first.
79
80 map<string, uint64> g;
81 map<string, uint64> c;
82
83 if (gDeflines) {
84 fprintf(stderr, "Reading genomic deflines from '%s'\n", gDeflines);
85 addToDict(g, gDeflines);
86 }
87
88 if (cDeflines) {
89 fprintf(stderr, "Reading genomic deflines from '%s'\n", cDeflines);
90 addToDict(c, cDeflines);
91 }
92
93 // Read all the matches, changing IIDs. If we find a defline
94 // with no IID, holler and die.
95
96 sim4polishWriter *W = new sim4polishWriter("-", style);
97 sim4polishReader *R = new sim4polishReader("-");
98 sim4polish *p = 0L;
99
100 if (R->getsim4polishStyle() != style)
101 fprintf(stderr, "warning: input format and output format differ.\n");
102
103 fprintf(stderr, "Filtering polishes.\n");
104
105 while (R->nextAlignment(p)) {
106 string cd = p->_estDefLine;
107 string gd = p->_genDefLine;
108
109 if (cDeflines != 0L) {
110 if (c.find(cd) == c.end())
111 // EST defline not in the input sequences, don't output.
112 continue;
113 p->_estID = c[cd];
114 }
115
116 if (gDeflines != 0L) {
117 if (g.find(gd) == g.end())
118 // Genomic defline not in the input sequences, don't output.
119 continue;
120 p->_genID = g[gd];
121 }
122
123 W->writeAlignment(p);
124 }
125
126 delete R;
127 delete W;
128 }