Example #1
0
void setup_environment(Char *argv[], boolean *canbeplotted)
{
  boolean firsttree;
  char* treestr;

  /* Set up all kinds of fun stuff */
#ifdef MAC
  OSErr retcode;
  FInfo  fndrinfo;
  macsetup("Drawgram","Preview");
#endif
#ifdef TURBOC
  if ((registerbgidriver(EGAVGA_driver) <0) ||
      (registerbgidriver(Herc_driver) <0)   ||
      (registerbgidriver(CGA_driver) <0)){
    printf("Graphics error: %s ",grapherrormsg(graphresult()));
    exit(-1);}
#endif

  

 
  printf("DRAWGRAM from PHYLIP version %s\n",VERSION);
  printf("Reading tree ... \n");
  firsttree = true;
  treestr = ajStrGetuniquePtr(&phylotrees[0]->Tree); 
  allocate_nodep(&nodep, treestr, &spp);
  treeread (&treestr, &root, treenode, &goteof, &firsttree,
            nodep, &nextnode, &haslengths,
            &grbg, initdrawgramnode,true,-1);
  root->oldlen = 0.0;
  printf("Tree has been read.\n");
  printf("Loading the font .... \n");
  loadfont(font,argv[0]);
  printf("Font loaded.\n");
  previewing = false;
  ansi = ANSICRT;
  ibmpc = IBMCRT;
  firstscreens = true;
  initialparms();
  (*canbeplotted) = false;
}  /* setup_environment */
Example #2
0
void maketree()
{
  /* constructs a binary tree from the pointers in treenode.
     adds each node at location which yields highest "likelihood"
     then rearranges the tree for greatest "likelihood" */
  long i, j, nextnode;
  double gotlike;
  node *item, *nufork, *dummy, *p;
  char *treestr;

  fullset = (1L << (bits + 1)) - (1L << 1);
  if (!usertree) {
    for (i = 1; i <= (spp); i++)
      enterorder[i - 1] = i;
    if (jumble)
      randumize(seed, enterorder);
    root = treenode[enterorder[0] - 1];
    add(treenode[enterorder[0] - 1], treenode[enterorder[1] - 1],
        treenode[spp], &root, treenode);
    if (progress) {
      printf("Adding species:\n");
      writename(0, 2, enterorder);
#ifdef WIN32
      phyFillScreenColor();
#endif
    }
    lastrearr = false;
    for (i = 3; i <= (spp); i++) {
      bestyet = -350.0 * spp * chars;
      item = treenode[enterorder[i - 1] - 1];
      nufork = treenode[spp + i - 2];
      addpreorder(root, item, nufork);
      add(there, item, nufork, &root, treenode);
      like = bestyet;
      rearrange(&root);
      if (progress) {
        writename(i - 1, 1, enterorder);
#ifdef WIN32
        phyFillScreenColor();
#endif
      }
      lastrearr = (i == spp);
      if (lastrearr) {
        if (progress) {
          printf("\nDoing global rearrangements\n");
          printf("  !");
          for (j = 1; j <= (nonodes); j++)
            if ( j % (( nonodes / 72 ) + 1 ) == 0 )
              putchar('-');
          printf("!\n");
#ifdef WIN32
          phyFillScreenColor();
#endif
        }
        bestlike = bestyet;
        if (jumb == 1) {
          bstlike2 = bestlike;
          nextree = 1;
        }
        do {
          if (progress)
            printf("   ");
          gotlike = bestlike;
          for (j = 0; j < (nonodes); j++) {
            bestyet = - 350.0 * spp * chars;
            item = treenode[j];
            if (item != root) {
              nufork = treenode[j]->back;
              re_move(&item, &nufork, &root, treenode);
              there = root;
              addpreorder(root, item, nufork);
              add(there, item, nufork, &root, treenode);
            }
            if (progress) {
              if ( j % (( nonodes / 72 ) + 1 ) == 0 )
                putchar('.');
              fflush(stdout);
            }
          }
          if (progress) {
            putchar('\n');
#ifdef WIN32
            phyFillScreenColor();
#endif

          }
        } while (bestlike > gotlike);
      }
    }
    if (progress)
      putchar('\n');
    for (i = spp - 1; i >= 1; i--)
      re_move(&treenode[i], &dummy, &root, treenode);
    if (jumb == njumble) {
      if (treeprint) {
        putc('\n', outfile);
        if (nextree == 2)
          fprintf(outfile, "One most parsimonious tree found:\n");
        else
          fprintf(outfile, "%6ld trees in all found\n", nextree - 1);
      }
      if (nextree > maxtrees + 1) {
        if (treeprint)
          fprintf(outfile, "here are the first%4ld of them\n", (long)maxtrees);
        nextree = maxtrees + 1;
      }
      if (treeprint)
        putc('\n', outfile);
      for (i = 0; i <= (nextree - 2); i++) {
        root = treenode[0];
        add(treenode[0], treenode[1], treenode[spp], &root, treenode);
        for (j = 3; j <= spp; j++) {
          add(treenode[bestrees[i].btree[j - 1] - 1], treenode[j - 1],
              treenode[spp + j - 2], &root, treenode);}
        evaluate(root);
        printree(1.0, treeprint, root);
        describe();
        for (j = 1; j < (spp); j++)
          re_move(&treenode[j], &dummy, &root, treenode);
      }
    }
  } else {
    
    if (numtrees > 2) {
      emboss_initseed(inseed, &inseed0, seed);
      printf("\n");
    }
    if (treeprint) {
      fprintf(outfile, "User-defined tree");
      if (numtrees > 1)
        putc('s', outfile);
      fprintf(outfile, ":\n");
    }
    names = (boolean *)Malloc(spp*sizeof(boolean));
    which = 1;
    firsttree = true;                       /**/
    nodep = NULL;                           /**/
    nextnode = 0;                           /**/
    haslengths = 0;                         /**/
    phirst = 0;                             /**/
    zeros = (long *)Malloc(chars*sizeof(long));         /**/
    for (i = 0; i < chars; i++)             /**/
      zeros[i] = 0;                         /**/
    while (which <= numtrees) {
      treestr = ajStrGetuniquePtr(&phylotrees[which-1]->Tree);
      treeread(&treestr, &root, treenode, &goteof, &firsttree,
                nodep, &nextnode, &haslengths,
                &grbg, initdollopnode,false,nonodes);

      for (i = spp; i < (nonodes); i++) {
        p = treenode[i];
        for (j = 1; j <= 3; j++) {
          p->stateone = (bitptr)Malloc(words*sizeof(long));
          p->statezero = (bitptr)Malloc(words*sizeof(long));
          p = p->next;
        }
      } /* debug: see comment at initdollopnode() */
      if (treeprint)
        fprintf(outfile, "\n\n");
      evaluate(root);
      printree(1.0, treeprint, root);
      describe();
      which++;
    }
    FClose(intree);
    fprintf(outfile, "\n\n");
    if (numtrees > 1 && chars > 1)
      standev(numtrees, minwhich, minsteps, nsteps, fsteps, seed);
    free(names);
  }
  if (jumb == njumble) {
    if (progress) {
      printf("Output written to file \"%s\"\n\n", outfilename);
      if (trout)
        printf("Trees also written onto file \"%s\"\n\n", outtreename);
    }
    if (ancseq)
      freegarbage(&garbage);
  }
}  /* maketree */
Example #3
0
AjBool ajReadlinePos(AjPFile file, AjPStr* Pdest, ajlong* Ppos)
{
    const char *cp;
    char *buff;
    ajint isize;
    ajint ilen;
    ajint jlen;
    ajint ipos;
    ajuint buffsize;
    size_t iread;
    const char* pnewline = NULL;
 
    MAJSTRDEL(Pdest);

    if(file->Buffsize)
        buffsize = file->Buffsize;
    else
        buffsize = ajFileValueBuffsize();

    if(!file->Buff)
      ajStrAssignResC(&file->Buff, buffsize, "");
    else if(buffsize > MAJSTRGETRES(file->Buff))
      ajStrSetRes(&file->Buff, buffsize);

    if(MAJSTRGETUSE(file->Buff) == 1)
      buff = MAJSTRGETPTR(file->Buff);
    else
      buff  = ajStrGetuniquePtr(&file->Buff);

    isize = MAJSTRGETRES(file->Buff);
    ilen  = 0;
    ipos  = 0;
    
    if(!file->fp)
	ajWarn("ajFileGets file not found");
    
    *Ppos = file->Filepos;

    while(buff)
    {
	if(file->End)
	{
	    ajStrAssignClear(Pdest);
	    ajDebug("at EOF: File already read to end %F\n", file);

	    return ajFalse;
	}
	

#ifndef __ppc__
        if(file->Readblock)
        {
            if(file->Blockpos >= file->Blocklen)
            {
                iread = fread(file->Readblock,
                              1, file->Blocksize,
                              file->fp);

                if(!iread && ferror(file->fp))
                    ajFatal("fread failed with error:%d '%s'",
                            ferror(file->fp), strerror(ferror(file->fp)));

                file->Blockpos = 0;
                file->Blocklen = iread;
                file->Readblock[iread] = '\0';
                /*ajDebug("++ fread %u Ppos:%Ld\n", iread, *Ppos);*/
             }

            if(file->Blockpos < file->Blocklen)
            {

                /* we know we have something in Readblock to process */

                pnewline = strchr(&file->Readblock[file->Blockpos], '\n');

                if(pnewline)
                    jlen = pnewline - &file->Readblock[file->Blockpos] + 1;
                else
                    jlen = file->Blocklen - file->Blockpos;

                /*ajDebug("ipos:%d jlen:%d pnewline:%p "
                          "Readblock:%p blockpos:%d blocklen:%d\n",
                          ipos, jlen, pnewline, file->Readblock,
                          file->Blockpos, file->Blocklen);*/
                memmove(&buff[ipos], &file->Readblock[file->Blockpos], jlen);
                buff[ipos+jlen]='\0';
                cp = &buff[ipos];
                file->Blockpos += jlen;
            }
            else
            {
                jlen = 0;
                cp = NULL;
            }
        }
        else
        {
            cp = fgets(&buff[ipos], isize, file->fp);
            jlen = strlen(&buff[ipos]);
        }
        
#else
	cp = ajSysFuncFgets(&buff[ipos], isize, file->fp);
	jlen = strlen(&buff[ipos]);
#endif

        if(!cp && !ipos)
	{
	    if(feof(file->fp))
	    {
		file->End = ajTrue;
		ajStrAssignClear(Pdest);
		ajDebug("EOF ajFileGetsL file %F\n", file);

		return ajFalse;
	    }
	    else
		ajFatal("Error reading from file '%S'\n", ajFileGetNameS(file));
	}

	ilen += jlen;
        file->Filepos += jlen;

	/*
	 ** We need to read again if:
	 ** We have read the entire buffer
	 ** and we don't have a newline at the end
	 ** (must be careful about that - we may just have read enough)
	 */

	if(((file->Readblock && !pnewline) ||(jlen == (isize-1))) &&
	   (buff[ilen-1] != '\n'))
	{
            MAJSTRSETVALIDLEN(&file->Buff, ilen); /* fix before resizing! */
	    ajStrSetResRound(&file->Buff, ilen+buffsize+1);
	    /*ajDebug("more to do: jlen: %d ipos: %d isize: %d ilen: %d "
		    "Size: %d\n",
		    jlen, ipos, isize, ilen, ajStrGetRes(file->Buff));*/
	    ipos += jlen;
	    buff = ajStrGetuniquePtr(&file->Buff);
	    isize = ajStrGetRes(file->Buff) - ipos;
	    /*ajDebug("expand to: ipos: %d isize: %d Size: %d\n",
              ipos, isize, ajStrGetRes(file>Buff));*/
	}
	else
	    buff = NULL;
    }
    
    MAJSTRSETVALIDLEN(&file->Buff, ilen);
    if (ajStrGetCharLast(file->Buff) != '\n')
    {
	/*ajDebug("Appending missing newline to '%S'\n", file->Buff);*/
	ajStrAppendK(&file->Buff, '\n');
    }
    ajStrAssignRef(Pdest, file->Buff);

/*
  if(file->Readblock)
        ajDebug("ajFileGetsL done blocklen:%d blockpos:%d readlen:%u\n",
                file->Blocklen, file->Blockpos, ajStrGetLen(file->Buff));
*/
    return ajTrue;
}
Example #4
0
ajint ajUserGet(AjPStr* pthis, const char* fmt, ...)
{
    AjPStr thys;
    const char *cp;
    char *buff;
    va_list args;
    ajint ipos;
    ajint isize;
    ajint ilen;
    ajint jlen;
    ajint fileBuffSize = ajFileValueBuffsize();

    va_start(args, fmt);
    ajFmtVError(fmt, args);
    va_end(args);

    if(ajFileValueRedirectStdin())
    {
	ajUser("(Standard input in use: using default)");
	ajStrAssignC(pthis, "");

	return ajStrGetLen(*pthis);
    }

    ajStrSetRes(pthis, fileBuffSize);
    buff  = ajStrGetuniquePtr(pthis);
    thys = *pthis;
    isize = ajStrGetRes(thys);
    ilen  = 0;
    ipos  = 0;
    

    /*ajDebug("ajUserGet buffer len: %d res: %d ptr: %x\n",
	     ajStrGetLen(thys), ajStrGetRes(thys), thys->Ptr);*/

    while(buff)
    {

#ifndef __ppc__
	cp = fgets(&buff[ipos], isize, stdin);
#else
	cp = ajSysFuncFgets(&buff[ipos], isize, stdin);
#endif

        if(!cp && !ipos)
	{
	    if(feof(stdin))
	    {
		ajErr("Unable to get reply from user - end of standard input");
		ajExitBad();
	    }
	    else
		ajFatal("Error reading from user: '******'\n",
			strerror(errno));
	}

	jlen = strlen(&buff[ipos]);
	ilen += jlen;

	/*
	 ** We need to read again if:
	 ** We have read the entire buffer
	 ** and we don't have a newline at the end
	 ** (must be careful about that - we may just have read enough)
	 */
	ajStrSetValidLen(pthis, ilen);
	thys = *pthis;

	if((jlen == (isize-1)) &&
	   (ajStrGetCharLast(thys) != '\n'))
	{
	    ajStrSetRes(pthis, ajStrGetRes(thys)+fileBuffSize);
	    thys = *pthis;
	    /*ajDebug("more to do: jlen: %d ipos: %d isize: %d ilen: %d "
		    "Size: %d\n",
		    jlen, ipos, isize, ilen, ajStrGetRes(thys));*/
	    ipos += jlen;
	    buff = ajStrGetuniquePtr(pthis);
	    isize = ajStrGetRes(thys) - ipos;
	    /* ajDebug("expand to: ipos: %d isize: %d Size: %d\n",
		    ipos, isize, ajStrGetRes(thys)); */

	}
	else
	    buff = NULL;
    }
    
    ajStrSetValidLen(pthis, ilen);

    if(ajStrGetCharLast(*pthis) == '\n')
	ajStrCutEnd(pthis, 1);

    /* PC files have \r\n Macintosh files have just \r : this fixes both */

    if(ajStrGetCharLast(*pthis) == '\r')
    {
	/*ajDebug("Remove carriage-return characters from PC-style files\n");*/
	ajStrCutEnd(pthis, 1);
    }

    ajStrTrimWhite(pthis);

    return ajStrGetLen(*pthis);
}
Example #5
0
static void merger_Merge(AjPAlign align, AjPStr *ms,
			 const char *a, const char *b,
			 const AjPStr m, const AjPStr n, ajint start1,
			 ajint start2,
			 const char *namea, const char *nameb)
{
    ajint apos;
    ajint bpos;
    ajint i;
    AjPStr mm = NULL;
    AjPStr nn = NULL;

    char *p;
    char *q;

    ajint olen;				/* length of walk alignment */
    size_t tt;
    
    /* lengths of the sequences after the aligned region */
    ajint alen;
    ajint blen;
    AjPStr tmpstr = NULL;

    mm = ajStrNewS(m);
    nn = ajStrNewS(n);

    p    = ajStrGetuniquePtr(&mm);
    q    = ajStrGetuniquePtr(&nn);
    olen = ajStrGetLen(mm);

    /* output the left hand side */
    if(start1 > start2)
    {
	for(i=0; i<start1; i++)
	    ajStrAppendK(ms, a[i]);

	if(start2)
	{
	    ajFmtPrintAppS(&tmpstr, "WARNING: *************************"
			   "********\n");
	    ajFmtPrintAppS(&tmpstr, "The region of alignment only starts at "
			   "position %d of sequence %s\n", start2+1, nameb);
	    ajFmtPrintAppS(&tmpstr, "Only the sequence of %s is being used "
			   "before this point\n\n", namea);
	    ajAlignSetTailApp(align, tmpstr);
	    ajStrDel(&tmpstr);
	}
    }
    else if(start2 > start1)
    {
	for(i=0; i<start2; i++)
	    ajStrAppendK(ms, b[i]);

	if(start1)
	{
	    ajFmtPrintAppS(&tmpstr, "WARNING: **************************"
			   "*******\n");
	    ajFmtPrintAppS(&tmpstr, "The region of alignment only starts at "
			   "position %d of sequence %s\n", start1+1, namea);
	    ajFmtPrintAppS(&tmpstr, "Only the sequence of %s is being used "
			   "before this point\n\n", nameb);
	    ajAlignSetTailApp(align, tmpstr);
	    ajStrDel(&tmpstr);
	}
    }
    else if(start1 && start2)
    {
	/* both the same length and > 1 before the aligned region */
	ajFmtPrintAppS(&tmpstr,
			      "WARNING: *********************************\n");
	ajFmtPrintAppS(&tmpstr, "There is an equal amount of unaligned "
		       "sequence (%d) at the start of the sequences.\n",
		       start1);
	ajFmtPrintAppS(&tmpstr, "Sequence %s is being arbitrarily chosen "
		       "for the merged sequence\n\n", namea);
	ajAlignSetTailApp(align, tmpstr);
	ajStrDel(&tmpstr);

	for(i=0; i<start1; i++)
	    ajStrAppendK(ms, a[i]);
    }

    /* header */
    ajFmtPrintS(&tmpstr, "Conflicts: %15.15s %15.15s\n", namea, nameb);
    ajFmtPrintAppS(&tmpstr,
		   "             position base   position base Using\n");
    ajAlignSetTailApp(align, tmpstr);

    /* make the merged sequence
    **
    **  point to the start of the alignment in the complete unaligned
    **  sequences
    */
    apos = start1;
    bpos = start2;

    for(i=0; i<olen; i++)
    {
	if(p[i]=='.' || p[i]==' ' || p[i]=='-' ||
	   q[i]=='.' || q[i]==' ' || q[i]=='-')
	{				/* gap! */
	    if(merger_bestquality(a, b, apos, bpos))
	    {
		p[i] = toupper((ajint)p[i]);
		if(p[i] != '.' && p[i] != ' ' && p[i] != '-')
		    ajStrAppendK(ms, p[i]);
		ajFmtPrintS(&tmpstr,
			    "             %8d  '%c'   %8d  '%c'   '%c'\n",
			    apos+1, p[i], bpos+1, q[i], p[i]);
		ajAlignSetTailApp(align, tmpstr);
	    }
	    else
	    {
		q[i] = toupper((ajint)q[i]);
		if(q[i] != '.' && q[i] != ' ' && q[i] != '-')
		    ajStrAppendK(ms, q[i]);
		ajFmtPrintS(&tmpstr,
			    "             %8d  '%c'   %8d  '%c'   '%c'\n",
			    apos+1, p[i], bpos+1, q[i], q[i]);
		ajAlignSetTailApp(align, tmpstr);
	    }

	}
	else if(p[i]=='n' || p[i]=='N')
	{
	    q[i] = toupper((ajint)q[i]);
	    ajStrAppendK(ms, q[i]);
	}
	else if(q[i]=='n' || q[i]=='N')
	{
	    p[i] = toupper((ajint)p[i]);
	    ajStrAppendK(ms, p[i]);
	}
	else if(p[i] != q[i])
	{
	    /*
	    **  get the sequence with the best quality and use the base
	    **  from that one
	    */
	    if(merger_bestquality(a, b, apos, bpos))
	    {
		p[i] = toupper((ajint)p[i]);
		ajStrAppendK(ms, p[i]);
		ajFmtPrintS(&tmpstr,
			    "             %8d  '%c'   %8d  '%c'   '%c'\n",
			    apos+1, p[i], bpos+1, q[i], p[i]);
		ajAlignSetTailApp(align, tmpstr);
	    }
	    else
	    {
		q[i] = toupper((ajint)q[i]);
		ajStrAppendK(ms, q[i]);
		ajFmtPrintS(&tmpstr,
			    "             %8d  '%c'   %8d  '%c'   '%c'\n",
			    apos+1, p[i], bpos+1, q[i], q[i]);
		ajAlignSetTailApp(align, tmpstr);
	    }

	}
	else
	    ajStrAppendK(ms, p[i]);

	/* update the positions in the unaligned complete sequences */
	if(p[i] != '.' &&  p[i] != ' ' &&  p[i] != '-') apos++;
	if(q[i] != '.' &&  q[i] != ' ' &&  q[i] != '-') bpos++;
    }

    /* output the right hand side */
    tt = strlen(&a[apos]);
    alen = (ajint) tt;

    tt = strlen(&b[bpos]);
    blen = (ajint) tt;
    
    if(alen > blen)
    {
	ajStrAppendC(ms, &a[apos]);
	if(blen)
	{
	    ajFmtPrintAppS(&tmpstr, "WARNING: ***************************"
			   "******\n");
	    ajFmtPrintAppS(&tmpstr, "The region of alignment ends at "
			   "position %d of sequence %s\n",
			   bpos+1, nameb);
	    ajFmtPrintAppS(&tmpstr, "Only the sequence of %s is being used "
			   "after this point\n\n", namea);
	    ajAlignSetTailApp(align, tmpstr);
	    ajStrDel(&tmpstr);
	}

    }

    if(blen > alen)
    {
	ajStrAppendC(ms, &b[bpos]);
	if(alen)
	{
	    ajFmtPrintAppS(&tmpstr, "WARNING: ***************************"
			   "******\n");
	    ajFmtPrintAppS(&tmpstr, "The region of alignment ends at "
			   "position %d of sequence %s\n",
			   apos+1, namea);
	    ajFmtPrintAppS(&tmpstr, "Only the sequence of %s is being used "
			   "after this point\n\n", nameb);
	    ajAlignSetTailApp(align, tmpstr);
	    ajStrDel(&tmpstr);
	}
    }
    else if(alen && blen)
    {	/* both the same length and > 1 */
	ajFmtPrintAppS(&tmpstr, "WARNING: ************************"
		       "*********\n");
	ajFmtPrintAppS(&tmpstr, "There is an equal amount of unaligned "
		       "sequence (%d) at the end of the sequences.\n",
		       alen);
	ajFmtPrintAppS(&tmpstr, "Sequence %s is being arbitrarily chosen "
		       "for the merged sequence\n\n", namea);
	ajStrAppendC(ms, &a[apos]);
	ajAlignSetTailApp(align, tmpstr);
	ajStrDel(&tmpstr);
    }

    ajStrDel(&mm);
    ajStrDel(&nn);
    ajStrDel(&tmpstr);

    return;
}
static size_t dbiblast_memfreadS(AjPStr* dest, size_t size, size_t num_items,
				 PMemFile mf)
{

    return dbiblast_memfread(ajStrGetuniquePtr(dest), size, num_items, mf);
}
Example #7
0
void maketree()
{ /* construct the tree */
  long i;
  char* treestr;

  if (usertree) {
    if(numtrees > MAXSHIMOTREES)
      shimotrees = MAXSHIMOTREES;
    else
      shimotrees = numtrees;
    if (numtrees > 2)
      emboss_initseed(inseed, &inseed0, seed);
    if (treeprint) {
      fprintf(outfile, "User-defined tree");
      if (numtrees > 1)
        putc('s', outfile);
      putc('\n', outfile);
    }
    setuptree(&curtree, nonodes2);
    for (which = 1; which <= spp; which++)
      inittip(which, &curtree);
    which = 1;
    while (which <= numtrees) {
      treestr = ajStrGetuniquePtr(&phylotrees[which-1]->Tree);
      treeread2 (&treestr, &curtree.start, curtree.nodep,
        lengths, &trweight, &goteof, &haslengths, &spp,false,nonodes2);
      curtree.start = curtree.nodep[outgrno - 1]->back;
      treevaluate();
      printree();
      summarize();
      which++;
    }
    FClose(intree);
    if (numtrees > 1 && loci > 1 ) {
      weight = (long *)Malloc(loci*sizeof(long));
      for (i = 0; i < loci; i++)
        weight[i] = 1;
      standev2(numtrees, maxwhich, 0, loci-1, maxlogl, l0gl, l0gf, seed);
      free(weight);
      fprintf(outfile, "\n\n");
    }
  } else { /* if ( !usertree ) */
    if (jumb == 1) {
      setuptree(&curtree, nonodes2);
      setuptree(&priortree, nonodes2);
      setuptree(&bestree, nonodes2);
      if (njumble > 1) 
        setuptree(&bestree2, nonodes2);
    }
    for (i = 1; i <= spp; i++)
      enterorder[i - 1] = i;
    if (jumble)
      randumize(seed, enterorder);
    nextsp = 3;
    buildsimpletree(&curtree);
    curtree.start = curtree.nodep[enterorder[0] - 1]->back;
    if (jumb == 1) numtrees = 1;
    nextsp = 4;
    if (progress) {
      printf("Adding species:\n");
      writename(0, 3, enterorder);
#ifdef WIN32
      phyFillScreenColor();
#endif
    }
    while (nextsp <= spp) {
      buildnewtip(enterorder[nextsp - 1], &curtree, nextsp);
      copy_(&curtree, &priortree);
      bestree.likelihood = -DBL_MAX;
      addtraverse(curtree.nodep[enterorder[nextsp - 1] - 1]->back,
                  curtree.start, true );
      copy_(&bestree, &curtree);
      if (progress) {
        writename(nextsp - 1, 1, enterorder);
#ifdef WIN32
        phyFillScreenColor();
#endif
      }
      if (global && nextsp == spp) {
        if (progress) {
          printf("\nDoing global rearrangements\n");
          printf("  !");
          for (i = 1; i <= spp - 2; i++)
            if ( (i - spp) % (( nonodes2 / 72 ) + 1 ) == 0 )
              putchar('-');
          printf("!\n");
          printf("   ");
        }
      }
      succeeded = true;
      while (succeeded) {
        succeeded = false;
        if ( global && nextsp == spp )
          globrearrange();
        else 
          rearrange(curtree.start);
        if (global && nextsp == spp)
          putc('\n', outfile);
      }
      if (global && nextsp == spp && progress)
        putchar('\n');
      if (njumble > 1) {
        if (jumb == 1 && nextsp == spp)
          copy_(&bestree, &bestree2);
        else if (nextsp == spp) {
          if (bestree2.likelihood < bestree.likelihood)
            copy_(&bestree, &bestree2);
        }
      }
      if (nextsp == spp && jumb == njumble) {
        if (njumble > 1) copy_(&bestree2, &curtree);
        curtree.start = curtree.nodep[outgrno - 1]->back;
        printree();
        summarize();
      }
      nextsp++;
    }
  }
  if ( jumb < njumble)
    return;
  if (progress) {
    printf("\nOutput written to file \"%s\"\n", outfilename);
    if (trout)
      printf("\nTree also written onto file \"%s\"\n\n", outtreename);
  }
  freeview(&curtree, nonodes2);
  if (!usertree) {
    freeview(&bestree, nonodes2);
    freeview(&priortree, nonodes2);
  }
  for (i = 0; i < spp; i++)
    free(x[i]);
  if (!contchars) {
    free(locus);
    free(pbar);
  }
}  /* maketree */
Example #8
0
ajuint embMatProtScanInt(const AjPStr s, const AjPStr n, const EmbPMatPrints m,
			AjPList *l,
			AjBool *all, AjBool *ordered, AjBool overlap)
{
    EmbPMatMatch mm;
    AjPStr t;
    char   *p;
    char   *q;
    ajint slen;
    ajint score;
    ajint mlen;
    ajint elem;
    ajint minpc;
    ajint maxscore;
    ajint limit;
    ajint sum;
    ajint hpe;
    ajint hpm;

    ajint lastelem;
    ajint lastpos;
    ajint op;

    ajint i;
    ajint j;

    t = ajStrNewC(ajStrGetPtr(s));
    ajStrFmtUpper(&t);
    p = q = ajStrGetuniquePtr(&t);
    slen = ajStrGetLen(t);

    for(i=0;i<slen;++i,++p)
	*p = ajSysCastItoc(ajBasecodeToInt((ajint)*p));

    p = q;

    *all = *ordered = ajTrue;
    lastelem = lastpos = INT_MAX;

    hpm=0;

    for(elem=(m)->n - 1;elem >= 0;--elem)
    {
	hpe = 0;

	mlen     = (m)->len[elem];
	minpc    = (m)->thresh[elem];
	maxscore = (m)->max[elem];

	limit = slen-mlen;

	for(i=0;i<limit;++i)
	{
	    sum = 0;

	    for(j=0;j<mlen;++j)
		sum += (m)->matrix[elem][(ajint) p[i+j]][j];

	    score = (sum*100)/maxscore;

	    if(score>=minpc)
	    {
		if(elem<lastelem && *ordered)
		{
		    if(lastelem == INT_MAX)
		    {
			lastelem = elem;
			lastpos  = i;
		    }
		    else
		    {
			lastelem = elem;
			op = i;

			if(!overlap)
			    op += mlen;

			if(op >= lastpos)
			    *ordered = ajFalse;

			lastpos = i;
		    }
		}

		++hpe;
		++hpm;
		matPushHitInt(n,m,l,i,score,elem,hpe,hpm);
	    }
	}

	if(!hpe)
	    *all = ajFalse;
    }

    if(hpm)
    {
	ajListPop(*l,(void **)&mm);

	if(*all)
	{
	    mm->all = ajTrue;

	    if(*ordered)
		mm->ordered = ajTrue;
	    else
		mm->ordered = ajFalse;
	}
	else
	{
	    mm->all = ajFalse;

	    if(*ordered)
		mm->ordered = ajTrue;
	    else
		mm->ordered = ajFalse;
	}

	ajListPush(*l,(void *)mm);
    }

    ajStrDel(&t);

    return hpm;
}
Example #9
0
static PSilent silent_checktrans(const AjPStr seq,const EmbPMatMatch match,
				const PRinfo rlp, ajint begin, ajint radj,
				AjBool rev, ajint end)
{
    PSilent ret;
    const char *p = NULL;
    const char *q = NULL;
    const char *s = NULL;
    char *t;
    const char *u;
    ajint matchpos;
    ajint framep;

    ajint count;
    AjPTrn table = NULL;
    AjPStr s1 = NULL;
    AjPStr s2 = NULL;
    char c;
    char rc;
    ajint  min = INT_MAX;          /* Reverse sense intentional! */
    ajint  max = -INT_MAX;
    ajint fpos;
    ajint rpos;
    ajint x;
    AjPStr tstr = NULL;

    matchpos = match->start;
    fpos = matchpos;
    rpos=radj-fpos-match->len;

    tstr = ajStrNewS(seq);
    t = ajStrGetuniquePtr(&tstr);

    p = t+fpos-(begin+1);

    u = q = ajStrGetPtr(rlp->site);

    /* Test here for whether cut site is within sequence substring */
    if(rlp->ncuts==4)
    {
	min = AJMIN(rlp->cut1,rlp->cut2);
	max = AJMAX(rlp->cut3,rlp->cut4);
    }
    else if(rlp->ncuts==2)
    {
	min = AJMIN(rlp->cut1,rlp->cut2);
	max = AJMAX(rlp->cut1,rlp->cut2);
    }
    else
    {
        ajWarn("Possibly corrupt RE file");
	ajStrDel(&tstr);
        return NULL;
    }

    if(!rev)                  /* forward strand */
    {
	if(matchpos+min<0||matchpos+max>end+1)
	{
	    /*Cut site not in sequence range*/
	    ajStrDel(&tstr);
	    return NULL;
	}
    }
    else                       /* reverse strand */
    {
	if(radj-matchpos-1-min>end+1||radj-matchpos-1-max<begin)
	{
	    /*Cut site not in sequence range*/
	    ajStrDel(&tstr);
	    return NULL;
	}
    }


    count=0;
    while(ajBaseAlphaToBin(*q++) & ajBaseAlphaToBin(*p++))
          ++count;

    /* Changed base postion */
    x = fpos+count-(begin+1);
    /* Where the frame starts on the reverse strand */
    framep = (end-begin+1)%3;

    c  = t[x];
    rc = u[count];

    if(!rev)            /* forward strand */
	s = t+x-x%3;
    else                /* reverse strand */
        s = t+x-(x-framep)%3;

    table = ajTrnNewI(0);

    /* translates codon pointed to by s (original seq) */
    s1 = ajStrNewK(ajTrnCodonC(table,s));

    t[x] = rc;

    /*  translates codon pointed to by s (mutated base from RS pattern */
    s2 = ajStrNewK(ajTrnCodonC(table,s));

    t[x] = c;  /* changes mutated base in seq back to original base */

    AJNEW(ret);
    ret->obase = c;
    ret->nbase = rc;
    ret->code  = ajStrNewC(ajStrGetPtr(rlp->code));
    ret->site  = ajStrNewC(ajStrGetPtr(rlp->site));
    ret->seqaa = ajStrNewC(ajStrGetPtr(s1));
    ret->reaa  = ajStrNewC(ajStrGetPtr(s2));
    if(ajStrMatchS(s1,s2))
	ret->issilent = ajTrue;
    else
	ret->issilent = ajFalse;
    if(!rev)
    {
       	ret->match = matchpos;
        ret->base  = matchpos+count;
    }
    else
    {
	ret->match = rpos;
	ret->base  = rpos+match->len-1-count;
    }

    ajStrDel(&tstr);
    ajStrDel(&s1);
    ajStrDel(&s2);
    ajTrnDel(&table);

    return ret;
}
Example #10
0
static char* cutgextract_next(AjPFile inf, const AjPStr wildspecies,
			      AjPStr* pspecies, AjPStr* pdoc)
{
    AjPStrTok handle    = NULL;
    AjPStr token = NULL;
    ajint i;
    ajint len;
    char *p = NULL;
    char c;
    AjBool done = ajFalse;

    if(!cutgextractLine)
	cutgextractLine = ajStrNew();

    if(!cutgextractOrg)
	cutgextractOrg  = ajStrNew();

    ajStrAssignC(&cutgextractLine,"");
    ajStrAssignC(pdoc,"");
    while (!done)
    {

	while(ajStrGetCharFirst(cutgextractLine) != '>')
	    if(!ajReadlineTrim(inf,&cutgextractLine))
		return NULL;

	handle = ajStrTokenNewC(cutgextractLine,"\\\n\t\r");
	for(i=0;i<7;++i) {
	    ajStrTokenNextParseC(&handle,"\\\n\t\r",&token);
	    if(i==5)
	    {
		ajStrAssignC(&cutgextractOrg,"E");
		ajStrAppendS(&cutgextractOrg, token);
		ajStrAssignS(pspecies, token);
		if(ajStrMatchWildS(token,wildspecies))
		{
		    done = ajTrue;
		}
	    }

	    switch(i)
	    {
	    case 0:
		ajStrAppendC(pdoc, "#ID ");
		ajStrAppendS(pdoc, token);
		ajStrAppendC(pdoc, "\n");
		break;
	    case 1:
		ajStrAppendC(pdoc, "#AC ");
		ajStrAppendS(pdoc, token);
		ajStrAppendC(pdoc, "\n");
		break;
	    case 2:
		ajStrAppendC(pdoc, "#FT ");
		ajStrAppendS(pdoc, token);
		ajStrAppendC(pdoc, "\n");
		break;
	    case 3:
		ajStrAppendC(pdoc, "#FL ");
		ajStrAppendS(pdoc, token);
		ajStrAppendC(pdoc, "\n");
		break;
	    case 4:
		ajStrAppendC(pdoc, "#PI ");
		ajStrAppendS(pdoc, token);
		ajStrAppendC(pdoc, "\n");
		ajStrAssignS(&cutgextractSavepid, token);
		break;
	    case 5:
		ajStrAppendC(pdoc, "#OS ");
		ajStrAppendS(pdoc, token);
		ajStrAppendC(pdoc, "\n");
		break;
	    case 6:
		ajStrAppendC(pdoc, "#DE ");
		ajStrAppendS(pdoc, token);
		ajStrAppendC(pdoc, "\n");
		break;
	    default:
		break;
	    }
	}

	ajStrTokenDel(&handle);
	ajStrDel(&token);
	if(!done)
	    if(!ajReadlineTrim(inf,&cutgextractLine))
		return NULL;
    }

    len = ajStrGetLen(cutgextractOrg);
    p   = ajStrGetuniquePtr(&cutgextractOrg);
    for(i=0;i<len;++i)
    {
	c = p[i];
	if(c=='/' || c==' ' || c=='.' || c=='\'')
	    p[i]='_';
    }

    if(p[strlen(p)-1]=='_')
	p[strlen(p)-1]='\0';
    ajStrDel(&token);
    ajStrTokenDel(&handle);

    return p;
}
Example #11
0
int main(int argc, Char *argv[])
{  
  pattern_elm  ***pattern_array;
  long tip_count = 0;
  double ln_maxgrp;
  double ln_maxgrp1;
  double ln_maxgrp2;
  node * p;

#ifdef MAC
  argc = 1;                /* macsetup("Treedist", "");        */
  argv[0] = "Treedist";
#endif
  init(argc, argv);
  emboss_getoptions("ftreedist",argc,argv);

  /* Initialize option-based variables, then ask for changes regarding
     their values. */
 

  ntrees = 0.0;
  lasti  = -1;

  /* read files to determine size of structures we'll be working with */
  countcomma(ajStrGetuniquePtr(&phylotrees[0]->Tree),&tip_count);
  tip_count++; /* countcomma does a raw comma count, tips is one greater */


  /* 
   * EWFIX.BUG.756 -- this section may be killed if a good solution
   * to bug 756 is found
   * 
   * inside cons.c there are several arrays which are allocated
   * to size "maxgrp", the maximum number of groups (sets of
   * tips more closely connected than the rest of the tree) we
   * can see as the code executes.
   *
   * We have two measures we use to determine how much space to
   * allot:
   *  (1) based on the tip count of the trees in the infile
   *  (2) based on total number of trees in infile, and 
   *
   * (1) -- Tip Count Method
   * Since each group is a subset of the set of tips we must
   * represent at most pow(2,tips) different groups. (Technically
   * two fewer since we don't store the empty or complete subsets,
   * but let's keep this simple.
   *
   * (2) -- Total Tree Size Method
   * Each tree we read results in 
   *      singleton groups for each tip, plus
   *      a group for each interior node except the root
   * Since the singleton tips are identical for each tree, this gives
   * a bound of #tips + ( #trees * (# tips - 2 ) )
   *
   *
   * Ignoring small terms where expedient, either of the following should
   * result in an adequate allocation:
   *       pow(2,#tips)
   *       (#trees + 1) * #tips 
   *
   * Since "maxgrp" is a limit on the number of items we'll need to put
   * in a hash, we double it to make space for quick hashing
   *
   * BUT -- all of this has the possibility for overflow, so -- let's
   * make the initial calculations with doubles and then convert
   *
   */

  /* limit chosen to make hash arithmetic work */
  maxgrp = LONG_MAX / 2;
  ln_maxgrp = log((double)maxgrp);

  /* 2 * (#trees + 1) * #tips */
  ln_maxgrp1  = log(2.0 * (double)tip_count * 
                            ((double)trees_in_1 + (double)trees_in_2));

  /* ln only for 2 * pow(2,#tips) */
  ln_maxgrp2 = (double)(1 + tip_count) * log(2.0);

  /* now -- find the smallest of the three */
  if(ln_maxgrp1 < ln_maxgrp)
  {
    maxgrp = 2 * (trees_in_1 + trees_in_2 + 1) * tip_count;
    ln_maxgrp = ln_maxgrp1;
  }
  if(ln_maxgrp2 < ln_maxgrp)
  {
    maxgrp = pow(2,tip_count+1);
  }
   

  /* Read the (first) tree file and put together grouping, order, and
     timesseen */
  read_groups (&pattern_array, trees_in_1 + trees_in_2, tip_count, phylotrees);

  if ((tree_pairing == ADJACENT_PAIRS) ||
      (tree_pairing == ALL_IN_FIRST)) {

    /* Here deal with the adjacent or all-in-first pairing
       difference computation */

    compute_distances (pattern_array, trees_in_1, 0);

   } else if (tree_pairing == NO_PAIRING) {
    /* Compute the consensus tree. */
    putc('\n', outfile);
    /* consensus();         Reserved for future development */
  }

  if (progress)
    printf("\nOutput written to file \"%s\"\n\n", outfilename);

  FClose(outtree);
  FClose(intree);
  FClose(outfile);

  if ((tree_pairing == ALL_IN_1_AND_2) || 
      (tree_pairing == CORR_IN_1_AND_2))
    FClose(intree2);

#ifdef MAC
  fixmacfile(outfilename);
  fixmacfile(outtreename);
#endif

  free_patterns (pattern_array, trees_in_1 + trees_in_2);
  clean_up_final();
  /* clean up grbg */
  p = grbg;
  while (p != NULL) {
     node * r = p;
     p = p->next;
     free(r->nodeset);
     free(r->view);
     free(r);
  }


  printf("Done.\n\n");

  embExit();
  return 0;
}  /* main */