📄 dnatrans.cc
字号:
//----------------------------------------------------//// dnatrans.cc //// Translates DNA to protein in all 6 frames //// Latest revision: 05-06-2000 ////----------------------------------------------------//#include <stdio.h>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <string.h>#include <sys/stat.h>#define max(x,y) (((x) > (y)) ? (x) : (y))#define min(x,y) (((x) < (y)) ? (x) : (y))int dna(int pos);char *s1, *s1r, *s2[3];int filesize(char *filename);int main(int argc, char **argv){ int index = 0; int a, dotpos, frame, i, k, fsize; FILE *fp1, *fp2; char file1[128], file2[128], tempstring[128]; char buffer[128]; char junk[1024]; printf("dnatrans - Translates DNA or RNA sequence into peptide\n"); printf("Sequence file:"); fflush(stdout); scanf("%s",file1); printf("Output file: "); fflush(stdout); scanf("%s",file2); // Allocate extra space in case it gets edited fsize = filesize(file1); fsize = max(10000, 2 * fsize + 10); s1 = new char[fsize*3]; s1r = new char[fsize*3]; s2[0] = new char[fsize]; s2[1] = new char[fsize]; s2[2] = new char[fsize]; if(s2[2]==NULL){ printf("Insufficient memory\n"); exit(1); } if((fp1=fopen(file1,"rb")) == NULL) { printf("\nUnable to open file %s\n",file1); exit(1); } //----Read the DNA sequence-----// while(!feof(fp1)) { a = fgetc(fp1); if(a=='#') fgets(junk,1024,fp1); // comment a = toupper(a); if(a == 'A' || a == 'T' || a == 'U' || a == 'C' || a == 'G' || a == 'N') s1[index++] = a; } printf("\n\n"); fclose(fp1); //----Generate base filename---// dotpos = strlen(file2); for(k=0; k<(int)strlen(file2); k++) if(file2[k]=='.') dotpos=k; file2[dotpos]=0; //----Translate 1st 3 reading frames----// i = 0; for(k=0;k<index-2;k+=3) { s2[0][i] = dna(k ); s2[1][i] = dna(k+1); s2[2][i] = dna(k+2); i++; } for(frame=0;frame<=2;frame++) { sprintf(tempstring,"%s.%d",file2,frame+1); if((fp2=fopen(tempstring,"wt")) == NULL) { printf("\nUnable to open file %s\n",file2); exit(1); } for(k=0; k<(int)strlen(s2[frame]); k+=50) { strncpy(buffer, s2[frame]+k, 50); buffer[50]=0; fprintf(fp2,"%s\n", buffer); printf("%s\n", buffer); } fclose(fp2); } //----Reverse the DNA sequence-----// for(k=0;k<index;k++) { a = s1[k]; switch(a) { case 'U': a = 'A'; break; case 'T': a = 'A'; break; case 'A': a = 'T'; break; case 'G': a = 'C'; break; case 'C': a = 'G'; break; case 'N': a = 'N'; break; } s1r[index-k-1] = a; } memcpy(s1,s1r,index); //----Translate last 3 reading frames----// i = 0; for(k=0;k<index-2;k+=3) { s2[0][i] = dna(k ); s2[1][i] = dna(k+1); s2[2][i] = dna(k+2); i++; } for(frame=0;frame<=2;frame++) { sprintf(tempstring,"%s.%d",file2,frame+4); if((fp2=fopen(tempstring,"wt")) == NULL) { printf("\nUnable to open file %s\n",file2); exit(1); } for(k=0; k<(int)strlen(s2[frame]); k+=50) { strncpy(buffer, s2[frame]+k, 50); buffer[50]=0; fprintf(fp2,"%s\n", buffer); printf("%s\n", buffer); } fclose(fp2); } exit(EXIT_SUCCESS);}//---------------------------------------------------------------//// dna ////---------------------------------------------------------------//int dna(int pos){ char code[5]; code[0] = toupper(s1[pos ]); code[1] = toupper(s1[pos+1]); code[2] = toupper(s1[pos+2]); code[3] = 0; if(code[0]=='T') code[0]='U'; if(code[1]=='T') code[1]='U'; if(code[2]=='T') code[2]='U'; if(!(strcmp(code,"UUU"))) return 'F'; if(!(strcmp(code,"UUC"))) return 'F'; if(!(strcmp(code,"UUA"))) return 'L'; if(!(strcmp(code,"UUG"))) return 'L'; if(!(strcmp(code,"UCU"))) return 'S'; if(!(strcmp(code,"UCC"))) return 'S'; if(!(strcmp(code,"UCA"))) return 'S'; if(!(strcmp(code,"UCG"))) return 'S'; if(!(strcmp(code,"UCN"))) return 'S'; if(!(strcmp(code,"UAU"))) return 'Y'; if(!(strcmp(code,"UAC"))) return 'Y'; if(!(strcmp(code,"UAA"))) return 'x'; if(!(strcmp(code,"UAG"))) return 'x'; if(!(strcmp(code,"UAN"))) return 'Y'; if(!(strcmp(code,"UGU"))) return 'C'; if(!(strcmp(code,"UGC"))) return 'C'; if(!(strcmp(code,"UGA"))) return 'x'; if(!(strcmp(code,"UGG"))) return 'W'; if(!(strcmp(code,"CUU"))) return 'L'; if(!(strcmp(code,"CUC"))) return 'L'; if(!(strcmp(code,"CUA"))) return 'L'; if(!(strcmp(code,"CUG"))) return 'L'; if(!(strcmp(code,"CUN"))) return 'L'; if(!(strcmp(code,"CCU"))) return 'P'; if(!(strcmp(code,"CCC"))) return 'P'; if(!(strcmp(code,"CCA"))) return 'P'; if(!(strcmp(code,"CCG"))) return 'P'; if(!(strcmp(code,"CCN"))) return 'P'; if(!(strcmp(code,"CAU"))) return 'H'; if(!(strcmp(code,"CAC"))) return 'H'; if(!(strcmp(code,"CAA"))) return 'Q'; if(!(strcmp(code,"CAG"))) return 'Q'; if(!(strcmp(code,"CGU"))) return 'R'; if(!(strcmp(code,"CGC"))) return 'R'; if(!(strcmp(code,"CGA"))) return 'R'; if(!(strcmp(code,"CGG"))) return 'R'; if(!(strcmp(code,"CGN"))) return 'R'; if(!(strcmp(code,"AUU"))) return 'I'; if(!(strcmp(code,"AUC"))) return 'I'; if(!(strcmp(code,"AUA"))) return 'I'; if(!(strcmp(code,"AUG"))) return 'M'; if(!(strcmp(code,"ACU"))) return 'T'; if(!(strcmp(code,"ACC"))) return 'T'; if(!(strcmp(code,"ACA"))) return 'T'; if(!(strcmp(code,"ACG"))) return 'T'; if(!(strcmp(code,"ACN"))) return 'T'; if(!(strcmp(code,"AAU"))) return 'N'; if(!(strcmp(code,"AAC"))) return 'N'; if(!(strcmp(code,"AAA"))) return 'K'; if(!(strcmp(code,"AAG"))) return 'K'; if(!(strcmp(code,"AGU"))) return 'S'; if(!(strcmp(code,"AGC"))) return 'S'; if(!(strcmp(code,"AGA"))) return 'R'; if(!(strcmp(code,"AGG"))) return 'R'; if(!(strcmp(code,"GUU"))) return 'V'; if(!(strcmp(code,"GUC"))) return 'V'; if(!(strcmp(code,"GUA"))) return 'V'; if(!(strcmp(code,"GUG"))) return 'V'; if(!(strcmp(code,"GUN"))) return 'V'; if(!(strcmp(code,"GCU"))) return 'A'; if(!(strcmp(code,"GCC"))) return 'A'; if(!(strcmp(code,"GCA"))) return 'A'; if(!(strcmp(code,"GCG"))) return 'A'; if(!(strcmp(code,"GCN"))) return 'A'; if(!(strcmp(code,"GAU"))) return 'D'; if(!(strcmp(code,"GAC"))) return 'D'; if(!(strcmp(code,"GAA"))) return 'E'; if(!(strcmp(code,"GAG"))) return 'E'; if(!(strcmp(code,"GGU"))) return 'G'; if(!(strcmp(code,"GGC"))) return 'G'; if(!(strcmp(code,"GGA"))) return 'G'; if(!(strcmp(code,"GGG"))) return 'G'; if(!(strcmp(code,"GGN"))) return 'G'; return 'x';}//-----------------------------------------------------------------//// filesize ////-----------------------------------------------------------------//int filesize(char *filename){ struct stat dstatus; stat(filename, &dstatus); return dstatus.st_size;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -