Example #1
0
/*
 * Simple-minded "can it be here?" routine
 */
static boolean
e_survives_at(struct entity *etmp, int x, int y)
{
    if (noncorporeal(etmp->edata))
        return TRUE;
    if (is_pool(level, x, y))
        return (boolean) (waterwalks(etmp->emon) || unbreathing(etmp->emon) ||
                          swims(etmp->emon) || flying(etmp->emon) ||
                          levitates(etmp->emon));
    /* must force call to lava_effects in e_died if is_u */
    if (is_lava(level, x, y))
        return (boolean) (likes_lava(etmp->edata) || flying(etmp->emon) ||
                          levitates(etmp->emon));
    if (is_db_wall(x, y))
        return !!phasing(etmp->emon);
    return TRUE;
}
Example #2
0
/*
 * These are never directly affected by a bridge or portcullis.
 */
static boolean
automiss(struct entity *etmp)
{
    return (boolean) (phasing(etmp->emon) ||
                      noncorporeal(etmp->edata));
}
Example #3
0
RcppExport SEXP FitPhasingBurst(SEXP R_signal, SEXP R_flowCycle, SEXP R_read_sequence,
                SEXP R_phasing, SEXP R_burstFlows, SEXP R_maxEvalFlow, SEXP R_maxSimFlow) {

 SEXP ret = R_NilValue;
 char *exceptionMesg = NULL;

 try {

     Rcpp::NumericMatrix  signal(R_signal);
     Rcpp::NumericMatrix  phasing(R_phasing);     // Standard phasing parameters
     string flowCycle   = Rcpp::as<string>(R_flowCycle);
     Rcpp::StringVector   read_sequences(R_read_sequence);
     Rcpp::NumericVector  phasing_burst(R_burstFlows);
     Rcpp::NumericVector  max_eval_flow(R_maxEvalFlow);
     Rcpp::NumericVector  max_sim_flow(R_maxSimFlow);
     int window_size    = 38; // For normalization


     ion::FlowOrder flow_order(flowCycle, flowCycle.length());
     unsigned int num_flows = flow_order.num_flows();
     unsigned int num_reads = read_sequences.size();


     // Containers to store results
     Rcpp::NumericVector null_fit(num_reads);
     Rcpp::NumericMatrix null_prediction(num_reads, num_flows);
     Rcpp::NumericVector best_fit(num_reads);
     Rcpp::NumericVector best_ie_value(num_reads);
     Rcpp::NumericMatrix best_prediction(num_reads, num_flows);


     BasecallerRead bc_read;
     DPTreephaser dpTreephaser(flow_order);
     DPPhaseSimulator PhaseSimulator(flow_order);
     vector<double> cf_vec(num_flows, 0.0);
     vector<double> ie_vec(num_flows, 0.0);
     vector<double> dr_vec(num_flows, 0.0);


     // IE Burst Estimation Loop
     for (unsigned int iRead=0; iRead<num_reads; iRead++) {

       // Set read object
       vector<float> my_signal(num_flows);
       for (unsigned int iFlow=0; iFlow<num_flows; iFlow++)
         my_signal.at(iFlow) = signal(iRead, iFlow);
       bc_read.SetData(my_signal, num_flows);
       string my_sequence = Rcpp::as<std::string>(read_sequences(iRead));

       // Default phasing as baseline
       double my_best_fit, my_best_ie;
       double base_cf  = (double)phasing(iRead, 0);
       double base_ie  = (double)phasing(iRead, 1);
       double base_dr  = (double)phasing(iRead, 2);
       int burst_flow = (int)phasing_burst(iRead);
       vector<float> my_best_prediction;

       cf_vec.assign(num_flows, base_cf);
       dr_vec.assign(num_flows, base_dr);
       int my_max_flow  = min((int)num_flows, (int)max_sim_flow(iRead));
       int my_eval_flow = min(my_max_flow, (int)max_eval_flow(iRead));

       PhaseSimulator.SetBaseSequence(my_sequence);
       PhaseSimulator.SetMaxFlows(my_max_flow);
       PhaseSimulator.SetPhasingParameters_Basic(base_cf, base_ie, base_dr);
       PhaseSimulator.UpdateStates(my_max_flow);
       PhaseSimulator.GetPredictions(bc_read.prediction);
       dpTreephaser.WindowedNormalize(bc_read, (my_eval_flow/window_size), window_size, true);


       my_best_ie = base_ie;
       my_best_prediction = bc_read.prediction;
       my_best_fit = 0;
       for (int iFlow=0; iFlow<my_eval_flow; iFlow++) {
         double residual = bc_read.raw_measurements.at(iFlow) - bc_read.prediction.at(iFlow);
         my_best_fit += residual*residual;
       }
       for (unsigned int iFlow=0; iFlow<num_flows; iFlow++)
         null_prediction(iRead, iFlow) = bc_read.prediction.at(iFlow);
       null_fit(iRead) = my_best_fit;

       // Make sure that there are enough flows to fit a burst
       if (burst_flow < my_eval_flow-10) {
    	 int    num_steps  = 0;
    	 double step_size  = 0.0;
    	 double step_start = 0.0;
    	 double step_end   = 0.0;

         // Brute force phasing burst value estimation using grid search, crude first, then refine
         for (unsigned int iIteration = 0; iIteration<3; iIteration++) {

           switch(iIteration) {
             case 0:
               step_size = 0.05;
               step_end = 0.8;
               break;
             case 1:
               step_end   = (floor(my_best_ie / step_size)*step_size) + step_size;
               step_start = max(0.0, (step_end - 2.0*step_size));
               step_size  = 0.01;
               break;
             default:
               step_end   = (floor(my_best_ie / step_size)*step_size) + step_size;
               step_start = max(0.0, step_end - 2*step_size);
               step_size = step_size / 10;
           }
           num_steps  = 1+ ((step_end - step_start) / step_size);

           for (int iPhase=0; iPhase <= num_steps; iPhase++) {

        	 double try_ie = step_start+(iPhase*step_size);
             ie_vec.assign(num_flows, try_ie);

             PhaseSimulator.SetBasePhasingParameters(burst_flow, cf_vec, ie_vec, dr_vec);
             PhaseSimulator.UpdateStates(my_max_flow);
             PhaseSimulator.GetPredictions(bc_read.prediction);
             dpTreephaser.WindowedNormalize(bc_read, (my_eval_flow/window_size), window_size, true);

             double my_fit = 0.0;
             for (int iFlow=burst_flow+1; iFlow<my_eval_flow; iFlow++) {
               double residual = bc_read.raw_measurements.at(iFlow) - bc_read.prediction.at(iFlow);
               my_fit += residual*residual;
             }
             if (my_fit < my_best_fit) {
               my_best_fit = my_fit;
               my_best_ie  = try_ie;
               my_best_prediction = bc_read.prediction;
             }
           }
         }
       }

       // Set output information for this read
       best_fit(iRead) = my_best_fit;
       best_ie_value(iRead)   = my_best_ie;
       for (unsigned int iFlow=0; iFlow<num_flows; iFlow++)
         best_prediction(iRead, iFlow) = my_best_prediction.at(iFlow);
     }

     ret = Rcpp::List::create(Rcpp::Named("null_fit")        = null_fit,
                              Rcpp::Named("null_prediction") = null_prediction,
                              Rcpp::Named("burst_flow")      = phasing_burst,
                              Rcpp::Named("best_fit")        = best_fit,
                              Rcpp::Named("best_ie_value")   = best_ie_value,
                              Rcpp::Named("best_prediction") = best_prediction);


 } catch(std::exception& ex) {
   forward_exception_to_r(ex);
 } catch(...) {
   ::Rf_error("c++ exception (unknown reason)");
 }

 if(exceptionMesg != NULL)
   Rf_error(exceptionMesg);
 return ret;

}