Debian Codegraph @

src/pssm_asn_subs.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* $Id: pssm_asn_subs.c 1265 2014-06-30 16:13:49Z wrp $ */
2
3 /* copyright (C) 2005, 2014 by William R. Pearson and The Rector &
4 Vistors of the University of Virginia */
5
6 /* Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under this License is distributed on an "AS
14 IS" BASIS, WITHOUT WRRANTIES OR CONDITIONS OF ANY KIND, either
15 express or implied. See the License for the specific language
16 governing permissions and limitations under the License.
17 */
18
19 /* read_asn_dest modified 26-Jul-2007 to skip over text/bytes if dest is NULL */
20
21 /* this code is designed to parse the ASN.1 binary encoded scoremat
22 object produced by blastpgp -C file.ckpt_asn -u 2 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "defs.h"
29
30 int parse_pssm_asn();
31 int parse_pssm2_asn();
32
33 int
34 parse_pssm_asn_fa(FILE *afd, int *n_rows, int *n_cols,
35 unsigned char **query, double ***wfreqs, double ***freqs, int ***iscores,
36 char *matrix, int *gap_open, int *gap_extend,
37 double *lambda);
38
39 #define COMPO_NUM_TRUE_AA 20
40
41 /**positions of true characters in protein alphabet*/
42 /*
43 static int trueCharPositions[COMPO_NUM_TRUE_AA] = {
44 1,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22
45 };
46 */
47
48 #define COMPO_LARGEST_ALPHABET 28
49
50 /*
51 static char ncbieaatoa[COMPO_LARGEST_ALPHABET] = {"-ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
52
53 static int alphaConvert[COMPO_LARGEST_ALPHABET] = {
54 (-1), 0, (-1), 4, 3, 6, 13, 7, 8, 9, 11, 10, 12, 2, 14, 5, 1, 15,
55 16, 19, 17, (-1), 18, (-1), (-1), (-1), (-1), (-1)
56 };
57 */
58
59 int pssm_aa_order[20] = { 1, /*A*/
60 16, /*R*/
61 13, /*N*/
62 4, /*D*/
63 3, /*C*/
64 15, /*Q*/
65 5, /*E*/
66 7, /*G*/
67 8, /*H*/
68 9, /*I*/
69 11, /*L*/
70 10, /*K*/
71 12, /*M*/
72 6, /*F*/
73 14, /*P*/
74 17, /*S*/
75 18, /*T*/
76 20, /*W*/
77 22, /*Y*/
78 19}; /*V*/
79
80 #define ABP *asnp->abp
81 #define ABPP asnp->abp
82 #define ABP_INC2 asnp->abp += 2
83
84 #define ASN_SEQ 48
85 #define ASN_SET 48
86 #define ASN_SEQOF 49
87 #define ASN_SETOF 49
88
89 #define ASN_PSSM_QUERY 166
90 #define ASN_PSSM2_VERSION 160
91 #define ASN_PSSM2_QUERY 161
92 #define ASN_PSSM2_MATRIX 162
93
94 #define ASN_PSSM_IS_PROT 160
95 #define ASN_PSSM_NROWS 162
96 #define ASN_PSSM_NCOLS 163
97
98 #define ASN_PSSM_BYCOL 165
99 #define ASN_PSSM_INTERMED_DATA 167
100 #define ASN_PSSM_INTERMED_RES_FREQS 160
101 #define ASN_PSSM_INTERMED_WRES_FREQS 161
102 #define ASN_PSSM_INTERMED_FREQ_RATIOS 162
103 #define ASN_PSSM_INTERMED_INFO_CONTENT 163
104 #define ASN_PSSM_INTERMED_GAPL_COLWTS 164
105 #define ASN_PSSM_INTERMED_SIGMA 165
106 #define ASN_PSSM_INTERMED_INTVAL_SIZE 166
107 #define ASN_PSSM_INTERMED_NUM_MATCH_SEQ 167
108
109 #define ASN_PSSM_FREQS 162
110
111 #define ASN_PSSM_FINAL_DATA 168 /* Sequence */
112 #define ASN_PSSM_FINAL_DATA_SCORES 160 /* sequence of integer */
113 #define ASN_PSSM_FINAL_DATA_LAMBDA 161 /* real */
114 #define ASN_PSSM_FINAL_DATA_KAPPA 162 /* real */
115 #define ASN_PSSM_FINAL_DATA_H 163 /* real */
116 #define ASN_PSSM_FINAL_DATA_SCALEF 164 /* integer */
117 #define ASN_PSSM_FINAL_DATA_ULAMBDA 165 /* real */
118 #define ASN_PSSM_FINAL_DATA_UKAPPA 166 /* real */
119 #define ASN_PSSM_FINAL_DATA_UH 167 /* real */
120
121 #define ASN_PSSM2_IS_PROTEIN 160
122 #define ASN_PSSM2_MATRIX_NAME 161
123 #define ASN_PSSM2_MATRIX_COMMENT 162 /* not used */
124 #define ASN_PSSM2_NCOLS 163
125 #define ASN_PSSM2_NROWS 164
126 #define ASN_PSSM2_SCORES 165
127 #define ASN_PSSM2_KARLIN_K 166
128 #define ASN_PSSM2_FREQS 167
129
130 #define ASN_IS_STR 26
131 #define ASN_IS_SSTR 65
132 #define ASN_IS_INT 2
133 #define ASN_IS_BOOL 1
134 #define ASN_IS_OCTSTR 4
135 #define ASN_IS_OCTSSTR 65
136 #define ASN_IS_REAL 9
137 #define ASN_IS_ENUM 10
138 #define ASN_IS_ENUM0 1
139
140 #define ASN_OBJ_INT 160
141 #define ASN_OBJ_STR 161
142
143 struct asn_bstruct {
144 FILE *fd;
145 unsigned char *buf;
146 unsigned char *abp;
147 unsigned char *buf_max;
148 int len;
149 };
150
151 #define ASN_BUF 4096
152
153 void *
154 new_asn_bstruct(int buf_siz) {
155
156 struct asn_bstruct *asnp;
157
158 if ((asnp=calloc(1,sizeof(struct asn_bstruct)))==NULL) {
159 fprintf(stderr, "cannot allocate asn_bstruct\n");
160 exit(1);
161 }
162
163 if ((asnp->buf = (unsigned char *)calloc(buf_siz, sizeof(char))) == NULL ) {
164 fprintf(stderr, " cannot allocate asn_buf (%d)\n",buf_siz);
165 exit(1);
166 }
167
168 return asnp;
169 }
170
171 void
172 free_asn_bstruct(struct asn_bstruct *asnp) {
173
174 if (asnp == NULL) return;
175 if (asnp->buf != NULL) free(asnp->buf);
176 free(asnp);
177 }
178
179 unsigned char *
180 chk_asn_buf(struct asn_bstruct *asnp, int v) {
181 int new_buf;
182
183 if (v > ASN_BUF) {
184 fprintf(stderr," attempt to read %d bytes ASN.1 data > buffer size (%d)\n",
185 v, ASN_BUF);
186 exit(1);
187 }
188
189 if (asnp->abp + v > asnp->buf_max) {
190
191 /* move down the left over stuff */
192 asnp->len = asnp->buf_max - asnp->abp;
193
194 memmove(asnp->buf, asnp->abp, asnp->len);
195
196 asnp->abp = asnp->buf;
197 new_buf = ASN_BUF - asnp->len;
198
199 if (asnp->fd && !feof(asnp->fd) &&
200 (new_buf=fread(asnp->buf + asnp->len, sizeof(char), new_buf, asnp->fd)) != 0) {
201 asnp->len += new_buf;
202 }
203
204 asnp->buf_max = asnp->buf + asnp->len;
205
206 if (asnp->len < v) {
207 fprintf(stderr, " Unable to read %d bytes\n",v);
208 exit(1);
209 }
210 }
211 /* otherwise, v bytes are currently in the buffer */
212
213 return asnp->abp;
214 }
215
216 unsigned char *
217 asn_error(char *func, char *token, int tval,
218 struct asn_bstruct *asnp, int len) {
219 int i;
220
221 fprintf(stderr," %s %s [%0x]:",func, token, tval);
222 for (i=0; i<len; i++) {
223 fprintf(stderr," %0x",asnp->abp[i]);
224 }
225 fprintf(stderr,"\n");
226 return asnp->abp;
227 }
228
229 /*
230 read_asn_dest reads v bytes into oct_str if v <= o_len - otherwise
231 fails - the correct size buffer must be pre-allocated read_asn_dest
232 is required for ASN data entities that are longer than ASN_BUF
233 (1024)
234
235 skip over if oct_str==NULL;
236 */
237 unsigned char *
238 read_asn_dest(struct asn_bstruct *asnp, int v, unsigned char *oct_str, int o_len) {
239 int new_buf;
240 unsigned char *oct_ptr;
241
242
243 if (oct_str != NULL && v > o_len) {
244 fprintf(stderr, " read_asn_dest - cannot read %d bytes into %d buffer\n",
245 v, o_len);
246 exit(1);
247 }
248
249 if (asnp->abp + v <= asnp->buf_max) {
250 if (oct_str != NULL) memmove(oct_str, asnp->abp, v);
251 return asnp->abp+v;
252 }
253 else {
254 /* move down the left over stuff */
255
256 asnp->len = asnp->buf_max - asnp->abp;
257
258 if (oct_str != NULL) memmove(oct_str, asnp->abp, asnp->len);
259 oct_ptr = oct_str+asnp->len;
260 v -= asnp->len;
261
262 asnp->abp = asnp->buf;
263 new_buf = ASN_BUF;
264
265 while ((new_buf=fread(asnp->buf, sizeof(char), new_buf, asnp->fd)) != 0) {
266 asnp->len = new_buf;
267 asnp->buf_max = asnp->buf + asnp->len;
268 if (v <= new_buf) { /* we have it all this time */
269 if (oct_str != NULL) memmove(oct_ptr, asnp->buf, v);
270 asnp->len -= v;
271 asnp->abp = asnp->buf + v;
272 break;
273 }
274 else { /* we need to read some more */
275 if (oct_str != NULL) memmove(oct_ptr, asnp->buf, new_buf);
276 v -= new_buf;
277 new_buf = ASN_BUF;
278 }
279 }
280 }
281 return asnp->buf + v;
282 }
283
284 unsigned char *
285 get_astr_bool(struct asn_bstruct *asnp, int *val) {
286
287 int v_len, v;
288
289 asnp->abp = chk_asn_buf(asnp,16);
290
291 v = 0;
292 if (*asnp->abp++ != 1) { /* check for int */
293 fprintf(stderr," bool missing\n");
294 }
295 else {
296 v_len = *asnp->abp++;
297 if (v_len != 1) {
298 fprintf(stderr, "boolean length != 1 : %d\n", v_len);
299 v = *asnp->abp++;
300 }
301 else { v = *asnp->abp++;}
302 }
303 *val = v;
304 return asnp->abp;
305 }
306
307 unsigned char *
308 get_astr_int(struct asn_bstruct *asnp, long *val) {
309
310 int i_len, v_len, v;
311
312 v = 0;
313
314 asnp->abp = chk_asn_buf(asnp,32);
315
316 if (*asnp->abp++ != ASN_IS_INT) { /* check for int */
317 return asn_error("get_astr_int", "ASN_IS_INT", ASN_IS_INT, asnp, 4);
318 }
319 else {
320 i_len = v_len = *asnp->abp++;
321 while (i_len-- > 0) {
322 v *= 256;
323 v += *asnp->abp++;
324 }
325 }
326
327 if (v_len == 1 && v > 127) { v = v - 256; }
328 else if (v_len == 2 && v > 32767) {v = v - 65536;}
329
330 *val = v;
331 return asnp->abp;
332 }
333
334 unsigned char *
335 get_astr_real(struct asn_bstruct *asnp,
336 double *val) {
337
338 int v_len, v;
339 asnp->abp = chk_asn_buf(asnp,32);
340
341 if (ABP != ASN_IS_REAL) {
342 fprintf(stderr," real missing\n");
343 return asnp->abp;
344 }
345 else {
346 v_len = asnp->abp[1];
347 ABP_INC2;
348 }
349
350 *val = 0.0;
351 if (ABP != '\0') {
352 fprintf(stderr," float missing\n");
353 return asnp->abp;
354 }
355 else {
356 sscanf((char *)asnp->abp+1,"%lg",val);
357 asnp->abp += v_len;
358 }
359 return asnp->abp;
360 }
361
362 unsigned char *
363 get_astr_enum(struct asn_bstruct *asnp, int *val) {
364
365 int v_len, v;
366
367 asnp->abp = chk_asn_buf(asnp,16);
368
369 v = 0;
370 if (*asnp->abp++ != ASN_IS_ENUM) { /* check for int */
371 fprintf(stderr," enum missing\n");
372 }
373 else {
374 v_len = *asnp->abp++;
375 while (v_len-- > 0) { v *= 256; v += *asnp->abp++; }
376 }
377 *val = v;
378
379 return asnp->abp;
380 }
381
382 unsigned char *
383 get_astr_packedreal(struct asn_bstruct *asnp, long *l_val_p, double *d_val_p) {
384
385 int v_len;
386 char tmp_str[64];
387
388 asnp->abp = chk_asn_buf(asnp,32);
389
390 if (*asnp->abp++ != ASN_IS_REAL) { /* check for packed float */
391 fprintf(stderr,"*** ERROR [%s:%d] - float missing\n",__FILE__,__LINE__);
392 *d_val_p = 0;
393 return asnp->abp;
394 }
395 else {
396 v_len = *asnp->abp++;
397
398 if (v_len > 63) {
399 fprintf(stderr,"*** ERROR [%s:%d] - real string too long: %d\n",__FILE__,__LINE__,v_len);
400 }
401
402 asnp->abp = chk_asn_buf(asnp,v_len+16);
403
404 if (v_len == 2 && *asnp->abp == '\0' && *(asnp->abp+1)=='0') {
405 ABP_INC2;
406 *d_val_p = 0.0;
407 }
408 else { /* copy and scan it */
409 if (*asnp->abp != '\0') {
410 fprintf(stderr, "*** ERROR [%s:%d] - packedreal - expected 0, got %d\n", __FILE__,__LINE__,*asnp->abp);
411 *d_val_p = -1.0;
412 return asnp->abp;
413 }
414 asnp->abp++;
415 strncpy(tmp_str, (char *)asnp->abp, v_len);
416 tmp_str[v_len-1] = '\0';
417 tmp_str[63] = '\0';
418 sscanf(tmp_str,"%lg", d_val_p);
419 asnp->abp += v_len-1;
420 }
421 }
422 return asnp->abp;
423 }
424
425 unsigned char *
426 get_astr_packedint(struct asn_bstruct *asnp, long *l_val_p, double *d_val_p) {
427
428 asnp->abp = chk_asn_buf(asnp,32);
429 ABPP = get_astr_int(asnp, l_val_p);
430 return asnp->abp;
431 }
432
433 unsigned char *
434 get_astr_str(struct asn_bstruct *asnp, char *text, int t_len) {
435
436 int v_len, tv_len;
437
438 asnp->abp = chk_asn_buf(asnp,32);
439
440 if (text != NULL) text[0] = '\0';
441
442 if (ABP != ASN_IS_STR && ABP != ASN_IS_SSTR) { /* check for str */
443 return asn_error("get_astr_str", "ASN_IS_STR", ASN_IS_STR, asnp, 4);
444 }
445 asnp->abp++;
446
447 v_len = *asnp->abp++;
448 if (v_len > 128) { /* need to read the length from the next bytes */
449 tv_len = v_len &0x7f;
450
451 asnp->abp = chk_asn_buf(asnp,tv_len+32);
452
453 for (v_len =0; tv_len; tv_len--) { v_len = (v_len << 8) + *asnp->abp++; }
454 }
455
456 /* read v_len bytes */
457
458 if (v_len < t_len) { /* the string fits in the buffer */
459 asnp->abp = read_asn_dest(asnp,v_len, (unsigned char *)text, t_len);
460 }
461 else { /* it does not fit, fill the buffer and skip */
462 if (t_len > 0)
463 asnp->abp = read_asn_dest(asnp,t_len, (unsigned char *)text, t_len);
464 asnp->abp = read_asn_dest(asnp,v_len - t_len, NULL, 0);
465 }
466 if (text != NULL && t_len > 0) {text[min(v_len,t_len)]='\0';}
467 return asnp->abp;
468 }
469
470 unsigned char *
471 get_astr_octstr(struct asn_bstruct *asnp,
472 unsigned char *oct_str,
473 int o_len) {
474
475 int q_len, v_len;
476
477 asnp->abp = chk_asn_buf(asnp,32);
478
479 if (ABP == ASN_IS_OCTSTR || ABP == ASN_IS_OCTSSTR) {
480 ABPP++;
481 /* get length of length */
482 if (ABP > 128) {
483 v_len = *asnp->abp++ & 0x7f;
484
485 asnp->abp = chk_asn_buf(asnp,v_len+32);
486
487 q_len = 0;
488 while (v_len-- > 0) {
489 q_len *= 256;
490 q_len += *asnp->abp++;
491 }
492 }
493 else {
494 q_len = *asnp->abp++ & 0x7f;
495 }
496
497 if (q_len < o_len) { /* the string fits in the buffer */
498 asnp->abp = read_asn_dest(asnp,q_len, oct_str, o_len);
499 }
500 else { /* it does not fit, fill the buffer and skip */
501 asnp->abp = read_asn_dest(asnp,o_len, oct_str, o_len);
502 asnp->abp = read_asn_dest(asnp,q_len - o_len, NULL, 0);
503 }
504 if (oct_str != NULL && o_len > 0) oct_str[min(q_len,o_len)]='\0';
505
506 /* asnp->abp += 2; */ /* skip characters and NULL's */
507 }
508 return asnp->abp;
509 }
510
511 /* something to try to skip over stuff we don't want */
512 unsigned char *
513 get_astr_junk(struct asn_bstruct *asnp) {
514
515 int seq_cnt = 0;
516 long tmp;
517 char string[256];
518
519 while (ABP) {
520 if ( ABP == ASN_SEQ) { ABP_INC2; seq_cnt++;}
521 else if ( ABP == ASN_IS_BOOL ) {
522 ABP_INC2;
523 ABPP = get_astr_int(asnp, &tmp) + 2;
524 }
525 else if ( ABP == ASN_IS_INT ) {
526 ABP_INC2;
527 ABPP = get_astr_int(asnp, &tmp) + 2;
528 }
529 else if ( ABP == ASN_IS_STR ) {
530 ABP_INC2;
531 ABPP = get_astr_str(asnp, string, sizeof(string)-1) + 2;
532 }
533 }
534
535 while (seq_cnt-- > 0) ABP_INC2;
536 return asnp->abp;
537 }
538
539 #define ASN_SEQINST_NCBIEAA 167
540 #define ASN_SEQINST_NCBISTDAA 169
541 #define ASN_SEQINST_IUPACAA 161
542
543 unsigned char *
544 get_astr_iseqd(struct asn_bstruct *asnp,
545 unsigned char *query,
546 int nq) {
547
548 asnp->abp = chk_asn_buf(asnp,32);
549
550 /* check for the sequence type - NCBIstdaa or NCBIstdeaa */
551
552 if (ABP == ASN_SEQINST_NCBIEAA) {
553 ABP_INC2;
554 return get_astr_str(asnp, (char *)query, nq) + 2;
555 }
556 else if (ABP == ASN_SEQINST_NCBISTDAA) {
557 ABP_INC2;
558 return get_astr_octstr(asnp, query, nq) + 2;
559 }
560 else if (ABP == ASN_SEQINST_IUPACAA) {
561 ABP_INC2;
562 return get_astr_str(asnp, (char *)query, nq) + 2;
563 }
564 else {
565 return asn_error("get_astr_iseqd","",-1,asnp,4);
566 }
567 }
568
569 unsigned char *
570 get_astr_objid(struct asn_bstruct *asnp, int *type, int *val, char *text, int t_len) {
571
572 long local_ival;
573
574 asnp->abp = chk_asn_buf(asnp,32);
575
576 if (text != NULL) text[0] = '\0';
577 if (val != NULL) *val = 0;
578 *type = 0;
579
580 /* object could be text, or could be int */
581
582 if (ABP == ASN_OBJ_INT) {
583 ABP_INC2;
584 asnp->abp = get_astr_int(asnp, &local_ival)+2;
585 if (val != NULL) *val = local_ival;
586 *type = 1;
587 }
588 else if (ABP == ASN_OBJ_STR) {
589 ABP_INC2;
590 asnp->abp = get_astr_str(asnp, text, t_len)+2;
591 *type = 2;
592 }
593 else {
594 return asn_error("get_astr_objid","ASN_OBJ_STR",ASN_OBJ_STR,asnp,4);
595 }
596 return asnp->abp;
597 }
598
599 #define ASN_BIOSEQ_SEQ 160
600 #define ASN_BIOSEQ_ID_VAL 160
601 #define ASN_BIOSEQ_ID_OBJ 160
602 #define ASN_BIOSEQ_ID_LOCAL 161
603 #define ASN_BIOSEQ_ID_GIBBSQ 162
604 #define ASN_BIOSEQ_ID_GIBBMT 163
605 #define ASN_BIOSEQ_ID_GB 164
606 #define ASN_BIOSEQ_ID_EMBL 165
607 #define ASN_BIOSEQ_ID_PIR 166
608 #define ASN_BIOSEQ_ID_SP 167
609 #define ASN_BIOSEQ_ID_PATENT 168
610 #define ASN_BIOSEQ_ID_OTHER 169
611 #define ASN_BIOSEQ_ID_GEN 170
612 #define ASN_BIOSEQ_ID_GI 171
613 #define ASN_BIOSEQ_ID_DDBJ 172
614 #define ASN_BIOSEQ_ID_PDB 173
615 #define ASN_BIOSEQ_ID_TPG 174
616 #define ASN_BIOSEQ_ID_TPE 175
617 #define ASN_BIOSEQ_ID_TPD 176
618
619 #define ASN_BIOSEQ_TEXTID_NAME 160
620 #define ASN_BIOSEQ_TEXTID_ACC 161
621 #define ASN_BIOSEQ_TEXTID_REL 162
622 #define ASN_BIOSEQ_TEXTID_VER 163
623
624 #define ASN_BIOSEQ_ID 160
625 #define ASN_BIOSEQ_DESCR 161
626 #define ASN_BIOSEQ_INST 162
627 #define ASN_BIOSEQ_ANNOT 163
628
629 #define ASN_BIOSEQ_D_NAME 163
630 #define ASN_BIOSEQ_D_TITLE 164
631 #define ASN_BIOSEQ_D_PIR 169
632 #define ASN_BIOSEQ_D_GB 170
633 #define ASN_BIOSEQ_D_USER 173
634 #define ASN_BIOSEQ_D_SP 174
635
636 #define ASN_BIOSEQ_INST_REPR 160
637 #define ASN_BIOSEQ_INST_MOL 161
638 #define ASN_BIOSEQ_INST_LEN 162
639 #define ASN_BIOSEQ_INST_SEQD 166
640 #define ASN_BIOSEQ_INST_HIST 168
641
642 #define ASN_USERFLD_D_STR 160
643 #define ASN_USERFLD_D_INT 161
644 #define ASN_USERFLD_D_REAL 162
645 #define ASN_USERFLD_D_BOOL 163
646 #define ASN_USERFLD_D_OS 164
647 #define ASN_USERFLD_D_USER 165
648 #define ASN_USERFLD_D_STRS 166
649 #define ASN_USERFLD_D_INTS 167
650 #define ASN_USERFLD_D_REALS 168
651 #define ASN_USERFLD_D_OSS 169
652 #define ASN_USERFLD_D_FIELDS 170
653 #define ASN_USERFLD_D_OBJS 171
654
655 unsigned char *
656 get_astr_userfld_data(struct asn_bstruct *asnp) {
657 double real;
658 long ival;
659 int bool;
660
661 ABPP = chk_asn_buf(asnp, 32);
662
663 switch (ABP) {
664 case ASN_USERFLD_D_STR :
665 ABP_INC2;
666 ABPP = get_astr_str(asnp, NULL, 0) + 2;
667 break;
668 case ASN_USERFLD_D_INT :
669 ABP_INC2;
670 ABPP = get_astr_int(asnp, &ival) + 2;
671 break;
672 case ASN_USERFLD_D_REAL :
673 ABP_INC2;
674 ABPP = get_astr_real(asnp, &real) + 2;
675 break;
676 case ASN_USERFLD_D_BOOL :
677 ABP_INC2;
678 ABPP = get_astr_bool(asnp, &bool) + 2;
679 break;
680 case ASN_USERFLD_D_OS :
681 ABP_INC2;
682 ABPP = get_astr_octstr(asnp, NULL, 0)+2;
683 break;
684 case ASN_USERFLD_D_OSS :
685 asnp->abp += 4;
686 ABPP = get_astr_octstr(asnp, NULL, 0)+4;
687 break;
688 default:
689 return asn_error("get_astr_userfld_data","",0,asnp,4);
690 }
691 return asnp->abp;
692 }
693
694 #define ASN_USERFLD_LABEL 160
695 #define ASN_USERFLD_NUM 161
696 #define ASN_USERFLD_DATA 162
697
698 unsigned char *
699 get_astr_userfld(struct asn_bstruct *asnp) {
700
701 char *func = "get_astr_userfld";
702 long num;
703 int type, in_seq=0;
704
705 asnp->abp = chk_asn_buf(asnp, 32);
706
707 if (ABP == ASN_SEQ) { in_seq = 1; ABP_INC2;}
708
709 if (ABP != ASN_USERFLD_LABEL) {
710 return asn_error(func, "ASN_USERFLD_LABEL", ASN_USERFLD_LABEL, asnp, 4);
711 }
712 else {
713 ABP_INC2;
714 asnp->abp = get_astr_objid(asnp, &type, NULL, NULL, 0)+2;
715 }
716
717 if (ABP == ASN_USERFLD_NUM) {
718 asnp->abp +=2;
719 asnp->abp = get_astr_int(asnp, &num)+2;
720 }
721
722 if (ABP != ASN_USERFLD_DATA) {
723 return asn_error(func, "ASN_USERFLD_DATA", ASN_USERFLD_DATA, asnp, 4);
724 }
725 else {
726 ABP_INC2;
727 asnp->abp = get_astr_userfld_data(asnp)+2;
728 }
729
730 asnp->abp = chk_asn_buf(asnp,8);
731 if (in_seq) ABP_INC2;
732 return asnp->abp;
733 }
734
735 #define ASN_USER_CLASS 160
736 #define ASN_USER_TYPE 161
737 #define ASN_USER_DATA 162
738
739 unsigned char *
740 get_astr_user(struct asn_bstruct *asnp) {
741 int type;
742
743 char *func = "get_astr_user";
744 asnp->abp = chk_asn_buf(asnp,32);
745
746 ABP_INC2; /* skip SEQ */
747 if (ABP == ASN_USER_CLASS) {
748 ABP_INC2;
749 asnp->abp = get_astr_str(asnp, NULL, 0) + 2;
750 }
751 if (ABP != ASN_USER_TYPE) {
752 return asn_error(func, "ASN_USER_TYPE", ASN_USER_TYPE, asnp, 4);
753 }
754 else {
755 ABP_INC2;
756 asnp->abp = get_astr_objid(asnp, &type, NULL, NULL, 0) + 2;
757 }
758
759 if (ABP != ASN_USER_DATA) {
760 return asn_error(func,"ASN_USER_DATA", ASN_USER_DATA, asnp, 4);
761 }
762 else {
763 asnp->abp += 4; /* skip over, data, SEQ */
764 asnp->abp = chk_asn_buf(asnp,32);
765 asnp->abp = get_astr_userfld(asnp);
766 asnp->abp += 4;
767 }
768 return asnp->abp;
769 }
770
771 unsigned char *
772 get_astr_seqdescr(struct asn_bstruct *asnp,
773 char *descr) {
774
775 int end_seq=0;
776
777 /* get seqof '1' */
778 /* get 164/128 - title */
779 /* get string */
780 /* pop nulls */
781
782 asnp->abp = chk_asn_buf(asnp,16);
783
784 if (ABP == ASN_SEQOF) {
785 end_seq++;
786 ABP_INC2;
787 }
788 else {
789 fprintf(stderr, "*** ERROR [%s:%d] - missing ASN_SEQOF '1': %0x %0x\n",__FILE__, __LINE__,ABP, asnp->abp[1]);
790 }
791
792 while (ABP != '\0') {
793
794 if (ABP == ASN_BIOSEQ_D_TITLE) {
795 ABP_INC2; /* skip token */
796 asnp->abp = get_astr_str(asnp, descr, MAX_STR) + 2;
797 }
798 else if (ABP == ASN_BIOSEQ_D_USER) {
799 ABP_INC2;
800 asnp->abp = get_astr_user(asnp);
801 }
802 else {
803 fprintf(stderr, "*** ERROR [%s:%d] - Un-parsed Seq-descr: %x %x\n",__FILE__,__LINE__,asnp->abp[0],asnp->abp[1]);
804 return asnp->abp;
805 }
806 }
807
808 asnp->abp = chk_asn_buf(asnp,8);
809
810 if (end_seq) ABP_INC2;
811
812 return asnp->abp;
813 }
814
815 unsigned char *
816 get_astr_seqinst(struct asn_bstruct *asnp,
817 unsigned char **query,
818 int *nq) {
819
820 int end_seq=0, tmp;
821 long l_val;
822
823 /* get sequence '0' */
824 /* get 160/128/10/len/val - repr enum raw val */
825 /* get 161/128/10/len/val - mol enum aa val */
826 /* get 162/128/02/len/val - length int val */
827 /* get 166/128 - topology (empty) */
828 /* get 167/128 - seq-data */
829 /* get 65/len+128/len/octet_string */
830 /* pop nulls */
831
832 asnp->abp = chk_asn_buf(asnp,32);
833
834 if (ABP == ASN_SEQ) {
835 end_seq++;
836 ABP_INC2;
837 }
838 else {
839 fprintf(stderr, "*** ERROR [%s:%d] - missing ASN_SEQ '0': %0x %0x\n",__FILE__, __LINE__, ABP, asnp->abp[1]);
840 }
841
842 if (ABP == ASN_BIOSEQ_INST_REPR && *(asnp->abp+1) == 128) {
843 ABP_INC2;
844 asnp->abp = get_astr_enum(asnp, &tmp)+2;
845 }
846 else {
847 fprintf(stderr, "*** ERROR [%s:%d] - missing ASN_BIOSEQ_INST_REPR 160: %0x %0x\n",__FILE__,__LINE__,ABP, asnp->abp[1]);
848 }
849
850 if (ABP == ASN_BIOSEQ_INST_MOL && *(asnp->abp+1) == 128) {
851 ABP_INC2;
852 asnp->abp = get_astr_enum(asnp, &tmp)+2;
853 }
854 else {
855 fprintf(stderr, "*** ERROR [%s:%d] - missing ASN_BIOSEQ_INST_MOL 161: %0x %0x\n",__FILE__,__LINE__,ABP, asnp->abp[1]);
856 }
857
858 if (ABP == ASN_BIOSEQ_INST_LEN) {
859 ABP_INC2;
860 asnp->abp = get_astr_int(asnp, &l_val)+2;
861 *nq = l_val;
862 }
863 else {
864 fprintf(stderr, "*** ERROR [%s:%d] - missing ASN_BIOSEQ_INST_LEN 161: %0x %0x\n",__FILE__, __LINE__, ABP, asnp->abp[1]);
865 return asnp->abp;
866 }
867
868 if ((*query = (unsigned char *)calloc(*nq + 1, sizeof(char)))==NULL) {
869 fprintf(stderr, " cannot allocate %d char query\n", *nq+1);
870 }
871
872 if (ABP == ASN_BIOSEQ_INST_SEQD) {
873 ABP_INC2;
874 asnp->abp = get_astr_iseqd(asnp, *query, *nq+1 ) + 2;
875 }
876 else {
877 fprintf(stderr, "*** ERROR [%s:%d] - missing ASN_BIOSEQ_INST_SEQD 166: %0x %0x\n",__FILE__, __LINE__, ABP, asnp->abp[1]);
878 free(*query);
879 *query = NULL;
880 return asnp->abp;
881 }
882
883 if (ABP == ASN_BIOSEQ_INST_HIST ) {
884 fprintf(stderr, "*** ERROR [%s:%d] - Cannot parse bioseq inst history\n",__FILE__,__LINE__);
885 exit(1);
886 }
887
888 if (end_seq) ABP_INC2;
889
890 return asnp->abp;
891 }
892
893
894 unsigned char *
895 get_astr_textid( struct asn_bstruct *asnp,
896 char *name,
897 char *acc) {
898 int end_seq = 0;
899 long ver;
900 char this_func[]="get_astr_textid";
901
902 chk_asn_buf(asnp,32);
903
904 if (ABP != ASN_SEQ) {
905 fprintf(stderr, "*** ERROR [%s:%d] - %s - Expected ASN_SEQ: %0x %0x\n",__FILE__,__LINE__,this_func,ABP, asnp->abp[1]);
906 }
907 else {ABP_INC2; end_seq++;}
908
909 name[0] = acc[0] = '\0';
910
911 if (ABP == ASN_BIOSEQ_TEXTID_NAME) {
912 ABP_INC2;
913 asnp->abp = get_astr_str(asnp, name, MAX_SSTR) + 2;
914 }
915
916 if (ABP == ASN_BIOSEQ_TEXTID_ACC) {
917 ABP_INC2;
918 asnp->abp = get_astr_str(asnp, acc, MAX_SSTR) + 2;
919 }
920
921 if (ABP == ASN_BIOSEQ_TEXTID_REL) {
922 ABP_INC2;
923 asnp->abp = get_astr_str(asnp, NULL, 0) + 2;
924 }
925
926 if (ABP == ASN_BIOSEQ_TEXTID_VER) {
927 ABP_INC2;
928 asnp->abp = get_astr_int(asnp, &ver)+2;
929 }
930
931 if (end_seq) ABP_INC2;
932 return asnp->abp;
933 }
934
935 unsigned char *
936 get_astr_seqid (struct asn_bstruct *asnp,
937 long *gi,
938 char *name,
939 char *acc) {
940 int type;
941 int val;
942
943 *gi = 0;
944 acc[0] = '\0';
945 while (ABP != '\0') {
946
947 switch (ABP) {
948 case ASN_BIOSEQ_ID_OBJ:
949 ABP_INC2;
950 asnp->abp = get_astr_objid(asnp, &type, &val, name, MAX_SSTR) + 2;
951 break;
952 case ASN_BIOSEQ_ID_LOCAL:
953 ABP_INC2;
954 asnp->abp = get_astr_str(asnp, name, MAX_SSTR) + 2;
955 break;
956 case ASN_BIOSEQ_ID_GI:
957 ABP_INC2;
958 asnp->abp = get_astr_int(asnp, gi) + 2;
959 break;
960
961 case ASN_BIOSEQ_ID_GB:
962 case ASN_BIOSEQ_ID_EMBL:
963 case ASN_BIOSEQ_ID_PIR:
964 case ASN_BIOSEQ_ID_SP:
965 case ASN_BIOSEQ_ID_OTHER:
966 ABP_INC2;
967 asnp->abp = get_astr_textid(asnp, name, acc) + 2;
968 break;
969 default:
970 return asn_error("get_atr_seqid", "", -1, asnp,4);
971 }
972 }
973 return asnp->abp;
974 }
975
976 /*
977 Bioseq ::= SEQUENCE {
978 id SET OF Seq-id , -- equivalent identifiers
979 descr Seq-descr OPTIONAL , -- descriptors
980 inst Seq-inst, -- the sequence data
981 annot SET OF Seq-annot OPTIONAL }
982 */
983
984 /* modified 8-Nov-2009 to allow additional information after the inst */
985
986 unsigned char *
987 get_astr_bioseq(struct asn_bstruct *asnp,
988 long *gi,
989 char *name,
990 char *acc,
991 char *descr,
992 unsigned char **query,
993 int *nq
994 ) {
995
996 int end_seq = 0;
997
998 asnp->abp = chk_asn_buf(asnp,64);
999
1000 if (ABP == ASN_SEQ) {
1001 end_seq++;
1002 ABP_INC2;
1003 }
1004
1005 if (ABP != ASN_BIOSEQ_ID) {
1006 fprintf(stderr, "*** ERROR [%s:%d] - Bioseq - missing ID tag: %2x %2x\n",__FILE__,__LINE__,ABP, asnp->abp[1]);
1007 return asnp->abp;
1008 }
1009 else {
1010 /* skip over bioseq-id tag */
1011 ABP_INC2;
1012 if (ABP == ASN_SETOF) { /* jump over ASN_SETOF */
1013 ABP_INC2;
1014 asnp->abp = get_astr_seqid(asnp, gi, name, acc);
1015 ABP_INC2; /* close ASN_SETOF */
1016 }
1017 else {
1018 return asn_error("get_astr_bioseq","ASN_SEQOF", ASN_SEQOF, asnp, 4);
1019 }
1020 ABP_INC2; /* jump over seq-id tag end */
1021 }
1022
1023 if (ABP == ASN_BIOSEQ_DESCR) {
1024 ABP_INC2;
1025 asnp->abp = get_astr_seqdescr(asnp, descr);
1026 ABP_INC2; /* skip nulls */
1027 }
1028 else { descr[0] = '\0';}
1029
1030 while (ABP == '\0') { ABP_INC2;}
1031
1032 if (ABP != ASN_BIOSEQ_INST) {
1033 fprintf(stderr, "*** ERROR [%s:%d] - Bioseq - missing ID tag: %2x %2x\n",__FILE__,__LINE__,ABP, asnp->abp[1]);
1034 return asnp->abp;
1035 }
1036 else {
1037 ABP_INC2;
1038 asnp->abp = get_astr_seqinst(asnp, query, nq);
1039 ABP_INC2; /* skip nulls */
1040 }
1041
1042 if (end_seq--) {
1043 ABP_INC2;
1044 }
1045
1046 return asnp->abp;
1047 }
1048
1049 /*
1050 get_pssm_intermed_null() captures and throws away an array of data
1051 rather than have different functions for different datatypes, get_data_func()
1052 reads the data, saving it to *d_val, where it will be discarded
1053 */
1054 unsigned char *
1055 get_pssm_intermed_null(struct asn_bstruct *asnp,
1056 int n_rows,
1057 int n_cols,
1058 int by_row,
1059 unsigned char *(*get_data_func)(struct asn_bstruct *, long *, double *),
1060 long *l_val_p,
1061 double *d_val_p
1062 ) {
1063
1064 int i_rows, i_cols;
1065 int in_seq = 0;
1066
1067 asnp->abp = chk_asn_buf(asnp,32);
1068
1069 if (ABP == ASN_SEQ) {
1070 ABP_INC2;
1071 in_seq = 1;
1072 }
1073
1074 if (!by_row) {
1075 for (i_cols = 0; i_cols < n_cols; i_cols++) {
1076 for (i_rows = 0; i_rows < n_rows; i_rows++) {
1077 asnp->abp = (*get_data_func)(asnp, l_val_p, d_val_p);
1078 }
1079 }
1080 }
1081 else {
1082 for (i_rows = 0; i_rows < n_rows; i_rows++) {
1083 for (i_cols = 0; i_cols < n_cols; i_cols++) {
1084 asnp->abp = (*get_data_func)(asnp, l_val_p, d_val_p);
1085 }
1086 }
1087 }
1088
1089 asnp->abp = chk_asn_buf(asnp,32);
1090 if (in_seq) {asnp->abp +=2;} /* skip nulls */
1091 ABP_INC2;
1092 return asnp->abp;
1093 }
1094
1095 unsigned char *
1096 get_pssm_freqs(struct asn_bstruct *asnp,
1097 double **freqs,
1098 int n_rows,
1099 int n_cols,
1100 int by_row) {
1101
1102 int i_rows, i_cols;
1103 int in_seq = 0;
1104 long l_val;
1105 double f_val;
1106
1107 asnp->abp = chk_asn_buf(asnp,64);
1108
1109 if (ABP == ASN_SEQ) {
1110 ABP_INC2;
1111 in_seq = 1;
1112 }
1113
1114 if (!by_row) {
1115 for (i_cols = 0; i_cols < n_cols; i_cols++) {
1116 for (i_rows = 0; i_rows < n_rows; i_rows++) {
1117 asnp->abp = get_astr_packedreal(asnp, &l_val, &f_val);
1118 freqs[i_cols][i_rows] = f_val;
1119 }
1120 }
1121 }
1122 else {
1123 for (i_rows = 0; i_rows < n_rows; i_rows++) {
1124 for (i_cols = 0; i_cols < n_cols; i_cols++) {
1125 asnp->abp = get_astr_packedreal(asnp, &l_val, &f_val);
1126 freqs[i_rows][i_cols] = f_val;
1127 }
1128 }
1129 }
1130
1131 asnp->abp = chk_asn_buf(asnp,32);
1132 if (in_seq) {asnp->abp +=2;} /* skip nulls */
1133 ABP_INC2;
1134 return asnp->abp;
1135 }
1136
1137 unsigned char *
1138 get_pssm_intermed(struct asn_bstruct *asnp,
1139 double ***wfreqs,
1140 double ***freqs,
1141 int n_rows,
1142 int n_cols,
1143 int by_row) {
1144
1145 long long_data;
1146 double real_data;
1147 int i;
1148
1149 asnp->abp = chk_asn_buf(asnp,32);
1150
1151 if (ABP == ASN_SEQ) {
1152 ABP_INC2;
1153 if (ABP == ASN_PSSM_INTERMED_RES_FREQS) {
1154 ABP_INC2;
1155 asnp->abp = get_pssm_intermed_null(asnp, n_rows, n_cols, by_row,
1156 &get_astr_packedint, &long_data, &real_data);
1157 }
1158
1159 if (ABP == ASN_PSSM_INTERMED_WRES_FREQS) {
1160 if (((*wfreqs) = (double **)calloc(n_cols, sizeof(double *)))==NULL) {
1161 fprintf(stderr, "*** ERROR [%s:%d] - cannot allocate wfreq cols - %d\n", __FILE__, __LINE__, n_cols);
1162 exit(1);
1163 }
1164
1165 if (((*wfreqs)[0] = (double *) calloc(n_cols * n_rows, sizeof(double)))==NULL) {
1166 fprintf(stderr, "*** ERROR [%s:%d] - cannot allocate freq rows * cols - %d * %d\n", __FILE__, __LINE__, n_rows, n_cols);
1167 exit(1);
1168 }
1169
1170 for (i=1; i < n_cols; i++) {
1171 (*wfreqs)[i] = (*wfreqs)[i-1] + n_rows;
1172 }
1173
1174 ABP_INC2;
1175 asnp->abp = get_pssm_freqs(asnp, *wfreqs, n_rows, n_cols, by_row);
1176 }
1177
1178 if (ABP == ASN_PSSM_INTERMED_FREQ_RATIOS) {
1179 if ((*freqs = (double **) calloc(n_cols, sizeof(double *)))==NULL) {
1180 fprintf(stderr, "*** ERROR [%s:%d] - cannot allocate wfreq cols - %d\n", __FILE__, __LINE__, n_cols);
1181 exit(1);
1182 }
1183
1184 if (((*freqs)[0] = (double *) calloc(n_cols * n_rows, sizeof(double)))==NULL) {
1185 fprintf(stderr, "*** ERROR [%s:%d] - cannot allocate freq rows * cols - %d * %d\n", __FILE__, __LINE__, n_rows, n_cols);
1186 exit(1);
1187 }
1188
1189 for (i=1; i < n_cols; i++) {
1190 (*freqs)[i] = (*freqs)[i-1] + n_rows;
1191 }
1192
1193 ABP_INC2;
1194 asnp->abp = get_pssm_freqs(asnp, *freqs, n_rows, n_cols, by_row);
1195 }
1196
1197 if (ABP == ASN_PSSM_INTERMED_INFO_CONTENT) {
1198 ABP_INC2;
1199 asnp->abp = get_pssm_intermed_null(asnp, 1, n_cols, by_row,
1200 &get_astr_packedreal, &long_data, &real_data);
1201 }
1202
1203 if (ABP == ASN_PSSM_INTERMED_GAPL_COLWTS) {
1204 ABP_INC2;
1205 asnp->abp = get_pssm_intermed_null(asnp, 1, n_cols, by_row,
1206 &get_astr_packedreal, &long_data, &real_data);
1207 }
1208
1209 if (ABP == ASN_PSSM_INTERMED_SIGMA) {
1210 ABP_INC2;
1211 asnp->abp = get_pssm_intermed_null(asnp, 1, n_cols, by_row,
1212 &get_astr_packedreal, &long_data, &real_data);
1213 }
1214
1215 if (ABP == ASN_PSSM_INTERMED_INTVAL_SIZE) {
1216 ABP_INC2;
1217 asnp->abp = get_pssm_intermed_null(asnp, 1, n_cols, by_row,
1218 &get_astr_packedint, &long_data, &real_data);
1219 }
1220
1221 if (ABP == ASN_PSSM_INTERMED_NUM_MATCH_SEQ) {
1222 ABP_INC2;
1223 asnp->abp = get_pssm_intermed_null(asnp, 1, n_cols, by_row,
1224 &get_astr_packedint, &long_data, &real_data);
1225 }
1226
1227 asnp->abp +=2; /* skip nulls */
1228 }
1229 ABP_INC2;
1230 return asnp->abp;
1231 }
1232
1233
1234 #define ASN_PSSM_PARAMS 161
1235 #define ASN_PSSM_PARAMS_PSEUDOCNT 160
1236 #define ASN_PSSM_PARAMS_RPSPARAMS 161
1237 #define ASN_PSSM_RPSPARAMS_MATRIX 160
1238 #define ASN_PSSM_RPSPARAMS_GAPOPEN 161
1239 #define ASN_PSSM_RPSPARAMS_GAPEXT 162
1240
1241 unsigned char *
1242 get_pssm_rpsparams(struct asn_bstruct *asnp,
1243 char *matrix,
1244 int *gap_open_p,
1245 int *gap_ext_p) {
1246
1247 int end_seq=0;
1248 long l_val;
1249
1250 asnp->abp = chk_asn_buf(asnp,32);
1251
1252 if (ABP == ASN_SEQ) {
1253 ABP_INC2;
1254 end_seq++;
1255 }
1256
1257 asnp->abp = chk_asn_buf(asnp,32);
1258 if (ABP == ASN_PSSM_RPSPARAMS_MATRIX) {
1259 ABP_INC2;
1260 asnp->abp = get_astr_str(asnp, matrix, MAX_SSTR) + 2;
1261 }
1262 else {
1263 strncpy(matrix,"BLOSUM62", MAX_SSTR);
1264 }
1265
1266 asnp->abp = chk_asn_buf(asnp,16);
1267 if (ABP == ASN_PSSM_RPSPARAMS_GAPOPEN) {
1268 ABP_INC2;
1269 asnp->abp = get_astr_int(asnp, &l_val)+2;
1270 *gap_open_p = l_val;
1271 }
1272 else {*gap_open_p = -11;}
1273
1274 asnp->abp = chk_asn_buf(asnp,16);
1275 if (ABP == ASN_PSSM_RPSPARAMS_GAPEXT) {
1276 ABP_INC2;
1277 asnp->abp = get_astr_int(asnp, &l_val)+2;
1278 *gap_ext_p = l_val;
1279 }
1280 else {*gap_ext_p = -1;}
1281
1282 if (end_seq) { chk_asn_buf(asnp,(end_seq * 2)+16); }
1283 while (end_seq-- > 0) { ABP_INC2; }
1284 return asnp->abp;
1285 }
1286
1287 /* this routine skips over the final scores */
1288 unsigned char *
1289 get_pssm_final_scores(struct asn_bstruct *asnp, int ***iscores, int n_rows, int n_cols, int by_row) {
1290
1291 int i_rows, i_cols, i;
1292 int in_seq = 0;
1293 long l_val;
1294
1295 if (ABP == ASN_SEQ) { ABP_INC2; in_seq=1;}
1296
1297 if (((*iscores) = (int **) calloc(n_cols, sizeof(int *)))==NULL) {
1298 fprintf(stderr, "*** ERROR [%s:%d] - cannot allocate wfreq cols - %d\n", __FILE__, __LINE__, n_cols);
1299 exit(1);
1300 }
1301
1302 if (((*iscores)[0] = (int *) calloc(n_cols * n_rows, sizeof(int)))==NULL) {
1303 fprintf(stderr, "*** ERROR [%s:%d] - cannot allocate freq rows * cols - %d * %d\n", __FILE__, __LINE__, n_rows, n_cols);
1304 exit(1);
1305 }
1306
1307 for (i=1; i < n_cols; i++) {
1308 (*iscores)[i] = (*iscores)[i-1] + n_rows;
1309 }
1310
1311 if (!by_row) {
1312 for (i_cols = 0; i_cols < n_cols; i_cols++) {
1313 for (i_rows = 0; i_rows < n_rows; i_rows++) {
1314 asnp->abp = get_astr_int(asnp, &l_val);
1315 (*iscores)[i_cols][i_rows] = l_val;
1316 }
1317 }
1318 }
1319 else {
1320 for (i_rows = 0; i_rows < n_rows; i_rows++) {
1321 for (i_cols = 0; i_cols < n_cols; i_cols++) {
1322 asnp->abp = get_astr_int(asnp, &l_val);
1323 (*iscores)[i_cols][i_rows] = l_val;
1324 }
1325 }
1326 }
1327
1328 asnp->abp = chk_asn_buf(asnp,16);
1329 if (in_seq) {asnp->abp +=2;} /* skip nulls */
1330 ABP_INC2;
1331 return asnp->abp;
1332 }
1333
1334 unsigned char *
1335 get_pssm_params(struct asn_bstruct *asnp,
1336 int *pseudo_cnts,
1337 char *matrix,
1338 int *gap_open_p,
1339 int *gap_ext_p) {
1340
1341 int end_seq=0;
1342 long l_val;
1343
1344 asnp->abp = chk_asn_buf(asnp,16);
1345
1346 if (ABP == ASN_SEQ) {
1347 ABP_INC2;
1348 end_seq++;
1349 }
1350
1351 if (ABP == ASN_PSSM_PARAMS_PSEUDOCNT) {
1352 ABP_INC2;
1353 asnp->abp = get_astr_int(asnp, &l_val)+2;
1354 *pseudo_cnts = l_val;
1355 }
1356
1357 if (ABP == ASN_PSSM_PARAMS_RPSPARAMS) {
1358 ABP_INC2;
1359 asnp->abp = get_pssm_rpsparams(asnp, matrix, gap_open_p, gap_ext_p);
1360 ABP_INC2;
1361 }
1362 else {
1363 *gap_open_p = -11;
1364 *gap_ext_p = -1;
1365 strncpy(matrix,"BLOSUM62",MAX_SSTR);
1366 }
1367
1368 while (end_seq-- > 0) { ABP_INC2; }
1369 return asnp->abp;
1370 }
1371
1372 unsigned char *
1373 get_pssm2_scores(struct asn_bstruct *asnp,
1374 int *have_scores
1375 ) {
1376
1377 int end_seq=0;
1378
1379 if (have_scores != NULL) *have_scores = 0;
1380
1381 if (ABP == ASN_SEQ) {
1382 end_seq++;
1383 ABP_INC2;
1384 }
1385
1386 if (ABP == '\0') { /* no scores */
1387 if (end_seq) ABP_INC2;
1388 }
1389 else {
1390 if (have_scores != NULL) *have_scores = 1;
1391 }
1392 return asnp->abp;
1393 }
1394
1395 unsigned char *
1396 get_pssm2_intermed(struct asn_bstruct *asnp,
1397 double ***wfreqs,
1398 double ***freqs,
1399 int n_rows,
1400 int n_cols) {
1401
1402 int i;
1403 double **my_freqs, **my_wfreqs;
1404 int **my_iscores;
1405
1406 if ((my_freqs = (double **) calloc(n_cols, sizeof(double *)))==NULL) {
1407 fprintf(stderr, "*** ERROR [%s:%d] - cannot allocate freq cols - %d\n", __FILE__, __LINE__, n_cols);
1408 exit(1);
1409 }
1410
1411 if ((my_wfreqs = (double **) calloc(n_cols, sizeof(double *)))==NULL) {
1412 fprintf(stderr, "*** ERROR [%s:%d] - cannot allocate wfreq cols - %d\n", __FILE__, __LINE__, n_cols);
1413 exit(1);
1414 }
1415
1416 if ((my_freqs[0] = (double *) calloc(n_cols * n_rows, sizeof(double)))==NULL) {
1417 fprintf(stderr, "*** ERROR [%s:%d] - cannot allocate freq rows * cols - %d * %d\n", __FILE__, __LINE__, n_rows, n_cols);
1418 exit(1);
1419 }
1420
1421 if ((my_wfreqs[0] = (double *) calloc(n_cols * n_rows, sizeof(double)))==NULL) {
1422 fprintf(stderr, "*** ERROR [%s:%d] - cannot allocate freq rows * cols - %d * %d\n", __FILE__, __LINE__, n_rows, n_cols);
1423 exit(1);
1424 }
1425
1426 for (i=1; i < n_cols; i++) {
1427 my_freqs[i] = my_freqs[i-1] + n_rows;
1428 my_wfreqs[i] = my_wfreqs[i-1] + n_rows;
1429 }
1430
1431 *wfreqs = my_wfreqs;
1432 *freqs = my_freqs;
1433
1434 chk_asn_buf(asnp, 16);
1435
1436 return get_pssm_freqs(asnp, my_freqs, n_rows, n_cols, 0);
1437 }
1438
1439 int
1440 parse_pssm2_asn(struct asn_bstruct *asnp,
1441 long *gi,
1442 char *name,
1443 char *acc,
1444 char *descr,
1445 unsigned char **query,
1446 int *nq,
1447 int *n_rows,
1448 int *n_cols,
1449 double ***wfreqs,
1450 double ***freqs,
1451 int ***iscores,
1452 int *pseudo_cnts,
1453 char *matrix,
1454 double *lambda_p) {
1455
1456 int is_protein;
1457 int have_rows=0, have_cols=0;
1458 long l_val;
1459 int have_scores=0;
1460
1461 chk_asn_buf(asnp, 32);
1462
1463 /* first get the query */
1464
1465 if (memcmp(asnp->abp, "\241\2000\200",4) != 0) {
1466 asn_error("parse_pssm2_asn","ASN_PSSM2_QUERY",ASN_PSSM2_QUERY,asnp,4);
1467 return -1;
1468 }
1469 else {
1470 asnp->abp+=4;
1471 asnp->abp = get_astr_bioseq(asnp, gi, name, acc, descr, query, nq) + 4;
1472 }
1473
1474 /* finish up the nulls */
1475 /* perhaps we have parsed correctly and do not need this */
1476 /* while (ABP == '\0') { ABP_INC2;} */
1477
1478 if (memcmp(asnp->abp, "\242\2000\200",4) != 0) {
1479 asn_error("parse_pssm2_asn","ASN_PSSM2_MATRIX",ASN_PSSM2_MATRIX,asnp,4);
1480 return -1;
1481 }
1482 else {
1483 asnp->abp+=4;
1484
1485 if (ABP == ASN_PSSM_IS_PROT) {
1486 ABP_INC2;
1487 asnp->abp = get_astr_bool(asnp, &is_protein)+2;
1488 }
1489
1490 if (ABP == ASN_PSSM2_MATRIX_NAME) {
1491 ABP_INC2;
1492 asnp->abp = get_astr_str(asnp, matrix, MAX_SSTR) + 2;
1493 }
1494
1495 if (ABP == ASN_PSSM2_NCOLS) {
1496 ABP_INC2;
1497 asnp->abp = get_astr_int(asnp, &l_val)+2;
1498 *n_cols = l_val;
1499 have_cols = 1;
1500 }
1501
1502 if (ABP == ASN_PSSM2_NROWS) {
1503 ABP_INC2;
1504 asnp->abp = get_astr_int(asnp, &l_val)+2;
1505 *n_rows = l_val;
1506 have_rows = 1;
1507 }
1508
1509 if (ABP == ASN_PSSM2_SCORES) {
1510 /* right now, this is always empty */
1511 ABP_INC2;
1512 asnp->abp = get_pssm2_scores(asnp, &have_scores) + 2;
1513 if (have_scores) return 0;
1514 }
1515
1516 if (ABP == ASN_PSSM2_KARLIN_K) {
1517 ABP_INC2;
1518 asnp->abp = get_astr_packedreal(asnp, &l_val, lambda_p) + 2;
1519 }
1520
1521 if (ABP == ASN_PSSM2_FREQS) {
1522 asnp->abp += 4;
1523 asnp->abp = get_pssm2_intermed(asnp, wfreqs, freqs, *n_rows, *n_cols) + 4;
1524 }
1525 }
1526
1527 return 1;
1528 }
1529
1530 int
1531 parse_pssm_asn(FILE *afd,
1532 long *gi,
1533 char *name,
1534 char *acc,
1535 char *descr,
1536 unsigned char **query,
1537 int *nq,
1538 int *n_rows,
1539 int *n_cols,
1540 double ***wfreqs,
1541 double ***freqs,
1542 int ***iscores,
1543 int *pseudo_cnts,
1544 char *matrix,
1545 int *gap_open_p,
1546 int *gap_ext_p,
1547 double *lambda_p) {
1548
1549 int is_protein;
1550 int pssm_version;
1551 long l_val;
1552 int i;
1553 long itmp;
1554 int have_rows=0, have_cols=0, by_col=0;
1555 double **my_freqs=NULL, **my_wfreqs=NULL, dtmp;
1556 int **my_iscores=NULL;
1557 struct asn_bstruct *asnp;
1558
1559 *wfreqs = NULL;
1560 *freqs = NULL;
1561 *iscores = NULL;
1562
1563 asnp = new_asn_bstruct(ASN_BUF);
1564
1565 asnp->fd = afd;
1566 asnp->len = ASN_BUF;
1567 asnp->abp = asnp->buf_max = asnp->buf + ASN_BUF;
1568
1569 chk_asn_buf(asnp, 32);
1570
1571 if (memcmp(asnp->abp, "0\200\240\200",4) != 0) {
1572 fprintf(stderr, "*** ERROR [%s:%d] - improper PSSM header\n",__FILE__,__LINE__);
1573 return -1;
1574 }
1575 else {asnp->abp+=4;}
1576
1577 if (ABP == ASN_IS_INT) {
1578 asnp->abp = get_astr_int(asnp, &l_val)+2;
1579 pssm_version = l_val;
1580 if (pssm_version != 2) {
1581 fprintf(stderr, "*** ERROR [%s:%d] - PSSM2 version mismatch: %d\n",__FILE__,__LINE__,pssm_version);
1582 return -1;
1583 }
1584 *gap_open_p = *gap_ext_p = 0;
1585 return parse_pssm2_asn(asnp, gi, name, acc, descr,
1586 query, nq,
1587 n_rows, n_cols,
1588 wfreqs, freqs, iscores,
1589 pseudo_cnts, matrix,
1590 lambda_p);
1591 }
1592
1593 if (ABP == ASN_SEQ) { asnp->abp += 2; }
1594
1595 if (ABP == ASN_PSSM_IS_PROT ) {
1596 ABP_INC2;
1597 asnp->abp = get_astr_bool(asnp, &is_protein)+2;
1598 }
1599
1600 if (ABP == ASN_PSSM_NROWS ) {
1601 ABP_INC2;
1602 asnp->abp = get_astr_int(asnp, &l_val)+2;
1603 *n_rows = l_val;
1604
1605 if (*n_rows > 0) { have_rows = 1; }
1606 else {
1607 fprintf(stderr, "*** ERROR [%s:%d] - bad n_row count\n",__FILE__,__LINE__);
1608 exit(1);
1609 }
1610 }
1611
1612 if (ABP == ASN_PSSM_NCOLS ) {
1613 ABP_INC2;
1614 asnp->abp = get_astr_int(asnp, &l_val)+2;
1615 *n_cols = l_val;
1616 if (*n_cols > 0) {
1617 have_cols = 1;
1618 }
1619 else {
1620 fprintf(stderr, "*** ERROR [%s:%d] - bad n_row count\n",__FILE__,__LINE__);
1621 exit(1);
1622 }
1623 }
1624
1625 if (ABP == ASN_PSSM_BYCOL ) {
1626 ABP_INC2;
1627 asnp->abp = get_astr_bool(asnp, &by_col)+2;
1628 }
1629
1630 /* we have read everything up to the query
1631
1632 n_cols gives us the query length, which we can allocate;
1633 */
1634
1635 if (ABP == ASN_PSSM_QUERY ) {
1636 asnp->abp+=4; /* skip token and CHOICE */
1637 asnp->abp = get_astr_bioseq(asnp, gi, name, acc, descr, query, nq) + 4;
1638 *nq = *n_cols;
1639 }
1640
1641 /* finish up the nulls */
1642
1643
1644 while (ABP == '\0') { asnp->abp += 2;}
1645
1646 if (ABP == ASN_PSSM_INTERMED_DATA) {
1647
1648 if (!have_rows || !have_cols) {
1649 fprintf(stderr, "*** ERROR [%s:%d] - cannot allocate freq - missing rows/cols - %d/%d\n",
1650 __FILE__,__LINE__, have_rows, have_cols);
1651 return -1;
1652 }
1653
1654 ABP_INC2;
1655 asnp->abp = get_pssm_intermed(asnp, &my_wfreqs, &my_freqs, *n_rows, *n_cols, by_col);
1656 *wfreqs = my_wfreqs;
1657 *freqs = my_freqs;
1658 }
1659
1660 if (ABP == ASN_PSSM_FINAL_DATA) {
1661 ABP_INC2;
1662 if (ABP == ASN_SEQ) { asnp->abp += 2; }
1663 if (ABP == ASN_PSSM_FINAL_DATA_SCORES) {
1664 ABP_INC2;
1665
1666 asnp->abp = get_pssm_final_scores(asnp, iscores, *n_rows, *n_cols, by_col) + 2;
1667
1668 ABP_INC2;
1669 }
1670 if (ABP == ASN_PSSM_FINAL_DATA_LAMBDA) {
1671 ABP_INC2;
1672 ABPP = get_astr_real(asnp, &dtmp) + 2;
1673 }
1674 if (ABP == ASN_PSSM_FINAL_DATA_KAPPA) {
1675 ABP_INC2;
1676 ABPP = get_astr_real(asnp, &dtmp) + 2;
1677 }
1678 if (ABP == ASN_PSSM_FINAL_DATA_H) {
1679 ABP_INC2;
1680 ABPP = get_astr_real(asnp, &dtmp) + 2;
1681 }
1682 if (ABP == ASN_PSSM_FINAL_DATA_SCALEF) {
1683 ABP_INC2;
1684 ABPP = get_astr_int(asnp, &itmp) + 2;
1685 }
1686 if (ABP == ASN_PSSM_FINAL_DATA_ULAMBDA) {
1687 ABP_INC2;
1688 ABPP = get_astr_real(asnp, &dtmp) + 2;
1689 }
1690 if (ABP == ASN_PSSM_FINAL_DATA_UKAPPA) {
1691 ABP_INC2;
1692 ABPP = get_astr_real(asnp, &dtmp) + 2;
1693 }
1694 if (ABP == ASN_PSSM_FINAL_DATA_UH) {
1695 ABP_INC2;
1696 ABPP = get_astr_real(asnp, &dtmp) + 2;
1697 }
1698 asnp->abp += 8;
1699 }
1700
1701 if (ABP == ASN_PSSM_PARAMS ) {
1702 ABP_INC2;
1703 asnp->abp = get_pssm_params(asnp, pseudo_cnts, matrix, gap_open_p, gap_ext_p) + 2;
1704 }
1705 else {
1706 *gap_open_p = -11;
1707 *gap_ext_p = -1;
1708 strncpy(matrix,"BLOSUM62",MAX_SSTR);
1709 if (ABP == 0) {ABP_INC2;}
1710 }
1711
1712 free_asn_bstruct(asnp);
1713
1714 return 1;
1715 }
1716
1717 int
1718 parse_pssm_asn_fa( FILE *fd,
1719 int *n_rows_p, int *n_cols_p,
1720 unsigned char **query,
1721 double ***wfreq2d,
1722 double ***freq2d,
1723 int ***iscores2d,
1724 char *matrix,
1725 int *gap_open_p,
1726 int *gap_extend_p,
1727 double *lambda_p
1728 ) {
1729
1730 int qi, rj;
1731 long gi;
1732 double tmp_freqs[COMPO_LARGEST_ALPHABET];
1733 char name[MAX_SSTR], acc[MAX_SSTR], descr[MAX_STR];
1734 int nq;
1735 int pseudo_cnts;
1736 int ret_val;
1737
1738 /* parse the file */
1739
1740 ret_val = parse_pssm_asn(fd, &gi, name, acc, descr, query, &nq,
1741 n_rows_p, n_cols_p, wfreq2d, freq2d, iscores2d,
1742 &pseudo_cnts, matrix, gap_open_p, gap_extend_p,
1743 lambda_p);
1744
1745 if (ret_val <=0) return ret_val;
1746
1747 for (qi = 0; qi < *n_cols_p; qi++) {
1748 for (rj = 0; rj < *n_rows_p; rj++) { tmp_freqs[rj] = (*freq2d)[qi][rj];}
1749
1750 for (rj = 0; rj < COMPO_NUM_TRUE_AA; rj++) {
1751 (*freq2d)[qi][rj] = tmp_freqs[pssm_aa_order[rj]];
1752 }
1753 }
1754
1755 return 1;
1756 }