1 /******************************************************************
2 Copyright 2006 by Michael Farrar. All rights reserved.
3 This program may not be sold or incorporated into a commercial product,
4 in whole or in part, without written consent of Michael Farrar. For
5 further information regarding permission for use or reproduction, please
6 contact: Michael Farrar at farrar.michael@gmail.com.
7 *******************************************************************/
10 Written by Michael Farrar, 2006.
11 Please send bug reports and/or suggestions to farrar.michael@gmail.com.
23 char *skipSpaces (char *line)
25 while (isspace (*line)) {
32 signed char *readMatrix (char *file)
40 int order[ALPHA_SIZE];
48 if ((fp = fopen (file, "r")) == NULL) {
49 fprintf (stderr, "Unable to open file %s\n", file);
53 matrix = (signed char *) malloc (ALPHA_SIZE * ALPHA_SIZE);
55 fprintf (stderr, "Unable to allocate memory for scoring matrix\n");
59 /* initialize the order and mark arrays */
60 for (i = 0; i < ALPHA_SIZE; ++i) {
65 /* read the first line of the matrix giving the amino acid order */
67 while (!done && fgets (line, BUF_SIZE, fp) != NULL) {
68 char *ptr = skipSpaces (line);
69 if (*ptr && *ptr != '#') {
71 while (*ptr && *ptr != '#') {
72 int inx = AMINO_ACID_VALUE[*ptr];
75 fprintf (stderr, "Unknown amino acid %c in %s\n", *ptr, file);
77 } else if (mark[inx] != -1) {
78 fprintf (stderr, "Amino acid %c defined twice\n", *ptr);
80 } else if (count >= ALPHA_SIZE) {
81 /* this should not happen, but we will be safe */
82 fprintf (stderr, "Too many amino acids %d\n", count);
88 ptr = skipSpaces (ptr + 1);
95 /* make sure all amino acids are defined */
96 for (i = 0; i < ALPHA_SIZE; ++i) {
98 fprintf (stderr, "Missing column for amino acid %c\n",
106 fprintf (stderr, "Terminating due to errors in matrix file\n");
110 /* read the scores for the amino acids */
111 while (fgets (line, BUF_SIZE, fp) != NULL) {
113 char *ptr = skipSpaces (line);
114 if (*ptr && *ptr != '#') {
115 char aminoAcid = *ptr;
116 int inx = AMINO_ACID_VALUE[*ptr];
118 fprintf (stderr, "Unknown amino acid %c in matrix\n", *ptr);
120 } else if (mark[inx] != -1) {
121 fprintf (stderr, "Row %c defined twice\n", *ptr);
125 row = &matrix[inx * ALPHA_SIZE];
127 for (i = 0; i < ALPHA_SIZE; ++i) {
131 ptr = skipSpaces (ptr + 1);
140 if (*ptr >= '0' && *ptr <= '9') {
141 num = num * 10 + (*ptr - '0');
146 if (isspace (*ptr)) {
148 } else if (*ptr == 0) {
149 pName = "end of line";
155 fprintf (stderr, "Row %c Expecting digit found %s\n",
159 } while (*ptr && !isspace (*ptr));
163 if (num < -128 || num > 127) {
164 fprintf (stderr, "Weight %d out of range row %c\n",
170 row[order[i]] = (char) num;
173 if (i < ALPHA_SIZE) {
174 fprintf (stderr, "Amino acid row %c incomplete\n", aminoAcid);
182 /* make sure all amino acids are defined */
183 for (i = 0; i < ALPHA_SIZE; ++i) {
185 fprintf (stderr, "Missing row for amino acid %c\n",
192 fprintf (stderr, "Terminating due to errors in matrix %s\n", file);