Skip to main content
Code Review

Return to Question

Tweeted twitter.com/StackCodeReview/status/984363690622357504
added 27 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I decided to redo the first program I wrote (after the hello world thing). So, what it does is to convert a DNA sequence string to an array of strings that represent the amino acids that are made in a cell by ribosomes when that DNA sequence gets used (https://en.wikipedia.org/wiki/DNA_codon_tableDNA codon table).

I decided to redo the first program I wrote (after the hello world thing). So, what it does is to convert a DNA sequence string to an array of strings that represent the amino acids that are made in a cell by ribosomes when that DNA sequence gets used (https://en.wikipedia.org/wiki/DNA_codon_table).

I decided to redo the first program I wrote (after the hello world thing). So, what it does is to convert a DNA sequence string to an array of strings that represent the amino acids that are made in a cell by ribosomes when that DNA sequence gets used (DNA codon table).

Source Link

Implementing a DNA codon table in C

I decided to redo the first program I wrote (after the hello world thing). So, what it does is to convert a DNA sequence string to an array of strings that represent the amino acids that are made in a cell by ribosomes when that DNA sequence gets used (https://en.wikipedia.org/wiki/DNA_codon_table).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* codon_table[4][4][4] =
{
 {
 {"Phe", "Phe", "Len", "Len"},
 {"Ser", "Ser", "Ser", "Ser"},
 {"Tyr", "Tyr", "Stop", "Stop"},
 {"Cys", "Cys", "Stop", "Trp"}
 },
 {
 {"Len", "Len", "Len", "Len"},
 {"Pro", "Pro", "Pro", "Pro"},
 {"His", "His", "Gln", "Gln"},
 {"Arg", "Arg", "Arg", "Arg"}
 },
 {
 {"Ile", "Ile", "Ile", "Met"},
 {"Thr", "Thr", "Thr", "Thr"},
 {"Asn", "Asn", "Lys", "Lys"},
 {"Ser", "Ser", "Arg", "Arg"}
 },
 {
 {"Val", "Val", "Val", "Val"},
 {"Ala", "Ala", "Ala", "Ala"},
 {"Asp", "Asp", "Glu", "Glu"},
 {"Gly", "Gly", "Gly", "Gly"}
 }
};
int check_nucleobase_string(char* nucleobase_string)
{
 if(nucleobase_string==NULL)
 {
 return 1;
 }
 for(size_t i = 0; nucleobase_string[i] != '0円'; i+=1)
 {
 if(
 nucleobase_string[i] != 't' &&
 nucleobase_string[i] != 'c' &&
 nucleobase_string[i] != 'a' &&
 nucleobase_string[i] != 'g'
 )
 {
 return 0;
 }
 }
 return 1;
}
size_t nucleobase_to_aminoacid(char* nucleobase_string, char*** aminoacid_string)
{
 if(!check_nucleobase_string(nucleobase_string))
 {
 printf("Erroneous input: %s\n", nucleobase_string);
 return 0;
 }
 size_t nucleobase_count ;
 for(nucleobase_count = 0; nucleobase_string[nucleobase_count] != '0円'; nucleobase_count+=1);
 size_t aminoacid_count = nucleobase_count/3;
 (*aminoacid_string) = malloc(aminoacid_count);
 for(size_t i = 0, slow_i = 0; slow_i < aminoacid_count; i+=3, slow_i+=1)
 {
 (*aminoacid_string)[slow_i] =
 codon_table[
 nucleobase_string[i] == 't' ? 0 :
 nucleobase_string[i] == 'c' ? 1 :
 nucleobase_string[i] == 'a' ? 2 :
 nucleobase_string[i] == 'g' ? 3 : -1
 ][
 nucleobase_string[i+1] == 't' ? 0 :
 nucleobase_string[i+1] == 'c' ? 1 :
 nucleobase_string[i+1] == 'a' ? 2 :
 nucleobase_string[i+1] == 'g' ? 3 : -1
 ][
 nucleobase_string[i+2] == 't' ? 0 :
 nucleobase_string[i+2] == 'c' ? 1 :
 nucleobase_string[i+2] == 'a' ? 2 :
 nucleobase_string[i+2] == 'g' ? 3 : -1
 ];
 }
 return aminoacid_count;
}
int main()
{
 printf("Enter DNA sequence in 5' → 3' direction.\n");
 char* input_string = NULL;
 size_t n = 0;
 ssize_t line_length = getline(&input_string, &n, stdin);
 if(line_length == -1)
 {
 printf("Failed to read a line.\n");
 return 0;
 }
 char* nucleobase_string = malloc(line_length);
 memcpy(nucleobase_string, input_string, line_length-1);
 nucleobase_string[line_length-1] = '0円';
 char** aminoacid_string = NULL;
 size_t aminoacid_count = nucleobase_to_aminoacid(nucleobase_string, &aminoacid_string);
 for(size_t i = 0; i < aminoacid_count; i+=1)
 {
 if(i!=0)
 {
 printf(" + ");
 }
 printf("%s", aminoacid_string[i]);
 }
 printf("\n");
 return 0;
};
lang-c

AltStyle によって変換されたページ (->オリジナル) /