/************************************************* misc.c P. Clote Miscellaneous routines for reading in RNA either as string from command line or FASTA file, etc. NOTES: 1) I chose a[] to be an array of short int, rather than char, since a[0] is the length of the original RNA sequence, stored in a[1],...,a[ a[0] ] Thus this requires a special irintRNA() function. *************************************************/ #include #include #include #include //for INT_MAX #include "spectrum.h" //includes def for DEBUG #include // character handling, eg toupper() #define GUbond -1 #define AUbond -2 #define CGbond -3 void printRNA(short *a,short flag){ //if flag=0 then don't print vertical bars int i, len; len = a[0]; //print RNA for (i=1;i<=len;i++) printf("%c",a[i]); printf("\n"); //now print separation markers every 10 spaces if (flag!=0 && len >= 10){ for (i=1;i<=len;i++) { if (i%10==0) printf("|"); else printf(" "); } printf("\n"); } } char * constructString(short *a){ int i, n; char *s; n = a[0]; s = (char *) calloc(n+1,sizeof(char)); //print RNA for (i=0;i=NN-2) { fprintf(stderr,"Length of RNA exceeds array size %d\n",NN); exit(1); } for (i=0;i') { //now remove FASTA comment while (ch != '\n') ch = fgetc(infile); } // This only works on Unix, since EOLN marker for DOS is \r\n // and of Macintosh is \r // At this point, ch is \n, since we have exited while loop a[0] = 0; //we'll need to return to set a[0] to be sequence length n=1; //current position of insert into RNA string while (ch != EOF && n < NN){ if (ch == '\n') { ch = fgetc(infile); if (ch == EOF) { a[0]=n-1; break; } } a[n] = toupper(ch); n++; ch = fgetc(infile); } if (n>=NN){ fprintf(stderr,"Length of RNA exceeds array size %d\n",NN); exit(1); } a[0] = n-1; //string length is n-1 a[n+1] = '\0'; //string terminator //printRNA(a,1); fclose(infile); } return a; } void error(char *s){ fprintf(stderr,"%s\n",s); exit(1); } int guPair(char x, char y){ return ( x=='G'&&y=='U' || x=='U'&&y=='G' ); } int auPair(char x, char y){ return ( x=='A'&&y=='U' || x=='U'&&y=='A' ); } int cgPair(char x, char y){ return ( x=='C'&&y=='G' || x=='G'&&y=='C' ); } int bondEnergy(int i, int j, char *s) { //returns the energy value of the pairing between //the ith and jth base in s if (i < strlen (s) && j < strlen (s)) { if (auPair(s[i],s[j])) return AUbond; else if (cgPair(s[i],s[j])) return CGbond; else if (guPair(s[i],s[j])) return GUbond; else return INF; } else error("Indices out of bounds for RNA sequence\n"); } int basePair(char x, char y){ return( auPair(x,y) || cgPair(x,y) || guPair(x,y) ); } int watsonCrick(char x, char y){ return( auPair(x,y) || cgPair(x,y) ); } void printMatrix(MatrixType M, int n){ int i,j,k,s,b; for (k=0;k