Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define RNACOMP(x0, x1) (rna[i + 2] == x0 || rna[i + 2] == x1)
- char *dna_to_rna(char *dna)
- {
- size_t sz = strlen(dna);
- unsigned i;
- char *rna = malloc(sz);
- if (!rna) {
- fprintf(stderr, "cannot allocate memory for rna strand\n");
- return NULL;
- }
- for (i = 0; i < sz; i++) {
- switch (dna[i]) {
- case 'A':
- rna[i] = 'U';
- break;
- case 'U':
- rna[i] = 'A';
- break;
- case 'T':
- rna[i] = 'A';
- break;
- case 'G':
- rna[i] = 'C';
- break;
- case 'C':
- rna[i] = 'G';
- break;
- default:
- fprintf(stderr, "%c is not a valid base!\n", dna[i]);
- break;
- }
- }
- return rna;
- }
- char *rna_to_trna(char *rna)
- {
- return dna_to_rna(rna);
- }
- char *rna_to_aminoacid(char *rna)
- {
- size_t sz = strlen(rna);
- unsigned i;
- char *polypeptide = malloc(sz);
- if (sz % 3) {
- fprintf(stderr, "length of RNA is not multiple of 3\n");
- free(polypeptide);
- return NULL;
- }
- for (i = 0; i < sz; i+=3) {
- switch (rna[i]) {
- case 'U':
- switch (rna[i + 1]) {
- case 'U':
- if (RNACOMP('U', 'C'))
- strcat(polypeptide, "phe"); /* UUU UUC */
- else
- strcat(polypeptide, "leu"); /* UUA UUG */
- break;
- case 'A':
- if (RNACOMP('U', 'C'))
- strcat(polypeptide, "tyr");
- else
- strcat(polypeptide, "end");
- break;
- case 'C':
- strcat(polypeptide, "ser");
- break;
- case 'G':
- if (RNACOMP('U', 'C'))
- strcat(polypeptide, "cys");
- else if (rna[i + 2] == 'A')
- strcat(polypeptide, "end");
- else
- strcat(polypeptide, "trp");
- break;
- default:
- break;
- }
- break;
- case 'A':
- switch (rna[i + 1]) {
- case 'U':
- if (rna[i + 2] == 'G')
- strcat(polypeptide, "met");
- else
- strcat(polypeptide, "ile");
- break;
- case 'A':
- if (RNACOMP('U', 'C'))
- strcat(polypeptide, "asn");
- else
- strcat(polypeptide, "lys");
- break;
- case 'C':
- strcat(polypeptide, "thr");
- break;
- case 'G':
- if (RNACOMP('U', 'C'))
- strcat(polypeptide, "ser");
- else
- strcat(polypeptide, "arg");
- break;
- default:
- break;
- }
- break;
- case 'C':
- switch (rna[i + 1]) {
- case 'U':
- strcat(polypeptide, "leu");
- break;
- case 'A':
- if (RNACOMP('U', 'C'))
- strcat(polypeptide, "his");
- else
- strcat(polypeptide, "gln");
- break;
- case 'C':
- strcat(polypeptide, "pro");
- break;
- case 'G':
- strcat(polypeptide, "arg");
- break;
- default:
- break;
- }
- break;
- case 'G':
- switch (rna[i + 1]) {
- case 'U':
- strcat(polypeptide, "val");
- break;
- case 'A':
- if (RNACOMP('U', 'C'))
- strcat(polypeptide, "asp");
- else
- strcat(polypeptide, "glu");
- break;
- case 'C':
- strcat(polypeptide, "ala");
- break;
- case 'G':
- strcat(polypeptide, "gly");
- break;
- default:
- break;
- }
- break;
- default:
- break;
- }
- }
- return polypeptide;
- }
- int main(int argc, char **argv)
- {
- char *rna;
- char *trna;
- char *polypeptide;
- if (argc != 2) {
- fprintf(stderr, "Usage: %s [dna here]\n", argv[0]);
- return 0;
- }
- rna = dna_to_rna(argv[1]);
- trna = rna_to_trna(rna);
- polypeptide = rna_to_aminoacid(rna);
- printf("RNA: %s\ntRNA: %s\nPolypeptide: %s\n", rna, trna, polypeptide);
- free(rna);
- free(trna);
- free(polypeptide);
- return 0;
- }
Add Comment
Please, Sign In to add comment