source: tags/arb_5.1/CONVERTALN/gcg.c

Last change on this file was 5390, checked in by westram, 17 years ago
  • TAB-Ex
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 KB
Line 
1#include <stdio.h>
2#include "convert.h"
3#include "global.h"
4
5/* ------------------------------------------------------------------
6 *   Function to_gcg().
7 *       Convert from whatever to GCG format.
8 */
9void
10to_gcg(intype, inf)
11     int    intype;
12     char *inf;
13{
14    FILE        *IFP1, *IFP2, *IFP3, *ofp;
15    FILE_BUFFER  ifp1 = 0, ifp2 = 0, ifp3 = 0;
16    char         temp[TOKENNUM], *eof, line[LINENUM], key[TOKENNUM];
17    char         line1[LINENUM], line2[LINENUM], line3[LINENUM], name[LINENUM];
18    char        *eof1, *eof2, *eof3;
19    char         outf[TOKENNUM];
20    int          seqdata;
21
22    if(intype==MACKE)   {
23        if((IFP1=fopen(inf, "r"))==NULL||(IFP2=fopen(inf, "r"))==NULL ||(IFP3=fopen(inf, "r"))==NULL)   {
24            sprintf(temp, "Cannot open input file %s\n", inf);
25            error(38, temp);
26        }
27        ifp1 = create_FILE_BUFFER(inf, IFP1);
28        ifp2 = create_FILE_BUFFER(inf, IFP2);
29        ifp3 = create_FILE_BUFFER(inf, IFP3);
30    }
31    else {
32        if((IFP1=fopen(inf, "r")) == NULL)  {
33            sprintf(temp, "CANNOT open input file %s, exit.\n", inf);
34            error(37, temp);
35        }
36        ifp1 = create_FILE_BUFFER(inf, IFP1);
37    }
38    if(intype==MACKE)   {
39        /* skip to #=; where seq. first appears */
40        for(eof1=Fgetline(line1, LINENUM, ifp1); eof1!=NULL&&(line1[0]!='#'||line1[1]!='=') ;
41            eof1=Fgetline(line1, LINENUM, ifp1)) ;
42        /* skip to #:; where the seq. information is */
43        for(eof2=Fgetline(line2, LINENUM, ifp2); eof2!=NULL&&(line2[0]!='#'||line2[1]!=':');
44            eof2=Fgetline(line2, LINENUM, ifp2)) ;
45        /* skip to where seq. data starts */
46        for(eof3=Fgetline(line3,LINENUM, ifp3); eof3!=NULL&&line3[0]=='#';
47            eof3=Fgetline(line3, LINENUM, ifp3)) ;
48        /* for each seq. print out one gcg file. */
49        for(; eof1!=NULL&&(line1[0]=='#'&&line1[1]=='=');
50            eof1=Fgetline(line1, LINENUM, ifp1))    {
51            macke_abbrev(line1, key, 2);
52            Cpystr(temp, key);
53            gcg_output_filename(temp, outf);
54            if((ofp=fopen(outf, "w"))==NULL) {
55                sprintf(temp, "CANNOT open file %s, exit.\n", outf);
56                error(39, temp);
57            }
58            for(macke_abbrev(line2, name, 2);
59                eof2!=NULL&&line2[0]=='#'&&line2[1]==':'&&Cmpstr(name, key)==EQ;
60                eof2=Fgetline(line2, LINENUM, ifp2), macke_abbrev(line2, name, 2))
61                gcg_doc_out(line2, ofp);
62            eof3 = macke_origin(key, line3, ifp3);
63            gcg_seq_out(ofp, key);
64            fclose(ofp);
65            init_seq_data();
66        }
67    } else  {
68        seqdata=0;  /* flag of doc or data */
69        eof = Fgetline(line, LINENUM, ifp1);
70        while(eof!=NULL)    {
71            if(intype==GENBANK) {
72                genbank_key_word(line, 0, temp, TOKENNUM);
73                if((Cmpstr(temp, "LOCUS"))==EQ) {
74                    genbank_key_word(line+12, 0, key, TOKENNUM);
75                    gcg_output_filename(key, outf);
76                    if((ofp=fopen(outf, "w"))==NULL) {
77                        sprintf(temp, "CANNOT open file %s, exit.\n", outf);
78                        error(40, temp);
79                    }
80                } else if(Cmpstr(temp, "ORIGIN")==EQ)   {
81                    gcg_doc_out(line, ofp);
82                    seqdata = 1;
83                    eof = genbank_origin(line, ifp1);
84                }
85            } else { /* EMBL or SwissProt */
86                if(Lenstr(line)>2&&line[0]=='I'&&line[1]=='D')
87                {
88                    embl_key_word(line, 5, key, TOKENNUM);
89                    gcg_output_filename(key, outf);
90                    if((ofp=fopen(outf, "w"))==NULL) {
91                        sprintf(temp, "CANNOT open file %s, exit.\n", outf);
92                        error(41, temp);
93                    }
94                } else if(Lenstr(line)>1&&line[0]=='S'
95                          &&line[1]=='Q') {
96                    gcg_doc_out(line, ofp);
97                    seqdata = 1;
98                    eof = embl_origin(line, ifp1);
99                }
100            }
101            if(seqdata) {
102                gcg_seq_out(ofp, key);
103                init_seq_data();
104                seqdata=0;
105                fclose(ofp);
106            } else {
107                gcg_doc_out(line, ofp);
108                eof = Fgetline(line, LINENUM, ifp1);
109            }
110        }
111    }
112}
113/* ----------------------------------------------------------------
114 *   Function gcg_seq_out().
115 *       Output sequence data in gcg format.
116 */
117void
118gcg_seq_out(ofp, key)
119     FILE    *ofp;
120     char    *key;
121{
122    /*  int indi, indj, indk; */
123    /*  char    *today_date(), *gcg_date(); */
124    /*  void    gcg_out_origin(); */
125
126    fprintf(ofp, "\n%s  Length: %d  %s  Type: N  Check: %d  ..\n\n", key,
127            gcg_seq_length(), gcg_date(today_date()),
128            checksum(data.sequence, data.seq_length));
129    gcg_out_origin(ofp);
130}
131/* --------------------------------------------------------------------
132 *   Function gcg_doc_out().
133 *       Output non-sequence data(document) of gcg format.
134 */
135void
136gcg_doc_out(line, ofp)
137     char    *line;
138     FILE    *ofp;
139{
140    int indi, len;
141    int previous_is_dot;
142
143    for(indi=0, len=Lenstr(line), previous_is_dot=0; indi<len; indi++) {
144        if(previous_is_dot) {
145            if(line[indi]=='.') fprintf(ofp, " ");
146            else previous_is_dot=0;
147        }
148        fprintf(ofp, "%c", line[indi]);
149        if(line[indi]=='.') previous_is_dot=1;
150    }
151}
152/* -----------------------------------------------------------------
153 *   Function checksum().
154 *       Calculate checksum for GCG format.
155 */
156int
157checksum(string, numofstr)
158     char    *string;
159     int numofstr;
160{
161    int cksum=0, indi, count=0, charnum;
162
163    for(indi=0; indi<numofstr; indi++)  {
164        if(string[indi]=='.'||string[indi]=='-'||string[indi]=='~')
165            continue;
166        count++;
167        if(string[indi]>='a'&&string[indi]<='z')
168            charnum = string[indi]-'a'+'A';
169        else charnum = string[indi];
170        cksum = ((cksum+count*charnum) % 10000);
171        if(count==57) count=0;
172    }
173    return(cksum);
174}
175/* --------------------------------------------------------------------
176 *   Fcuntion gcg_out_origin().
177 *       Output sequence data in gcg format.
178 */
179void
180gcg_out_origin(fp)
181     FILE    *fp;
182{
183
184    int indi, indj, indk;
185
186    for(indi=0, indj=0, indk=1; indi<data.seq_length; indi++)   {
187        if(data.sequence[indi]=='.'||data.sequence[indi]=='~'
188           ||data.sequence[indi]=='-') continue;
189        if((indk % 50)==1) fprintf(fp, "%8d  ", indk);
190        fprintf(fp, "%c", data.sequence[indi]);
191        indj++;
192        if(indj==10) { fprintf(fp, " "); indj=0; }
193        if((indk % 50)==0) fprintf(fp, "\n\n");
194        indk++;
195    }
196    if((indk % 50)!=1) fprintf(fp, " \n");
197}
198/* --------------------------------------------------------------
199 *   Function gcg_output_filename().
200 *       Get gcg output filename, convert all '.' to '_' and
201 *           append ".RDP" as suffix.
202 */
203void
204gcg_output_filename(prefix, name)
205     char    *prefix, *name;
206{
207    int indi, len;
208
209    for(indi=0, len=Lenstr(prefix); indi<len; indi++)
210        if(prefix[indi]=='.') prefix[indi]='_';
211    sprintf(name, "%s.RDP", prefix);
212}
213/* ------------------------------------------------------------------
214 *   Function gcg_seq_length().
215 *       Calculate sequence length without gap.
216 */
217int
218gcg_seq_length()    {
219
220    int indi, len;
221
222    for(indi=0, len=data.seq_length; indi<data.seq_length; indi++)
223        if(data.sequence[indi]=='.'||data.sequence[indi]=='-'
224           ||data.sequence[indi]=='~') len--;
225
226    return(len);
227}
Note: See TracBrowser for help on using the repository browser.