| 1 | // This file is part of A2Amapper. |
| 2 | // Copyright (c) 2005 J. Craig Venter Institute |
| 3 | // Author: Brian Walenz |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation; either version 2 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received (LICENSE.txt) a copy of the GNU General Public |
| 16 | // License along with this program; if not, write to the Free Software |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <ctype.h> |
| 23 | |
| 24 | #include "util++.H" |
| 25 | |
| 26 | // Filters out matches that are too short. |
| 27 | // |
| 28 | // Original implementation in Python by Clark Mobarry. |
| 29 | |
| 30 | void |
| 31 | readHeader(char *inLine, FILE *in, uint32 &minLength, FILE *out) { |
| 32 | bool printedLength = false; |
| 33 | |
| 34 | fgets(inLine, 1024, in); |
| 35 | while (!feof(in) && (inLine[0] != 'M')) { |
| 36 | |
| 37 | if (strncmp(inLine, "/globalMatchMinSize", 18) == 0) { |
| 38 | if (minLength > 0) { |
| 39 | // Skip any whitespace, the =, and more whitespace. Copy. |
| 40 | char *tmp = inLine + 14; |
| 41 | while (isspace(*tmp)) tmp++; |
| 42 | while (*tmp == '=') tmp++; |
| 43 | while (isspace(*tmp)) tmp++; |
| 44 | minLength = strtouint32(tmp, 0L); |
| 45 | } |
| 46 | sprintf(inLine, "/globalMatchMinSize="uint32FMT"\n", minLength); |
| 47 | printedLength = true; |
| 48 | } |
| 49 | |
| 50 | if (out) |
| 51 | fputs(inLine, out); |
| 52 | |
| 53 | fgets(inLine, 1024, in); |
| 54 | } |
| 55 | |
| 56 | if (printedLength == false) |
| 57 | fprintf(stdout, "/globalMatchMinSize="uint32FMT"\n", minLength); |
| 58 | |
| 59 | if (minLength == 0) { |
| 60 | fprintf(stderr, "I didn't find /globalMatchMinSize, please set it with -l\n"); |
| 61 | exit(1); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | int |
| 67 | main(int argc, char **argv) { |
| 68 | char inLine[1024] = {0}; |
| 69 | uint32 minLength = 0; |
| 70 | uint32 totalDumped = 0; |
| 71 | uint32 totalDumpedLength = 0; |
| 72 | uint32 totalSaved = 0; |
| 73 | uint32 totalSavedLength = 0; |
| 74 | |
| 75 | int arg = 1; |
| 76 | while (arg < argc) { |
| 77 | if (strcmp(argv[arg], "-l") == 0) { |
| 78 | minLength = strtouint32(argv[++arg], 0L); |
| 79 | } else { |
| 80 | fprintf(stderr, "usage: %s [-h] [-l length] < matches.atac > matches.atac\n", argv[0]); |
| 81 | fprintf(stderr, " filters out all matches less than 'length' long.\n"); |
| 82 | exit(1); |
| 83 | } |
| 84 | arg++; |
| 85 | } |
| 86 | |
| 87 | readHeader(inLine, stdin, minLength, stdout); |
| 88 | |
| 89 | // we need to parse the header to get globalMatchMinSize, |
| 90 | // and then let the command line override it. just make |
| 91 | // a custom readHeader() for here, do it there. nothing |
| 92 | // difficult. |
| 93 | |
| 94 | while (!feof(stdin)) { |
| 95 | if (inLine[0] == 'M') { |
| 96 | splitToWords S(inLine); |
| 97 | |
| 98 | if ((strtouint32(S[ 6], 0L) >= minLength) && |
| 99 | (strtouint32(S[10], 0L) >= minLength)) { |
| 100 | totalSaved++; |
| 101 | totalSavedLength += strtouint32(S[ 6], 0L); |
| 102 | fputs(inLine, stdout); |
| 103 | } else { |
| 104 | totalDumped++; |
| 105 | totalDumpedLength += strtouint32(S[ 6], 0L); |
| 106 | } |
| 107 | } else { |
| 108 | fputs(inLine, stdout); |
| 109 | } |
| 110 | |
| 111 | fgets(inLine, 1024, stdin); |
| 112 | } |
| 113 | |
| 114 | fprintf(stderr, "lengthFilter: Discarded "uint32FMTW(8)" matches with total length "uint32FMTW(10)", %7.3f%% of the sequence in matches.\n", |
| 115 | totalDumped, totalDumpedLength, (double)totalDumpedLength / (totalDumpedLength + totalSavedLength) * 100.0); |
| 116 | fprintf(stderr, "lengthFilter: Saved "uint32FMTW(8)" matches with total length "uint32FMTW(10)", %7.3f%% of the sequence in matches.\n", |
| 117 | totalSaved, totalSavedLength, (double)totalSavedLength / (totalDumpedLength + totalSavedLength) * 100.0); |
| 118 | } |