-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcatsequences.c
More file actions
executable file
·313 lines (267 loc) · 8.57 KB
/
catsequences.c
File metadata and controls
executable file
·313 lines (267 loc) · 8.57 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TEMP
#define TEMP 2
#endif
#define MAXLEN 1000000
#define NAMEDELIMITER " |." /* species name is initial string up to first ' ', '|' or '.' */
void clean_exit(int x);
char **species_names = NULL, ***sequences = NULL;
int *seqlens = NULL, numspecies = 0, numseqs = 0, num_files=0;
int main(int argc, char *argv[])
{
FILE *infile1 = NULL, *infile2 = NULL, *outfile2 = NULL;
char *filename = NULL, string[MAXLEN], string2[MAXLEN], c = '\0';
int i = 0, j = 0, k=0, found = 0, aln_len =0, error = FALSE, filenum=0, previous=FALSE, totlen=0;
if(argc < 2)
{
printf("\ncatsequences is designed to concatenate sequence alignments. \n\nUsage: catsequences filename\n\tWhere filename contains a list of files with sequences to be concatenated\n\tThe concatenated alignments are outputted to a file called \"allseqs.fas\"\n\tPartition information will be written to the file \"allseqs.partitions.txt\"\n\n\n\tThe sequence names are read up to the first \".\" or \"|\" (or the end of the name)\n\n");
exit(1);
}
/* open file with list of files to be concatenated */
if((infile1 = fopen(argv[1], "r")) == NULL) /* check to see if the file is there */
{ /* Open the fundamental tree file */
fprintf(stderr, "Error: Cannot open list file %s\n", argv[1]);
exit(1);
}
outfile2 = fopen("allseqs.partitions.txt", "w");
filename=malloc(1000*sizeof(char));
filename[0]='\0';
string[0] ='\0';
string2[0] = '\0';
/* assign the arry to hold the names of the species (max 10000 in this build) */
species_names = malloc(10000*sizeof(char*));
if(species_names == NULL) clean_exit(1);
/* Count the number of file to be read in */
i=0;
while(!feof(infile1))
{
c= getc(infile1);
if(c == '\n' || c == '\r') i++;
}
rewind(infile1);
num_files = i;
/* assign the array to hold the length of the aligned sequences in each file */
seqlens = malloc(num_files*sizeof(int));
if(seqlens == NULL) clean_exit(6);
j=0;
while(!feof(infile1) && !error)
{
fscanf(infile1, "%s\n", filename); /* Get the name of the sequene file tobe opened */
if((infile2 = fopen(filename, "r")) == NULL) /* check to see if the file is there */
{ /* Open the fundamental tree file */
fprintf(stderr, "Error: Cannot open alignment file %s\n", filename);
exit(1);
}
/*infile2 = fopen(filename, "r"); */ /* open the sequence file */
/*printf("%s\n", filename);*/
fprintf(outfile2, "%s\t=\t%d-", filename, totlen-j+1);
aln_len = -1;
/* read in the names and see how many species we need for this gene */
c = getc(infile2);
while(!feof(infile2) && !error)
{
if(c == '>') /* read in the names are */
{
i=0;
string[i] = c;
i++;
while(!feof(infile2) && (c=getc(infile2)) != '\n' && c != '\r' && c != '.')
{
if(c != '\r' && c != '\n' && !feof(infile2))
{
string[i] = c;
i++;
}
if(i == MAXLEN)
{
printf("ERROR: The total concatenated sequence length is greater than the maximum length defined (%d)!\n", MAXLEN);
printf("\tTo fix this increase the value of MAXLEN on line 15 of 'catsequences.c` and recompile the tool\n");
exit(1);
}
}
if(c == '.')
{
while(!feof(infile2) && (c=getc(infile2)) != '\n' && c != '\r');
}
string[i] = '\0';
if(string[0] == '>')
{
string[strcspn(string, NAMEDELIMITER)] = '\0'; /* Truncate the name at the first instnace of any of the NAME delimiters (to capture the species name and exclude gene names for instnace) */
found = -1;
for(i=0; i<numspecies; i++)
{
if(strcmp(string, species_names[i])==0)
found = i;
}
if(found == -1)
{
/* printf("found species %d: %s\n", numspecies, string); */
if (!(species_names[numspecies] = strdup(string)))
{
perror("strdup");
exit(EXIT_FAILURE);
}
found = numspecies;
numspecies++;
}
}
}
i=0;
while(!feof(infile2) && (c=getc(infile2)) != '>') /* count the length of the sequences */
{
if(c != ' ' && c != '\n' && c != '\r') i++;
}
if(!feof(infile2)) i++;
if(aln_len == -1)
{
aln_len = i;
seqlens[j] = aln_len;
totlen+=aln_len;
/* printf("aln_len = %d seqlens[%d] = %d\n",aln_len, j, seqlens[j]); */
}
else
{
if(aln_len != i)
{
printf("ERROR not all sequences the same length (%d != %d)\n", i , aln_len);
error=TRUE;
}
}
}
fprintf(outfile2, "%d;\n", totlen-j-1);
fclose(infile2);
j++;
}
fclose(infile1);
fclose(outfile2);
printf("num_species = %d, num_files = %d\n", numspecies, num_files);
printf("expected concatenated sequence length %d\n", totlen-num_files);
printf("\nAll partitions written to \"allseqs.partitions.txt\"\n");
/*read in the sequences for real */
/* Assign the array to hold all the sequences */
sequences = malloc(num_files*sizeof(char**));
if(sequences == NULL) clean_exit(2);
for(j=0; j<num_files; j++)
{
sequences[j] = malloc(numspecies*sizeof(char*));
if(sequences[j] == NULL) clean_exit(3);
for(k=0; k<numspecies; k++)
{
sequences[j][k] = malloc((seqlens[j]+1)*sizeof(char));
if(sequences[j][k] == NULL) clean_exit(4);
sequences[j][k][0] = '\0';
}
}
printf("Done assignment\n");
/**** READ IN THE SEQUENCES ***/
printf("\n");
infile1 = fopen(argv[1], "r"); /* open up the list of files again */
filenum=0;
while(!feof(infile1) && !error)
{
fscanf(infile1, "%s\n", filename); /* read in the name of the file */
/* printf("File opened: %s\n", filename);*/
infile2 = fopen(filename, "r"); /* open the file */
previous=FALSE;
while(!feof(infile2) && !error)
{
i=0;
while(!feof(infile2) && (c=getc(infile2)) != '\n' && c != '\r')
{
if(c != '\r' && c != '\n' && !feof(infile2))
{
string[i++] = c;
}
if(i == MAXLEN)
{
printf("ERROR: The total concatenated sequence length is greater than the maximum length defined (%d)!\n", MAXLEN);
printf("\tTo fix this increase the value of MAXLEN on line 15 of 'catsequences.c` and recompile the tool\n");
exit(1);
}
}
string[i] = '\0';
/* printf("%s\n", string);*/
if(string[0] == '>')
{
string[strcspn(string, NAMEDELIMITER)] = '\0'; /* species name is initial string up to [ |.] */
found = -1;
for(i=0; i<numspecies; i++)
{
if(strcmp(string, species_names[i]) == 0)
{
found = i;
if(strlen(sequences[filenum][found]) > 0)
{
previous=TRUE;
printf("Found an extra copy of %s in file %s.... ignoring this copy\n", species_names[i], filename);
}
i=numspecies;
}
}
if(found == -1)
{
printf("error: %s not found\n", string);
exit(EXIT_FAILURE);
}
}
else
{
if(!previous)
{
strcat(sequences[filenum][found], string);
if(strlen(sequences[filenum][found]) > seqlens[filenum]) printf("%lu/%d\n", strlen(sequences[filenum][found]), seqlens[filenum]);
}
}
i=0;
}
fclose(infile2);
filenum++;
}
fclose(infile1);
/* for(i=0; i<filenum; i++)
{
printf("File %d seqlen = %d. fulllen = %d\n", i, strlen(sequences[i][0]), seqlens[i]);
}
*/
infile1 = fopen("allseqs.fas", "w");
found = 0;
for(i=0; i<numspecies; i++)
{
fprintf(infile1, "%s\n", species_names[i]);
for(j=0; j<num_files; j++)
{
if(strlen(sequences[j][i]) != 0)
{
fprintf(infile1, "%s", sequences[j][i]);
if(strlen(sequences[j][i])+1 != seqlens[j]) printf("File %d seqlen = %lu. fulllen = %d\n", i, strlen(sequences[j][i]), seqlens[j]);
/* printf("%d/%d\t", strlen(sequences[j][i]), seqlens[j]);*/
}
else
{
/* printf("-%d\t", seqlens[j]-1); */
for(k=0; k<(seqlens[j]-1); k++)
{
fprintf(infile1, "?");
}
}
}
fprintf(infile1, "\n");
/* printf("\n"); */
}
fclose(infile1);
free(filename);
printf("Finished! Cancatenated alignment written to \"allseqs.fas\"\n\n");
}
void clean_exit(int x)
{
printf("Error: out of memory at %d\n", x);
exit(EXIT_FAILURE);
}