-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.c
213 lines (187 loc) · 5.57 KB
/
main.c
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "kseq.h"
#include <getopt.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <zlib.h>
#include <omp.h>
#define VERSION "0.8.2"
#define EXENAME "snp-dists"
#define GITHUB_URL "https://github.com/tseemann/snp-dists"
const int MAX_SEQ = 100000;
const char IGNORE_CHAR = '.';
int cpus = 1;
KSEQ_INIT(gzFile, gzread)
//------------------------------------------------------------------------
size_t distance(const char* restrict a, const char* restrict b, size_t L)
{
size_t diff=0;
for (size_t i=0; i < L; i++) {
if (a[i] != b[i] && a[i] != IGNORE_CHAR && b[i] != IGNORE_CHAR) {
diff++;
}
}
return diff;
}
//------------------------------------------------------------------------
void show_help(int retcode)
{
FILE* out = (retcode == EXIT_SUCCESS ? stdout : stderr);
static const char str[] = {
"SYNOPSIS\n Pairwise SNP distance matrix from a FASTA alignment\n"
"USAGE\n %s [opts] aligned.fasta[.gz] > matrix.tsv\n"
"OPTIONS\n"
" -h Show this help\n"
" -v Print version and exit\n"
" -j CPUS Threads to use [%d]\n"
" -q Quiet mode; no progress messages\n"
" -a Count all differences not just [AGTC]\n"
" -k Keep case, don't uppercase all letters\n"
" -m Output MOLTEN instead of TSV\n"
" -c Use comma instead of tab in output\n"
" -b Blank top left corner cell\n"
"URL\n %s\n"};
fprintf(out, str, EXENAME, cpus, GITHUB_URL);
exit(retcode);
}
//------------------------------------------------------------------------
int main(int argc, char* argv[])
{
// parse command line parameters
int opt, quiet = 0, csv = 0, corner = 1, allchars = 0, keepcase = 0, molten = 0;
while ((opt = getopt(argc, argv, "hj:qcakmbv")) != -1) {
switch (opt) {
case 'h': show_help(EXIT_SUCCESS); break;
case 'j': cpus = atoi(optarg); break;
case 'q': quiet = 1; break;
case 'c': csv = 1; break;
case 'a': allchars = 1; break;
case 'k': keepcase = 1; break;
case 'm': molten = 1; break;
case 'b': corner = 0; break;
case 'v': printf("%s %s\n", EXENAME, VERSION); exit(EXIT_SUCCESS);
default: show_help(EXIT_FAILURE);
}
}
// require a filename argument
if (optind >= argc) {
show_help(EXIT_FAILURE);
return 0;
}
const char* fasta = argv[optind];
// say hello
if (!quiet)
fprintf(stderr, "This is %s %s\n", EXENAME, VERSION);
// override $OMP_NUM_THREADS
if (!quiet)
fprintf(stderr, "Will use %d threads.\n", cpus);
omp_set_num_threads(cpus);
// open filename via libz
gzFile fp = gzopen(fasta, "r");
if (!fp) {
fprintf(stderr, "ERROR: Could not open filename '%s'\n", fasta);
exit(EXIT_FAILURE);
}
// load all the sequences
char** seq = calloc(MAX_SEQ, sizeof(char*));
char** name = calloc(MAX_SEQ, sizeof(char*));
ssize_t l, N=0, L=-1;
kseq_t* kseq = kseq_init(fp);
while ((l = kseq_read(kseq)) >= 0) {
// first sequence
if (L < 0) {
L = l;
}
// not first sequence - so ensure length matches first one
if (l != L) {
fprintf(stderr,
"ERROR: sequence #%zu '%s' has length %zu but expected %zu\n",
N + 1, kseq->name.s, l, L);
exit(EXIT_FAILURE);
}
// have we exceeded the number of sequences we can handle?
if (N >= MAX_SEQ) {
fprintf(stderr,
"ERROR: %s can only handle %d sequences at most. Please change MAX_SEQ "
"and recompile.\n",
EXENAME, MAX_SEQ);
exit(EXIT_FAILURE);
}
// save the sequence and name
seq[N] = (char*)calloc(kseq->seq.l + 1, sizeof(char));
strcpy(seq[N], kseq->seq.s);
name[N] = (char*)calloc(kseq->name.l + 1, sizeof(char));
strcpy(name[N], kseq->name.s);
// uppercase all sequences
if (!keepcase) {
for (char* s = seq[N]; *s; s++) {
*s = toupper(*s);
}
}
// clean the sequence depending on -a option
if (!allchars) {
for (char* s = seq[N]; *s; s++) {
if (*s != 'A' && *s != 'T' && *s != 'C' && *s != 'G') {
*s = IGNORE_CHAR;
}
}
}
// keep track of how many we have
N++;
}
kseq_destroy(kseq);
gzclose(fp);
if (N < 1) {
fprintf(stderr, "ERROR: file contained no sequences\n");
exit(EXIT_FAILURE);
}
if (!quiet)
fprintf(stderr, "Read %zu sequences of length %zu\n", N, L);
// output TSV or CSV
char sep = csv ? ',' : '\t';
size_t *d = malloc(N * sizeof(size_t));
if (molten) {
// "molten" format, one row per pair
for (int j = 0; j < N; j++) {
#pragma omp parallel for
for (int i=0; i < N; i++) {
size_t d = distance(seq[j], seq[i], L);
printf("%s%c%s%c%zu\n", name[j], sep, name[i], sep, d);
}
}
}
else {
// regular TSV matrix output
// header seq
if (corner)
printf("%s %s", EXENAME, VERSION);
for (int j = 0; j < N; j++) {
printf("%c%s", sep, name[j]);
}
printf("\n");
// Output the distance matrix to stdout
// (does full matrix, wasted computation i know)
for (int j = 0; j < N; j++) {
printf("%s", name[j]);
#pragma omp parallel for
for (int i=0; i < N; i++) {
d[i] = distance(seq[j], seq[i], L);
}
for (int i=0; i < N; i++) {
printf("%c%zu", sep, d[i]);
}
printf("\n");
}
}
free(d);
// free memory
for (int k = 0; k < N; k++) {
free(seq[k]);
free(name[k]);
}
free(seq);
free(name);
return 0;
}
//------------------------------------------------------------------------