| 1 | /* PAML interface. |
| 2 | * |
| 3 | * Ziheng Yang, "Phylogenetic Analysis by Maximum Likelihood" [Yang97] |
| 4 | * http://abacus.gene.ucl.ac.uk/software/paml.html |
| 5 | */ |
| 6 | #include <esl_config.h> |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include "easel.h" |
| 13 | #include "esl_alphabet.h" |
| 14 | #include "esl_vectorops.h" |
| 15 | #include "esl_dmatrix.h" |
| 16 | #include "esl_fileparser.h" |
| 17 | #include "esl_paml.h" |
| 18 | |
| 19 | /* Function: esl_paml_ReadE() |
| 20 | * Incept: SRE, Fri Jul 9 09:27:24 2004 [St. Louis] |
| 21 | * |
| 22 | * Purpose: Read an amino acid rate matrix in PAML format from stream |
| 23 | * <fp>. Return it in two pieces: the symmetric E |
| 24 | * exchangeability matrix in <E>, and the stationary |
| 25 | * probability vector $\pi$ in <pi>. |
| 26 | * Caller provides the memory for both <E> and <pi>. <E> |
| 27 | * is a $20 \times 20$ matrix allocated as |
| 28 | * <esl_dmatrix_Create(20, 20)>. <pi> is an array with |
| 29 | * space for at least 20 doubles. |
| 30 | * |
| 31 | * The <E> matrix is symmetric for off-diagonal elements: |
| 32 | * $E_{ij} = E_{ij}$ for $i \neq j$. The on-diagonal |
| 33 | * elements $E_{ii}$ are not valid and should not be |
| 34 | * accessed. (They are set to zero.) |
| 35 | |
| 36 | * The rate matrix will later be obtained from <E> |
| 37 | * and <pi> as |
| 38 | * $Q_{ij} = E_{ij} \pi_j$ for $i \neq j$ |
| 39 | * and |
| 40 | * $Q_{ii} = -\sum_{j \neq i} Q_{ij}$ |
| 41 | * then scaled to units of one |
| 42 | * substitution/site; see <esl_ratemx_E2Q()> and |
| 43 | * <esl_ratemx_ScaleTo()>. |
| 44 | * |
| 45 | * Data file format: First 190 numbers are a |
| 46 | * lower-triangular matrix E of amino acid |
| 47 | * exchangeabilities $E_{ij}$. Next 20 numbers are the |
| 48 | * amino acid frequencies $\pi_i$. Remainder of the |
| 49 | * datafile is ignored. |
| 50 | * |
| 51 | * The alphabet order in the matrix and the frequency |
| 52 | * vector is assumed to be "ARNDCQEGHILKMFPSTWYV" |
| 53 | * (alphabetical by three-letter code), which appears to be |
| 54 | * PAML's default order. This is transformed to Easel's |
| 55 | * "ACDEFGHIKLMNPQRSTVWY" (alphabetical by one-letter code) |
| 56 | * in the $E_{ij}$ and $\pi_i$ that are returned. |
| 57 | * |
| 58 | * Args: fp - open datafile for reading. |
| 59 | * E - RETURN: E matrix of amino acid exchangeabilities e_ij, |
| 60 | * symmetric (E_ij = E_ji), |
| 61 | * in Easel amino acid alphabet order A..Y. |
| 62 | * Caller provides appropriately allocated space. |
| 63 | * pi - RETURN: \pi_i vector of amino acid frequencies, |
| 64 | * in Easel amino acid alphabet order A..Y. |
| 65 | * Caller provides appropriately allocated space. |
| 66 | * |
| 67 | * Returns: <eslOK> on success. |
| 68 | * Returns <eslEOF> on premature end of file (parse failed), in which |
| 69 | * case the contents of <E> and <pi> are undefined. |
| 70 | * |
| 71 | * Throws: <eslEMEM> on internal allocation failure, |
| 72 | * and the contents of <E> and <pi> are undefined. |
| 73 | * |
| 74 | * Xref: STL8/p.56. |
| 75 | */ |
| 76 | int |
| 77 | esl_paml_ReadE(FILE *fp, ESL_DMATRIX *E, double *pi) |
| 78 | { |
| 79 | int status; |
| 80 | ESL_FILEPARSER *efp = NULL; |
| 81 | char *tok; |
| 82 | int i,j; |
| 83 | char *pamlorder = "ARNDCQEGHILKMFPSTWYV"; |
| 84 | char *eslorder = "ACDEFGHIKLMNPQRSTVWY"; |
| 85 | int perm[20]; |
| 86 | |
| 87 | if ((status = esl_dmatrix_SetZero(E)) != eslOK) goto ERROR; |
| 88 | esl_vec_DSet(pi, 20, 0.); |
| 89 | |
| 90 | if ((efp = esl_fileparser_Create(fp)) == NULL) goto ERROR; |
| 91 | if ((status = esl_fileparser_SetCommentChar(efp, '#')) != eslOK) goto ERROR; |
| 92 | |
| 93 | /* Construct the alphabet permutation we need. |
| 94 | * perm[i] -> original row/column i goes to row/column perm[i] |
| 95 | */ |
| 96 | for (i = 0; i < 20; i++) |
| 97 | perm[i] = (int) (strchr(eslorder, pamlorder[i]) - eslorder); |
| 98 | |
| 99 | /* Read the s_ij matrix data in, permuting as we go. */ |
| 100 | |
| 101 | for (i = 1; i < 20; i++) |
| 102 | for (j = 0; j < i; j++) |
| 103 | { |
| 104 | if ((status = esl_fileparser_GetToken(efp, &tok, NULL)) != eslOK) goto ERROR; |
| 105 | E->mx[perm[i]][perm[j]] = atof(tok); |
| 106 | E->mx[perm[j]][perm[i]] = E->mx[perm[i]][perm[j]]; |
| 107 | } |
| 108 | |
| 109 | /* Read the pi_i vector in, permuting as we read. */ |
| 110 | for (i = 0; i < 20; i++) |
| 111 | { |
| 112 | if ((status = esl_fileparser_GetToken(efp, &tok, NULL)) != eslOK) goto ERROR; |
| 113 | pi[perm[i]] = atof(tok); |
| 114 | } |
| 115 | |
| 116 | esl_fileparser_Destroy(efp); |
| 117 | return eslOK; |
| 118 | |
| 119 | ERROR: |
| 120 | if (efp != NULL) esl_fileparser_Destroy(efp); |
| 121 | return status; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /***************************************************************** |
| 126 | * Utility: reformat a PAML file to a static vector |
| 127 | *****************************************************************/ |
| 128 | #ifdef eslPAML_UTILITY1 |
| 129 | |
| 130 | /* gcc -g -Wall -o utility -I. -L. -DeslPAML_UTILITY1 esl_paml.c -leasel -lm |
| 131 | */ |
| 132 | #include "easel.h" |
| 133 | #include "esl_dmatrix.h" |
| 134 | #include "esl_paml.h" |
| 135 | |
| 136 | int |
| 137 | main(int argc, char **argv) |
| 138 | { |
| 139 | char *filename = argv[1]; |
| 140 | FILE *fp = NULL; |
| 141 | ESL_DMATRIX *E = NULL; |
| 142 | double *pi = NULL; |
| 143 | int i,j,n; |
| 144 | |
| 145 | E = esl_dmatrix_Create(20, 20); |
| 146 | pi = malloc(20 * sizeof(double)); |
| 147 | if ((fp = fopen(filename, "r")) == NULL) esl_fatal("open failed"); |
| 148 | if (esl_paml_ReadE(fp, E, pi) != eslOK) esl_fatal("parse failed"); |
| 149 | |
| 150 | n = 1; |
| 151 | for (i = 1; i < 20; i++) |
| 152 | for (j = 0; j < i; j++) |
| 153 | { |
| 154 | printf("%8.6f, ", E->mx[i][j]); |
| 155 | if (n++ == 10) { puts(""); n=1; } |
| 156 | } |
| 157 | |
| 158 | puts(""); |
| 159 | |
| 160 | n = 1; |
| 161 | for (i = 0; i < 20; i++) |
| 162 | { |
| 163 | printf("%8.6f, ", pi[i]); |
| 164 | if (n++ == 10) { puts(""); n=1; } |
| 165 | } |
| 166 | |
| 167 | fclose(fp); |
| 168 | free(pi); |
| 169 | esl_dmatrix_Destroy(E); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | #endif /*eslPAML_UTILITY1*/ |
| 174 |