JPRED-2 Add alscript to the Git repository
[jpred.git] / sources / alscript / src / agetbloc.c
1 /****************************************************************************
2
3  AGETBLOC - a routine to read an AMPS block file
4
5    Copyright:  Geoffrey J. Barton (1992,1997)
6
7    email: geoff@ebi.ac.uk
8
9    Please see README for conditions of use.
10
11 ****************************************************************************
12
13 History:
14
15 15th November 1992 - ANSI C version - also uses GJ... routines.
16 This version adapted with error messages for alscript.
17
18 11th June 1992.
19 Agetbloc:  like getbloc, but does not require that every character read into
20 the seqs structure is an alphabetic character.  Also does not contain the 
21 option to convert the "sequences" read in into integer format
22
23 getbloc:  Read an AMPS style block file into the seqs array
24 the nbloc aligned sequences are stored in positions 1-nbloc.
25
26 This is a straight-ish translation of the fortran routine fbloc.f, hence
27 the non-C like goto's...
28
29 Sequence lengths are actual length +1 + 1.  This allows position 0 to 
30 be reserved for future use, and preserves the '\0' for output.
31
32 18/Feb/1993:Fix (i) to (i+1) in realloc.  Spotted by RBR.
33
34  $Id: agetbloc.c,v 1.2 1998/09/17 16:54:58 geoff Exp $
35  $Log: agetbloc.c,v $
36  Revision 1.2  1998/09/17 16:54:58  geoff
37  Check consistency with archive
38
39
40 */
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <ctype.h>
46 #include "array.h"
47 #include "gjutil.h"
48
49
50 int Agetbloc(FILE *bfile,struct seqdat *bloc,int *nbloc)
51
52 {
53     int i,llen;
54     extern int MAXnseq, MAXslen, MAXilen, MAXtlen, MAXnbloc;
55     char *buff;
56     extern FILE *std_in,*std_out,*std_err;
57
58     char *idstart, *idend, *bstart, sident = 0;
59     int idlen,totseq = 0,k,j;
60
61     buff = (char *) GJmalloc(sizeof(char) * MAXtlen);
62
63 l1: 
64     buff = fgets(buff,MAXtlen,bfile);
65     if(buff == NULL){
66         fprintf(std_err,"Premature end of BLOCK FILE\n");
67         return 0;
68     }
69     if((idstart = strchr(buff,'>')) != NULL){
70         if(++totseq == MAXnbloc){
71             fprintf(std_err,
72             "Max Number of block file sequences exceeded: %d\n",
73             totseq);
74             fprintf(std_err,"Use MAX_NSEQ command to increase value");
75             return 0;
76         }
77         sident = 1;
78         idend = strchr(idstart,' ');
79         if(idend == NULL){
80           idend = strchr(idstart,'\0');
81         }
82         if(idend == NULL){
83           fprintf(std_err,"Error reading identifier:%s\n",idstart);
84           error("Exiting",1);
85         }
86         idlen = (idend - idstart) + 1;
87         bloc[totseq].id = (char *) malloc(sizeof(char) * idlen);
88         bloc[totseq].id = GJstrblank(bloc[totseq].id,idlen);
89         strncpy(bloc[totseq].id,idstart+1,idlen-1);   /* don't copy the ">" symbol */
90         bloc[totseq].ilen = idlen-1;
91         bloc[totseq].id[idlen-1] = '\0';
92
93         bloc[totseq].tlen = strlen(idend)+1;
94         bloc[totseq].title = (char *) GJmalloc(sizeof(char) * bloc[totseq].tlen);
95         bloc[totseq].title = GJstrblank(bloc[totseq].title,bloc[totseq].tlen);
96         strcpy(bloc[totseq].title,idend);
97
98         bloc[totseq].seq = (char *) GJmalloc(sizeof(char) * MAXslen);
99         bloc[totseq].seq[0] = ' ';
100         goto l1;
101     } else if(sident){
102         if((idstart = strchr(buff,'*')) != NULL){
103             i = 0;
104             while((buff = fgets(buff,MAXtlen,bfile)) != NULL){
105                 if(*idstart == '*'){
106 /*                  fprintf(stdout,"Blocfile read: Length: %d\n",i);*/
107                     ++i;
108                     for(k=1;k<totseq+1;++k){
109                         bloc[k].slen = i;
110                         bloc[k].seq[i] = '\0';
111                         bloc[k].seq = (char *) realloc(bloc[k].seq,sizeof(char)*(i+1)); /*i+1 fix suggested by rbr*/
112                     }
113                     *nbloc = totseq;
114                     free(buff);
115                     return 1;
116                 }
117                 bstart = idstart;
118                 ++i;
119                 if(i==MAXslen)error("Max Sequence length exceeded - use MAX_SEQ_LEN command to increase",1);
120                 for(j=1;j<totseq+1;++j){
121                     /*cope with short lines */
122 /*                  if(!isalnum(*bstart)) *bstart = ' '; */
123                     bloc[j].seq[i] = *bstart++;
124                 }
125             }
126             fprintf(std_err,"No terminating * in blocfile\n");
127             return 0;
128         }else{
129           goto l1;
130         }
131     } else {
132         goto l1;
133     }
134 }
135