Debian Codegraph @

sim4dbutils/trimSequencesBasedOnMatches.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6
7 #include "bri++.H"
8 #include "sim4reader.h"
9
10 int
11 main(int argc, char ** argv) {
12 FastA *seqs = 0L;
13 FastABuffer seqsbuffer;
14 FILE *pfile = 0L;
15 sim4polish *p = 0L;
16
17 if (argc == 1) {
18 fprintf(stderr, "usage: %s -sequence s.fasta -polishes p.polished\n", argv[0]);
19 exit(1);
20 }
21
22 int arg = 1;
23 while (arg < argc) {
24 if (strncmp(argv[arg], "-sequence", 2) == 0) {
25 seqs = new FastA(argv[++arg], true);
26 } else if (strncmp(argv[arg], "-polishes", 2) == 0) {
27 errno = 0;
28 pfile = fopen(argv[++arg], "r");
29 if (errno) {
30 fprintf(stderr, "Can't open '%s': %s\n", argv[arg], strerror(errno));
31 exit(1);
32 }
33 } else {
34 fprintf(stderr, "%s: unknown option '%s'\n", argv[arg]);
35 }
36
37 arg++;
38 }
39
40 if (seqs == 0L) {
41 fprintf(stderr, "error: you need to specify '-sequence s.fasta'\n");
42 exit(1);
43 }
44
45 if (pfile == 0L) {
46 fprintf(stderr, "error: you need to specify '-polishes p.polished'\n");
47 exit(1);
48 }
49
50 uint32 numseqs = seqs->numberOfSequences();
51 uint32 *lrange = new uint32 [numseqs];
52 uint32 *hrange = new uint32 [numseqs];
53
54 for (uint32 i=0; i<numseqs; i++) {
55 lrange[i] = ~uint32ZERO;
56 hrange[i] = uint32ZERO;
57 }
58
59 uint32 numRead = 0;
60
61 while ((p = readPolish(pfile)) != 0L) {
62 if (lrange[p->estID] > p->exons[0].estFrom-1)
63 lrange[p->estID] = p->exons[0].estFrom-1;
64
65 if (hrange[p->estID] < p->exons[p->numExons-1].estTo)
66 hrange[p->estID] = p->exons[p->numExons-1].estTo;
67
68 numRead++;
69 if ((numRead & 0xff) == 0) {
70 fprintf(stderr, "Reading matches: %u\r", numRead);
71 fflush(stderr);
72 }
73
74 destroyPolish(p);
75 }
76
77 fprintf(stderr, "\n");
78
79 uint32 seqcopylen = 128 * 1024;
80 char *seqcopy = new char [seqcopylen + 1];
81 char *defcopy = new char [128 * 1024];
82
83 seqs->first(seqsbuffer);
84
85 for (uint32 i=0; i<numseqs; i++, seqs->next(seqsbuffer)) {
86
87 // If there is no polish for the sequence, just write the whole
88 // thing out. This is a hack, so that svi will run.
89 //
90 if (lrange[i] >= hrange[i]) {
91 lrange[i] = 0;
92 hrange[i] = seqsbuffer.sequenceLength();
93 }
94
95 if (lrange[i] < hrange[i]) {
96 //seqs->seek(seqsbuffer, i);
97
98 if (seqsbuffer.sequenceLength() > seqcopylen) {
99 delete [] seqcopy;
100
101 seqcopylen = seqsbuffer.sequenceLength() + 128 * 1024;
102 seqcopy = new char [seqcopylen + 1];
103 }
104
105 for (uint32 j=0, k=lrange[i]; k<hrange[i]; j++, k++)
106 seqcopy[j] = seqsbuffer.sequence()[k];
107
108 seqcopy[hrange[i] - lrange[i]] = 0;
109
110 // Mangle the defline
111 //
112 uint32 j = 0;
113 for (j=0; !isspace(seqsbuffer.header()[j]) && j<seqsbuffer.headerLength(); j++)
114 defcopy[j] = seqsbuffer.header()[j];
115
116 defcopy[j] = 0;
117
118 #if 0
119 fprintf(stdout, "%u] Trim from 0:%u to %u:%u\n",
120 i,
121 seqsbuffer.sequenceLength(),
122 lrange[i],
123 hrange[i]);
124 #endif
125
126 fprintf(stdout, "%s trimmed to %u:%u\n%s\n", defcopy, lrange[i], hrange[i], seqcopy);
127 }
128
129 if ((i & 0x1ff) == 0) {
130 fprintf(stderr, "Writing trimmed sequences: %u\r", i);
131 fflush(stderr);
132 }
133 }
134
135 fprintf(stderr, "\n");
136
137 return(0);
138 }