1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/* ureadasn.c
-- parse, mangle and otherwise rewrite ASN1 file/entries for readseq reading
-- from NCBI toolkit (ncbi.nlm.nih.gov:/toolkit)
*/
#ifdef NCBI
#include <stdio.h>
#include <ctype.h>
#include <string.h>
/* NCBI toolkit :include: must be on lib path */
#include <ncbi.h>
#include <seqport.h>
#define UREADASN
#include "ureadseq.h"
#pragma segment ureadasn
/* this stuff is hacked up from tofasta.c of ncbitools */
#define kBaseAny 0
#define kBaseNucleic 1
#define kBaseAmino 2
typedef struct tofasta {
Boolean idonly;
short *seqnum;
short whichSeq;
char **seq, **seqid;
long *seqlen;
} FastaDat, PNTR FastaPtr;
void BioseqRawToRaw(BioseqPtr bsp, Boolean idonly,
short whichSeq, short *seqnum,
char **seq, char **seqid, long *seqlen)
{
SeqPortPtr spp;
SeqIdPtr bestid;
Uint1 repr, code, residue;
CharPtr tmp, title;
long outlen, outmax;
char localid[256], *sp;
/* !!! this may be called several times for a single sequence
because SeqEntryExplore looks for parts and joins them...
assume seq, seqid, seqlen may contain data (or NULL)
*/
if (bsp == NULL) return;
repr = Bioseq_repr(bsp);
if (!(repr == Seq_repr_raw || repr == Seq_repr_const)) return;
(*seqnum)++;
if (!(whichSeq == *seqnum || whichSeq == 0)) return;
bestid = SeqIdFindBest(bsp->id, (Uint1) 0);
title = BioseqGetTitle(bsp);
if (idonly) {
sprintf(localid, " %d) ", *seqnum);
tmp= localid + strlen(localid)-1;
}
else {
strcpy(localid," ");
tmp= localid;
}
tmp = SeqIdPrint(bestid, tmp, PRINTID_FASTA_SHORT);
tmp = StringMove(tmp, " ");
StringNCpy(tmp, title, 200);
/* fprintf(stderr,"BioseqRawToRaw: localid='%s'\n",localid); */
/* < seqid is fixed storage */
/* strcpy( *seqid, localid); */
/* < seqid is variable sized */
outmax= strlen(localid) + 3;
if (*seqid==NULL) {
*seqid= (char*) malloc(outmax);
if (*seqid==NULL) return;
strcpy(*seqid, localid);
}
else {
outmax += strlen(*seqid) + 2;
*seqid= (char*) realloc( *seqid, outmax);
if (*seqid==NULL) return;
if (!idonly) strcat(*seqid, "; ");
strcat(*seqid, localid);
}
if (idonly) {
strcat(*seqid,"\n");
return;
}
if (ISA_na(bsp->mol)) code = Seq_code_iupacna;
else code = Seq_code_iupacaa;
spp = SeqPortNew(bsp, 0, -1, 0, code);
SeqPortSeek(spp, 0, SEEK_SET);
sp= *seq;
if (sp==NULL) { /* this is always true now !? */
outlen= 0;
outmax= 500;
sp= (char*) malloc(outmax);
}
else {
outlen= strlen(sp);
outmax= outlen + 500;
sp= (char*) realloc( sp, outmax);
}
if (sp==NULL) return;
while ((residue = SeqPortGetResidue(spp)) != SEQPORT_EOF) {
if (outlen>=outmax) {
outmax= outlen + 500;
sp= (char*) realloc(sp, outmax);
if (sp==NULL) return;
}
sp[outlen++] = residue;
}
sp= (char*) realloc(sp, outlen+1);
if (sp!=NULL) sp[outlen]= '\0';
*seq= sp;
*seqlen= outlen;
SeqPortFree(spp);
return;
}
static void SeqEntryRawseq(SeqEntryPtr sep, Pointer data, Int4 index, Int2 indent)
{
FastaPtr tfa;
BioseqPtr bsp;
if (!IS_Bioseq(sep)) return;
bsp = (BioseqPtr)sep->data.ptrvalue;
tfa = (FastaPtr) data;
BioseqRawToRaw(bsp, tfa->idonly, tfa->whichSeq, tfa->seqnum,
tfa->seq, tfa->seqid, tfa->seqlen);
}
void SeqEntryToRaw(SeqEntryPtr sep, Boolean idonly, short whichSeq, short *seqnum,
char **seq, char **seqid, long *seqlen)
{
FastaDat tfa;
if (sep == NULL) return;
tfa.idonly= idonly;
tfa.seqnum= seqnum;
tfa.whichSeq= whichSeq;
tfa.seq = seq;
tfa.seqid = seqid;
tfa.seqlen= seqlen;
SeqEntryExplore(sep, (Pointer)&tfa, SeqEntryRawseq);
}
char *listASNSeqs(const char *filename, const long skiplines,
const short format, /* note: this is kASNseqentry or kASNseqset */
short *nseq, short *error )
{
AsnIoPtr aip = NULL;
SeqEntryPtr the_set;
AsnTypePtr atp, atp2;
AsnModulePtr amp;
Boolean inIsBinary= FALSE; /* damn, why can't asn routines test this? */
char *seq = NULL;
char *seqid = NULL, stemp[256];
long seqlen;
int i, count;
*nseq= 0;
*error= 0;
/* asn dictionary setups */
/*fprintf(stderr,"listASNSeqs: SeqEntryLoad\n");*/
if (! SeqEntryLoad()) goto errxit; /* sequence alphabets (and sequence parse trees) */
amp = AsnAllModPtr(); /* get pointer to all loaded ASN.1 modules */
if (amp == NULL) goto errxit;
atp = AsnFind("Bioseq-set"); /* get the initial type pointers */
if (atp == NULL) goto errxit;
atp2 = AsnFind("Bioseq-set.seq-set.E");
if (atp2 == NULL) goto errxit;
/*fprintf(stderr,"listASNSeqs: AsnIoOpen\n");*/
/* open the ASN.1 input file in the right mode */
/* !!!! THIS FAILS when filename has MAC PATH (& other paths?) (:folder:filename) */
if ((aip = AsnIoOpen(filename, inIsBinary?"rb":"r")) == NULL) goto errxit;
for (i=0; i<skiplines; i++) fgets( stemp, 255, aip->fp); /* this may mess up asn routines... */
if (! ErrSetLog ("stderr")) goto errxit;
else ErrSetOpts(ERR_CONTINUE, ERR_LOG_ON); /*?? log errors instead of die */
if (format == kASNseqentry) { /* read one Seq-entry */
/*fprintf(stderr,"listASNSeqs: SeqEntryAsnRead\n");*/
the_set = SeqEntryAsnRead(aip, NULL);
SeqEntryToRaw(the_set, true, 0, nseq, &seq, &seqid, &seqlen);
if (seq) free(seq); seq= NULL;
SeqEntryFree(the_set);
}
else { /* read Seq-entry's from a Bioseq-set */
count = 0;
/*fprintf(stderr,"listASNSeqs: AsnReadId\n");*/
while ((atp = AsnReadId(aip, amp, atp)) != NULL) {
if (atp == atp2) { /* top level Seq-entry */
the_set = SeqEntryAsnRead(aip, atp);
SeqEntryToRaw(the_set, true, 0, nseq, &seq, &seqid, &seqlen);
SeqEntryFree(the_set);
if (seq) free(seq); seq= NULL;
}
else
AsnReadVal(aip, atp, NULL);
count++;
}
}
AsnIoClose(aip);
*error= 0;
return seqid;
errxit:
AsnIoClose(aip);
if (seqid) free(seqid);
*error= eASNerr;
return NULL;
}
char *readASNSeq(const short whichEntry, const char *filename,
const long skiplines,
const short format, /* note: this is kASNseqentry or kASNseqset */
long *seqlen, short *nseq,
short *error, char **seqid )
{
AsnIoPtr aip = NULL;
SeqEntryPtr the_set;
AsnTypePtr atp, atp2;
AsnModulePtr amp;
Boolean inIsBinary= FALSE; /* damn, why can't asn routines test this? */
char *seq, stemp[200];
int i, count;
*seqlen= 0;
*nseq= 0;
*error= 0;
seq= NULL;
/*fprintf(stderr,"readASNseq: SeqEntryLoad\n");*/
/* asn dictionary setups */
if (! SeqEntryLoad()) goto errxit; /* sequence alphabets (and sequence parse trees) */
amp = AsnAllModPtr(); /* get pointer to all loaded ASN.1 modules */
if (amp == NULL) goto errxit;
atp = AsnFind("Bioseq-set"); /* get the initial type pointers */
if (atp == NULL) goto errxit;
atp2 = AsnFind("Bioseq-set.seq-set.E");
if (atp2 == NULL) goto errxit;
/* open the ASN.1 input file in the right mode */
/*fprintf(stderr,"readASNseq: AsnIoOpen(%s)\n", filename);*/
if ((aip = AsnIoOpen(filename, inIsBinary?"rb":"r")) == NULL) goto errxit;
for (i=0; i<skiplines; i++) fgets( stemp, 255, aip->fp); /* this may mess up asn routines... */
if (! ErrSetLog ("stderr")) goto errxit;
else ErrSetOpts(ERR_CONTINUE, ERR_LOG_ON); /*?? log errors instead of die */
seq= NULL;
if (format == kASNseqentry) { /* read one Seq-entry */
/*fprintf(stderr,"readASNseq: SeqEntryAsnRead\n");*/
the_set = SeqEntryAsnRead(aip, NULL);
SeqEntryToRaw(the_set, false, whichEntry, nseq, &seq, seqid, seqlen);
SeqEntryFree(the_set);
goto goodexit;
}
else { /* read Seq-entry's from a Bioseq-set */
count = 0;
/*fprintf(stderr,"readASNseq: AsnReadId\n");*/
while ((atp = AsnReadId(aip, amp, atp)) != NULL) {
if (atp == atp2) { /* top level Seq-entry */
the_set = SeqEntryAsnRead(aip, atp);
SeqEntryToRaw(the_set, false, whichEntry, nseq, &seq, seqid, seqlen);
SeqEntryFree(the_set);
if (*nseq >= whichEntry) goto goodexit;
}
else
AsnReadVal(aip, atp, NULL);
count++;
}
}
goodexit:
AsnIoClose(aip);
*error= 0;
return seq;
errxit:
AsnIoClose(aip);
*error= eASNerr;
if (seq) free(seq);
return NULL;
}
#endif /*NCBI*/
|