/*------------------------------------------------------------- Copyright (C) 2000 Stefan Schoenauer, Peter Clote. All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation for NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies. THE AUTHOR AND PUBLISHER MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. -------------------------------------------------------------*/ /**************************************************************************** * general.c: Implementation of general support functions for the project * "Error optimized genetic code". ****************************************************************************/ /* Include section */ #include #include #include #include "general.h" /* global variables */ /* The standard natural genetic code. */ unsigned int std_code_array[4][4][4] = { { {13, 15, 18, 4}, {13, 15, 18, 4}, {10, 15, 20, 20}, {10, 15, 20, 17} }, { {10, 14, 8, 1}, {10, 14, 8, 1}, {10, 14, 5, 1}, {10, 14, 5, 1} }, { {9, 16, 2, 15}, {9, 16, 2, 15}, {9, 16, 11, 1}, {12, 16, 11, 1} }, { {19, 0, 3, 7}, {19, 0, 3, 7}, {19, 0, 6, 7}, {19, 0, 6, 7} } }; gcode std_code=(gcode)std_code_array; /* This is a table to convert the number of an amino acid into the one-letter code plus a symbol for the stop codon. */ unsigned char num2char[NUM_ACIDS_SS] = { 'A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V', '#' }; /* This is a table to convert the number of an amino acid into the three-letter code plus a symbol for the stop codon. */ char *num2triple[NUM_ACIDS_SS] = { "Ala", "Arg", "Asn", "Asp", "Cys", "Gln", "Glu", "Gly", "His", "Ile", "Leu", "Lys", "Met", "Phe", "Pro", "Ser", "Thr", "Trp", "Tyr", "Val", "Stop" }; /* smatrix is a matrix which contains a similarity measure for amino acid substitutions. */ float smatrix[NUM_ACIDS][NUM_ACIDS]; /* dmatrix is a matrix which contains a similarity measure for amino acid substitutions. */ float dmatrix[NUM_ACIDS][NUM_ACIDS]; /**************************************************************************** read_matrix(): function to read a scoring matrix from disk. Input is a 20 x 20 floating pt similarity matrix, where the amino acids 0..19 are in alphabetical order for 3-letter codes, separated by blanks. input: 1. name of the matrix file output: 0 == Everything O.K. != 0 Error ocurred ****************************************************************************/ int read_matrix(char *filename, float matrix[NUM_ACIDS][NUM_ACIDS]) { float temp = 0; /* contains the last value read */ FILE *fp = fopen(filename, "r"); int i,j; /* counters for loops */ if (fp == NULL) { fprintf(stderr, "Could not open scoring matrix file!\n"); return 1; } for (i = 0; i < NUM_ACIDS; i++) { for (j = i; j < NUM_ACIDS; j++) { if (fscanf(fp, "%f ", &temp) == EOF) { fprintf(stderr, "File contains no scoring matrix!\n"); fclose(fp); return 1; } matrix[i][j] = temp; matrix[j][i] = temp; } } return 0; } void print_matrix(float matrix[NUM_ACIDS][NUM_ACIDS]) { int i,j; printf(" "); for (j = 0; j < NUM_ACIDS; j++) printf(" %s",num2triple[j]); printf("\n"); for (i = 0; i < NUM_ACIDS; i++) { printf("%s",num2triple[i]); for (j = 0; j < NUM_ACIDS; j++) { printf(" %5.1f",matrix[i][j]); } printf("\n"); } } /************************************************************ * * Some more procedural abstraction. 07/99 SW * ************************************************************/ /* rand_flt() returns random number in [0..1[ */ double rand_flt() { double r=rand()/(RAND_MAX+1.0); assert (r>=0.0 && r<1.0); return r; } /* rand_int() returns random integer number in [0..n[ */ int rand_int(int n) { int r = (int) (n*rand_flt()); assert(r>=0 && r