Guest User

Untitled

a guest
Dec 16th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define RNACOMP(x0, x1) (rna[i + 2] == x0 || rna[i + 2] == x1)
  6.  
  7. char *dna_to_rna(char *dna)
  8. {
  9. size_t sz = strlen(dna);
  10. unsigned i;
  11.  
  12. char *rna = malloc(sz);
  13.  
  14. if (!rna) {
  15. fprintf(stderr, "cannot allocate memory for rna strand\n");
  16. return NULL;
  17. }
  18.  
  19. for (i = 0; i < sz; i++) {
  20. switch (dna[i]) {
  21. case 'A':
  22. rna[i] = 'U';
  23. break;
  24. case 'U':
  25. rna[i] = 'A';
  26. break;
  27. case 'T':
  28. rna[i] = 'A';
  29. break;
  30. case 'G':
  31. rna[i] = 'C';
  32. break;
  33. case 'C':
  34. rna[i] = 'G';
  35. break;
  36. default:
  37. fprintf(stderr, "%c is not a valid base!\n", dna[i]);
  38. break;
  39. }
  40. }
  41.  
  42. return rna;
  43. }
  44.  
  45. char *rna_to_trna(char *rna)
  46. {
  47. return dna_to_rna(rna);
  48. }
  49.  
  50. char *rna_to_aminoacid(char *rna)
  51. {
  52. size_t sz = strlen(rna);
  53. unsigned i;
  54.  
  55. char *polypeptide = malloc(sz);
  56.  
  57. if (sz % 3) {
  58. fprintf(stderr, "length of RNA is not multiple of 3\n");
  59. free(polypeptide);
  60. return NULL;
  61. }
  62.  
  63. for (i = 0; i < sz; i+=3) {
  64. switch (rna[i]) {
  65. case 'U':
  66. switch (rna[i + 1]) {
  67. case 'U':
  68. if (RNACOMP('U', 'C'))
  69. strcat(polypeptide, "phe"); /* UUU UUC */
  70. else
  71. strcat(polypeptide, "leu"); /* UUA UUG */
  72. break;
  73. case 'A':
  74. if (RNACOMP('U', 'C'))
  75. strcat(polypeptide, "tyr");
  76. else
  77. strcat(polypeptide, "end");
  78. break;
  79. case 'C':
  80. strcat(polypeptide, "ser");
  81. break;
  82. case 'G':
  83. if (RNACOMP('U', 'C'))
  84. strcat(polypeptide, "cys");
  85. else if (rna[i + 2] == 'A')
  86. strcat(polypeptide, "end");
  87. else
  88. strcat(polypeptide, "trp");
  89. break;
  90. default:
  91. break;
  92. }
  93. break;
  94. case 'A':
  95. switch (rna[i + 1]) {
  96. case 'U':
  97. if (rna[i + 2] == 'G')
  98. strcat(polypeptide, "met");
  99. else
  100. strcat(polypeptide, "ile");
  101. break;
  102. case 'A':
  103. if (RNACOMP('U', 'C'))
  104. strcat(polypeptide, "asn");
  105. else
  106. strcat(polypeptide, "lys");
  107. break;
  108. case 'C':
  109. strcat(polypeptide, "thr");
  110. break;
  111. case 'G':
  112. if (RNACOMP('U', 'C'))
  113. strcat(polypeptide, "ser");
  114. else
  115. strcat(polypeptide, "arg");
  116. break;
  117. default:
  118. break;
  119. }
  120. break;
  121. case 'C':
  122. switch (rna[i + 1]) {
  123. case 'U':
  124. strcat(polypeptide, "leu");
  125. break;
  126. case 'A':
  127. if (RNACOMP('U', 'C'))
  128. strcat(polypeptide, "his");
  129. else
  130. strcat(polypeptide, "gln");
  131. break;
  132. case 'C':
  133. strcat(polypeptide, "pro");
  134. break;
  135. case 'G':
  136. strcat(polypeptide, "arg");
  137. break;
  138. default:
  139. break;
  140. }
  141. break;
  142. case 'G':
  143. switch (rna[i + 1]) {
  144. case 'U':
  145. strcat(polypeptide, "val");
  146. break;
  147. case 'A':
  148. if (RNACOMP('U', 'C'))
  149. strcat(polypeptide, "asp");
  150. else
  151. strcat(polypeptide, "glu");
  152. break;
  153. case 'C':
  154. strcat(polypeptide, "ala");
  155. break;
  156. case 'G':
  157. strcat(polypeptide, "gly");
  158. break;
  159. default:
  160. break;
  161. }
  162. break;
  163. default:
  164. break;
  165. }
  166. }
  167.  
  168. return polypeptide;
  169. }
  170.  
  171. int main(int argc, char **argv)
  172. {
  173. char *rna;
  174. char *trna;
  175. char *polypeptide;
  176.  
  177. if (argc != 2) {
  178. fprintf(stderr, "Usage: %s [dna here]\n", argv[0]);
  179. return 0;
  180. }
  181.  
  182. rna = dna_to_rna(argv[1]);
  183. trna = rna_to_trna(rna);
  184. polypeptide = rna_to_aminoacid(rna);
  185.  
  186. printf("RNA: %s\ntRNA: %s\nPolypeptide: %s\n", rna, trna, polypeptide);
  187.  
  188. free(rna);
  189. free(trna);
  190. free(polypeptide);
  191.  
  192. return 0;
  193. }
Add Comment
Please, Sign In to add comment