1 /* @source embread ************************************************************
2 **
3 ** Data file reading routines
4 **
5 ** @author Copyright (c) 1999 Alan Bleasby
6 ** @version $Revision: 1.18 $
7 ** @modified $Date: 2011/11/08 15:12:52 $ by $Author: rice $
8 ** @@
9 **
10 ** This library is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU Lesser General Public
12 ** License as published by the Free Software Foundation; either
13 ** version 2.1 of the License, or (at your option) any later version.
14 **
15 ** This library is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ** Lesser General Public License for more details.
19 **
20 ** You should have received a copy of the GNU Lesser General Public
21 ** License along with this library; if not, write to the Free Software
22 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 ** MA  02110-1301,  USA.
24 **
25 ******************************************************************************/
26 
27 
28 #include "ajlib.h"
29 
30 #include "embread.h"
31 #include "ajfiledata.h"
32 #include "ajfileio.h"
33 #include "ajsys.h"
34 #include "ajbase.h"
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 
41 
42 
43 
44 /* @func embReadAminoDataDoubleC **********************************************
45 **
46 ** Read amino acid properties from amino.dat
47 **
48 ** @param [r] s [const char*] datafile name
49 ** @param [w] a [double**] array for amino acid values
50 ** @param [r] fill [double] initialisation value
51 **
52 ** @return [AjBool] ajTrue on success
53 **
54 ** @release 1.0.0
55 ** @@
56 ******************************************************************************/
57 
embReadAminoDataDoubleC(const char * s,double ** a,double fill)58 AjBool embReadAminoDataDoubleC(const char *s, double **a, double fill)
59 {
60     AjPFile inf;
61     AjPStr  line;
62 
63     const char *p;
64     ajint  idx;
65     ajint  i;
66 
67     inf = ajDatafileNewInNameC(s);
68 
69     if(!inf)
70     {
71 	ajWarn("File [%s] not found",s);
72 
73 	return ajFalse;
74     }
75 
76     *a = AJALLOC(AJREADAMINO*sizeof(double));
77 
78     for(i=0;i<AJREADAMINO;++i)
79 	(*a)[i] = fill;
80 
81     line = ajStrNew();
82 
83     while(ajReadlineTrim(inf,&line))
84     {
85 	p = ajStrGetPtr(line);
86 
87 	if(*p=='#' || *p=='!' || !*p)
88 	    continue;
89 
90 	p = ajSysFuncStrtok(p," \t\r");
91 
92 	if(!p || *(p+1))
93 	{
94 	    ajWarn("First token is not a single letter");
95 	    ajFileClose(&inf);
96 	    ajStrDel(&line);
97 	    AJFREE(*a);
98 
99 	    return ajFalse;
100 	}
101 
102 	idx = ajBasecodeToInt(*p);
103 	p = ajSysFuncStrtok(NULL," \t\r");
104 
105 	if(!p)
106 	{
107 	    ajWarn("Missing second token");
108 	    ajFileClose(&inf);
109 	    ajStrDel(&line);
110 	    AJFREE(*a);
111 
112 	    return ajFalse;
113 	}
114 
115 	if(sscanf(p,"%lf",&(*a)[idx])!=1)
116 	{
117 	    ajWarn("Bad numeric conversion [%s]",p);
118 	    ajFileClose(&inf);
119 	    ajStrDel(&line);
120 	    AJFREE(*a);
121 
122 	    return ajFalse;
123 	}
124     }
125 
126     ajFileClose(&inf);
127     ajStrDel(&line);
128 
129     return ajTrue;
130 }
131 
132 
133 
134 
135 /* @func embReadAminoDataFloatC ***********************************************
136 **
137 ** Read amino acid properties from amino.dat
138 **
139 ** @param [r] s [const char*] datafile name
140 ** @param [w] a [float**] array for amino acid values
141 ** @param [r] fill [float] initialisation value
142 **
143 ** @return [AjBool] ajTrue on success
144 **
145 ** @release 1.0.0
146 ** @@
147 ******************************************************************************/
148 
embReadAminoDataFloatC(const char * s,float ** a,float fill)149 AjBool embReadAminoDataFloatC(const char *s, float **a, float fill)
150 {
151     AjPFile inf;
152     AjPStr  line;
153 
154     const char *p;
155     ajint  idx;
156     ajint  i;
157 
158     inf = ajDatafileNewInNameC(s);
159 
160     if(!inf)
161     {
162 	ajWarn("File [%s] not found",s);
163 
164 	return ajFalse;
165     }
166 
167     *a = AJALLOC(AJREADAMINO*sizeof(float));
168 
169     for(i=0;i<AJREADAMINO;++i)
170 	(*a)[i] = fill;
171 
172     line = ajStrNew();
173 
174     while(ajReadlineTrim(inf,&line))
175     {
176 	p = ajStrGetPtr(line);
177 
178 	if(*p=='#' || *p=='!' || !*p)
179 	    continue;
180 
181 	p = ajSysFuncStrtok(p," \t\r");
182 
183 	if(!p || *(p+1))
184 	{
185 	    ajWarn("First token is not a single letter");
186 	    ajFileClose(&inf);
187 	    ajStrDel(&line);
188 	    AJFREE(*a);
189 
190 	    return ajFalse;
191 	}
192 
193 	idx = ajBasecodeToInt(*p);
194 	p   = ajSysFuncStrtok(NULL," \t\r");
195 
196 	if(!p)
197 	{
198 	    ajWarn("Missing second token");
199 	    ajFileClose(&inf);
200 	    ajStrDel(&line);
201 	    AJFREE(*a);
202 
203 	    return ajFalse;
204 	}
205 
206 	if(sscanf(p,"%f",&(*a)[idx])!=1)
207 	{
208 	    ajWarn("Bad numeric conversion [%s]",p);
209 	    ajFileClose(&inf);
210 	    ajStrDel(&line);
211 	    AJFREE(*a);
212 
213 	    return ajFalse;
214 	}
215     }
216 
217     ajFileClose(&inf);
218     ajStrDel(&line);
219 
220     return ajTrue;
221 }
222 
223 
224 
225 
226 /* @func embReadAminoDataIntC *************************************************
227 **
228 ** Read amino acid properties from amino.dat
229 **
230 ** @param [r] s [const char*] datafile name
231 ** @param [w] a [ajint**] array for amino acid values
232 ** @param [r] fill [ajint] initialisation value
233 **
234 ** @return [AjBool] ajTrue on success
235 **
236 ** @release 1.0.0
237 ** @@
238 ******************************************************************************/
239 
embReadAminoDataIntC(const char * s,ajint ** a,ajint fill)240 AjBool embReadAminoDataIntC(const char *s, ajint **a, ajint fill)
241 {
242     AjPFile inf;
243     AjPStr  line;
244 
245     const char *p;
246     ajint  idx;
247     ajint  i;
248 
249     inf = ajDatafileNewInNameC(s);
250 
251     if(!inf)
252     {
253 	ajWarn("File [%s] not found",s);
254 
255 	return ajFalse;
256     }
257 
258     *a = AJALLOC(AJREADAMINO*sizeof(ajint));
259 
260     for(i=0;i<AJREADAMINO;++i)
261 	(*a)[i] = fill;
262 
263 
264     line = ajStrNew();
265 
266     while(ajReadlineTrim(inf,&line))
267     {
268 	p = ajStrGetPtr(line);
269 
270 	if(*p=='#' || *p=='!' || !*p)
271 	    continue;
272 
273 	p = ajSysFuncStrtok(p," \t\r");
274 
275 	if(!p || *(p+1))
276 	{
277 	    ajWarn("First token is not a single letter");
278 	    ajFileClose(&inf);
279 	    ajStrDel(&line);
280 	    AJFREE(*a);
281 
282 	    return ajFalse;
283 	}
284 
285 	idx = ajBasecodeToInt(*p);
286 	p   = ajSysFuncStrtok(NULL," \t\r");
287 
288 	if(!p)
289 	{
290 	    ajWarn("Missing second token");
291 	    ajFileClose(&inf);
292 	    ajStrDel(&line);
293 	    AJFREE(*a);
294 
295 	    return ajFalse;
296 	}
297 
298 	if(sscanf(p,"%d",&(*a)[idx])!=1)
299 	{
300 	    ajWarn("Bad numeric conversion [%s]",p);
301 	    ajFileClose(&inf);
302 	    ajStrDel(&line);
303 	    AJFREE(*a);
304 
305 	    return ajFalse;
306 	}
307     }
308 
309     ajFileClose(&inf);
310     ajStrDel(&line);
311 
312     return ajTrue;
313 }
314