/
appears.cpp
199 lines (175 loc) · 5.22 KB
/
appears.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#define DDEBUG 0
#define _FILE_OFFSET_BITS 64
#define _LARGEFILE_SOURCE
#include "ddebug.h"
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cerrno>
#include <cstdlib>
#include <bitset>
/* here we allow overriding on the g++/cl command line */
#ifndef APPEARS_SEQSIZE
#define APPEARS_SEQSIZE 15L /* sequence string size */
#endif
/* 4^APPEARS_SEQSIZE */
const size_t TABLESIZE = 1L << (2 * APPEARS_SEQSIZE);
/* We make this naming convention that a "token" is a nucleotide */
const int BITS_PER_TOKEN = 2;
const int TOKEN_MASK = 3; /* "11" in binary */
typedef std::bitset<APPEARS_SEQSIZE * BITS_PER_TOKEN> index_t;
enum {
G = 0, A, T, C
};
std::bitset<TABLESIZE> BitTable;
/* forward declarations */
static bool parse_seq_file (const char* fname);
static void search_missing_combinations ();
static void index_to_tokens (unsigned long index, char* buf);
int
main (int argc, char* argv[]) {
const char* fname;
if (argc < 2) {
fprintf(stderr, "ERROR: No input file specified.\n");
return 1;
}
fname = argv[1];
fprintf(stderr, "INFO: Reading file %s...\n", fname);
if ( ! parse_seq_file(fname) ) {
return 1;
}
fprintf(stderr,
"INFO: Using a bit array of length %lu"
" (sequence length: %ld).\n",
(unsigned long)TABLESIZE,
APPEARS_SEQSIZE);
fprintf(stderr,
"INFO: Searching missing combinations of sequences...\n");
search_missing_combinations();
return 0;
}
/* parse_seq_file: read in the sequence stream and set
* the global BitTable variable accordingly. */
bool
parse_seq_file (const char* fname) {
FILE* infile;
char c;
unsigned long lineno = 1L;
index_t index;
unsigned long tokens_seen = 0L;
if (NULL == (infile = fopen(fname, "r"))) {
fprintf(stderr,
"ERROR: Failed to open input file %s for reading: "
"%s\n",
fname, strerror(errno));
return false;
}
for (;;) {
bool found_token = false;
c = getc(infile);
if (EOF == c) {
if (ferror(infile)) {
fprintf(stderr, "ERROR: Failed to read from file %s:"
"%s\n",
fname, strerror(errno));
return false;
}
break;
}
switch (c) {
case '\n':
lineno++;
case ' ':
case '\t':
case '\r':
/* ignored spaces */
break;
case 'G':
index <<= BITS_PER_TOKEN;
index |= G;
found_token = true;
break;
case 'A':
index <<= BITS_PER_TOKEN;
index |= A;
found_token = true;
break;
case 'T':
index <<= BITS_PER_TOKEN;
index |= T;
found_token = true;
break;
case 'C':
index <<= BITS_PER_TOKEN;
index |= C;
found_token = true;
break;
default:
fprintf(stderr, "ERROR: File %s: line %lu: "
"Invalid character found: '%c' (0x%x)\n",
fname, lineno, c, c);
return false;
break;
}
if (found_token) {
if (tokens_seen < APPEARS_SEQSIZE) {
tokens_seen++;
}
if (tokens_seen >= APPEARS_SEQSIZE) {
/* register this subsequence combination */
unsigned long index_ulong = index.to_ulong();
DD("setting the %luth bit", index_ulong);
BitTable.set(index_ulong, 1);
}
}
}
fclose(infile);
return true;
}
/* index_to_tokens: decode the binary index numerals to G/A/T/C
* token sequences */
void
index_to_tokens (unsigned long index, char* buf) {
for (unsigned long i = 0; i < APPEARS_SEQSIZE; i++) {
int encoded_token =
(index >> (i * BITS_PER_TOKEN)) & TOKEN_MASK;
switch (encoded_token) {
case G:
buf[APPEARS_SEQSIZE - i - 1] = 'G';
break;
case A:
buf[APPEARS_SEQSIZE - i - 1] = 'A';
break;
case T:
buf[APPEARS_SEQSIZE - i - 1] = 'T';
break;
case C:
buf[APPEARS_SEQSIZE - i - 1] = 'C';
break;
default:
break;
}
}
buf[APPEARS_SEQSIZE] = '\0';
}
/* search_missing_combinations: traverse the BitTable
* to find 0 bits and print out the corresponding
* character sequences */
void
search_missing_combinations () {
unsigned long missing_combs = 0;
char buf[APPEARS_SEQSIZE + 1];
for (unsigned long i = 0; i < TABLESIZE; i++) {
if (!BitTable.test(i)) {
index_to_tokens(i, buf);
printf(" %s (index %lu)\n", buf, i);
missing_combs++;
}
}
if (missing_combs) {
printf("For total %lu missing combinations found.\n",
missing_combs);
} else {
printf("All combinations have appeared.\n");
}
}