Debian Codegraph @

leaff/leaff.C

raw ◀ ▶
1 Debian patch touch this file
1 #include "bio++.H"
2 #include "seqCache.H"
3 #include "seqStore.H"
4
5
6 // Analysis functions
7 //
8 void dumpBlocks(char *filename);
9 void stats(char *filename, uint64 refLen);
10 void partitionBySize(char *prefix, uint64 partitionSize, char *filename);
11 void partitionByBucket(char *prefix, uint64 partitionSize, char *filename);
12 void partitionBySegment(char *prefix, uint64 numSegments, char *filename);
13 void simseq(char *,char *,int,int,int,int,double);
14 void computeGCcontent(char *name);
15 void findDuplicates(char *filename);
16 void mapDuplicates(char *filea, char *fileb);
17
18 void processFile(char *filename);
19 void processArray(int argc, char **argv);
20
21 bool doReverse = false;
22 bool doComplement = false;
23 bool withDefLine = true;
24 char *specialDefLine = 0L;
25 uint32 withLineBreaks = 0;
26
27 bool toUppercase = false;
28 char translate[256] = {0};
29
30 seqCache *fasta = 0L;
31
32 uint32 begPos = (uint32)0;
33 uint32 endPos = ~(uint32)0;
34
35 uint32 endExtract = ~(uint32)0;
36
37 mt_s *mtctx = 0L;
38
39
40 static
41 void
42 failIfNoSource(void) {
43 if (fasta == 0L)
44 fprintf(stderr, "No source file specified.\n"), exit(1);
45 }
46
47 static
48 void
49 failIfNotRandomAccess(void) {
50 if (fasta->randomAccessSupported() == false)
51 fprintf(stderr, "Algorithm required random access; soruce file not supported.\n"), exit(1);
52 }
53
54
55 static
56 void
57 helpStandard(char *program) {
58 fprintf(stderr, "usage: %s [-f fasta-file] [options]\n", program);
59 fprintf(stderr, "\n");
60 fprintf(stderr, "SOURCE FILES\n");
61 fprintf(stderr, " -f file: use sequence in 'file' (-F is also allowed for historical reasons)\n");
62 fprintf(stderr, " -A file: read actions from 'file'\n");
63 fprintf(stderr, "\n");
64 fprintf(stderr, "SOURCE FILE EXAMINATION\n");
65 fprintf(stderr, " -d: print the number of sequences in the fasta\n");
66 fprintf(stderr, " -i name: print an index, labelling the source 'name'\n");
67 fprintf(stderr, "\n");
68 fprintf(stderr, "OUTPUT OPTIONS\n");
69 fprintf(stderr, " -6 <#>: insert a newline every 60 letters\n");
70 fprintf(stderr, " (if the next arg is a number, newlines are inserted every\n");
71 fprintf(stderr, " n letters, e.g., -6 80. Disable line breaks with -6 0,\n");
72 fprintf(stderr, " or just don't use -6!)\n");
73 fprintf(stderr, " -e beg end: Print only the bases from position 'beg' to position 'end'\n");
74 fprintf(stderr, " (space based, relative to the FORWARD sequence!) If\n");
75 fprintf(stderr, " beg == end, then the entire sequence is printed. It is an\n");
76 fprintf(stderr, " error to specify beg > end, or beg > len, or end > len.\n");
77 fprintf(stderr, " -ends n Print n bases from each end of the sequence. One input\n");
78 fprintf(stderr, " sequence generates two output sequences, with '_5' or '_3'\n");
79 fprintf(stderr, " appended to the ID. If 2n >= length of the sequence, the\n");
80 fprintf(stderr, " sequence itself is printed, no ends are extracted (they\n");
81 fprintf(stderr, " overlap).\n");
82 fprintf(stderr, " -C: complement the sequences\n");
83 fprintf(stderr, " -H: DON'T print the defline\n");
84 fprintf(stderr, " -h: Use the next word as the defline (\"-H -H\" will reset to the\n");
85 fprintf(stderr, " original defline\n");
86 fprintf(stderr, " -R: reverse the sequences\n");
87 fprintf(stderr, " -u: uppercase all bases\n");
88 fprintf(stderr, "\n");
89 fprintf(stderr, "SEQUENCE SELECTION\n");
90 fprintf(stderr, " -G n s l: print n randomly generated sequences, 0 < s <= length <= l\n");
91 fprintf(stderr, " -L s l: print all sequences such that s <= length < l\n");
92 fprintf(stderr, " -N l h: print all sequences such that l <= %% N composition < h\n");
93 fprintf(stderr, " (NOTE 0.0 <= l < h < 100.0)\n");
94 fprintf(stderr, " (NOTE that you cannot print sequences with 100%% N\n");
95 fprintf(stderr, " This is a useful bug).\n");
96 fprintf(stderr, " -q file: print sequences from the seqid list in 'file'\n");
97 fprintf(stderr, " -r num: print 'num' randomly picked sequences\n");
98 fprintf(stderr, " -s seqid: print the single sequence 'seqid'\n");
99 fprintf(stderr, " -S f l: print all the sequences from ID 'f' to 'l' (inclusive)\n");
100 fprintf(stderr, " -W: print all sequences (do the whole file)\n");
101 fprintf(stderr, "\n");
102 fprintf(stderr, "LONGER HELP\n");
103 fprintf(stderr, " -help analysis\n");
104 fprintf(stderr, " -help examples\n");
105 }
106
107
108 static
109 void
110 helpAnalysis(char *program) {
111 fprintf(stderr, "usage: %s [-f <fasta-file>] [options]\n", program);
112 fprintf(stderr, "\n");
113 fprintf(stderr, " --findduplicates a.fasta\n");
114 fprintf(stderr, " Reports sequences that are present more than once. Output\n");
115 fprintf(stderr, " is a list of pairs of deflines, separated by a newline.\n");
116 fprintf(stderr, "\n");
117 fprintf(stderr, " --mapduplicates a.fasta b.fasta\n");
118 fprintf(stderr, " Builds a map of IIDs from a.fasta and b.fasta that have\n");
119 fprintf(stderr, " identical sequences. Format is \"IIDa <-> IIDb\"\n");
120 fprintf(stderr, "\n");
121 fprintf(stderr, " --md5 a.fasta:\n");
122 fprintf(stderr, " Don't print the sequence, but print the md5 checksum\n");
123 fprintf(stderr, " (of the entire sequence) followed by the entire defline.\n");
124 fprintf(stderr, "\n");
125 fprintf(stderr, " --partition prefix [ n[gmk]bp | n ] a.fasta\n");
126 fprintf(stderr, " --partitionmap [ n[gmk]bp | n ] a.fasta\n");
127 fprintf(stderr, " Partition the sequences into roughly equal size pieces of\n");
128 fprintf(stderr, " size nbp, nkbp, nmbp or ngbp; or into n roughly equal sized\n");
•129 fprintf(stderr, " partitions. Sequences larger that the partition size are\n");
130 fprintf(stderr, " in a partition by themself. --partitionmap writes a\n");
131 fprintf(stderr, " description of the partition to stdout; --partiton creates\n");
132 fprintf(stderr, " a fasta file 'prefix-###.fasta' for each partition.\n");
133 fprintf(stderr, " Example: -F some.fasta --partition parts 130mbp\n");
134 fprintf(stderr, " -F some.fasta --partition parts 16\n");
135 fprintf(stderr, "\n");
136 fprintf(stderr, " --segment prefix n a.fasta\n");
137 fprintf(stderr, " Splits the sequences into n files, prefix-###.fasta.\n");
138 fprintf(stderr, " Sequences are not reordered.\n");
139 fprintf(stderr, "\n");
140 fprintf(stderr, " --gccontent a.fasta\n");
141 fprintf(stderr, " Reports the GC content over a sliding window of\n");
142 fprintf(stderr, " 3, 5, 11, 51, 101, 201, 501, 1001, 2001 bp.\n");
143 fprintf(stderr, "\n");
144 fprintf(stderr, " --testindex a.fasta\n");
145 fprintf(stderr, " Test the index of 'file'. If index is up-to-date, leaff\n");
146 fprintf(stderr, " exits successfully, else, leaff exits with code 1. If an\n");
147 fprintf(stderr, " index file is supplied, that one is tested, otherwise, the\n");
148 fprintf(stderr, " default index file name is used.\n");
149 fprintf(stderr, "\n");
150 fprintf(stderr, " --dumpblocks a.fasta\n");
151 fprintf(stderr, " Generates a list of the blocks of N and non-N. Output\n");
152 fprintf(stderr, " format is 'base seq# beg end len'. 'N 84 483 485 2' means\n");
153 fprintf(stderr, " that a block of 2 N's starts at space-based position 483\n");
154 fprintf(stderr, " in sequence ordinal 84. A '.' is the end of sequence\n");
155 fprintf(stderr, " marker.\n");
156 fprintf(stderr, "\n");
157 fprintf(stderr, " --errors L N C P a.fasta\n");
158 fprintf(stderr, " For every sequence in the input file, generate new\n");
159 fprintf(stderr, " sequences including simulated sequencing errors.\n");
160 fprintf(stderr, " L -- length of the new sequence. If zero, the length\n");
161 fprintf(stderr, " of the original sequence will be used.\n");
162 fprintf(stderr, " N -- number of subsequences to generate. If L=0, all\n");
163 fprintf(stderr, " subsequences will be the same, and you should use\n");
164 fprintf(stderr, " C instead.\n");
165 fprintf(stderr, " C -- number of copies to generate. Each of the N\n");
166 fprintf(stderr, " subsequences will have C copies, each with different\n");
167 fprintf(stderr, " errors.\n");
168 fprintf(stderr, " P -- probability of an error.\n");
169 fprintf(stderr, "\n");
170 fprintf(stderr, " HINT: to simulate ESTs from genes, use L=500, N=10, C=10\n");
171 fprintf(stderr, " -- make C=10 sequencer runs of N=10 EST sequences\n");
172 fprintf(stderr, " of length 500bp each.\n");
173 fprintf(stderr, " to simulate mRNA from genes, use L=0, N=10, C=10\n");
174 fprintf(stderr, " to simulate reads from genomes, use L=800, N=10, C=1\n");
175 fprintf(stderr, " -- of course, N= should be increased to give the\n");
176 fprintf(stderr, " appropriate depth of coverage\n");
177 fprintf(stderr, "\n");
178 fprintf(stderr, " --stats a.fasta [refLen]\n");
179 fprintf(stderr, " Reports size statistics; number, N50, sum, largest.\n");
180 fprintf(stderr, " If 'refLen' is supplied, N50 is based on this size.\n");
181 fprintf(stderr, "\n");
182 fprintf(stderr, " --seqstore out.seqStore\n");
183 fprintf(stderr, " Converts the input file (-f) to a seqStore file.\n");
184 }
185
186
187 static
188 void
189 helpExamples(char *program) {
190 fprintf(stderr, "usage: %s [-f <fasta-file>] [options]\n", program);
191 fprintf(stderr, "\n");
192 fprintf(stderr, "Options are ORDER DEPENDENT. Sequences are printed whenever an ACTION occurs\n");
193 fprintf(stderr, "on the command line. SEQUENCE OPTIONS are not reset when a sequence is printed.\n");
194 fprintf(stderr, "\n");
195 fprintf(stderr, "SEQUENCES are numbered starting at ZERO, not one.\n");
196 fprintf(stderr, "\n");
197 fprintf(stderr, " Print the first 10 bases of the fourth sequence in file 'genes':\n");
198 fprintf(stderr, " -f genes -e 0 10 -s 3\n");
199 fprintf(stderr, "\n");
200 fprintf(stderr, " Print the first 10 bases of the fourth and fifth sequences:\n");
201 fprintf(stderr, " -f genes -e 0 10 -s 3 -s 4\n");
202 fprintf(stderr, "\n");
203 fprintf(stderr, " Print the fourth and fifth sequences reverse complemented, and the sixth\n");
204 fprintf(stderr, " sequence forward. The second set of -R -C toggle off reverse-complement:\n");
205 fprintf(stderr, " -f genes -R -C -s 3 -s 4 -R -C -s 5\n");
206 fprintf(stderr, "\n");
207 fprintf(stderr, " Convert file 'genes' to a seqStore 'genes.seqStore'. The seqStore\n");
208 fprintf(stderr, " provides better performance with the kmer tools.\n");
209 fprintf(stderr, " -f genes --seqstore genes.seqStore\n");
210 }
211
212
213 static
214 void
215 printSequence(char *def,
216 char *seq,
217 uint32 beg,
218 uint32 end) {
219
220 if (beg >= end)
221 return;
222
223 if ((endExtract != ~uint32ZERO) &&
224 (endExtract + endExtract < end - beg)) {
225 char d[1024];
226 uint32 l = strlen(seq);
227
228 sprintf(d, "%s_5", def);
229 printSequence(d, seq, 0, endExtract);
230
231 sprintf(d, "%s_3", def);
232 printSequence(d, seq, l-endExtract, l);
233
234 return;
235 }
236
237 if (specialDefLine)
238 def = specialDefLine;
239
240 if (withDefLine == false)
241 def = 0L;
242
243 uint32 limit = end - beg;
244 char *n = new char [end - beg + 1];
245 char *m;
246
247 if ((doReverse == false) && (doComplement == false)) {
248 m = n;
249 seq += beg;
250 while (limit--)
251 *(m++) = translate[*(seq++)];
252
253 } else if ((doReverse == true) && (doComplement == false)) {
254 m = n + limit - 1;
255 seq += beg;
256 while (limit--)
257 *(m--) = translate[*(seq++)];
258
259 } else if ((doReverse == false) && (doComplement == true)) {
260 m = n;
261 seq += beg;
262 while (limit--)
263 *(m++) = complementSymbol[translate[*(seq++)]];
264
265 } else if ((doReverse == true) && (doComplement == true)) {
266 m = n + limit - 1;
267 seq += beg;
268 while (limit--)
269 *(m--) = complementSymbol[translate[*(seq++)]];
270 }
271
272 n[end-beg] = 0;
273
274 if (def)
275 fprintf(stdout, ">%s\n", def);
276
277 if (withLineBreaks) {
278 char *t = n;
279 char *a = new char [withLineBreaks+1];
280
281 while (*t) {
282 uint32 i=0;
283 while ((*t) && (i < withLineBreaks))
284 a[i++] = *(t++);
285 a[i++] = '\n';
286 a[i] = 0;
287 fprintf(stdout, "%s", a);
288 }
289
290 delete [] a;
291 } else {
292 fprintf(stdout, "%s\n", n);
293 }
294
295 delete [] n;
296 }
297
298
299 static
300 void
301 printSequence(seqInCore *sic) {
302 printSequence(sic->header(), sic->sequence(), (begPos!=(uint32)0) ? begPos:0, (endPos!=~uint32(0)) ? endPos:sic->sequenceLength());
303 }
304
305
306 static
307 void
308 printSequence(uint32 sid) {
309 seqInCore *sic = fasta->getSequenceInCore(sid);
310 if (sic == 0L)
311 fprintf(stderr, "WARNING: Didn't find sequence with iid '"uint32FMT"'\n", sid);
312 else
313 printSequence(sic);
314 delete sic;
315 }
316
317
318 static
319 void
320 printSequence(char *sid) {
321 seqInCore *sic = fasta->getSequenceInCore(sid);
322 if (sic == 0L)
323 fprintf(stderr, "WARNING: Didn't find sequence with name/iid '%s'\n", sid);
324 else
325 printSequence(sic);
326 delete sic;
327 }
328
329
330 static
331 void
332 printIDsFromFile(char *name) {
333 uint32 idLen = 0;
334 uint32 idMax = 63;
335 char *id = new char [idMax+1];
336
337 readBuffer B(name);
338 char x = B.read();
339
340 // For optimal performance, we should sort the list of ID's given
341 // by their IID, but the user might have a good reason for wanting
342 // them unsorted.
343
344 while (B.eof() == false) {
345 while (whitespaceSymbol[x] && (B.eof() == false))
346 x = B.read();
347
348 if (B.eof() == false) {
349 idLen = 0;
350
351 while (!whitespaceSymbol[x] && (B.eof() == false)) {
352 id[idLen++] = x;
353 x = B.read();
354
355 if (idLen >= idMax) {
356 idMax *= 2;
357 char *newid = new char [idMax+1];
358 memcpy(newid, id, sizeof(char) * idLen);
359 delete [] id;
360 id = newid;
361 }
362 }
363
364 id[idLen] = 0;
365
366 seqInCore *S = fasta->getSequenceInCore(id);
367
368 if (S == 0L)
369 fprintf(stderr, "WARNING: Didn't find sequence with name/iid '%s'\n", id);
370 else
371 printSequence(S);
372 }
373 }
374
375 delete [] id;
376 }
377
378
379 void
380 processArray(int argc, char **argv) {
381
382 int arg = 1;
383 while (arg < argc) {
384
385 if ((strcmp(argv[arg], "-f") == 0) ||
386 (strcmp(argv[arg], "-F") == 0)) {
387 delete fasta;
388 fasta = new seqCache(argv[++arg]);
389
390 } else if (strcmp(argv[arg], "-i") == 0) {
391
392 failIfNoSource();
393
394 ++arg;
395 if ((argv[arg] == 0L) || (argv[arg][0] == '-'))
396 fprintf(stderr, "ERROR: next arg to -i should be 'name', I got '%s'\n",
397 (argv[arg] == 0L) ? "(nullpointer)" : argv[arg]), exit(1);
398
399 for (uint32 s=0; s<fasta->getNumberOfSequences(); s++)
400 fprintf(stdout, "G\tseq\t%s:"uint32FMT"\t"uint32FMT"\t%s\n",
401 argv[arg], s, fasta->getSequenceLength(s), ">unimplemented");
402
403 } else if (strcmp(argv[arg], "-d") == 0) {
404 failIfNoSource();
405 printf(uint32FMT"\n", fasta->getNumberOfSequences());
406
407 } else if (strcmp(argv[arg], "-L") == 0) {
408 uint32 small = strtouint32(argv[++arg], 0L);
409 uint32 large = strtouint32(argv[++arg], 0L);
410
411 failIfNoSource();
412
413 for (uint32 s=0; s<fasta->getNumberOfSequences(); s++)
414 if ((small <= fasta->getSequenceLength(s)) && (fasta->getSequenceLength(s) < large))
415 printSequence(s);
416
417 } else if (strcmp(argv[arg], "-N") == 0) {
418 double small = atof(argv[++arg]);
419 double large = atof(argv[++arg]);
420
421 failIfNoSource();
422
423 for (uint32 s=0; s<fasta->getNumberOfSequences(); s++) {
424 seqInCore *S = fasta->getSequenceInCore(s);
425 uint32 Ns = 0;
426 uint32 len = S->sequenceLength();
427 char *seq = S->sequence();
428
429 for (uint32 i=begPos; i<len && i<endPos; i++)
430 if ((seq[i] == 'n') || (seq[i] == 'N'))
431 Ns++;
432
433 double Np = 100.0 * Ns / len;
434
435 if ((small <= Np) && (Np < large))
436 printSequence(S);
437
438 delete S;
439 }
440
441 } else if (strcmp(argv[arg], "-W") == 0) {
442 failIfNoSource();
443
444 for (uint32 s=0; s<fasta->getNumberOfSequences(); s++)
445 printSequence(s);
446
447 } else if (strcmp(argv[arg], "-G") == 0) {
448 uint32 n = strtouint32(argv[++arg], 0L);
449 uint32 s = strtouint32(argv[++arg], 0L);
450 uint32 l = strtouint32(argv[++arg], 0L);
451
452 char bases[4] = {'A', 'C', 'G', 'T'};
453 char *def = new char [1024];
454 char *seq = new char [l + 1];
455
456 if (s == 0)
457 s = 1;
458 if (s > l)
459 fprintf(stderr, "leaff: usage: -G num-seqs min-length max-length\n"), exit(1);
460
461 for (uint32 i=0; i<n; i++) {
462 uint32 j = s + ((l-s == 0) ? 0 : (mtRandom32(mtctx) % (l-s)));
463 uint32 p = 0;
464
465 while (p < j)
466 seq[p++] = bases[mtRandom32(mtctx) & 0x3];
467 seq[p] = 0;
468
469 sprintf(def, "random"uint32FMTW(06), i);
470
471 printSequence(def, seq, 0, j);
472 }
473
474 delete [] seq;
475 delete [] def;
476
477 } else if (strcmp(argv[arg], "-s") == 0) {
478 failIfNoSource();
479 failIfNotRandomAccess(); // Easy to fix, just read the first N sequences
480 printSequence(argv[++arg]);
481
482 } else if (strcmp(argv[arg], "-S") == 0) {
483 failIfNoSource();
484 failIfNotRandomAccess(); // Easy to fix, just read the first N sequences
485
486 uint32 lowID = fasta->getSequenceIID(argv[++arg]);
487 uint32 highID = fasta->getSequenceIID(argv[++arg]);
488
489 if (lowID > highID) {
490 uint32 t = lowID;
491 lowID = highID;
492 highID = t;
493 }
494
495 for (uint32 s=lowID; (s <= highID) && (s <= fasta->getNumberOfSequences()); s++)
496 printSequence(s);
497
498 } else if (strcmp(argv[arg], "-r") == 0) {
499 uint32 num = strtouint32(argv[++arg], 0L);
500
501 failIfNoSource();
502 failIfNotRandomAccess(); // Impossible to fix, or load whole thing into memory
503
504 if (num >= fasta->getNumberOfSequences())
505 num = fasta->getNumberOfSequences();
506
507 uint32 *seqs = new uint32 [fasta->getNumberOfSequences()];
508
509 for (uint32 i=0; i<fasta->getNumberOfSequences(); i++)
510 seqs[i] = i;
511
512 for (uint32 i=0; i<fasta->getNumberOfSequences(); i++) {
513 uint32 j = mtRandom32(mtctx) % (fasta->getNumberOfSequences() - i) + i;
514 uint32 t = seqs[j];
515 seqs[j] = seqs[i];
516 seqs[i] = t;
517 }
518
519 for (uint32 i=0; i<num; i++)
520 printSequence(seqs[i]);
521
522 delete [] seqs;
523
524 } else if (strcmp(argv[arg], "-q") == 0) {
525 failIfNoSource();
526 failIfNotRandomAccess(); // Impossible to fix, or load whole thing into memory
527 printIDsFromFile(argv[++arg]);
528
529 } else if (strcmp(argv[arg], "-6") == 0) {
530 withLineBreaks = 60;
531 if ((argv[arg+1] != 0L) && (argv[arg+1][0] != '-'))
532 withLineBreaks = strtouint32(argv[++arg], 0L);
533
534 } else if (strcmp(argv[arg], "-w") == 0) {
535 toUppercase = !toUppercase;
536 for (int z=0; z<256; z++)
537 translate[z] = (toUppercase) ? (char)toUpper[z] : (char)z;
538
539 } else if (strcmp(argv[arg], "-R") == 0) {
540 doReverse = !doReverse;
541
542 } else if (strcmp(argv[arg], "-C") == 0) {
543 doComplement = !doComplement;
544
545 } else if (strcmp(argv[arg], "-H") == 0) {
546 withDefLine = !withDefLine;
547 specialDefLine = 0L;
548
549 } else if (strcmp(argv[arg], "-h") == 0) {
550 withDefLine = true;
551 specialDefLine = argv[++arg];
552
553 } else if (strcmp(argv[arg], "-e") == 0) {
554 begPos = strtouint32(argv[++arg], 0L);
555 endPos = strtouint32(argv[++arg], 0L);
556
557 } else if (strcmp(argv[arg], "-ends") == 0) {
558 endExtract = strtouint32(argv[++arg], 0L);
559
560 } else if (strcmp(argv[arg], "-A") == 0) {
561 processFile(argv[++arg]);
562
563
564
565 } else if (strcmp(argv[arg], "--findduplicates") == 0) {
566 findDuplicates(argv[++arg]);
567 exit(0);
568
569 } else if (strcmp(argv[arg], "--mapduplicates") == 0) {
570 mapDuplicates(argv[arg+1], argv[arg+2]);
571 exit(0);
572
573 } else if (strcmp(argv[arg], "--md5") == 0) {
574 md5_s md5;
575 char sum[33];
576
577 fasta = new seqCache(argv[++arg]);
578
579 for (uint32 s=0; s<fasta->getNumberOfSequences(); s++) {
580 seqInCore *S = fasta->getSequenceInCore(s);
581 fprintf(stdout, "%s %s\n",
582 md5_toascii(md5_string(&md5, S->sequence(), S->sequenceLength()), sum),
583 S->header());
584 delete S;
585 }
586 delete fasta;
587 exit(0);
588
589 } else if ((strcmp(argv[arg], "--partition") == 0) ||
590 (strcmp(argv[arg], "--partitionmap") == 0)) {
591
592 char *prefix = 0L;
593 if (strcmp(argv[arg], "--partition") == 0)
594 prefix = argv[++arg];
595
596 // does the next arg end with gbp, mbp, kbp or bp? If so,
597 // partition by length, else partition into buckets.
598 //
599 int al = strlen(argv[arg+1]);
600 uint64 ps = strtouint64(argv[arg+1], 0L);
601
602 char a3 = (al<3) ? '0' : (char)toLower[argv[arg+1][al-3]];
603 char a2 = (al<2) ? '0' : (char)toLower[argv[arg+1][al-2]];
604 char a1 = (al<1) ? '0' : (char)toLower[argv[arg+1][al-1]];
605
606 // partition!
607
608 if (!isdigit(a1) || !isdigit(a2) || !isdigit(a3)) {
609 if ((a3 == 'g') && (a2 == 'b') && (a1 == 'p')) {
610 ps *= 1000000000;
611 } else if ((a3 == 'm') && (a2 == 'b') && (a1 == 'p')) {
612 ps *= 1000000;
613 } else if ((a3 == 'k') && (a2 == 'b') && (a1 == 'p')) {
614 ps *= 1000;
615 } else if (isdigit(a3) && (a2 == 'b') && (a1 == 'p')) {
616 ps *= 1;
617 } else {
618 fprintf(stderr, "Unknown partition size option '%s'\n", argv[arg+1]), exit(1);
619 }
620
621 if (ps == 0)
622 fprintf(stderr, "Unknown or zero partition size '%s'\n", argv[arg+1]), exit(1);
623 partitionBySize(prefix, ps, argv[arg+2]);
624 } else {
625 if (ps == 0)
626 fprintf(stderr, "Unknown or zero partition size '%s'\n", argv[arg+1]), exit(1);
627 partitionByBucket(prefix, ps, argv[arg+2]);
628 }
629 exit(0);
630
631 } else if (strcmp(argv[arg], "--segment") == 0) {
632 partitionBySegment(argv[arg+1], strtouint32(argv[arg+2], 0L), argv[arg+3]);
633 exit(0);
634
635 } else if (strcmp(argv[arg], "--gccontent") == 0) {
636 computeGCcontent(argv[++arg]);
637 exit(0);
638
639 } else if (strcmp(argv[arg], "--dumpblocks") == 0) {
640 dumpBlocks(argv[++arg]);
641 exit(0);
642
643 } else if (strcmp(argv[arg], "--stats") == 0) {
644 stats(argv[arg+1], (argv[arg+2] != 0L) ? strtouint64(argv[arg+2], 0L) : 0);
645 exit(0);
646
647 } else if (strcmp(argv[arg], "--errors") == 0) {
648 int L = strtouint32(argv[++arg], 0L); // Desired length
649 int l = 0; // min of desired length, length of sequence
650 int N = strtouint32(argv[++arg], 0L); // number of copies per sequence
651 int C = strtouint32(argv[++arg], 0L); // number of mutations per copy
652 double P = atof(argv[++arg]); // probability of mutation
653 uint32 i = 0;
654
655 fasta = new seqCache(argv[++arg]);
656
657 seqInCore *S = fasta->getSequenceInCore(i++);
658 while (S) {
659 char *seq = S->sequence();
660 char *hdr = S->header();
661 int len = S->sequenceLength();
662
663 l = len;
664 if ((L > 0) && (L < len))
665 l = L;
666
667 simseq(seq, hdr, len, N, l, C, P);
668
669 delete S;
670 S = fasta->getSequenceInCore(i++);
671 }
672 delete fasta;
673 exit(0);
674
675 } else if (strcmp(argv[arg], "--seqstore") == 0) {
676 constructSeqStore(argv[++arg], fasta);
677 exit(0);
678
679 } else if (strcmp(argv[arg], "-help") == 0) {
680 if ((argv[arg+1]) && (strcmp(argv[arg+1], "analysis") == 0))
681 helpAnalysis(argv[0]);
682 else if ((argv[arg+1]) && (strcmp(argv[arg+1], "examples") == 0))
683 helpExamples(argv[0]);
684 else
685 helpStandard(argv[0]);
686 exit(0);
687
688 } else {
689 helpStandard(argv[0]);
690 fprintf(stderr, "Unknown option '%s'\n", argv[arg]);
691 exit(1);
692 }
693
694 arg++;
695 }
696
697 delete fasta;
698 fasta = 0L;
699 }
700
701
702 void
703 processFile(char *filename) {
704 FILE *F = NULL;
705
706 if (strcmp(filename, "-") == 0) {
707 F = stdin;
708 } else {
709 errno = 0;
710 F = fopen(filename, "r");
711 if (errno)
712 fprintf(stderr, "Couldn't open '%s': %s\n", filename, strerror(errno)), exit(1);
713 }
714
715 uint64 max = 16 * 1024 * 1024;
716 uint64 pos = 0;
717 size_t len = 0;
718
719 char *data = new char [max];
720
721 // Suck the file into 'data'
722
723 while (!feof(F)) {
724 errno = 0;
725 len = fread(data+pos, 1, max - pos, F);
726 if (errno)
727 fprintf(stderr, "Couldn't read "uint64FMT" bytes from '%s': %s\n",
728 (uint64)(max-pos), filename, strerror(errno)), exit(1);
729
730 pos += len;
731
732 if (pos >= max) {
733 max += 16 * 1024 * 1024;
734 char *tmpd = new char [max];
735 memcpy(tmpd, data, pos);
736 delete [] data;
737 data = tmpd;
738 }
739 }
740
741 if (strcmp(filename, "-") != 0)
742 fclose(F);
743
744 len = pos;
745
746 // (over)count the number of words; we start at two, since the
747 // first arg is the name of the program, and if there is only one
748 // word and no whitespace in the file, the below loop fails to
749 // count the second word.
750
751 int argc = 2;
752 char **argv = 0L;
753
754 for (uint32 i=0; i<len; i++) {
755 if (isspace(data[i])) {
756 argc++;
757 data[i] = 0;
758 }
759 }
760
761 // Allocate space for word pointers, then set them. First arg in
762 // argv[] is the name of the program -- we'll substitute the name
763 // of the file instead.
764
765 argv = new char * [argc];
766
767 argv[0] = filename;
768 argc = 1;
769
770 // Three steps: Skip leading whitespace; save the arg if it's a
771 // real arg (and not the end of the file; then skip the word.
772
773 for (uint32 pos=0; pos<len; pos++) {
774 while ((data[pos] == 0) && (pos < len))
775 pos++;
776 if (pos < len)
777 argv[argc++] = data+pos;
778 while ((data[pos] != 0) && (pos < len))
779 pos++;
780 }
781
782 processArray(argc, argv);
783
784 delete [] argv;
785 delete [] data;
786 }
787
788
789 int
790 main(int argc, char **argv) {
791
792 if (argc < 2) {
793 helpStandard(argv[0]);
794 exit(1);
795 }
796
797 for (int z=0; z<256; z++)
798 translate[z] = (char)z;
799
800 mtctx = mtInit(getpid() * time(NULL));
801
802 processArray(argc, argv);
803
804 delete fasta;
805 }
806
807
808
809