1 /*****************************************************************************
2 *
3 * $Id: asn2xml.c,v 1.2 2001/08/10 18:56:42 kans Exp $
4 *
5 *   asn2xml.c
6 *
7 *      This program is based on asn2asn.c but modified specifically to
8 *   efficiently convert asn.1 Seq-entry or Bioseq-set into XML. The
9 *   only real difference with asn2asn -x is that a special step puts
10 *   the Seq-inst through the object loaders to convert 2 bit and 4 bit
11 *   encodings to text formats (1 base per letter) that XML fans prefer.
12 *
13 *   The defaults are set so that you can pipe the binary asn.1 Bioseq-set
14 *   in the GenBank release or update.
15 *
16 *   cat update.ent | asn2xml | myxmlreader
17 *
18 * Author: Jim Ostell <ostell@ncbi.nlm.nih.gov>
19 *
20 *****************************************************************************/
21 #include <seqport.h>
22 #include <objsub.h>
23 
24 #define NUMARG 6
25 Args myargs[NUMARG] = {
26 	{"Filename for asn.1 input","stdin",NULL,NULL,FALSE,'i',ARG_FILE_IN,0.0,0,NULL},
27 	{"Input is a Seq-entry","F", NULL ,NULL ,TRUE,'e',ARG_BOOLEAN,0.0,0,NULL},
28 	{"Input is a Seq-submit","F", NULL ,NULL ,TRUE,'s',ARG_BOOLEAN,0.0,0,NULL},
29 	{"Input asnfile in binary mode","T",NULL,NULL,TRUE,'b',ARG_BOOLEAN,0.0,0,NULL},
30 	{"Filename for XML output","stdout", NULL,NULL,TRUE,'o',ARG_FILE_OUT,0.0,0,NULL},
31     {"Log errors to file named:",NULL,NULL,NULL,TRUE,'l',ARG_FILE_OUT, 0.0,0,NULL}};
32 
33 /*****************************************************************************
34 *
35 *   Main program loop to read, process, write SeqEntrys
36 *
37 *****************************************************************************/
Main(void)38 Int2 Main(void)
39 {
40 	AsnIoPtr aipout=NULL, aipin;
41 	AsnTypePtr atp, atp_inst;
42 	AsnModulePtr amp;
43 	DataVal dv;
44 	CharPtr ftype;
45 	BioseqPtr bsp;
46 
47 					/* check command line arguments */
48 
49 	if ( ! GetArgs("asn2xml 1.0",NUMARG, myargs))
50 		return 1;
51 
52 					/* load the sequence alphabets  */
53 					/* (and sequence parse trees)   */
54 	if (! SeqEntryLoad())
55 		ErrShow();
56 	if (! SubmitAsnLoad())
57 		ErrShow();
58 	if (! SeqCodeSetLoad())
59 		ErrShow();
60 				    /* get pointer to all loaded ASN.1 modules */
61 	amp = AsnAllModPtr();
62 	if (amp == NULL)
63 	{
64 		ErrShow();
65 		return 1;
66 	}
67 
68 	if (myargs[1].intvalue)
69 		atp = AsnFind("Seq-entry");
70 	else if (myargs[2].intvalue)
71 		atp = AsnFind("Seq-submit");
72 	else
73 		atp = AsnFind("Bioseq-set");    /* get the initial type pointers */
74 	if (atp == NULL)
75 	{
76 		ErrShow();
77 		return 1;
78 	}
79 
80 	atp_inst = AsnFind("Bioseq.inst");
81 	if (atp_inst == NULL)
82 	{
83 		ErrShow();
84 		return 1;
85 	}
86 
87 					/* open the ASN.1 input file in the right mode */
88 
89 	if ((aipin = AsnIoOpen (myargs[0].strvalue, myargs[3].intvalue?"rb":"r"))
90           == NULL)
91 	{
92 		ErrPostEx(SEV_ERROR,0,0, "Can't open %s", myargs[0].strvalue);
93 		ErrShow();
94 		return 1;
95 	}
96 
97 					/* open the ASN.1 output file in the right mode */
98 
99 	if (myargs[4].strvalue != NULL)   /* output desired? */
100 	{
101 		ftype = "wx";
102 
103 		if ((aipout = AsnIoOpen (myargs[4].strvalue, ftype)) == NULL)
104 		{
105 			ErrPostEx(SEV_ERROR,0,0, "Can't open %s", myargs[4].strvalue);
106 			ErrShow();
107 			return 1;
108 		}
109 	}
110 
111                                 /* log errors instead of die */
112 	if (myargs[5].strvalue != NULL)
113 	{
114 		if (! ErrSetLog (myargs[5].strvalue))
115 		{
116 			ErrShow();
117 			return 1;
118 		}
119 		else
120 			ErrSetOpts (ERR_CONTINUE, ERR_LOG_ON);
121 	}
122 
123 	while ((atp = AsnReadId(aipin, amp, atp)) != NULL)
124 	{
125 		if (atp == atp_inst)    /* need object loader convert */
126 		{
127 			bsp = BioseqNew();  /* need newly initialized bsp */
128 			BioseqInstAsnRead(bsp, aipin, atp);
129 			BioseqInstAsnWrite(bsp, aipout, atp);
130 			bsp = BioseqFree(bsp);
131 		}
132 		else
133 		{
134 	 		AsnReadVal(aipin, atp, &dv); /* read it */
135 			AsnWrite(aipout, atp, &dv);	 /* write it */
136 			AsnKillValue(atp, &dv);      /* free it */
137 		}
138 	}
139 
140 	AsnIoClose(aipin);
141 	AsnIoClose(aipout);
142 
143 	return(0);
144 }
145 
146 
147