|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 Benjamin Jean-Marie Tremblay |
| 3 | + * |
| 4 | + * This file is part of sequenceshuffler. |
| 5 | + * |
| 6 | + * sequenceshuffler is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * sequenceshuffler is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with sequenceshuffler. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include <iostream> |
| 22 | +#include <fstream> |
| 23 | +#include <unistd.h> |
| 24 | +#include <string> |
| 25 | +using namespace std; |
| 26 | + |
| 27 | +void usage() { |
| 28 | + printf( |
| 29 | + "countfa v1.0 Copyright (C) 2019 Benjamin Jean-Marie Tremblay \n" |
| 30 | + " \n" |
| 31 | + " Usage: coutfa -i [filename] \n" |
| 32 | + " cat [filename] | coutfa \n" |
| 33 | + " \n" |
| 34 | + " -i <str> Input filename. File must be fasta-formatted. Alternatively, takes \n" |
| 35 | + " input from a pipe. \n" |
| 36 | + " -h Print usage and exit. \n" |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +void do_countfa(istream &input) { |
| 41 | + |
| 42 | + string name, line; |
| 43 | + int counter{0}; |
| 44 | + |
| 45 | + while (getline(input, line).good()) { |
| 46 | + |
| 47 | + if (line.empty() || line[0] == '>') { |
| 48 | + |
| 49 | + if (!name.empty()) { |
| 50 | + cout << name << endl; |
| 51 | + name.clear(); |
| 52 | + } |
| 53 | + if (!line.empty()) { |
| 54 | + name = line; |
| 55 | + } |
| 56 | + if (counter > 0) { |
| 57 | + cout << counter << endl; |
| 58 | + } |
| 59 | + counter = 0; |
| 60 | + |
| 61 | + } else if (!name.empty()) { |
| 62 | + |
| 63 | + if (line.find(' ') != string::npos) { |
| 64 | + line.erase(remove(line.begin(), line.end(), ' '), line.end()); |
| 65 | + } |
| 66 | + |
| 67 | + if (line.length() == 0) { |
| 68 | + name.clear(); |
| 69 | + counter = 0; |
| 70 | + } else { |
| 71 | + counter += line.length(); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + if (!name.empty()) { |
| 79 | + cout << name << endl; |
| 80 | + cout << counter << endl; |
| 81 | + } |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +int main(int argc, char **argv) { |
| 86 | + |
| 87 | + int opt; |
| 88 | + bool has_file{false}; |
| 89 | + ifstream seqfile; |
| 90 | + |
| 91 | + while ((opt = getopt(argc, argv, "i:h")) != -1) { |
| 92 | + switch (opt) { |
| 93 | + case 'i': if (optarg) { |
| 94 | + seqfile.open(optarg); |
| 95 | + if (seqfile.bad()) { |
| 96 | + cerr << "Error: file not found" << endl; |
| 97 | + exit(EXIT_FAILURE); |
| 98 | + } |
| 99 | + has_file = true; |
| 100 | + } |
| 101 | + break; |
| 102 | + case 'h': usage(); |
| 103 | + return 0; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (!has_file) { |
| 108 | + if (isatty(STDIN_FILENO)) { |
| 109 | + usage(); |
| 110 | + exit(EXIT_FAILURE); |
| 111 | + } |
| 112 | + do_countfa(cin); |
| 113 | + } else { |
| 114 | + do_countfa(seqfile); |
| 115 | + } |
| 116 | + |
| 117 | + return 0; |
| 118 | + |
| 119 | +} |
0 commit comments