1 /*****************************************************************************
2 *
3 * asn2asn.c
4 * This is a framework for reading an ASN.1 Seq-entry or Bioseq-set,
5 * doing some processing on it, and outputting it again. seqport.h
6 * is included since it covers most necessary utilities that you might
7 * need. You may need to add others for specialized reports and so on.
8 *
9 * The check for aipout == NULL is to show how to change the code if
10 * no output is desired. Change the default in myargs from "stdout" to
11 * NULL to make output command line optional.
12 *
13 * This program can be used "as is" to convert between binary and text
14 * ASN.1 through the object loaders.
15 *
16 *****************************************************************************/
17 #include <seqport.h>
18
19 #define NUMARG 7
20 Args myargs[NUMARG] = {
21 {"Filename for asn.1 input","stdin",NULL,NULL,FALSE,'i',ARG_FILE_IN,0.0,0,NULL},
22 {"Input is a Seq-entry","F", NULL ,NULL ,TRUE,'e',ARG_BOOLEAN,0.0,0,NULL},
23 {"Input asnfile in binary mode","F",NULL,NULL,TRUE,'b',ARG_BOOLEAN,0.0,0,NULL},
24 {"Filename for asn.1 output","stdout", NULL,NULL,TRUE,'o',ARG_FILE_OUT,0.0,0,NULL},
25 {"Output asnfile in binary mode?","F", NULL ,NULL ,TRUE,'s',ARG_BOOLEAN,0.0,0,NULL},
26 {"Log errors to file named:",NULL,NULL,NULL,TRUE,'l',ARG_FILE_OUT, 0.0,0,NULL},
27 {"Output asnfile in XML?","F", NULL ,NULL ,TRUE,'x',ARG_BOOLEAN,0.0,0,NULL}};
28
29
30 /*****************************************************************************
31 *
32 * Put any internal prototypes here
33 * NB: the function must match the prototype exactly, including "static" or
34 * not.
35 *
36 *****************************************************************************/
37 static void SeqEntryProcess PROTO((SeqEntryPtr sep)); /* dummy function */
38
39
40 /*****************************************************************************
41 *
42 * Main program loop to read, process, write SeqEntrys
43 *
44 *****************************************************************************/
Main(void)45 Int2 Main(void)
46 {
47 AsnIoPtr aipout=NULL, aipin;
48 SeqEntryPtr sep;
49 AsnTypePtr atp, atp2;
50 AsnModulePtr amp;
51 DataVal dv;
52 CharPtr ftype;
53
54 /* check command line arguments */
55
56 if ( ! GetArgs("asn2asn",NUMARG, myargs))
57 return 1;
58
59 /* load the sequence alphabets */
60 /* (and sequence parse trees) */
61 if (! SeqEntryLoad())
62 ErrShow();
63 /* get pointer to all loaded ASN.1 modules */
64 amp = AsnAllModPtr();
65 if (amp == NULL)
66 {
67 ErrShow();
68 return 1;
69 }
70
71 atp = AsnFind("Bioseq-set"); /* get the initial type pointers */
72 if (atp == NULL)
73 {
74 ErrShow();
75 return 1;
76 }
77 atp2 = AsnFind("Bioseq-set.seq-set.E");
78 if (atp2 == NULL)
79 {
80 ErrShow();
81 return 1;
82 }
83
84 /* open the ASN.1 input file in the right mode */
85
86 if ((aipin = AsnIoOpen (myargs[0].strvalue, myargs[2].intvalue?"rb":"r"))
87 == NULL)
88 {
89 ErrPostEx(SEV_ERROR,0,0, "Can't open %s", myargs[0].strvalue);
90 ErrShow();
91 return 1;
92 }
93
94 /* open the ASN.1 output file in the right mode */
95
96 if (myargs[3].strvalue != NULL) /* output desired? */
97 {
98 ftype = "w";
99 if (myargs[4].intvalue)
100 ftype = "wb";
101 if (myargs[6].intvalue)
102 ftype = "wx";
103
104 if ((aipout = AsnIoOpen (myargs[3].strvalue, ftype)) == NULL)
105 {
106 ErrPostEx(SEV_ERROR,0,0, "Can't open %s", myargs[3].strvalue);
107 ErrShow();
108 return 1;
109 }
110 }
111
112 /* log errors instead of die */
113 if (myargs[5].strvalue != NULL)
114 {
115 if (! ErrSetLog (myargs[5].strvalue))
116 {
117 ErrShow();
118 return 1;
119 }
120 else
121 ErrSetOpts (ERR_CONTINUE, ERR_LOG_ON);
122 }
123
124 if ( myargs[1].intvalue) /* read one Seq-entry */
125 {
126 sep = SeqEntryAsnRead(aipin, NULL);
127 SeqEntryProcess(sep); /* do any processing */
128 if (aipout != NULL)
129 SeqEntryAsnWrite(sep, aipout, NULL);
130 SeqEntryFree(sep);
131 }
132 else /* read Seq-entry's from a Bioseq-set */
133 {
134 while ((atp = AsnReadId(aipin, amp, atp)) != NULL)
135 {
136 if (atp == atp2) /* top level Seq-entry */
137 {
138 sep = SeqEntryAsnRead(aipin, atp);
139 SeqEntryProcess(sep); /* do any processing */
140 if (aipout != NULL)
141 SeqEntryAsnWrite(sep, aipout, atp);
142 SeqEntryFree(sep);
143 }
144 else
145 {
146 if (aipout == NULL) /* don't need to read the data */
147 AsnReadVal(aipin, atp, NULL);
148 else
149 {
150 AsnReadVal(aipin, atp, &dv); /* read it */
151 AsnWrite(aipout, atp, &dv); /* write it */
152 AsnKillValue(atp, &dv); /* free it */
153 }
154 }
155 }
156 }
157
158 AsnIoClose(aipin);
159 AsnIoClose(aipout);
160
161 return(0);
162 }
163
164
165 /*****************************************************************************
166 *
167 * void SeqEntryProcess (sep)
168 * just a dummy routine that does nothing
169 *
170 *****************************************************************************/
SeqEntryProcess(SeqEntryPtr sep)171 static void SeqEntryProcess (SeqEntryPtr sep)
172 {
173 if (sep == NULL)
174 return;
175 return;
176 }
177
178