예제 #1
0
파일: testmem.c 프로젝트: hsptools/hsp-wrap
Int2 Main(void)
{
	AsnIoPtr aip;
	SeqEntryPtr sep;
	BytePtr buf;
	Uint2 size = 32000, count;
	AsnIoMemPtr aimp;

					/* check command line arguments */

	if ( ! GetArgs("testmem",NUMARG, myargs))
		return 1;

					/* load the sequence alphabets  */
					/* (and sequence parse trees)   */
	if (! SeqEntryLoad())
		ErrShow();

					/* open the ASN.1 input file in the right mode */

	if ((aip = AsnIoOpen (myargs[0].strvalue, myargs[1].intvalue?"rb":"r"))
          == NULL)
		ErrShow();
	sep = SeqEntryAsnRead(aip, NULL);   	/* read the entry */
	AsnIoClose(aip);

	SeqEntryPack(sep);						/* pack it */

	buf = MemNew(size);                     /* allocate a buffer */
	aimp = AsnIoMemOpen("wb", buf, size);	/* open to write asn1 to it */
	SeqEntryAsnWrite(sep, aimp->aip, NULL);	/* write it */
	AsnIoFlush(aimp->aip);					/* flush it */
	count = aimp->count;					/* record how many bytes in it */
	AsnIoMemClose(aimp);					/* close it */

	SeqEntryFree(sep);						/* release the object */

	aimp = AsnIoMemOpen("rb", buf, count);	/* open to read from buffer */
	sep = SeqEntryAsnRead(aimp->aip, NULL);	/* read it */
	AsnIoMemClose(aimp);					/* close it */
	MemFree(buf);							/* free the buffer */

				  				/* open the output file */

	aip = AsnIoOpen (myargs[2].strvalue, "w");
	SeqEntryAsnWrite(sep, aip, NULL);	/* print it */
	AsnIoClose(aip);

	SeqEntryFree(sep);

	return(0);
}
예제 #2
0
파일: cdscan.c 프로젝트: hsptools/hsp-wrap
static void SeqEntryToFeat (SeqEntryPtr sep, FILE *fp)

{
  AsnExpOptPtr  aeop;
  AsnIoPtr      aip;
  ExpStructPtr  esp;

  if (sep != NULL && fp != NULL) {
    esp = MemNew (sizeof (ExpStruct));
    if (esp != NULL) {
      aip = AsnIoNullOpen ();
      if (aip != NULL) {
        esp->fp = fp;
        esp->aip = AsnIoNew (ASNIO_TEXT_OUT, fp, NULL, NULL, NULL);
        esp->is_na = is_na;
        esp->feat = 3;  /* look for CdRegion SeqFeat */
        aeop = AsnExpOptNew (aip, "Seq-feat", (Pointer) esp, GetSeqFeat);
        if (aeop != NULL) {
          SeqEntryAsnWrite (sep, aip, NULL);
          fflush (fp);
          AsnExpOptFree (aip, aeop);
        }
        AsnIoClose (aip);
      }
      MemFree (esp);
    }
  }
}
예제 #3
0
static Int4 ProcessStream (InputStreamPtr isp, OutputStreamPtr osp, AsnStreamPtr asp, Int4Ptr gap_sizes)
{
  AsnTypePtr   atp, atp_srch;
  AsnIoPtr asn_in, asn_out;
  Int4         rval = 0;
  SeqEntryPtr  sep;
  Uint2        entityID;
  DataVal av;
  
  if (isp == NULL || osp == NULL || asp == NULL || gap_sizes == NULL) return 1;

  asn_in = AsnIoFromInputStream (isp);
  asn_out = AsnIoFromOutputStream (osp);

  if (isp->is_seqentry) {
    atp = asp->atp_se;
    atp_srch = asp->atp_se;
  }
  else {
    atp = asp->atp_bss;
    atp_srch = asp->atp_bss_se;
  }

  while ((atp = AsnReadId(asn_in, asp->amp, atp)) != NULL && rval == 0) {
    if (atp != atp_srch) {
      AsnReadVal(asn_in, atp, &av);
      AsnWrite(asn_out, atp, &av);
      AsnKillValue(atp, &av);
      continue;
    }
    if ((sep = SeqEntryAsnRead(asn_in, atp)) == NULL) {
      Message (MSG_POSTERR, "SeqEntryAsnRead failure");
      rval = 1;
    } else  {
      entityID = ObjMgrRegister (OBJ_SEQENTRY, sep);
      ProcessSeqEntry (sep, gap_sizes);

      if (! SeqEntryAsnWrite(sep, asn_out, atp)) {
       Message (MSG_POSTERR, "SeqEntryAsnWrite failure");
       rval = 1;
      }
      AsnIoFlush(asn_out);
      ObjMgrFreeByEntityID (entityID);
    }
  }                             /* Endwhile, AsnReadId */

  AsnIoClose(asn_in);
  if (asn_out != osp->aip) {
    AsnIoClose(asn_out);
  }
  
  return rval;
}
예제 #4
0
파일: vsmfile.c 프로젝트: hsptools/hsp-wrap
static Boolean SaveSetsInOneSelectedSet (GatherObjectPtr gop)

{
  SetSavePtr      ssp;
  SelStructPtr    sel;
  BioseqSetPtr    bssp;
  CharPtr         filename, file_fmt = "%s_%d.sqn";
  SeqEntryPtr     sep;
  AsnIoPtr        aip;
#ifdef WIN_MAC
  FILE *fp;
#endif

  if (gop == NULL || gop->dataptr == NULL || gop->itemtype != OBJ_BIOSEQSET) return TRUE;
  ssp = (SetSavePtr) gop->userdata;
  if (ssp == NULL || StringHasNoText (ssp->file_base)) return TRUE;

  sel = ssp->sel;
  while (sel != NULL 
         && (sel->entityID != gop->entityID 
             || sel->itemtype != gop->itemtype 
             || sel->itemID != gop->itemID))
  {
    sel = sel->next;
  }

  if (sel == NULL) return TRUE;

  bssp = gop->dataptr;
   
  filename = (CharPtr) MemNew (sizeof (Char) + (StringLen (ssp->file_base) + StringLen (file_fmt) + 15));
  for (sep = bssp->seq_set; sep != NULL; sep = sep->next) {
    sprintf (filename, file_fmt, ssp->file_base, ssp->file_num);
    ssp->file_num++;
#ifdef WIN_MAC
        fp = FileOpen (filename, "r");
        if (fp != NULL) {
            FileClose (fp);
        } else {
            FileCreate (filename, "TEXT", "ttxt");
        }
#endif

        aip = AsnIoOpen(filename, "w");
        SeqEntryAsnWrite (sep, aip, NULL);
    AsnIoClose (aip);
  }
  filename = MemFree (filename);
  return TRUE;
}
예제 #5
0
static void WriteOneFile (
  OutputStreamPtr osp,
  SeqEntryPtr sep
)

{
  AsnIoPtr   aip;

  aip = AsnIoFromOutputStream (osp);
  if (aip != NULL) {
    SeqEntryAsnWrite (sep, aip, NULL);
    AsnIoFlush (aip);
  }
  if (aip != osp->aip) {
    AsnIoClose (aip);
  }
}
예제 #6
0
파일: sgd2asn.c 프로젝트: hsptools/hsp-wrap
static void WriteOneFile (
  CharPtr directory,
  CharPtr base,
  CharPtr suffix,
  SeqEntryPtr sep
)

{
  AsnIoPtr  aip;
  Char      file [FILENAME_MAX], path [PATH_MAX];

  StringNCpy_0 (path, directory, sizeof (path));
  sprintf (file, "%s.%s", base, suffix);
  FileBuildPath (path, NULL, file);

  aip = AsnIoOpen (path, "w");
  if (aip == NULL) return;

  SeqEntryAsnWrite (sep, aip, NULL);

  AsnIoFlush (aip);
  AsnIoClose (aip);
}
예제 #7
0
파일: gi2accn.c 프로젝트: hsptools/hsp-wrap
Int2 Main (void)

{
  AsnIoPtr     aip;
  Boolean      convertgis;
  Boolean      extractmrnas;
  CharPtr      infile, outfile;
  SeqEntryPtr  sep;

  /* standard setup */

  ErrSetFatalLevel (SEV_MAX);
  ErrClearOptFlags (EO_SHOW_USERSTR);
  UseLocalAsnloadDataAndErrMsg ();
  ErrPathReset ();

  /* finish resolving internal connections in ASN.1 parse tables */

  if (! AllObjLoad ()) {
    Message (MSG_FATAL, "AllObjLoad failed");
    return 1;
  }
  if (! SubmitAsnLoad ()) {
    Message (MSG_FATAL, "SubmitAsnLoad failed");
    return 1;
  }
  if (! FeatDefSetLoad ()) {
    Message (MSG_FATAL, "FeatDefSetLoad failed");
    return 1;
  }
  if (! SeqCodeSetLoad ()) {
    Message (MSG_FATAL, "SeqCodeSetLoad failed");
    return 1;
  }
  if (! GeneticCodeTableLoad ()) {
    Message (MSG_FATAL, "GeneticCodeTableLoad failed");
    return 1;
  }

  /* process command line arguments */

  if (! GetArgs ("gi2accn", sizeof (myargs) / sizeof (Args), myargs)) {
    return 0;
  }

  infile = (CharPtr) myargs [i_argInputFile].strvalue;
  outfile = (CharPtr) myargs [o_argOutputFile].strvalue;
  convertgis = (Boolean) myargs [c_argConvertGIs].intvalue;
  extractmrnas = (Boolean) myargs [x_argExtractMrnas].intvalue;

  aip = AsnIoOpen (infile, "r");
  if (aip == NULL) {
    Message (MSG_FATAL, "AsnIoOpen input file failed");
    return 1;
  }

  sep = SeqEntryAsnRead (aip, NULL);
  AsnIoClose (aip);
  if (sep == NULL) {
    Message (MSG_FATAL, "SeqEntryAsnRead failed");
    return 1;
  }

  PubSeqFetchEnable ();

  LookupFarSeqIDs (sep, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE);

  /* Extract mRNA hits from BLAST against nr */

  if (extractmrnas) {
    VisitAnnotsInSep (sep, NULL, FindBlastNR);
    DeleteMarkedObjects (0, OBJ_SEQENTRY, (Pointer) sep);
  }

  /* convert gi numbers to accession.version */

  if (convertgis) {
    VisitAlignmentsInSep (sep, NULL, UpdateAligns);
  }

  PubSeqFetchDisable ();

  BasicSeqEntryCleanup (sep);

  aip = AsnIoOpen (outfile, "w");
  if (aip == NULL) {
    Message (MSG_FATAL, "AsnIoOpen output file failed");
    return 1;
  }

  SeqEntryAsnWrite (sep, aip, NULL);
  AsnIoClose (aip);

  return 0;
}
예제 #8
0
static void DoRecord (SeqEntryPtr sep, Pointer userdata)

{
  BioseqPtr     bsp;
  SeqEntryPtr   nsep;
  ScanDataPtr   sdp;

  sdp = (ScanDataPtr) userdata;
  (sdp->recordCount)++;

  nsep = FindNthBioseq (sep, 1);
  if (nsep != NULL && IS_Bioseq (nsep)) {
    bsp = (BioseqPtr) nsep->data.ptrvalue;
    if (bsp != NULL) {
      SeqIdWrite (bsp->id, sdp->buf, PRINTID_FASTA_LONG, sizeof (sdp->buf));
    }
  }
#ifdef OS_UNIX
  /* printf ("%s\n", sdp->buf); */
#endif

  VisitPubdescsInSep (sep, (Pointer) sdp, DoThesis);

  /* check for 'genomic DNA' in DoTitle suppressed for bulk submissions */

  sdp->bulk = FALSE;
  VisitDescriptorsInSep (sep, (Pointer) sdp, LookForBulk);
  VisitDescriptorsInSep (sep, (Pointer) sdp, DoTitle);

  VisitFeaturesInSep (sep, (Pointer) sdp, DoImpCDSandTrna);

  /* index for pseudo cds, impfeat peptides codon frame */

  SeqMgrIndexFeatures (0, sep->data.ptrvalue);

  VisitFeaturesInSep (sep, (Pointer) sdp, DoPseudoCDS);

  VisitFeaturesInSep (sep, (Pointer) sdp, DoPeptide);

  /* now cleanup, index for overlapping peptides */

  SeriousSeqEntryCleanup (sep, NULL, NULL);
  SeqMgrIndexFeatures (0, sep->data.ptrvalue);

  VisitBioseqsInSep (sep, (Pointer) sdp, DoProteins);

  /*
  VisitBioseqsInSep (sep, (Pointer) sdp, DoGraphs);
  */

#if 0
  {
    Boolean  hasUser = FALSE;

    VisitFeaturesInSep (sep, (Pointer) &hasUser, DoUser);

    if (hasUser && sdp->aop != NULL && sdp->atp_se != NULL) {
      SeqEntryAsnWrite (sep, sdp->aop, sdp->atp_se);
    }
  }
#endif
}
예제 #9
0
파일: qbatch.c 프로젝트: hsptools/hsp-wrap
void BEPrintIds(BEDataPtr pBdata, Uint4 *ids, int count)
{
    Int4 i;
    SeqEntryPtr sep, sep_all;
    Boolean retvalue = TRUE;
    SeqIdPtr sip = NULL;
    BioseqPtr bsp;
    BGenBankPtr bgbp;
    AsnIoPtr aip;
    Boolean is_na = FALSE;
    
    if(pBdata->format == F_GILIST) {
        for(i = 0; i < count; i++)
            fprintf(stdout, "%d\n", (int) ids[i]);
        return;
    }
    
    if(pBdata->database == 0)
        is_na = TRUE;

    if(pBdata->format == F_ASN1_GENB)
        bgbp = BGenBankInit();
    
    for(i = 0; i < count; i++) {
        
        sep_all = BESeqEntryGet(ids[i]);
        
        if(sep_all == NULL) {
            ErrPostEx(SEV_ERROR, 88, 67, "Retrieving of blob for the "
                      "gi=%d failed", (int)ids[i]);
            continue;
        }
        
        if(!pBdata->allset) {
            ObjMgrRegister(OBJ_SEQENTRY, sep_all);
            sip = ValNodeNew(NULL);
            sip->choice = SEQID_GI;
            sip->data.intvalue = ids[i];
            
            if((bsp = BioseqFind(sip)) == NULL) {
                ErrPostEx(SEV_ERROR, 88, 67, 
                          "Error finding bioseq for gi=%d\n", (int)ids[i]);
                continue;
            }

            sep = SeqEntryNew();
            sep->choice = 1; /* Bioseq */
            sep->data.ptrvalue = bsp;
        } else {
            sep = sep_all;
        }
        
        switch(pBdata->format) {        
        case F_FASTA:               /* 1 */

            if(!SeqEntryToFasta(sep, stdout, is_na)) {
                if(!SeqEntryToFasta(sep, stdout, !is_na)) {
                    ErrPostEx(SEV_ERROR, 88, 67, "Printing of FASTA format "
                              "(gi=%d) failed\r\n", (int)ids[i]);
                }
            }

            break;
        case F_ASN1:                /* 2 */

            aip = AsnIoNew(ASNIO_TEXT_OUT, stdout, NULL, NULL, NULL);
            SeqEntryAsnWrite(sep, aip, NULL);
            AsnIoClose(aip);

            break;
        case F_GILIST:              /* 3 */
            for(i = 0; i < count; i++)
                fprintf(stdout, "%d\n", (int) ids[i]);
            break;
        case F_DLIST:               /* 4 */
            if (IS_Bioseq(sep))
                retvalue = SeqEntrysToDefline(sep, stdout, is_na, 3);
            else
                retvalue = SeqEntrysToDefline(sep, stdout, is_na, 0);
            
            if(retvalue == FALSE) {
                if (IS_Bioseq(sep))
                    retvalue = SeqEntrysToDefline(sep, stdout, !is_na, 3);
                else
                    retvalue = SeqEntrysToDefline(sep, stdout, !is_na, 0);
            }
            break;
        case F_ASN1_GENB:           /* 5 */
            retvalue = SeqEntryAsnWrite(sep, bgbp->aip, bgbp->atp);
            break;
        default:
        case F_GEN:                 /* 0 */
            if(!SeqEntryToFlatEx(sep_all, stdout, 
                                 is_na ? GENBANK_FMT : GENPEPT_FMT, 
                                 RELEASE_MODE, sip, FF_REGULAR)) {
                
                if(!SeqEntryToFlatEx(sep_all, stdout, 
                                     is_na ? GENPEPT_FMT : GENBANK_FMT, 
                                     RELEASE_MODE, sip, FF_REGULAR)) {
                }
            }
            break;
        }
        
        SeqEntryFree(sep_all);
        ValNodeFree(sip);
    }

    if(pBdata->format == F_ASN1_GENB)
        BGenBankClose(bgbp);
    
    return;
}
예제 #10
0
static void ProcessSingleRecord (
  CharPtr filename,
  CSpeedFlagPtr cfp
)

{
  AsnIoPtr      aip;
  BioseqPtr     bsp;
  ValNodePtr    bsplist = NULL;
  BioseqSetPtr  bssp;
  Pointer       dataptr = NULL;
  Uint2         datatype, entityID = 0;
  FileCache     fc;
  FILE          *fp;
  Int1          iotype;
  Char          line [512];
  Int4          maxio = 1;
  SeqEntryPtr   sep;
  time_t        starttime, stoptime, worsttime;
  CharPtr       str;
  Int4          x;

  if (cfp == NULL) return;

  if (StringHasNoText (filename)) return;

  if (StringChr (cfp->io, 'r') != NULL) {
    maxio = cfp->maxcount;
  }

  starttime = GetSecs ();

  for (x = 0; x < maxio; x++) {
    if (entityID != 0) {
      ObjMgrFreeByEntityID (entityID);
      entityID = 0;
      dataptr = NULL;
    }

    if (cfp->type == 1) {

      fp = FileOpen (filename, "r");
      if (fp == NULL) {
        Message (MSG_POSTERR, "Failed to open '%s'", filename);
        return;
      }

      dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, FALSE, FALSE);

      FileClose (fp);

      entityID = ObjMgrRegister (datatype, dataptr);

    } else if (cfp->type >= 2 && cfp->type <= 5) {

      aip = AsnIoOpen (filename, cfp->binary? "rb" : "r");
      if (aip == NULL) {
        Message (MSG_POSTERR, "AsnIoOpen failed for input file '%s'", filename);
        return;
      }

      switch (cfp->type) {
        case 2 :
          dataptr = (Pointer) SeqEntryAsnRead (aip, NULL);
          datatype = OBJ_SEQENTRY;
          break;
        case 3 :
          dataptr = (Pointer) BioseqAsnRead (aip, NULL);
          datatype = OBJ_BIOSEQ;
          break;
        case 4 :
          dataptr = (Pointer) BioseqSetAsnRead (aip, NULL);
          datatype = OBJ_BIOSEQSET;
          break;
        case 5 :
          dataptr = (Pointer) SeqSubmitAsnRead (aip, NULL);
          datatype = OBJ_SEQSUB;
          break;
        default :
          break;
      }

      AsnIoClose (aip);

      entityID = ObjMgrRegister (datatype, dataptr);

    } else if (cfp->type == 6) {

      fp = FileOpen (filename, "r");
      if (fp == NULL) {
        Message (MSG_POSTERR, "Failed to open '%s'", filename);
        return;
      }

      dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, FALSE, FALSE);

      FileClose (fp);

      entityID = ObjMgrRegister (datatype, dataptr);

    } else if (cfp->type == 7) {

      fp = FileOpen (filename, "r");
      if (fp == NULL) {
        Message (MSG_POSTERR, "Failed to open '%s'", filename);
        return;
      }

      FileCacheSetup (&fc, fp);

      str = FileCacheReadLine (&fc, line, sizeof (line), NULL);
      while (str != NULL) {
        str = FileCacheReadLine (&fc, line, sizeof (line), NULL);
      }

      FileClose (fp);

      return;

    } else {
      Message (MSG_POSTERR, "Input format type '%d' unrecognized", (int) cfp->type);
      return;
    }
  }

  if (entityID < 1 || dataptr == NULL) {
    Message (MSG_POSTERR, "Data read failed for input file '%s'", filename);
    return;
  }

  if (datatype == OBJ_SEQSUB || datatype == OBJ_SEQENTRY ||
        datatype == OBJ_BIOSEQ || datatype == OBJ_BIOSEQSET) {

    stoptime = GetSecs ();
    worsttime = stoptime - starttime;
    if (cfp->logfp != NULL) {
      fprintf (cfp->logfp, "ASN reading time %ld seconds\n", (long) worsttime);
      fflush (cfp->logfp);
    }

    sep = GetTopSeqEntryForEntityID (entityID);

    if (sep == NULL) {
      sep = SeqEntryNew ();
      if (sep != NULL) {
        if (datatype == OBJ_BIOSEQ) {
          bsp = (BioseqPtr) dataptr;
          sep->choice = 1;
          sep->data.ptrvalue = bsp;
          SeqMgrSeqEntry (SM_BIOSEQ, (Pointer) bsp, sep);
        } else if (datatype == OBJ_BIOSEQSET) {
          bssp = (BioseqSetPtr) dataptr;
          sep->choice = 2;
          sep->data.ptrvalue = bssp;
          SeqMgrSeqEntry (SM_BIOSEQSET, (Pointer) bssp, sep);
        } else {
          sep = SeqEntryFree (sep);
        }
      }
      sep = GetTopSeqEntryForEntityID (entityID);
    }

    if (sep != NULL) {

      if (cfp->lock) {
        starttime = GetSecs ();

        bsplist = LockFarComponents (sep);

        stoptime = GetSecs ();
        worsttime = stoptime - starttime;
        if (cfp->logfp != NULL) {
          fprintf (cfp->logfp, "Far component locking time %ld seconds\n", (long) worsttime);
          fflush (cfp->logfp);
        }
      }

      if (StringChr (cfp->io, 'w') != NULL) {
        starttime = GetSecs ();

        iotype = ASNIO_TEXT_OUT;
        if (StringChr (cfp->io, 'b') != NULL) {
          iotype = ASNIO_BIN_OUT;
        }

        for (x = 0; x < cfp->maxcount; x++) {
          aip = AsnIoNew (iotype, cfp->ofp, NULL, NULL, NULL);
          if (aip != NULL) {
            SeqEntryAsnWrite (sep, aip, NULL);
            AsnIoFree (aip, FALSE);
          }
        }

        stoptime = GetSecs ();
        worsttime = stoptime - starttime;
        if (cfp->logfp != NULL) {
          fprintf (cfp->logfp, "ASN writing time %ld seconds\n", (long) worsttime);
          fflush (cfp->logfp);
        }
      }

      starttime = GetSecs ();

      for (x = 0; x < cfp->maxcount; x++) {
        DoProcess (sep, entityID, cfp);
      }

      stoptime = GetSecs ();
      worsttime = stoptime - starttime;
      if (cfp->logfp != NULL) {
        fprintf (cfp->logfp, "Internal processing time %ld seconds\n", (long) worsttime);
        fflush (cfp->logfp);
      }

      ObjMgrFreeByEntityID (entityID);

      bsplist = UnlockFarComponents (bsplist);
    }

  } else {

    Message (MSG_POSTERR, "Datatype %d not recognized", (int) datatype);
  }
}