source: tags/initial/CONVERTALN/gcg.c

Last change on this file was 2, checked in by oldcode, 24 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 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)
11int     intype;
12char    *inf;
13{
14        FILE    *fopen(), *ifp1, *ifp2, *ifp3, *ofp;
15        char    temp[TOKENNUM], *eof, line[LINENUM], key[TOKENNUM];
16        char    line1[LINENUM], line2[LINENUM], line3[LINENUM], name[LINENUM];
17        char    *eof1, *eof2, *eof3;
18        char    outf[TOKENNUM], *Fgetline();
19        char    *embl_origin(), *genbank_origin(), *macke_origin();
20        void    init_seq_data(), gcg_seq_out(), gcg_doc_out();
21        void    embl_key_word(), genbank_key_word();
22        void    gcg_output_filename();
23        int     seqdata, Lenstr(), Cmpstr(), macke_abbrev();
24
25        if(intype==MACKE)       {
26                if((ifp1=fopen(inf, "r"))==NULL||(ifp2=fopen(inf, "r"))==NULL
27                ||(ifp3=fopen(inf, "r"))==NULL) {
28                        sprintf(temp, "Cannot open input file %s\n", inf);
29                        error(38, temp);
30                }
31        } else if((ifp1=fopen(inf, "r"))==NULL) {
32                sprintf(temp, "CANNOT open input file %s, exit.\n", inf);
33                error(37, temp);
34        }
35        if(intype==MACKE)       {
36                /* skip to #=; where seq. first appears */
37                for(eof1=Fgetline(line1, LINENUM, ifp1);
38                eof1!=NULL&&(line1[0]!='#'||line1[1]!='=');
39                eof1=Fgetline(line1, LINENUM, ifp1)) ;
40                /* skip to #:; where the seq. information is */
41                for(eof2=Fgetline(line2, LINENUM, ifp2);
42                eof2!=NULL&&(line2[0]!='#'||line2[1]!=':');
43                eof2=Fgetline(line2, LINENUM, ifp2)) ;
44                /* skip to where seq. data starts */
45                for(eof3=Fgetline(line3,LINENUM, ifp3);
46                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)
119FILE    *ofp;
120char    *key;
121{
122        int     checksum(), gcg_seq_length(), 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)
137char    *line;
138FILE    *ofp;
139{
140        int     indi, len, Lenstr();
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)
158char    *string;
159int     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)     
181FILE    *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)
205char    *prefix, *name;
206{
207        int     indi, len, Lenstr();
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.