Debian Codegraph @

leaff/fragmenter.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "bio++.H"
5 #include "seqCache.H"
6
7 // Splits a sequence into itty-bitty pieces.
8 //
9 // By default, splits into non-overlapping pieces of length L.
10 // Pieces will not start with nor end with N, but may have embedded N's.
11 //
12 // All pieces will be at least L long. Most pieces will be exactly L
13 // long. All pieces will be less than 2L long.
14 //
15 // If a piece has more than (currently) 50 N's, it will be broken --
16 // the first piece and last piece will be saved, and the middle (with
17 // the N's) will be discarded.
18
19
20 void
21 usage(char *name) {
22 fprintf(stderr, "usage: %s [-overlap len] -length len -input X.fasta -output Y.fasta -log T.log\n",
23 name);
24 exit(1);
25 }
26
27
28 int
29 main(int argc, char **argv) {
30 uint32 desiredLength = 0;
31 uint32 overlapLength = 0;
32 bool beVerbose = false;
33 seqCache *F = 0L;
34 seqInCore *B = 0L;
35 uint32 Bid = 0;
36 FILE *O = 0L;
37 FILE *L = 0L;
38
39 uint32 fragmentIndex = 0;
40
41 int arg=1;
42 while (arg < argc) {
43 if (strcmp(argv[arg], "-length") == 0) {
44 desiredLength = strtouint32(argv[++arg], 0L);
45 } else if (strcmp(argv[arg], "-overlap") == 0) {
46 overlapLength = strtouint32(argv[++arg], 0L);
47 } else if (strcmp(argv[arg], "-input") == 0) {
48 F = new seqCache(argv[++arg]);
49 } else if (strcmp(argv[arg], "-output") == 0) {
50 errno = 0;
51 O = fopen(argv[++arg], "w");
52 if (errno)
53 fprintf(stderr, "ERROR: Can't open output file '%s': %s\n", argv[arg], strerror(errno)), exit(1);
54 } else if (strcmp(argv[arg], "-log") == 0) {
55 errno = 0;
56 L = fopen(argv[++arg], "w");
57 if (errno)
58 fprintf(stderr, "ERROR: Can't open log file '%s': %s\n", argv[arg], strerror(errno)), exit(1);
59 } else if (strcmp(argv[arg], "-verbose") == 0) {
60 beVerbose = true;
61 } else {
62 usage(argv[arg]);
63 }
64
65 arg++;
66 }
67
68 if ((F == 0L) || (O == 0L) || (L == 0L))
69 usage(argv[0]);
70
71 B = F->getSequenceInCore(Bid);
72 while (B) {
73 if (beVerbose)
74 fprintf(stderr, "working on %s\n", B->header());
75
76 char *seq = (char *)B->sequence();
77
78 uint32 pos = 0;
79 uint32 max = 16384;
80 uint32 *sta = new uint32 [max];
81 uint32 *end = new uint32 [max];
82
83 // step 1: build a list of regions to output. Scan the sequence,
84 // making a new region if we see a significant chunk of N, or if we
85 // hit the desiredLength.
86 //
87 uint32 s = 0;
88 uint32 e = 0;
89 while (s < B->sequenceLength()) {
90
91 // Skip any N at the start
92 while ((seq[s] == 'n') || (seq[s] == 'N') && (s < B->sequenceLength()))
93 s++;
94
95 // Construct the preliminary block.
96 //
97 e = s + desiredLength;
98 if (e > B->sequenceLength())
99 e = B->sequenceLength();
100
101 fprintf(stderr, "got block1 "uint32FMT" - "uint32FMT"\n", s, e);
102
103 // Scan from s to e, looking for significant N. If we find it,
104 // reset e and stop.
105 //
106 uint32 numN = 0;
107 for (uint32 i=s; i<e; i++) {
108 if ((seq[i] == 'n') || (seq[i] == 'N')) {
109 numN++;
110 } else {
111 numN = 0;
112 }
113 if (numN >= 50) {
114 e = i;
115 break;
116 }
117 }
118
119 fprintf(stderr, "got block2 "uint32FMT" - "uint32FMT"\n", s, e);
120
121 // Back up e until we hit the first non-N
122 if ((s < e) && ((seq[e] == 'n') || (seq[e] == 'N'))) {
123 while ((s <= e) && ((seq[e] == 'n') || (seq[e] == 'N')))
124 e--;
125 e++;
126 }
127
128 fprintf(stderr, "got block3 "uint32FMT" - "uint32FMT"\n", s, e);
129
130 // Add this region
131 //
132 if (s > e) {
133 fprintf(stderr, "ERROR! s>e! "uint32FMT" "uint32FMT"\n", s, e);
134 }
135 if (s != e) {
136 fprintf(stderr, "ADD ["uint32FMTW(3)"] "uint32FMTW(9)" "uint32FMTW(9)" length "uint32FMTW(9)"\n", pos, s, e, e-s);
137 sta[pos] = s;
138 end[pos] = e;
139 pos++;
140 if (pos >= max) {
141 fprintf(stderr, "ERROR! max exceeded!\n");
142 }
143 }
144
145 s = e;
146 }
147
148
149 // If we're supposed to be overlapping, fiddle with the begin position to make it so.
150 //
151 if (overlapLength > 0) {
152 for (uint32 p=1; p<pos; p++) {
153 if (end[p-1] == sta[p]) {
154 sta[p] -= overlapLength;
155 fprintf(stderr, "ADJ ["uint32FMTW(3)"] "uint32FMTW(9)" "uint32FMTW(9)" length "uint32FMTW(9)"\n",
156 p, sta[p], end[p], end[p] - sta[p]);
157 }
158 }
159 }
160
161
162 if (beVerbose)
163 fprintf(stderr, "created %d regions\n", pos+1);
164
165
166 for (uint32 p=0; p<pos; p++) {
167
168 #if 1
169 fprintf(O, "%s begin "uint32FMT" end "uint32FMT" length "uint32FMT"\n",
170 B->header(), sta[p], end[p], end[p] - sta[p]);
171 fwrite(seq+sta[p], sizeof(char), end[p] - sta[p], O);
172 fprintf(O, "\n");
173 #endif
174
175 fprintf(L, uint32FMT" : "uint32FMT"["uint32FMT"-"uint32FMT"]\n",
176 fragmentIndex++,
177 B->getIID(),
178 sta[p],
179 end[p]);
180 }
181
182 delete [] sta;
183 delete [] end;
184
185 delete B;
186 B = F->getSequenceInCore(++Bid);
187 }
188
189 fclose(L);
190 fclose(O);
191 }