Example #1
0
void UHMMCalibrate::calibrate(plan7_s* hmm, const UHMMCalibrateSettings& s, TaskStateInfo& si) {
    
    struct histogram_s *hist;     // a resulting histogram 

    float max = 0;
    main_loop_serial(hmm, s.seed, s.nsample, s.lenmean, s.lensd, s.fixedlen,  &hist, &max, si.cancelFlag, si.progress);
    if (!si.cancelFlag) {
        // Fit an EVD to the observed histogram.
        // The TRUE left-censors and fits only the right slope of the histogram.
        // The 9999. is an arbitrary high number that means we won't trim
        // outliers on the right.

        if (! ExtremeValueFitHistogram(hist, TRUE, 9999.)) {
            si.setError(  "fit failed; num sequences may be set too small?\n" );
        } else {
            hmm->flags |= PLAN7_STATS;
            hmm->mu     = hist->param[EVD_MU];
            hmm->lambda = hist->param[EVD_LAMBDA];
        }
    }
    FreeHistogram(hist);
}
void HMMCalibrateParallelTask::cleanup() {
    if (wpool.hist!=NULL) {
        FreeHistogram(wpool.hist); // exception safe function -> does not depend on hmm context
        wpool.hist = NULL;
    }
}
Example #3
0
QList<UHMMSearchResult> UHMMSearch::search(plan7_s* _hmm, const char* seq, int seqLen, const UHMMSearchSettings& s, TaskStateInfo& si) 
{
    plan7_s * hmm = HMMIO::cloneHMM( _hmm );
    //Set up optional Pfam score thresholds. 
    threshold_s thresh;         // contains all threshold (cutoff) info
    thresh.globE   = s.globE; // use a reasonable Eval threshold
    thresh.globT   = -FLT_MAX;  // but no bit threshold
	thresh.domT    = s.domT;  // no domain bit threshold 
	thresh.domE    = s.domE;   // and no domain Eval threshold
    thresh.autocut = CUT_NONE;  // and no Pfam cutoffs used        
    thresh.Z       = s.eValueNSeqs; // Z not preset; use actual # of seqs 

    int   do_null2      = TRUE;    // TRUE to adjust scores with null model #2 
    int   do_forward    = FALSE;   // TRUE to use Forward() not Viterbi()      
    int   do_xnu        = FALSE;   // TRUE to filter sequences thru XNU        
    QList<UHMMSearchResult> res;   // the results of the method

    //get HMMERTaskLocalData
	HMMERTaskLocalData *tld = getHMMERTaskLocalData();
	alphabet_s *al = &tld->al;
	
    SetAlphabet(hmm->atype);

    P7Logoddsify(hmm, !do_forward); //TODO: clone model to avoid changes in it or make it thread safe??

    if (do_xnu && al->Alphabet_type == hmmNUCLEIC) {
        si.setError( "The HMM is a DNA model, and you can't use the --xnu filter on DNA data" );
        return res;
    }

    /*****************************************************************
    * Set up optional Pfam score thresholds. 
    * Can do this before starting any searches, since we'll only use 1 HMM.
    *****************************************************************/ 

    if (!SetAutocuts(&thresh, hmm)) {
        si.setError(  "HMM did not contain the GA, TC, or NC cutoffs you needed" );
        return res;
    }

    // set up structures for storing output
    histogram_s *histogram  = AllocHistogram(-200, 200, 100);  //keeps full histogram of all scores
    tophit_s   *ghit        = AllocTophits(200);               // per-seq hits: 200=lumpsize
    tophit_s   *dhit        = AllocTophits(200);               // domain hits:  200=lumpsize
    
    int     nseq = 0;         // number of sequences searched   
#ifdef UGENE_CELL
    if( HMMSearchAlgo_CellOptimized == s.alg ) {
        if( hmm->M < MAX_HMM_LENGTH ) {
            main_loop_spe(hmm, seq, seqLen, &thresh, do_forward, do_null2, do_xnu, histogram, ghit, dhit, &nseq, si);
        } else {
            main_loop_serial(hmm, seq, seqLen, &thresh, do_forward, do_null2, do_xnu, histogram, ghit, dhit, &nseq, si);
        }
    } else
#elif defined(HMMER_BUILD_WITH_SSE2)
    if( HMMSearchAlgo_SSEOptimized == s.alg ) {
        main_loop_opt(hmm, seq, seqLen, &thresh, do_forward, do_null2, do_xnu, histogram, ghit, dhit, &nseq, si, sseScoring);
    } else
#endif
    if( HMMSearchAlgo_Conservative == s.alg ) {
        main_loop_serial(hmm, seq, seqLen, &thresh, do_forward, do_null2, do_xnu, histogram, ghit, dhit, &nseq, si);
    }
    else {
        assert( false && "bad hmmsearch algorithm selected" );
    }
    // Process hit lists, produce text output

    // Set the theoretical EVD curve in our histogram using calibration in the HMM, if available. 
    if (hmm->flags & PLAN7_STATS) {
        ExtremeValueSetHistogram(histogram, hmm->mu, hmm->lambda, histogram->lowscore, histogram->highscore, 0);
    }
    if (!thresh.Z) {
        thresh.Z = nseq;       // set Z for good now that we're done
    }

    //report our output 

    FullSortTophits(dhit);

    //int namewidth = MAX(8, TophitsMaxName(ghit)); // max width of sequence name

    // Report domain hits (sorted on E-value)
    for (int i = 0; i < dhit->num && !si.cancelFlag; i++) {
        float   sc;                 // score of an HMM search                
        double  pvalue;             // pvalue of an HMM score
        double  evalue;             // evalue of an HMM score
        char    *name, *desc;       // hit sequence name and description
        double  motherp;            // pvalue of a whole seq HMM score
        float   mothersc;           // score of a whole seq parent of domain 
        int     sqfrom, sqto;       // coordinates in sequence                
        int     sqlen;              // length of seq that was hit
        int     hmmfrom, hmmto;     // coordinate in HMM                      
        int     ndom;               // total # of domains in this seq   
        int     domidx;             // number of this domain 

        GetRankedHit(dhit, i, &pvalue, &sc, &motherp, &mothersc,
                    &name, NULL, &desc,
                    &sqfrom, &sqto, &sqlen,      // seq position info
                    &hmmfrom, &hmmto, NULL,      // HMM position info 
                    &domidx, &ndom,              // domain info
                    NULL);                       // alignment info     

        evalue = pvalue * (double) thresh.Z;
        
        if (motherp * (double) thresh.Z > thresh.globE || mothersc < thresh.globT)  {
            continue;
        } else if (evalue <= thresh.domE && sc >= thresh.domT) {
            // hmm reports results in range [1...N] -> translate it to [0..N)
            res.append(UHMMSearchResult(U2Region(sqfrom-1, sqto-sqfrom+1), sc, evalue));
        }
    }

    //Clean-up and exit.
    FreeHistogram(histogram);
    FreeTophits(ghit);
    FreeTophits(dhit);
	FreePlan7( hmm );
    
    return res;
}
Example #4
0
int 
main(void)
{
  int      master_tid;		/* PVM TID of our master */
  int      slaveidx;		/* my slave index (0..nslaves-1) */
  struct plan7_s *hmm;		/* HMM to calibrate, sent from master */
  struct histogram_s *hist;     /* score histogram */
  int      hmmidx;		/* index of this HMM */
  char    *seq;			/* synthetic random sequence */
  char    *dsq;			/* digitized seq */
  int      len;			/* length of seq */
  float    sc;			/* score of seq aligned to HMM */
  float    max;			/* maximum score seen in sample */
  int      seed;		/* random number seed */
  int      nsample;		/* number of seqs to sample */
  int      fixedlen;		/* if nonzero, fixed length of seq */
  float    lenmean;		/* Gaussian mean length of seq */
  float    lensd;		/* Gaussian length std. dev. for seq */
  int      fitok;		/* TRUE if EVD fit was OK */
  float    randomseq[MAXABET];	/* iid frequencies of residues */
  float    p1;
  int      alphatype;		/* alphabet type, hmmAMINO or hmmNUCLEIC    */
  int      idx;
  int      code;

  /* Register leave_pvm() cleanup function so any exit() call
   * first calls pvm_exit().
   */
  if (atexit(leave_pvm) != 0) { pvm_exit(); Die("slave couldn't register leave_pvm()"); }

  /*****************************************************************
   * initialization.
   * Master broadcasts the problem to us: parameters of the
   * HMM calibration.  
   ******************************************************************/

  master_tid = pvm_parent();	/* who's our master? */

  pvm_recv(master_tid, HMMPVM_INIT);
  pvm_upkint(&nsample,  1, 1);
  pvm_upkint(&fixedlen, 1, 1);
  pvm_upkfloat(&lenmean,  1, 1);
  pvm_upkfloat(&lensd,    1, 1);

  /* tell the master we're OK and ready to go (or not)
   */
  code = HMMPVM_OK;
  pvm_initsend(PvmDataDefault);
  pvm_pkint(&code, 1, 1);	
  pvm_send(master_tid, HMMPVM_RESULTS);

  /*****************************************************************
   * Main loop.
   * Receive a random number seed, then an HMM to search against.
   * If we receive a -1 seed, we shut down. 
   *****************************************************************/ 
  
  slaveidx = -1;
  for (;;) 
    {
      pvm_recv(master_tid, HMMPVM_WORK);
      pvm_upkint(&seed, 1, 1);
      if (seed == -1) break;	/* shutdown signal */
      pvm_upkint(&hmmidx, 1, 1);
      pvm_upkint(&alphatype,1, 1);
      SetAlphabet(alphatype);
      hmm = PVMUnpackHMM();
      if (hmm == NULL) Die("oh no, the HMM never arrived");

      if (slaveidx == -1) slaveidx = hmmidx; 
      P7DefaultNullModel(randomseq, &p1);

      sre_srandom(seed);
      P7Logoddsify(hmm, TRUE);
      hist = AllocHistogram(-200, 200, 100);
      max  = -FLT_MAX;

      for (idx = 0; idx < nsample; idx++)
	{
  				/* choose length of random sequence */
	  if (fixedlen) len = fixedlen;
	  else do len = (int) Gaussrandom(lenmean, lensd); while (len < 1);
				/* generate it */
	  seq = RandomSequence(Alphabet, randomseq, Alphabet_size, len);
	  dsq = DigitizeSequence(seq, len);

	  if (P7ViterbiSize(len, hmm->M) <= RAMLIMIT)
	    sc = P7Viterbi(dsq, len, hmm, NULL);
	  else
	    sc = P7SmallViterbi(dsq, len, hmm, NULL);

	  AddToHistogram(hist, sc);
	  if (sc > max) max = sc;
	  
	  free(seq);
	  free(dsq);
	}

      /* Fit an EVD to the observed histogram.
       * The TRUE left-censors and fits only the right slope of the histogram.
       * The 9999. is an arbitrary high number that means we won't trim outliers
       * on the right.
       */
      fitok = ExtremeValueFitHistogram(hist, TRUE, 9999.);

      /* Return output to master.
       * Currently we don't send the histogram back, but we could.
       */
      pvm_initsend(PvmDataDefault);
      pvm_pkint(&slaveidx, 1, 1);
      pvm_pkint(&hmmidx, 1, 1);	
      PVMPackString(hmm->name);
      pvm_pkint(&fitok,  1, 1);
      pvm_pkfloat(&(hist->param[EVD_MU]), 1, 1);
      pvm_pkfloat(&(hist->param[EVD_LAMBDA]), 1, 1);
      pvm_pkfloat(&max, 1, 1);
      pvm_send(master_tid, HMMPVM_RESULTS);

      /* cleanup
       */
      FreeHistogram(hist);
      FreePlan7(hmm);
    }

  /*********************************************** 
   * Cleanup, return.
   ***********************************************/

  return 0;			/* pvm_exit() is called by atexit() registration. */
}