initial commit
[jalview.git] / forester / archive / RIO / others / hmmer / src / misc.c
1 /************************************************************
2  * HMMER - Biological sequence analysis with profile HMMs
3  * Copyright (C) 1992-1999 Washington University School of Medicine
4  * All Rights Reserved
5  * 
6  *     This source code is distributed under the terms of the
7  *     GNU General Public License. See the files COPYING and LICENSE
8  *     for details.
9  ************************************************************/
10
11 /* misc.c
12  * SRE, Thu Jul 15 18:49:19 1993
13  * 
14  * Functions that I don't know quite where to put yet.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include <float.h>
22 #include <limits.h>
23
24 #include "squid.h"
25 #include "config.h"
26 #include "structs.h"
27 #include "version.h"
28
29 /* Function: Getword()
30  * 
31  * Purpose:  little function used by ReadPrior() and ReadHMM() to parse
32  *           next valid field out of an open file, ignoring
33  *           comments. '#' marks the beginning of a comment.
34  *
35  * Arg:      fp   - open file for reading
36  *           type - sqdARG_INT, sqdARG_FLOAT, or sqdARG_STRING from squid.h
37  */
38 char *
39 Getword(FILE *fp, int type)
40 {
41   static char buffer[512];
42   static char *sptr = NULL;
43   
44   if (sptr != NULL) sptr = strtok(NULL, " \t\n");
45
46   while (sptr == NULL)
47     {
48       if ((sptr = fgets(buffer, 512, fp)) == NULL) return NULL;
49       if ((sptr = strchr(buffer, '#')) != NULL) *sptr = '\0';
50       sptr = strtok(buffer, " \t\n");
51     }
52
53   switch (type) {
54   case sqdARG_STRING: 
55     if (strlen(sptr) == 0) { 
56       Warn("Parse failed: expected string, got nothing"); 
57       sptr = NULL; 
58     }
59     break;
60   case sqdARG_INT:    
61     if (!IsInt(sptr)) {
62       Warn("Parse failed: expected integer, got %s", sptr);
63       sptr = NULL;
64     }
65     break;
66   case sqdARG_FLOAT:
67     if (!IsReal(sptr)) {
68       Warn("Parse failed: expected real value, got %s", sptr); 
69       sptr = NULL;
70     }
71     break;
72   }
73
74   return sptr;
75 }
76
77
78 /* Function: Getline()
79  * 
80  * Purpose:  Get the next non-blank, non-comment line from an open file.
81  *           A comment line has '#' as the first non-whitespace character.
82  *           Returns NULL if no line is found. 
83  *           Syntax is the same as fgets().
84  *           
85  * Args:     s  - allocated storage for line
86  *           n  - number of characters allocated for s
87  *           fp - open FILE *
88  *           
89  * Return:   Either s, or NULL if no new line is found.
90  */        
91 char * 
92 Getline(char *s, int n, FILE *fp)
93 {
94   char *first;
95
96   do {
97     if (fgets(s, n, fp) == NULL) return NULL;
98     first = s; while (isspace((int) (*first))) first++;
99   } while (*first == '#' || *first == '\0');
100   return s;
101 }
102
103
104 /* Function: SetAutocuts()
105  * Date:     SRE, Thu Jun  8 08:19:46 2000 [TW721 over Ireland]
106  *
107  * Purpose:  Set score thresholds using the GA, TC, or NC information
108  *           in an HMM.
109  *
110  * Args:     thresh - score threshold structure. autocut must be set
111  *                    properly (CUT_GA, CUT_NC, or CUT_TC).
112  *           hmm    - HMM containing appropriate score cutoff info
113  *
114  * Returns:  1 on success.
115  *           0 if HMM does not have the score cutoffs available -- caller
116  *             will have to decide on a fallback plan.
117  *           Has no effect (and returns success) if autocut is
118  *           CUT_NONE.
119  */
120 int
121 SetAutocuts(struct threshold_s *thresh, struct plan7_s *hmm)
122 {
123   if (thresh->autocut == CUT_GA) {
124     if (! (hmm->flags & PLAN7_GA)) return 0;
125     thresh->globT = hmm->ga1;
126     thresh->domT  = hmm->ga2;
127     thresh->globE = thresh->domE = FLT_MAX;
128   } else if (thresh->autocut == CUT_NC) {
129     if (! (hmm->flags & PLAN7_NC)) return 0;
130     thresh->globT = hmm->nc1;
131     thresh->domT  = hmm->nc2;
132     thresh->globE = thresh->domE = FLT_MAX;
133   } else if (thresh->autocut == CUT_TC) {
134     if (! (hmm->flags & PLAN7_TC)) return 0;
135     thresh->globT = hmm->tc1;
136     thresh->domT  = hmm->tc2;
137     thresh->globE = thresh->domE = FLT_MAX;
138   }
139   return 1;
140 }