-
Notifications
You must be signed in to change notification settings - Fork 3
/
fasta_validate.c
167 lines (140 loc) · 4.09 KB
/
fasta_validate.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
/*
* Validate a fasta file
*
* Rob Edwards January 2019.
*
* We exit with:
* 0: this is a valid fasta file
* 1: the first line does not start with a >
* 2: the ids are not unique
* 4: lines in the sequence (that do not start >) contain characters that do not match the perl regexp /[A-Z][a-z] /
*
* Over 200:
* internal errors, eg. unable to allocate memory, etc.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <search.h>
#include "fasta_validate.h"
int contains_non_word_characters(char *seq, int verbose) {
/*
* we consider word characters to be ASCII:
* 64 < char < 91 (A=65; Z=90)
* 96 < char < 123 (a=97; z=122)
*/
if (seq == NULL) {
fprintf(stderr, "Empty line receieved. Empty string?\n");
return 8;
}
for (int i=0; i<strlen(seq); i++) {
if ((int) seq[i] < 65) {
if (((int) seq[i] != 10) && ((int) seq[i] != 13))
return 1;
}
else if (((int) seq[i] > 90) && ((int) seq[i] < 97))
return 2;
else if ((int) seq[i] > 122)
return 3;
}
return 0;
}
void help(char *nm) {
printf("%s version %d\n\nCheck and validate a fasta file\n", nm, VERSION);
printf("Rob Edwards.\n\n");
printf("%s checks your fasta file and exits with:\n", nm);
printf("\t0: this is a valid fasta file\n");
printf("\t1: the first line does not start with a >\n");
printf("\t2: the ids are not unique\n");
printf("\t4: lines in the sequence (that do not start >) contain characters that do not match the perl regexp /[A-Z][a-z]/\n");
printf("\t8: There is a sequence with zero length in it\n\n");
printf("\tOver 200:\n");
printf("\t\tinternal errors, eg. unable to allocate memory, etc.\n\n");
printf("Usage: %s [options] [fasta file]\n\t-v: verbose output\n\t-V: current version\n\t-h: this help\n", nm);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("%s [-h] [-v] [-V] [fasta file]\n", argv[0]);
exit(1);
}
char *filename = argv[1];
int verbose = 0;
if (strcmp(filename, "-h") == 0) {
help(argv[0]);
return 0;
}
if (strcmp(filename, "-V") == 0) {
printf("%s version %d.\nFor more information please see https://github.com/linsalrob/fasta_validator\n", argv[0], VERSION);
return 0;
}
if (strcmp(filename, "-v") == 0) {
verbose = 1;
filename = argv[2];
}
// create a hash with 1,000,000 possible entries
int hc = hcreate(NUMSEQS);
if (hc == 0) {
fprintf(stderr, "Unable to create the hash table\n");
return -1;
}
FILE * fp;
char line[MAXLINELEN];
if ((fp = fopen(filename, "r")) == NULL) {
if (verbose)
fprintf(stderr, "Can't open file %s\n", argv[1]);
exit(1);
}
int firstline = 1;
int seqcount = 0;
while ((fgets(line, MAXLINELEN, fp)) != NULL) {
if ((int) line[0] == 62) { // not sure why I'm using an ascii comparison, but I'm thinking ascii at the moment
if (!firstline && seqcount == 0) {
if (verbose)
fprintf(stderr, "ERROR: We have an empty sequence\n");
return 8;
}
firstline = 0;
seqcount = 0;
// remove anything after the first space
char *p = strchr(line, ' ');
if (p)
*p = '\0';
// in case you need this!
// fprintf(stderr, "Parsing %s\n", line);
// check to see if we have seen this line
// if not, add it to the hash
ENTRY item;
item.key = strdup(line);
ENTRY *found_item;
if ((found_item = hsearch(item, FIND)) != NULL) {
if (verbose) {
fprintf(stderr, "ERROR: Found a duplicate id: |%s|\n", line);
fprintf(stderr, "ERROR: Found a duplicate id: |%s|\n", found_item->key);
}
return 2;
}
// fprintf(stderr, "adding |%s|\n", item.key);
(void) hsearch(item, ENTER);
} else {
if (firstline > 0) {
if (verbose)
fprintf(stderr, "ERROR: The first line should start with a >\n");
return 1; // the first line should start with a >
}
int nwc = contains_non_word_characters(line, verbose);
if (nwc > 0) {
if (verbose)
fprintf(stderr, "ERROR: We have a non word character!\n");
return 4;
}
seqcount += strlen(line);
}
}
if (seqcount == 0) {
if (verbose)
fprintf(stderr, "ERROR: at tend We have an empty sequence\n");
return 8;
}
return 0;
}