|
| 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 <string> |
| 22 | +#include <vector> |
| 23 | +#include <iostream> |
| 24 | +#include <fstream> |
| 25 | +#include <random> |
| 26 | +#include <unistd.h> |
| 27 | +using namespace std; |
| 28 | + |
| 29 | +void usage() { |
| 30 | + printf( |
| 31 | + "seqgen v1.0 Copyright (C) 2019 Benjamin Jean-Marie Tremblay \n" |
| 32 | + " \n" |
| 33 | + "Usage: seqgen [options] -a [letters] -l [length] -o [outfile] \n" |
| 34 | + " seqgen [options] -a [letters] -l [length] > [outfile] \n" |
| 35 | + " \n" |
| 36 | + " -o <str> Output filename. Alternatively, prints to stdout. \n" |
| 37 | + " -a <str> Comma-seperated sequence letters. Each letter can be made of any \n" |
| 38 | + " number of characters. \n" |
| 39 | + " -w <str> Comma-seperated letter weights. Order matches that of letters. If \n" |
| 40 | + " missing, assumes equal likelihood for all letters. \n" |
| 41 | + " -l <int> Final sequence length. This length is the number of characters in \n" |
| 42 | + " output string. \n" |
| 43 | + " -s <int> RNG seed number. Defaults to time in seconds. \n" |
| 44 | + " -h Print usage and exit. \n" |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +int main(int argc, char **argv) { |
| 49 | + |
| 50 | + unsigned int seqlen{0}; |
| 51 | + int alphlen, opt; |
| 52 | + ofstream outfile; |
| 53 | + bool has_out{false}, has_freqs{false}; |
| 54 | + default_random_engine gen; |
| 55 | + vector<string> lets; |
| 56 | + vector<double> freqs; |
| 57 | + string outletters, all_lets, all_freqs, final_freq, final_let; |
| 58 | + string comma = ","; |
| 59 | + unsigned int iseed = time(0); |
| 60 | + size_t last, next; |
| 61 | + |
| 62 | + if (argc == 1) { |
| 63 | + usage(); |
| 64 | + return 0; |
| 65 | + } |
| 66 | + |
| 67 | + while ((opt = getopt(argc, argv, "a:o:l:s:w:h")) != -1) { |
| 68 | + switch (opt) { |
| 69 | + case 'o': if (optarg) { |
| 70 | + outfile.open(optarg); |
| 71 | + if (outfile.bad()) { |
| 72 | + cerr << "Error: could not create outfile" << endl; |
| 73 | + exit(EXIT_FAILURE); |
| 74 | + } |
| 75 | + has_out = true; |
| 76 | + } |
| 77 | + break; |
| 78 | + case 'l': if (optarg) seqlen = atoi(optarg); |
| 79 | + break; |
| 80 | + case 's': if (optarg) iseed = atoi(optarg); |
| 81 | + break; |
| 82 | + case 'a': if (optarg) all_lets = string(optarg); |
| 83 | + break; |
| 84 | + case 'w': if (optarg) { |
| 85 | + all_freqs = string(optarg); |
| 86 | + has_freqs = true; |
| 87 | + } |
| 88 | + break; |
| 89 | + case 'h': usage(); |
| 90 | + return 0; |
| 91 | + default: usage(); |
| 92 | + return 0; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (seqlen < 1) { |
| 97 | + cerr << "Error: please input a desired sequence length above 0" << endl;; |
| 98 | + exit(EXIT_FAILURE); |
| 99 | + } |
| 100 | + |
| 101 | + /* split up letters */ |
| 102 | + |
| 103 | + last = 0; |
| 104 | + next = 0; |
| 105 | + while ((next = all_lets.find(comma, last)) != string::npos) { |
| 106 | + lets.push_back(all_lets.substr(last, next - last)); |
| 107 | + last = next + 1; |
| 108 | + } |
| 109 | + final_let = all_lets.substr(last); |
| 110 | + if (final_let.length() > 0) lets.push_back(final_let); |
| 111 | + |
| 112 | + alphlen = lets.size(); |
| 113 | + |
| 114 | + if (alphlen < 1) { |
| 115 | + cerr << "Error: could not parse sequence alphabet" << endl; |
| 116 | + exit(EXIT_FAILURE); |
| 117 | + } |
| 118 | + |
| 119 | + /* split up freqs */ |
| 120 | + |
| 121 | + if (has_freqs) { |
| 122 | + |
| 123 | + last = 0; |
| 124 | + next = 0; |
| 125 | + while ((next = all_freqs.find(comma, last)) != string::npos) { |
| 126 | + freqs.push_back(stod(all_freqs.substr(last, next - last))); |
| 127 | + last = next + 1; |
| 128 | + } |
| 129 | + final_freq = all_freqs.substr(last); |
| 130 | + if (final_freq.length() > 0) freqs.push_back(stod(final_freq)); |
| 131 | + |
| 132 | + if (lets.size() != freqs.size()) { |
| 133 | + cerr << "Error: mismatching number of letters [" << lets.size() |
| 134 | + << "] and frequencies [" << freqs.size() << "]" << endl; |
| 135 | + exit(EXIT_FAILURE); |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + /* main seq generation loop */ |
| 141 | + |
| 142 | + gen = default_random_engine(iseed); |
| 143 | + outletters = ""; |
| 144 | + |
| 145 | + if (!has_freqs) { |
| 146 | + |
| 147 | + while (outletters.length() < seqlen) { |
| 148 | + outletters += lets[gen() % alphlen]; |
| 149 | + } |
| 150 | + |
| 151 | + } else { |
| 152 | + |
| 153 | + discrete_distribution<int> next_let(freqs.begin(), freqs.end()); |
| 154 | + while (outletters.length() < seqlen) { |
| 155 | + outletters += lets[next_let(gen)]; |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + if (outletters.length() > seqlen) |
| 161 | + outletters = outletters.substr(0, seqlen); |
| 162 | + |
| 163 | + /* return */ |
| 164 | + |
| 165 | + if (has_out) { |
| 166 | + outfile << outletters << endl; |
| 167 | + } else { |
| 168 | + cout << outletters << endl; |
| 169 | + } |
| 170 | + |
| 171 | + return 1; |
| 172 | + |
| 173 | +} |
0 commit comments