source: tags/arb_5.3/CONVERTALN/paup.c

Last change on this file was 5390, checked in by westram, 16 years ago
  • TAB-Ex
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 KB
Line 
1#include <stdio.h>
2#include "convert.h"
3#include "global.h"
4
5/* ------------------------------------------------------------
6 *   Function init_paup().
7 *       Init. paup data.
8 */
9void init_paup() {
10    free_sequence_data(data.paup.ntax);
11    data.paup.ntax = 0;
12    data.paup.nchar = 0;
13}
14/* -------------------------------------------------------------
15 *   Function to_paup()
16 *       Convert from some format to PAUP format.
17 */
18void
19to_paup(inf, outf, informat)
20     char   *inf, *outf;
21     int    informat;
22{
23    FILE        *IFP, *ofp;
24    FILE_BUFFER  ifp;
25    int          maxsize, current, total_seq, first_line;
26    int          out_of_memory, indi;
27    char         temp[TOKENNUM], eof;
28    char        *name, *today;
29
30    if((IFP=fopen(inf, "r"))==NULL) {
31        sprintf(temp,
32                "Cannot open input file %s, exit\n", inf);
33        error(64, temp);
34    }
35    ifp              = create_FILE_BUFFER(inf, IFP);
36    if(Lenstr(outf) <= 0)   ofp = stdout;
37    else if((ofp=fopen(outf, "w"))==NULL)   {
38        sprintf(temp,
39                "Cannot open output file %s, exit\n", outf);
40        error(65, temp);
41    }
42    maxsize = 1;
43    out_of_memory = 0;
44    name = NULL;
45    init();
46    init_paup();
47    paup_print_header(ofp);
48    total_seq = 0;
49    do  {
50        if(informat==ALMA)  {
51            init_alma();
52            eof=alma_in(ifp);
53        } else if(informat==GENBANK)    {
54            init_genbank();
55            eof=genbank_in_locus(ifp);
56        } else if(informat==EMBL||informat==PROTEIN) {
57            init_embl();
58            eof=embl_in_id(ifp);
59        } else if(informat==MACKE)  {
60            init_macke();
61            eof=macke_in_name(ifp);
62        } else error(63,
63                     "UNKNOW input format when converting to PAUP format.");
64        if(eof==EOF) break;
65        if(informat==ALMA)  {
66            alma_key_word
67                (data.alma.id, 0, temp, TOKENNUM);
68        } else if(informat==GENBANK)    {
69            genbank_key_word
70                (data.gbk.locus, 0, temp, TOKENNUM);
71        } else if(informat==EMBL||informat==PROTEIN)    {
72            embl_key_word
73                (data.embl.id, 0, temp, TOKENNUM);
74        } else if(informat==MACKE)  {
75            Cpystr(temp, data.macke.seqabbr);
76        } else error(118,
77                     "UNKNOW input format when converting to PAUP format.");
78
79        total_seq++;
80
81        if((name = Dupstr(temp))==NULL&&temp!=NULL)
82        { out_of_memory=1; break; }
83        paup_verify_name(&name);
84
85        if(data.seq_length>maxsize) maxsize = data.seq_length;
86        if (!realloc_sequence_data(total_seq)) { out_of_memory = 1; break; }
87
88        data.ids[total_seq-1]     = name;
89        data.seqs[total_seq-1]    = (char*)Dupstr(data.sequence);
90        data.lengths[total_seq-1] = Lenstr(data.sequence);
91    } while(!out_of_memory);
92
93    if(out_of_memory)   {
94        /* cannot hold all seqs into mem. */
95        fprintf(stderr,
96                "Rerun the conversion throught one seq. by one seq. base.\n");
97
98        destroy_FILE_BUFFER(ifp); fclose(ofp);
99        to_paup_1x1(inf, outf, informat);
100        return;
101    }
102    current = 0;
103    while(maxsize>current)  {
104        first_line = 0;
105        for(indi=0; indi<total_seq; indi++) {
106            if(current<data.lengths[indi]) first_line++;
107            paup_print_line(data.ids[indi], data.seqs[indi], data.lengths[indi], current, (first_line==1), ofp);
108
109            /* Avoid repeating */
110            if(first_line==1) first_line++;
111        }
112        current +=(SEQLINE-10);
113        if(maxsize>current) fprintf(ofp, "\n");
114    }
115
116#ifdef log
117    fprintf(stderr, "Total %d sequences have been processed\n", total_seq);
118#endif
119
120    fprintf(ofp, "      ;\nENDBLOCK;\n");
121    /* rewrite output header */
122    rewind(ofp);
123    fprintf(ofp, "#NEXUS\n");
124    today = today_date();
125    if(today[Lenstr(today)-1]=='\n')
126        today[Lenstr(today)-1] = '\0';
127
128    fprintf(ofp,
129            "[! RDP - the Ribsomal Database Project, (%s).]\n", today);
130
131    fprintf(ofp,
132            "[! To get started, send HELP to rdp@info.mcs.anl.gov ]\n");
133
134    fprintf(ofp, "BEGIN DATA;\n   DIMENSIONS\n");
135    fprintf(ofp,
136            "      NTAX = %6d\n      NCHAR = %6d\n      ;\n",
137            total_seq, maxsize);
138
139    destroy_FILE_BUFFER(ifp); fclose(ofp);
140}
141/* ---------------------------------------------------------------
142 *   Function to_paup_1x1()
143 *       Convert from ALMA format to PAUP format,
144 *           one seq by one seq.
145 */
146void
147to_paup_1x1(inf, outf, informat)
148     char   *inf, *outf;
149     int    informat;
150{
151    FILE        *IFP, *ofp;
152    FILE_BUFFER  ifp;
153    int          maxsize, current, total_seq, first_line;
154    char         temp[TOKENNUM], eof;
155    char    *name, *today;
156
157    if((IFP=fopen(inf, "r"))==NULL) {
158        sprintf(temp, "Cannot open input file %s, exit\n", inf);
159        error(121, temp);
160    }
161    ifp              = create_FILE_BUFFER(inf, IFP);
162    if(Lenstr(outf) <= 0)   ofp = stdout;
163    else if((ofp=fopen(outf, "w"))==NULL)   {
164        sprintf(temp, "Cannot open output file %s, exit\n", outf);
165        error(122, temp);
166    }
167    maxsize = 1; current = 0;
168    name = NULL;
169    paup_print_header(ofp);
170    while(maxsize>current)  {
171        init();
172        FILE_BUFFER_rewind(ifp);
173        total_seq = 0;
174        first_line = 0;
175        do  {   /* read in one sequence */
176            init_paup();
177            if(informat==ALMA)  {
178                init_alma();
179                eof=alma_in(ifp);
180            } else if(informat==GENBANK)    {
181                init_genbank();
182                eof=genbank_in_locus(ifp);
183            } else if(informat==EMBL||informat==PROTEIN) {
184                init_embl();
185                eof=embl_in_id(ifp);
186            } else if(informat==MACKE)  {
187                init_macke();
188                eof=macke_in_name(ifp);
189            } else error(127,
190                         "UNKNOW input format when converting to PAUP format.");
191
192            if(eof==EOF) break;
193            if(informat==ALMA)  {
194                alma_key_word
195                    (data.alma.id, 0, temp, TOKENNUM);
196            } else if(informat==GENBANK)    {
197                genbank_key_word
198                    (data.gbk.locus, 0, temp, TOKENNUM);
199            } else if(informat==EMBL||informat==PROTEIN) {
200                embl_key_word
201                    (data.embl.id, 0, temp, TOKENNUM);
202            } else if(informat==MACKE)  {
203                macke_key_word
204                    (data.macke.name, 0, temp, TOKENNUM);
205            } else error(70,
206                         "UNKNOW input format when converting to PAUP format.");
207
208            Freespace(&name);
209            name = Dupstr(temp);
210            paup_verify_name(&name);
211
212            if(data.seq_length>maxsize)
213                maxsize = data.seq_length;
214
215            if(current<data.seq_length) first_line++;
216
217            paup_print_line(name, data.sequence, data.seq_length, current, (first_line==1), ofp);
218
219            /* Avoid repeating */
220            if(first_line==1) first_line++;
221
222            total_seq++;
223
224        } while(1);
225        current += (SEQLINE-10);
226        if(maxsize>current) fprintf(ofp, "\n");
227    }   /* print block by block */
228
229#ifdef log
230    fprintf(stderr, "Total %d sequences have been processed\n", total_seq);
231#endif
232
233    fprintf(ofp, "      ;\nENDBLOCK;\n");
234    /* rewrite output header */
235    rewind(ofp);
236    fprintf(ofp, "#NEXUS\n");
237    today = today_date();
238    if(today[Lenstr(today)-1]=='\n') today[Lenstr(today)-1] = '\0';
239
240    fprintf(ofp,
241            "[! RDP - the Ribsomal Database Project, (%s).]\n", today);
242
243    fprintf(ofp,
244            "[! To get started, send HELP to rdp@info.mcs.anl.gov ]\n");
245
246    fprintf(ofp, "BEGIN DATA;\n   DIMENSIONS\n");
247    fprintf(ofp,
248            "      NTAX = %6d\n      NCHAR = %6d\n      ;\n",
249            total_seq, maxsize);
250
251    destroy_FILE_BUFFER(ifp); fclose(ofp);
252}
253/* -----------------------------------------------------------
254 *   Function paup_verify_name().
255 *       Verify short_id in PUAP format.
256 */
257void
258paup_verify_name(string)
259     char    **string;
260{
261    int indi, len, index;
262    char    temp[TOKENNUM];
263    /*  void    Freespace(); */
264
265    for(indi=index=0,len=Lenstr((*string)); indi<len&&index==0;
266        indi++)
267        if((*string)[indi]=='*'||(*string)[indi]=='('
268           ||(*string)[indi]==')'||(*string)[indi]=='{'
269           ||(*string)[indi]=='/'||(*string)[indi]==','
270           ||(*string)[indi]==';'||(*string)[indi]=='_'
271           ||(*string)[indi]=='='||(*string)[indi]==':'
272           ||(*string)[indi]=='\\'||(*string)[indi]=='\'')
273            index=1;
274
275    if(index==0)    return;
276    else    {
277        temp[0]='\'';
278        for(indi=0, index=1; indi<len; indi++, index++) {
279            temp[index]=(*string)[indi];
280            if((*string)[indi]=='\'')
281                temp[++index]='\'';
282        }
283        temp[index++]='\'';
284        temp[index]='\0';
285        Freespace(string);
286        (*string)=(char*)Dupstr(temp);
287    }
288}
289/* --------------------------------------------------------------
290 *  Function paup_print_line().
291 *      print paup file.
292 */
293void
294paup_print_line(string, sequence, seq_length, index, first_line, fp)
295     char *string, *sequence;
296     int   seq_length;
297     int   index, first_line;
298     FILE *fp;
299{
300    int indi, indj, length;
301
302    length = SEQLINE-10;
303
304    fputs("      ", fp);
305    /* truncate if length of seq ID is greater than 10 */
306    for(indi=0; indi<10 && string[indi]; indi++) fputc(string[indi], fp);
307
308    if (seq_length == 0) seq_length = Lenstr(sequence);
309
310    if (index<seq_length) {
311        for (; indi<11; indi++) fputc(' ', fp);
312
313        for (indi=indj=0; indi<length; indi++)  {
314            if ((index+indi)<seq_length) {
315                fputc(sequence[index+indi], fp);
316                indj++;
317                if(indj==10 && indi<(length-1) && (indi+index)<(seq_length-1)) {
318                    fputc(' ', fp);
319                    indj = 0;
320                }
321            }
322            else break;
323        }
324    }
325
326    if(first_line)
327        fprintf(fp, " [%d - %d]", index+1, (index+indi));
328
329    fputc('\n', fp);
330}
331/* ----------------------------------------------------------
332 *   Function paup_print_header().
333 *       Print out the header of each paup format.
334 */
335void
336paup_print_header(ofp)
337     FILE    *ofp;
338{
339    char    *today;
340
341    fprintf(ofp, "#NEXUS\n");
342    today = today_date();
343    if(today[Lenstr(today)-1]=='\n')
344        today[Lenstr(today)-1] = '\0';
345
346    fprintf(ofp,
347            "[! RDP - the Ribsomal Database Project, (%s).]\n",
348            today);
349
350    fprintf(ofp,
351            "[! To get started, send HELP to rdp@info.mcs.anl.gov ]\n");
352
353    fprintf(ofp, "BEGIN DATA;\n   DIMENSIONS\n");
354
355    fprintf(ofp,
356            "      NTAX =       \n      NCHAR =       \n      ;\n");
357
358    fprintf(ofp, "   FORMAT\n      LABELPOS = LEFT\n");
359
360    fprintf(ofp,
361            "      MISSING = .\n      EQUATE = \"%s\"\n",
362            data.paup.equate);
363
364    fprintf(ofp,
365            "      INTERLEAVE\n      DATATYPE = RNA\n      GAP = %c\n      ;\n",
366            data.paup.gap);
367
368    fprintf(ofp,
369            "   OPTIONS\n      GAPMODE = MISSING\n      ;\n   MATRIX\n");
370}
Note: See TracBrowser for help on using the repository browser.