forked from aseetharam/common_scripts
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathfasttrans.c
executable file
·974 lines (427 loc) · 12.6 KB
/
fasttrans.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* FASTTRANS.C
28-JUL-2000, David Mathog, Biology Division, Caltech
Added optional second parameter = minimum size of orfs to
output. If present, then each ORF >= this minimum size
is output as a separate FASTA entry.
Also modified code so that if it finds an entry > 1Mb
it will automatically split it into 2 (or more) pieces.
30-MAY-2000, David Mathog, Biology Division, Caltech
Simple program that translates one or more frames in a DNA fasta
file to protein. Default is to do all 6 frames. Input
from stdin and output to stdout, always. The one argument is
123456 where 1->3 are the 3 forward frames and 4->6 the three
reverse frames. Specify as many as are desired in the translation.
For instance, infile:
>example
ACGCTCTCTCT
becomes
>example-1for
translation
>example-2for
translation
>example-3for
translation
>example-1rev
translation
>example-2rev
translation
>example-3rev
translation
Maximum input sequence length is 1Mb. To change that see the
MAXSEQIN parameter, below.
*/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h> /* for toupper */
#include <string.h> /* for strlen and such */
#include <unistd.h>
#define AVAL 0
#define CVAL 1
#define GVAL 2
#define TVAL 3
#define MAXSEQIN 1000000
#define MAXHEADER 1000
#define MAXOUTLINE 50
#define FRAGOVERLAP 498 /* MUST be multiple of 3 or the frame shifts!!! */
#define FOR1 1
#define FOR2 2
#define FOR3 4
#define REV1 8
#define REV2 16
#define REV3 32
typedef struct transrec ATRANSREC;
struct transrec {
char aa;
char nt[4];
};
char inseq[MAXSEQIN];
char accumseq[MAXSEQIN];
char outtrans[1 + MAXSEQIN/3 ];
char header[MAXHEADER];
char holdheader[MAXHEADER];
int frames;
int acclen;
int minaa;
ATRANSREC alltrans[64]={
{ 'K' , "AAA" },
{ 'N' , "AAC" },
{ 'K' , "AAG" },
{ 'N' , "AAT" },
{ 'T' , "ACA" },
{ 'T' , "ACC" },
{ 'T' , "ACG" },
{ 'T' , "ACT" },
{ 'R' , "AGA" },
{ 'S' , "AGC" },
{ 'R' , "AGG" },
{ 'S' , "AGT" },
{ 'I' , "ATA" },
{ 'I' , "ATC" },
{ 'M' , "ATG" },
{ 'I' , "ATT" },
{ 'Q' , "CAA" },
{ 'H' , "CAC" },
{ 'Q' , "CAG" },
{ 'H' , "CAT" },
{ 'P' , "CCA" },
{ 'P' , "CCC" },
{ 'P' , "CCG" },
{ 'P' , "CCT" },
{ 'R' , "CGA" },
{ 'R' , "CGC" },
{ 'R' , "CGG" },
{ 'R' , "CGT" },
{ 'L' , "CTA" },
{ 'L' , "CTC" },
{ 'L' , "CTG" },
{ 'L' , "CTT" },
{ 'E' , "GAA" },
{ 'D' , "GAC" },
{ 'E' , "GAG" },
{ 'D' , "GAT" },
{ 'A' , "GCA" },
{ 'A' , "GCC" },
{ 'A' , "GCG" },
{ 'A' , "GCT" },
{ 'G' , "GGC" },
{ 'G' , "GGA" },
{ 'G' , "GGG" },
{ 'G' , "GGT" },
{ 'V' , "GTA" },
{ 'V' , "GTC" },
{ 'V' , "GTG" },
{ 'V' , "GTT" },
{ '*' , "TAA" },
{ 'Y' , "TAC" },
{ '*' , "TAG" },
{ 'Y' , "TAT" },
{ 'S' , "TCA" },
{ 'S' , "TCC" },
{ 'S' , "TCG" },
{ 'S' , "TCT" },
{ '*' , "TGA" },
{ 'C' , "TGC" },
{ 'W' , "TGG" },
{ 'C' , "TGT" },
{ 'L' , "TTA" },
{ 'F' , "TTC" },
{ 'L' , "TTG" },
{ 'F' , "TTT" }
};
/* PROTOTYPES */
void sendout(int offset, char * string);
void translate(int offset);
void minaaout(char* string);
void orfout(char *orf);
void process_dna_seq(void);
/* translates the specified frame from nucleotides to amino acids */
void translate(int offset){
int i,j,k;
char *fptr;
int index;
int count;
outtrans[0]='\0';
for(fptr=&accumseq[offset],count=1,i=0,j=0,index=0 ; ; count++,j++,fptr++){
k = 1 << 2*(2-j);
if(*fptr == '\0'){ /* emit the final fragment, if any */
outtrans[i]='\0';
return;
}
else {
switch (*fptr){
case 'A':
index += (k * AVAL);
break;
case 'C':
index += (k * CVAL);
break;
case 'G':
index += (k * GVAL);
break;
case 'T':
index += (k * TVAL);
break;
default:
index=1000;
} /* switch */
if(j==2){
j=-1;
if(index < 64){
outtrans[i]=alltrans[index].aa;
}
else {
outtrans[i]='X';
}
index=0;
i++;
}
}
}
}
/* orfout sends the designated ORF string to stdout in chunks
of MAXOUTLINE characters (plus a terminator). It needs write
access to the ORF string, but it doesn't change anything
permanently */
void orfout(char *orf){
int front,back,count;
char hold;
for(front=back=0,count=1; ;back++,count++){
/* end of buffer writes out a line so long as that line
is not empty (just a zero byte). It cannot
be > MAXOUTLINE due to the logic of this routine */
if(orf[back] == '\0'){
if(count != 1)(void) fprintf(stdout,"%s\n",&orf[front]);
break;
}
/* if MAXOUTLINE characters are at hand send them out now */
if(count>MAXOUTLINE){
hold=orf[back];
orf[back]='\0';
(void) fprintf(stdout,"%s\n",&orf[front]);
orf[back]=hold;
front=back;
count=1;
}
}
}
/* minaaout scans the outtrans buffer for ORFs >= minaa
and uses orfout to emit those it finds */
void minaaout( char* string){
int retval;
int i,done;
int front,back;
retval=0;
for (front=back=0, i=1,done=0 ; done != 1 ; back++){
switch(outtrans[front]){
case '*':
front++;
back=front;
break;
case '\0':
done=1;
break;
default:
switch(outtrans[back]){
case '\0': /* have an ORF, this INTENTIONALLY FALLS THROUGH */
done = 1;
case '*': /* have an ORF */
outtrans[back]='\0'; /* terminate this piece */
if(back-front >= minaa){ /* satisfies length criterion */
(void)fprintf(stdout,"%s%s_%.6d\n",header,string,i);
orfout(&outtrans[front]);
i++;
}
front=back;
front++;
back++;
break;
default:
break;
}
break;
}
}
}
/* sendout translates the specified frame. And then causes the translation
to be written out in full (if minaa <=0 ) or as ORFs (minaa > 0) */
void sendout(int offset, char * string){
translate(offset);
if(minaa > 0){
minaaout(string);
}
else{
(void)fprintf(stdout,"%s%s\n",header,string);
orfout(outtrans);
}
}
/* process_dna_seq first strips out everything except XNACGTU.-
all of which may be in the sequence. It also converts
U to T if any are found. Then it calls sendout for each of the
specified frames*/
void process_dna_seq(void){
char *from;
char *to;
int count;
char flip[256];
char hold;
int i,j;
if(acclen <=0 )return;
count=0;
for(from=to=accumseq; *from!='\0'; from++){
*from=toupper(*from);
switch (*from){
case 'A':
case 'C':
case 'G':
case 'T':
*to=*from;
to++;
count++;
break;
case 'U':
*to='T';
to++;
count++;
break;
case '.':
case '-':
case 'N':
case 'X':
*to='X';
to++;
count++;
break;
}
}
*to='\0';
/* now do the various frames */
if(frames & FOR1)sendout(0,"-for1");
if(frames & FOR2)sendout(1,"-for2");
if(frames & FOR3)sendout(2,"-for3");
/* complement the sequence */
for(i=0; i<256; i++){
flip[i]='X';
}
flip['A']='T';
flip['C']='G';
flip['G']='C';
flip['T']='A';
for(i=0 ; i<count ; i++){
accumseq[i]=flip[accumseq[i]];
}
for(i=0,j=count-1 ; i<=j ; i++,j--){
hold=accumseq[j];
accumseq[j]=accumseq[i];
accumseq[i]=hold;
}
if(frames & REV1)sendout(0,"-rev1");
if(frames & REV2)sendout(1,"-rev2");
if(frames & REV3)sendout(2,"-rev3");
}
int main(int argc, char *argv[]){
char *fptr;
char *to;
int toolong;
if(argc < 2 || argc > 3){
(void) fprintf(stderr," usage (UNIX): fasttrans 123456 [minAA] <in.nfa >out.pfa \n");
(void) fprintf(stderr," usage (OpenVMS): pipe fasttrans 123456 [minAA] <in.nfa >out.pfa \n");
(void) fprintf(stderr," input is a fasta dna sequence via stdin\n");
(void) fprintf(stderr," output is the translated protein sequence via stdout\n");
(void) fprintf(stderr," Specify the set of frames to translate on command line\n");
(void) fprintf(stderr," 1,2,3 are the 3 forward frames\n");
(void) fprintf(stderr," 4,5,6 are the 3 reverse frames\n");
(void) fprintf(stderr," minAA is an optional value. If present and greater than zero\n");
(void) fprintf(stderr," it emits each ORF that has at least that many AA residues\n");
(void) fprintf(stderr," in it as a separate fasta fragment. If not present or set to zero\n");
(void) fprintf(stderr," or less the entire translated frame is emitted\n");
exit(EXIT_FAILURE);
}
/* figure out the frames we will process */
frames=0;
for(fptr=argv[1]; *fptr != '\0'; fptr++){
switch (*fptr){
case '1':
frames |= FOR1;
break;
case '2':
frames |= FOR2;
break;
case '3':
frames |= FOR3;
break;
case '4':
frames |= REV1;
break;
case '5':
frames |= REV2;
break;
case '6':
frames |= REV3;
break;
default:
(void) fprintf(stderr,"FASTTRANS: fatal error: invalid frame specified [%s]\n",fptr);
exit(EXIT_FAILURE);
} /* switch(*fptr) */
} /* for on *fptr */
if(argc==3){
if(sscanf(argv[2],"%d",&minaa) != 1){
(void) fprintf(stderr,"FASTTRANS: fatal error: invalid minaa specified [%s]\n",argv[2]);
exit(EXIT_FAILURE);
}
}
else{
minaa=0;
}
/* start reading */
fptr=inseq;
acclen=0;
toolong=0;
while(fptr != NULL){
fptr = fgets(inseq,MAXSEQIN-1,stdin);
if(fptr != NULL){
/* store the header */
if(*fptr=='>'){
process_dna_seq(); /* only if acclen > 0 */
for(to=header; (*fptr != ' ' && *fptr != '\0' && *fptr != '\t' && *fptr != '\n' && *fptr != '\r'); fptr++,to++){
*to=*fptr;
}
*to='\0';
accumseq[0]='\0';
acclen=0;
toolong=0;
}
else {
if(strlen(inseq) + acclen > MAXSEQIN){
if(acclen < 3*FRAGOVERLAP){
(void) fprintf(stderr,"FASTTRANS: fatal error: excessively long input line\n");
exit(EXIT_FAILURE);
}
/* store the header and process the big piece we already have */
if(toolong==0){
(void) strcpy(holdheader,header);
toolong++; /* number chunks from 1 */
}
/* this allows up to 10000 separately numbered fragments of size
one million. Unlikely there will ever be a single DNA sequence
longer than 10 GB! */
(void) sprintf(header,"%s.frag%.4d",holdheader,toolong);
toolong++;
process_dna_seq(); /* only if acclen > 0 */
/* save the last FRAGOVERLAP bases, and adjust acclen.
The strncpy should be safe as there will be no overlap
of the copied piece. */
(void) strncpy(accumseq,&accumseq[acclen-FRAGOVERLAP],FRAGOVERLAP);
accumseq[FRAGOVERLAP]='\0'; /* be sure that it's terminated */
acclen=FRAGOVERLAP;
}
(void) strcat(accumseq,inseq);
acclen += strlen(inseq);
} /* ftp test on '>' */
} /* ftpr != NULL (valid read) */
} /* while(fptr != NULL) */
if(acclen != 0){
if(toolong != 0)(void) sprintf(header,"%s.frag%.4d",holdheader,toolong);
process_dna_seq();
}
exit(EXIT_SUCCESS);
}