Skip to content

Instantly share code, notes, and snippets.

@carlos-jenkins
Last active August 29, 2015 14:07
Show Gist options
  • Save carlos-jenkins/c77808889b52b75e93e8 to your computer and use it in GitHub Desktop.
Save carlos-jenkins/c77808889b52b75e93e8 to your computer and use it in GitHub Desktop.
Genetic code conversion routine implemented in constant time using a lookup table. It supports both DNA and mRNA sequences.
/*
* Copyright (C) 2014 Carlos Jenkins <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Genetic code conversion routine implemented in constant time using a lookup
* table. It supports both DNA and mRNA sequences.
* Compile me with:
* gcc -o genetic_code genetic_code.c
*/
#include <stdio.h>
static const char GENETIC_CODE[4][4][4] = {
/* U C A G */
/* _________________ _________________ _________________ _________________ */
/* U C A G U C A G U C A G U C A G */
/* U */ { {'F','F','L','L'} , {'S','S','S','S'} , {'Y','Y','*','*'} , {'C','C','*','W'} },
/* C */ { {'L','L','L','L'} , {'P','P','P','P'} , {'H','H','Q','Q'} , {'R','R','R','R'} },
/* A */ { {'I','I','I','M'} , {'T','T','T','T'} , {'N','N','K','K'} , {'S','S','R','R'} },
/* G */ { {'V','V','V','V'} , {'A','A','A','A'} , {'D','D','E','E'} , {'G','G','G','G'} }
};
unsigned int base_index(char which_one)
{
int result = 0; // Default, U and T
switch(which_one) {
case 'C':
result = 1;
break;
case 'A':
result = 2;
break;
case 'G':
result = 3;
break;
}
return result;
}
int main(int argc, char **argv)
{
char code[] = "AAU";
char amino = GENETIC_CODE[base_index(code[0])][base_index(code[1])][base_index(code[2])];
printf("Aminoacid for AAU is: %c \n", amino);
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment