Ejemplo n.º 1
0
void Parser::applyState(
	std::stack<std::string> &frameStack,
	std::stack<std::string> &pStack, std::list<int> &output,
	int &s, std::string &a, int &ip, int &ip2, std::string &action
	)
{
	//=========================================================
	action = popStack(frameStack);
	ip2 = val(popStack(frameStack));
	ip = val(popStack(frameStack));
	a = popStack(frameStack);
	s = val(popStack(frameStack));
	//=========================================================
	writeSeq(output, popStack(frameStack));
	writeSeq(pStack, popStack(frameStack));
	//=========================================================
	this->pushState(frameStack, pStack, output, s, a, ip, ip2, action);
}
Ejemplo n.º 2
0
Archivo: readseq.c Proyecto: CPFL/gmeme
extern int main( int argc, char *argv[])
#endif
{
boolean   closein = false;
short     ifile, format, err = 0, seqtype = kDNA,
          nlines=0, seqout = 0, phylvers = 2;
long      atseq, nseq, i, skiplines, seqlen, seqlen0=0; /* changed atseq and nseq from short to long - James J (UQ) */
unsigned long  checksum= 0, checkall= 0;
char      *seq=NULL, *cp=NULL, *firstseq = NULL, *seqlist=NULL,
	    *progname;
/*char	  tempname[] = "readseqXXXXXX";*/
char	  *tempname = NULL;
char      seqid[MAXLINE], *seqidptr = seqid;
char      stempstore[256], *stemp = stempstore;
FILE      *ftmp=NULL, *fin=NULL, *fout=NULL;
long      outindexmax= 0, noutindex= 0, *outindex = NULL;

#define exit_main(err) {        \
  if (closeout) fclose(fout);   \
  if (closein) fclose(fin);   \
  if (tempname != NULL) remove(tempname);  \
  Exit(err); }

#define indexout()  if (interleaved) {\
  if (noutindex>=outindexmax) {\
    outindexmax= noutindex + 20;\
    outindex= (long*) realloc(outindex, sizeof(long)*outindexmax);\
    if (outindex==NULL) { err= eMemFull; erralert(err); exit_main(err); }\
    }\
  outindex[noutindex++]= ftell(fout);\
  }


  resetGlobals();
  foo = stdout;
  progname = argv[0];
  *oname = 0;
  /* initialize gPretty ?? -- done in header */

  for (i=1; i < argc; i++) {
    err= readopt( argv[i]);
    if (err <= 0) exit_main(err);
    }

                            /* pipe input from stdin !? */
  if (dopipe && !gotinputfile) {
    int c;
    int fd;
	/*
    tmpnam(tempname);
    inputfile = tempname;
    ftmp = fopen( inputfile, "w");
	*/
    tempname = strdup("readseqXXXXXX");
    fd = mkstemp(tempname);
    if (fd == -1) { erralert(eFileCreate); exit_main(eFileCreate); }
    strcpy(inputfile, tempname);
    while ((c = getc(stdin)) != EOF) write(fd, &c, 1);
    close(fd);
    gotinputfile= true;
    }

  quietly = (dopipe || (gotinputfile && (listonly || whichSeq != 0)));

  if (verbose || (!quietly && !gotinputfile)) fprintf( stderr, "%s", title);
  ifile = 1;

                            /* UI: Choose output */
  if (askout && !closeout && !quietly) {
    askout = false;
    fprintf(stderr,"\nName of output file (?=help, defaults to display): \n");
    fgets(oname= onamestore, 128, stdin);
    skipwhitespace(oname);
    if (*oname == '?') { usage(); exit_main(0); }
    else if (*oname != 0) {
      closeout = true;
      foo = fopen( oname, "w");
      if (!foo) { erralert(eFileCreate); exit_main(eFileCreate); }
      }
    }

  fout = foo;
  if (outform == kNoformat) outform = chooseFormat(quietly);

                          /* set up formats ... */
  switch ((int) outform) {
    case kPhylip2:
      interleaved= false;
      phylvers = 2;
      outform = kPhylip;
      break;

    case kPhylip4:
      interleaved= true;
      phylvers = 4;
      outform = kPhylip;
      break;

    case kMSF:
    case kPAUP:
      interleaved= true;
      break;

    case kPretty:
      gPretty.isactive= true;
      interleaved= true;
      break;

    }

  if (gPretty.isactive && gPretty.noleaves) interleaved= false;
  if (interleaved) {
    fout = ftmp = tmpfile();
    outindexmax= 30; noutindex= 0;
    outindex = (long*) malloc(outindexmax*sizeof(long));
    if (outindex==NULL) { err= eMemFull; erralert(err); exit_main(err); }
    }

                        /* big loop over all input files */
  do {
                        /* select next input file */
    /*gotinputfile = (*tempname != 0);*/
    gotinputfile = (tempname != NULL);
    while ((ifile < argc) && (!gotinputfile)) {
      if (*argv[ifile] != '-') {
        strcpy( inputfile, argv[ifile]);
        gotinputfile = (*inputfile != 0);
        --nfile;
        }
      ifile++;
      }

    while (!gotinputfile) {
      fprintf(stderr,"\nName an input sequence or -option: \n");
      inputfile= inputfilestore;

      fgets(stemp= stempstore, 256, stdin);
      if (*stemp==0) goto fini;  /* !! need this to finish work during interactive use */
      stemp= strtok(stempstore, " \n\r\t");
      while (stemp) {
        err= readopt( stemp); /* will read inputfile if it exists */
        if (err<0) exit_main(err);
        stemp= strtok( NULL, " \n\r\t");
        }
      }
              /* thanks to [email protected] for this PHYLIP3 fix: */
              /* head for end (interleave if needed) */
    if (*inputfile == 0) break;

    format = seqFileFormat( inputfile, &skiplines, &err);

    if (err == 0)  {
#ifdef NCBI
      if (format == kASNseqentry || format == kASNseqset)
        seqlist = listASNSeqs( inputfile, skiplines, format, &nseq, &err);
      else
#endif
        seqlist = listSeqs( inputfile, skiplines, format, &nseq, &err);
      }

    if (err != 0)
      erralert(err);

    else if (listonly) {
      dumpSeqList(seqlist,format);
      free( seqlist);
      }

    else {
                                /* choose whichSeq if needed */
      if (nseq == 1 || chooseall || (quietly && whichSeq == 0)) {
        chooseall= true;
        whichSeq = 1;
        quietly = true; /* no loop */
        }
      else if (whichSeq > nseq && quietly) {
        erralert(eItemNotFound);
        err= eItemNotFound;
        }
      else if (whichSeq > nseq || !quietly) {
        dumpSeqList(seqlist, format);
        fprintf(stderr,"\nChoose a sequence (# or All): \n");
        fgets(stemp= stempstore, 256, stdin);
        skipwhitespace(stemp);
        if (to_lower(*stemp) == 'a') {
          chooseall= true;
          whichSeq = 1;
          quietly = true; /* !? this means we don't ask for another file 
                            as well as no more whichSeqs... */
          }
        else if (isdigit((int) *stemp)) whichSeq= atol(stemp);
        else whichSeq= 1; /* default */
        }
      free( seqlist);

      if (false /*chooseall*/) {  /* this isn't debugged yet...*/
        fin = fopen(inputfile, "r");
        closein= true;
        }

      while (whichSeq > 0 && whichSeq <= nseq) {
                                /* need to open multiple output files ? */
        manyout = ((chooseall || nwhichlist>1) && nseq > 1
                  && (outform == kPlain || outform == kGCG));
        if (manyout) {
          if ( whichSeq == 1 ) erralert(eOneFormat);
          else if (closeout) {
            sprintf( stemp,"%s_%ld", oname, whichSeq);
            freopen( stemp, "w", fout);
            fprintf( stderr,"Writing sequence %ld to file %s\n", 
	      whichSeq, stemp);
            }
          }

        if (closein) {
          /* !! this fails... skips most seqs... */
          /* !! in sequential read, must count seqs already read from whichSeq ... */
          /* need major revision of ureadseq before we can do this */
          atseq= whichSeq-1;
          seqidptr= seqid;
          seq = readSeqFp( whichSeq, fin, skiplines, format,
                          &seqlen, &atseq, &err, seqidptr);
          skiplines= 0;
          }
        else {
          atseq= 0;
          seqidptr= seqid;
#ifdef NCBI
          if (format == kASNseqentry || format == kASNseqset) {
            seqidptr= NULL;
            seq = readASNSeq( whichSeq, inputfile, skiplines, format,
                     &seqlen, &atseq, &err, &seqidptr);
            }
          else
#endif
          seq = readSeq( whichSeq, inputfile, skiplines, format,
                          &seqlen, &atseq, &err, seqidptr);
          }


        if (gPretty.degap) {
          char *newseq;
          long newlen;
          newseq= compressSeq( gPretty.gapchar, seq, seqlen, &newlen);
          if (newseq) {
            free(seq); seq= newseq; seqlen= newlen;
            }
          }

        if (outform == kMSF) checksum= GCGchecksum(seq, seqlen, &checkall);
        else if (verbose) checksum= seqchecksum(seq, seqlen, &checkall);
        if (verbose)
          fprintf( stderr, 
	        "Sequence %ld, length= %ld, checksum= %X, format= %s, id= %s\n",
                whichSeq, seqlen, (int)checksum, formatstr(format), seqidptr);

        if (err != 0) erralert(err);
        else {
                                  /* format fixes that writeseq doesn't do */
          switch ((int) outform) {
            case kPIR:
              if (seqout == 0) fprintf( foo,"\\\\\\\n");
              break;
            case kASN1:
              if (seqout == 0) fprintf( foo, "%s", kASN1headline);
              break;

            case kPhylip:
              if (seqout == 0) {
                if (!interleaved) {  /*  bug, nseq is for 1st infile only */
                  if (chooseall) i= nseq; else i=1;
                  if (phylvers >= 4) fprintf(foo," %ld %ld\n", i, seqlen);
                  else fprintf(foo," %ld %ld YF\n", i, seqlen);
                  }
                seqlen0 = seqlen;
                }
              else if (seqlen != seqlen0) {
                erralert(eUnequalSize);
                if (seqlen < seqlen0) seq = (char *)realloc(seq, seqlen0);
                for (i=seqlen; i<seqlen0; i++) seq[i]= gPretty.gapchar;
                seqlen = seqlen0;
                seq[seqlen] = 0; 
                }
              break;

            case kPAUP:
              if (seqout == 0) {
                seqtype= getseqtype(seq, seqlen);
                seqlen0 = seqlen;
                }
              else if (seqlen != seqlen0) {
                erralert(eUnequalSize);
                if (seqlen < seqlen0) seq = (char *)realloc(seq, seqlen0); 
                for (i=seqlen; i<seqlen0; i++) seq[i]= gPretty.gapchar;
                seqlen = seqlen0;
                seq[seqlen] = 0; 
                }
              break;

            }

          if (doupper)
            for (i = 0; i<seqlen; i++) seq[i] = to_upper(seq[i]);
          else if (dolower)
            for (i = 0; i<seqlen; i++) seq[i] = to_lower(seq[i]);

          if (doreverse) {
            long  j, k;
            char  ctemp;
            for (j=0, k=seqlen-1; j <= k; j++, k--) {
              ctemp = compl[seq[j] - ' '];
              seq[j] = compl[seq[k] - ' '];
              seq[k] = ctemp;
              }
            }

          if ((gPretty.isactive || outform==kPAUP) && gPretty.domatch && firstseq != NULL) {
            for (i=0; i<seqlen; i++)
              if (seq[i]==firstseq[i]) seq[i]= gPretty.matchchar;
            }


          if (gPretty.isactive && gPretty.numtop && seqout == 0) {
            gPretty.numline = 1;
            indexout();
            (void) writeSeq( fout, seq, seqlen, outform, seqidptr);
            gPretty.numline = 2;
            indexout();
            (void) writeSeq( fout, seq, seqlen, outform, seqidptr);
            gPretty.numline = 0;
            }

          indexout();
          nlines = writeSeq( fout, seq, seqlen, outform, seqidptr);
          seqout++;
          }

        if ((gPretty.isactive || outform==kPAUP) && gPretty.domatch && firstseq == NULL) {
          firstseq= seq;
          seq = NULL;
          }
        else if (seq!=NULL) { free(seq); seq = NULL; }

#ifdef NCBI
       if ( (format == kASNseqentry || format == kASNseqset)
          && seqidptr && seqidptr!= seqid)
            free(seqidptr);
#endif
        if (chooseall) whichSeq++;
        else if (iwhichlist<nwhichlist) whichSeq= whichlist[iwhichlist++];
        else whichSeq= 0;
        }
      if (closein) { fclose(fin); closein= false; }
      }
    whichSeq  = 0;
  } while (nfile > 0 || !quietly);


fini:
  if (firstseq) { free(firstseq); firstseq= NULL; }
  if (err || listonly) exit_main(err);

  if (gPretty.isactive && gPretty.numbot) {
    gPretty.numline = 2;
    indexout();
    (void) writeSeq( fout, seq, seqlen, outform, seqidptr);
    gPretty.numline = 1;
    indexout();
    (void) writeSeq( fout, seq, seqlen, outform, seqidptr);
    gPretty.numline = 0;
    }

  if (outform == kMSF) {
    if (*oname) cp= oname; else cp= inputfile;
    fprintf(foo,
      "\n %s  MSF: %ld  Type: N  January 01, 1776  12:00  Check: %ld ..\n\n",
                  cp, seqlen, checkall);
    }

  if (outform == kPAUP) {
    fprintf(foo,"#NEXUS\n");
    if (*oname) cp= oname; else cp= inputfile;
    fprintf(foo,"[%s -- data title]\n\n", cp);
    /* ! now have header lines for each sequence... put them before "begin data;... */
    }

  if (outform==kPhylip && interleaved) {
    if (phylvers >= 4) fprintf(foo," %d %ld\n", seqout, seqlen);
    else fprintf(foo," %d %ld YF\n", seqout, seqlen);
    }

  if (interleaved) {
    /* interleave species lines in true output */
    /* nlines is # lines / sequence */
    short iline, j, leaf, iseq;
    char  *s = stempstore;

    indexout();  noutindex--; /* mark eof */

    for (leaf=0; leaf<nlines; leaf++) {
      if (outform == kMSF && leaf == 1) {
        fputs("//\n\n", foo);
        }
      if (outform == kPAUP && leaf==1) {
        switch (seqtype) {
          case kDNA     : cp= "dna"; break;
          case kRNA     : cp= "rna"; break;
          case kNucleic : cp= "dna"; break;
          case kAmino   : cp= "protein"; break;
          case kOtherSeq: cp= "dna"; break;
          }
        fprintf(foo,"\nbegin data;\n");
        fprintf(foo," dimensions ntax=%d nchar=%ld;\n", seqout, seqlen);
        fprintf(foo," format datatype=%s interleave missing=%c", cp, gPretty.gapchar);
        if (gPretty.domatch) fprintf(foo," matchchar=%c", gPretty.matchchar);
        fprintf(foo,";\n  matrix\n");
        }

      for (iseq=0; iseq<noutindex; iseq++) {
        fseek(ftmp, outindex[iseq], 0);
        for (iline=0; iline<=leaf; iline++)
          if (!fgets(s, 256, ftmp)) *s= 0;
        if (ftell(ftmp) <= outindex[iseq+1])
          fputs( s, foo);
        }

      for (j=0; j<gPretty.interline; j++)
        fputs( "\n", foo);  /* some want spacer line */
      }
    fclose(ftmp); /* tmp disappears */
    fout= foo;
    }

  if (outform == kASN1)  fprintf( foo, "} }\n");
  if (outform == kPAUP)  fprintf( foo,";\n  end;\n");

  if (outindex != NULL) free(outindex);
  exit_main(0);
}