MaxResults suboptSampler(Model B, PoSition **Pos, SimTime *S) { int **nNumMotifs, **startPos, /* startPos[MotifType][motifnum] */ i, j, seed_run = 0, t, k; int maxnum; /* maximum number of motif in one type motif */ register Mlist M; MaxResults maxData, locMax; RPType RP; IPtype IP; /* for quick access of input data */ PoSition *Pos_t; /* Pos_t=Pos[t] */ int n; /* BT 2/7/97 */ int nPos; int sum; double dCurrProb; #ifdef _MPI_ MPI_Status status; double dTempInfo[4]; #endif IP=B->IP; RP = B->RP; init_maxdata(&maxData); init_maxdata(&locMax); BeginTime(S); #ifdef _MPI_ if( ! IP->is_defined[cl_hm] ) { /* get the go ahead to start */ Gibbs_MPI_Recv( B, &dTempInfo, 4, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status ); if( status.MPI_TAG == G_MPI_DONE ) return maxData; if( IP->is_defined[cl_X] && B->AN->bExchange ) { B->AN->currTemp = dTempInfo[0]; B->AN->dMinTemp = B->AN->currTemp; B->AN->dMaxTemp = B->AN->currTemp; PrintTempOut(IP->Datafiles->mpiTemp_fpt, "currTemp = %f maxTemp = %f minTemp = %f\n", B->AN->currTemp, B->AN->dMaxTemp, B->AN->dMinTemp ); } } #endif /* save the inital number of motifs */ for(t = 0; t < IP->nNumMotifTypes; t++) /* BT 2/7/97 */ { RP->nPriorMotifSites[t][FORWARD] = IP->nNumMotifs[t][FORWARD]; RP->nPriorMotifSites[t][REVERSE] = IP->nNumMotifs[t][REVERSE]; } /* copy the original counts into current counts */ copy_counts(B); nNumMotifs = copy_motif_num(IP); zero_motifs(IP); /* set motif count to zero */ /* If not running Wilcox test initialize random number gerator. */ /* Wilcox already did this */ if( ! IP->is_defined[cl_l] ) /* BT 7/23/97 */ sRandom(B, IP->lSeedVal); /* calculate the probability of NULL model */ /* IP->dnull_map = CalcMapProb(B, IP->is_defined[cl_R]); */ IP->dnull_map = CalcNullMap(B); /* 2/16/2001 */ reset_motif_num(nNumMotifs,IP); /* mark all possible motif sites */ if( ! IP->inCentroidAlign ) set_indicator_vector(Pos, B->Seq, B); maxnum = findMaxNumMotif(IP); if( IP->is_defined[cl_E] || RP->nUseSpacingProb || IP->is_defined[cl_T] ) /* BT 11/20/2000 */ { CountAlignments( B, Pos ); SaveAlignmentCounts( B ); } /* allocate space for start positions of motifs */ NEWP(startPos,IP->nNumMotifTypes, int); for(i = 0; i <IP->nNumMotifTypes ; i++) NEW(startPos[i],maxnum, int); /* Loop through for a number of different seeds */ while(seed_run++ < IP->nSeeds) { if( IP->is_defined[cl_X] ) { /* if( ! B->AN->bExchange ) B->AN->currTemp = B->AN->dMaxTemp; */ if( ! IP->is_defined[cl_Z] ) printf( "Current temperature: %7.2f\n", B->AN->currTemp ); } IP->nSeedRun = seed_run; if(!IP->is_defined[cl_F]) initMask(B); if( IP->is_defined[cl_X] ) init_maxdata( &(B->AN->results[seed_run]) ); /* initialize the Pos */ for(t=0;t<IP->nNumMotifTypes;t++) { Pos_t=Pos[t]; for(i = 0; i < IP->nSeqLen; i++) { Pos_t[i].nInMotif=FALSE; Pos_t[i].nMotifStartPos=FALSE; } } for( t= 0; t < IP->nNumMotifTypes; t++ ) /* BT 5/23/97 */ { IP->nMotifLen[t] = IP->nInputMotifLen[t]; /* restore original lengths */ SetPossibleSites( B, t ); /* restore original site count */ } if( IP->is_defined[cl_d] && ! IP->inCentroidAlign ) set_indicator_vector(Pos, B->Seq, B); if( ! IP->site_samp ) /* BT 7/16/97 */ set_posterior_prob(IP, B->C); /* Set initial posterior prob */ /* random select a set of motifs */ if( ! IP->is_defined[cl_V] ) /* BT 04/16/03 */ { if( B->InitPos == NULL || seed_run > 1 ) { if( IP->is_defined[cl_A] ) { set_posterior_prob(IP, B->C); for( t = 0; t < IP->nNumMotifTypes; t++ ) { IP->nNumMotifs[t][FORWARD] = 0; IP->nNumMotifs[t][REVERSE] = 0; } } set_random_sequences(B, Pos, startPos); /* set alignment &*/ } else { SetInitSequences( B, Pos, startPos ); /* BT 8/5/98 */ set_posterior_prob(IP, B->First); if( RP->bUsePosMatrix ) InitializePosMatrix( B ); else if( RP->bUseTrans ) InitializeTransMatrix( B ); } } else { if( B->InitPos == NULL ) { for( t = 0; t < IP->nNumMotifTypes; t++ ) { IP->nNumMotifs[t][FORWARD] = 0; IP->nNumMotifs[t][REVERSE] = 0; } } else { SetInitSequences( B, Pos, startPos ); set_counts( B ); /* BT 10/23/03 */ set_posterior_prob(IP, B->First); } set_posterior_prob(IP, B->C); } reset_counts(B, startPos, Pos); /* its counts */ if( B->InitPos != NULL && seed_run == 1 ) { RP->dInitProb = CalcMapProb( B, IP->is_defined[cl_R] ); if( ! IP->inCentroidAlign ) { fprintf( IP->Datafiles->out_fpt, "seed = %d Initial MAP = %.5f sites = %d \n", IP->nSeedRun, RP->dInitProb, TotalNumMotifs( B )); for( t = 0; t < IP->nNumMotifTypes; t++ ) { fprintf(IP->Datafiles->out_fpt, "Motif %d Map = %.5f Frag = %.5f\n", t, CalcMotifMap(B, t, IP->is_defined[cl_R]), CalcMotifFragMap(B, t, IP->is_defined[cl_R]) ); } } #ifdef _MPI_ PrintTempOut( IP->Datafiles->mpiTemp_fpt, "rank = %d process = %d seed = %d Initial MAP = %.5f sites = %d\n", IP->nRank, IP->nMPIProcesses, IP->nSeedRun, RP->dInitProb, TotalNumMotifs( B ) ); for( t = 0; t < IP->nNumMotifTypes; t++ ) { PrintTempOut( IP->Datafiles->mpiTemp_fpt, "%d Motif = %.5f Frag = %.5f\n", t, CalcMotifMap(B, t, IP->is_defined[cl_R]), CalcMotifFragMap(B, t, IP->is_defined[cl_R]) ); } PrintTempOut( IP->Datafiles->mpiTemp_fpt, "Bkgnd = %.5f Beta = %.5f Null = %.5f\n", CalcBkgndMap(B, IP->is_defined[cl_R]), CalcBetaMap( B, IP->is_defined[cl_R]), IP->dnull_map ); #else fprintf( stdout, "seed = %d Initial MAP = %.5f sites = %d\n", IP->nSeedRun, B->RP->dInitProb, TotalNumMotifs( B ) ); for( t = 0; t < IP->nNumMotifTypes; t++ ) { fprintf( stdout, "%d Motif = %.5f Frag = %.5f\n", t, CalcMotifMap(B, t, IP->is_defined[cl_R]), CalcMotifFragMap(B, t, IP->is_defined[cl_R]) ); } fprintf( stdout, "Bkgnd = %.5f Beta = %.5f Seq = %.5f Null = %.5f\n", CalcBkgndMap(B, IP->is_defined[cl_R]), CalcBetaMap( B, IP->is_defined[cl_R]), CalcSitePerSeqMap( B ), IP->dnull_map ); #endif for( t = 0; t < IP->nNumMotifTypes; t++ ) { fprintf( stdout, "-----------------------------------------------------------\n" ); fprintf( stdout, " MOTIF %c\n\n", (char)(97 + t)); DumpMotifPositions( t, B, Pos, stdout ); fprintf( stdout, "%d sites\n", NUMMOTIFS( IP->nNumMotifs[t]) ); } fflush( stdout ); } /* create a motif element list */ M = set_motif_info(IP, startPos, B->Seq); #ifdef _DEBUG_ /* ==================================================================== */ for( t = 0; t < IP->nNumMotifTypes; t++ ) { fprintf( stdout, "-----------------------------------------------------------\n" ); fprintf( stdout, " MOTIF %c\n\n", (char)(97 + t)); DumpMotifPositions( t, B, Pos, stdout ); fprintf( stdout, "%d sites\n", NUMMOTIFS( IP->nNumMotifs[t]) ); DumpCounts( B, t, stdout ); fflush( stdout ); } /* ==================================================================== */ #endif if( ! IP->is_defined[cl_Z] ) { put_prior(B); fprintf(stdout, "\r** %d **\n", seed_run); fflush( stdout ); /* BT 9/19/97 */ } if(seed_run > 1) { RestoreAlignmentCounts( B ); if(IP->site_samp) locMax = site_sampler(B, Pos, M); else if( IP->is_defined[cl_bayes] ) locMax = bayes_sampler(B, Pos, M, seed_run); else if( IP->is_defined[cl_E] ) locMax = rsite_sampler(B, Pos, M, seed_run); else locMax = motif_sampler(B,Pos,M); if( IP->is_defined[cl_X] ) CopyMaxResults( &(B->AN->results[seed_run]), &locMax, B ); dCurrProb = locMax.dProbability; if(locMax.dProbability > maxData.dProbability) { /* Check to see if current run is max */ free_maxdata(&maxData, IP); maxData = locMax; maxData.nSuboptSeed = seed_run; if( (! IP->is_defined[cl_Z]) && IP->is_defined[cl_opt] ) print_maxData(IP->nNumMotifTypes, maxData); } else free_maxdata(&locMax, IP); } else { /* Set Maximum first time through */ if(IP->site_samp) maxData = site_sampler(B, Pos, M); else if( IP->is_defined[cl_bayes] ) maxData = bayes_sampler(B, Pos, M, seed_run); else if( IP->is_defined[cl_E] ) maxData = rsite_sampler(B, Pos, M, seed_run); else maxData = motif_sampler(B,Pos,M); dCurrProb = maxData.dProbability; maxData.nSuboptSeed = seed_run; if( IP->is_defined[cl_X] ) CopyMaxResults( &(B->AN->results[seed_run]), &maxData, B ); if( (! IP->is_defined[cl_Z]) && IP->is_defined[cl_opt] ) print_maxData(IP->nNumMotifTypes, maxData); } reset_motif_num(nNumMotifs, IP); copy_counts(B); /* Counts w/o motifs */ free_motifs(B, M); #ifdef _MPI_ if( IP->is_defined[cl_hm] ) { if( seed_run == IP->nSeeds ) SendSuboptMsg( B, G_MPI_SUBOPT_DONE, seed_run ); else SendSuboptMsg( B, G_MPI_SEED_DONE, seed_run ); } else { dTempInfo[1] = dCurrProb; dTempInfo[3] = 0; if( IP->is_defined[cl_opt] ) { for(t = 0; t < IP->nNumMotifTypes; t++) { dTempInfo[3] += (double) maxData.nNumMotifs[t]; } } Gibbs_MPI_Send( B, dTempInfo, 4, MPI_DOUBLE, 0, G_MPI_DATA, MPI_COMM_WORLD ); } Gibbs_MPI_Recv( B, dTempInfo, 4, MPI_DOUBLE, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status ); if( status.MPI_TAG == G_MPI_DONE ) { PrintTempOut( IP->Datafiles->mpiTemp_fpt, "Received MPI_DONE signal\n" ); break; } else if( status.MPI_TAG == G_MPI_FINISH ) { PrintTempOut( IP->Datafiles->mpiTemp_fpt, "Received subopt end signal\n" ); break; } #endif }/* end of while loop, at this time we have MaxData */ FREEP(startPos,IP->nNumMotifTypes ); if(!IP->is_defined[cl_F] && maxData.F) { for(t = 0; t < IP->nNumMotifTypes; t++) /* BT 2/7/97 */ { for(n = 0; n < B->F->nMaxLen[t]; n++) B->F->nColMask[t][n] = maxData.F->nColMask[t][n]; B->F->FragWidth[t] = maxData.F->FragWidth[t]; for( n = 0; n < IP->nMotifLen[t]; n++ ) B->F->fragPos[t][n] = maxData.F->fragPos[t][n]; } } for(t = 0; t < IP->nNumMotifTypes; t++) { /* Add in motifs */ if( maxData.nMotifLen ) IP->nMotifLen[t] = maxData.nMotifLen[t]; /* Reset Pos in case width changed */ if( ! IP->inCentroidAlign ) set_indicator_vector(Pos, B->Seq, B); /* BT 9/12/97 */ if( maxData.nNumMotifs ) { for(i = 0; i < maxData.nNumMotifs[t]; i++) /* from the maximum */ adjust_counts(B, ADD, maxData.nMotifLoc[i][t], /* alignment */ t, maxData.RevComp[i][t]); } } if( maxData.nNumMotifs ) { setMotifNum(IP, maxData); /* BT 5/30/97 */ } if( IP->is_defined[cl_u] ) print_info(B, maxData, TRUE, SUBOPT); /* BT 3/19/97 */ if( IP->is_defined[cl_d] && IP->is_defined[cl_q] ) GetWidthCounts( B ); FREEP(nNumMotifs, IP->nNumMotifTypes); free(nNumMotifs); if( IP->is_defined[cl_Q] ) /* BT 3/27/98 */ { fprintf( IP->Datafiles->occur_fpt, "seq pos pos2 motif count\n" ); for( i = 0; i < IP->nNumSequences; i++ ) { for( j = 0; j < SequenceLength( B, i ); j++ ) { nPos = SequenceStartPos( B, i ) + j; for( t = 0; t < IP->nNumMotifTypes; t++ ) { for( sum = 0, k = 1; k <= IP->nSeeds; k++ ) { sum += IP->nAlignCnts[k][t][i][j]; } fprintf( IP->Datafiles->occur_fpt, "%5d %5d %5d %5d %5d\n", i, j, nPos, t, sum ); } } fflush( IP->Datafiles->occur_fpt ); } } if( ! IP->is_defined[cl_Z] && ! IP->is_defined[cl_nopt] ) printf( "Max subopt MAP found on seed %d\n", maxData.nSuboptSeed ); return maxData; }
void process_cmd(char *readbuf, int length) { typedef enum pipe_cmd_type{ca,im,tl,px,bo,tv,an,as,at,ac,ab,sh,co,br,sa,is,vs,rl,ec,em,wb,mm,ie,ce,ro,fl,ri,ss,qu,pv,bi,ru,md,sc,rs,bu,mn,mt,mi,mb,me,mx,mf,vm,vp,wd,sy,cn,st} pipe_cmd_type; char pipe_cmds[] = "ca,im,tl,px,bo,tv,an,as,at,ac,ab,sh,co,br,sa,is,vs,rl,ec,em,wb,mm,ie,ce,ro,fl,ri,ss,qu,pv,bi,ru,md,sc,rs,bu,mn,mt,mi,mb,me,mx,mf,vm,vp,wd,sy,cn,st"; pipe_cmd_type pipe_cmd; int parcount; char pars[128][10]; long int par0; char cmd[3]; char par[MAX_COMMAND_LEN]; char *parstring=0, *temp; int key = -1; if (length < 2 || length > (MAX_COMMAND_LEN - 2)) return; //Get cmd strncpy(cmd, readbuf, 2); //find 2 letter command and translate into enum temp = strstr(pipe_cmds, cmd); if (temp == NULL) return; pipe_cmd = (pipe_cmd_type)((temp - pipe_cmds) / 3); if(length > 3) { strcpy(par, readbuf + 3); par[length-3] = 0; //extract space separated numeric parameters // and make separate string parameter (strtok changes the original) asprintf(&parstring, "%s", par); parcount = 0; temp = strtok(par, " "); while(parcount<10 && temp != NULL) { strcpy(pars[parcount], temp); parcount++; temp = strtok(NULL, " "); } par0 = strtol(pars[0], NULL, 10); } else { par0 = 0; } switch(pipe_cmd) { case ca: if(par0 == 1) { if (parcount > 1) { long vtime = strtol(pars[1], NULL, 10); video_stoptime = time(NULL) + vtime; printLog("Capturing %d seconds\n", vtime); } start_video(0); } else { stop_video(0); } break; case im: capt_img(); break; case tl: if(par0) { timelapse = 1; lapse_cnt = 1; updateStatus(); printLog("Timelapse started\n"); } else { image2_cnt++; timelapse = 0; updateStatus(); printLog("Timelapse stopped\n"); } break; case px: stop_all(); addUserValue(c_video_width, pars[0]); addUserValue(c_video_height, pars[1]); addUserValue(c_video_fps, pars[2]); addUserValue(c_MP4Box_fps, pars[3]); addUserValue(c_image_width, pars[4]); addUserValue(c_image_height, pars[5]); start_all(0); break; case bo: addUserValue(c_MP4Box, pars[0]); break; case tv: addUserValue(c_tl_interval, pars[0]); break; case an: addUserValue(c_annotation, parstring); break; case as: addUserValue(c_anno_text_size, pars[0]); break; case at: addUserValue(c_anno3_custom_text_colour, pars[0]); addUserValue(c_anno3_custom_text_Y, pars[1]); addUserValue(c_anno3_custom_text_U, pars[2]); addUserValue(c_anno3_custom_text_V, pars[3]); break; case ac: addUserValue(c_anno3_custom_background_colour, pars[0]); addUserValue(c_anno3_custom_background_Y, pars[1]); addUserValue(c_anno3_custom_background_U, pars[2]); addUserValue(c_anno3_custom_background_V, pars[3]); break; case ab: addUserValue(c_anno_background, pars[0]); break; case sh: key = c_sharpness; break; case co: key = c_contrast; break; case br: key = c_brightness; break; case sa: key = c_saturation; break; case is: key = c_iso; break; case vs: key = c_video_stabilisation; break; case rl: key = c_raw_layer; break; case ec: key = 1000 + c_exposure_compensation; break; case em: key = 1000 + c_exposure_mode; break; case wb: key = 1000 + c_white_balance; break; case mm: key = 1000 + c_metering_mode; break; case ie: key = 1000 + c_image_effect; break; case ce: addUserValue(c_colour_effect_u, pars[1]); addUserValue(c_colour_effect_v, pars[2]); key = c_colour_effect_en; break; case ro: key = c_rotation; break; case fl: if(par0 & 1) addUserValue(c_hflip, "1"); else addUserValue(c_hflip, "0"); if((par0 >> 1) & 1) addUserValue(c_vflip, "1"); else addUserValue(c_vflip, "0"); cam_set(c_hflip); break; case ri: addUserValue(c_sensor_region_y, pars[1]); addUserValue(c_sensor_region_w, pars[2]); addUserValue(c_sensor_region_h, pars[3]); key = c_sensor_region_x; break; case ss: addUserValue(c_shutter_speed, pars[0]); key = c_shutter_speed; break; case qu: key = c_image_quality; break; case pv: stop_all(); addUserValue(c_quality, pars[0]); addUserValue(c_width, pars[1]); addUserValue(c_divider, pars[2]); start_all(0); break; case bi: stop_all(); addUserValue(c_video_bitrate, pars[0]); start_all(0); break; case st: stop_all(); addUserValue(c_stat_pass, pars[0]); start_all(0); break; case wd: addUserValue(c_watchdog_interval, pars[0]); addUserValue(c_watchdog_errors, pars[1]); break; case ru: if (par0 == 0) { stop_all(); idle = 1; printLog("Stream halted\n"); } else { start_all(1); idle = 0; printLog("Stream continued\n"); } updateStatus(); break; case mx: key = c_motion_external; //If switching to internal with motion detection on then try to kill external motion if (cfg_val[c_motion_detection] != 0 && !par0) { if(system("killall motion") == -1) error("Could not stop external motion", 1); printLog("External motion detection stopped\n"); } break; case md: exec_macro(cfg_stru[c_do_cmd], readbuf); stop_all(); if (cfg_val[c_motion_external]) { if(par0 == 0) { if(system("killall motion") == -1) error("Could not stop external motion", 1); printLog("External motion detection stopped\n"); } else { if (cfg_val[c_motion_detection] == 0) { if(system("motion") == -1) error("Could not start external motion", 1); printLog("External motion detection started\n"); } else { printLog("Motion already running. md 1 ignored\n"); } } } else { if(par0 == 0) { printLog("Internal motion detection stopped\n"); } else { printLog("Internal motion detection started\n"); } } cfg_val[c_motion_detection] = par0?1:0; start_all(0); updateStatus(); break; case sc: set_counts(); printLog("Scan for highest count\n"); break; case rs: printLog("Reset settings to defaults\n"); stop_all(); read_config("/etc/raspimjpeg", 1); saveUserConfig(cfg_stru[c_user_config]); start_all(0); break; case bu: key = c_video_buffer; break; case vp: stop_all(); addUserValue(c_vector_preview, pars[0]); start_all(0); break; case mn: key = c_motion_noise; break; case mt: key = c_motion_threshold; break; case mi: key = c_motion_image + 1000; break; case mb: key = c_motion_startframes; break; case me: key = c_motion_stopframes; break; case mf: key = c_motion_file; break; case vm: key = c_vector_mode; break; case sy: exec_macro(parstring, NULL); break; case cn: stop_all(); addUserValue(c_camera_num, pars[0]); start_all(0); break; default: printLog("Unrecognised pipe command\n"); break; }