Exemplo n.º 1
0
int single_state_coin_toss()
{
  state single_state;
  model my_model;
  double symbols_single_state[2]={0.5,0.5};
  double trans_prob_single_state[1]={1.0};
  double trans_prob_single_state_rev[1]={1.0};
  int trans_id_single_state[1]={0};
  sequence_t* my_output;
  int silent_array[2] =  {0}; 

  my_model.model_type = 0;
  /* initialise this state */
  single_state.pi = 1.0;
  single_state.b=symbols_single_state;
  single_state.out_states=1;
  single_state.out_a=trans_prob_single_state;
  single_state.out_id=trans_id_single_state;
  single_state.in_states=1;
  single_state.in_id=trans_id_single_state;
  single_state.in_a=trans_prob_single_state_rev;
  single_state.fix=1;

  /* initialise model */
  my_model.N=1;
  my_model.M=2;
  my_model.s=&single_state;
  my_model.prior=-1;
  my_model.silent = silent_array;

  fprintf(stdout,"transition matrix:\n");
  model_A_print(stdout,&my_model,""," ","\n");
  fprintf(stdout,"observation symbol matrix:\n");
  model_B_print(stdout,&my_model,""," ","\n");

  my_output=model_generate_sequences(&my_model,0,10,10,100);
  sequence_print(stdout,my_output);

  sequence_free(&my_output);
  return 0;
}
Exemplo n.º 2
0
int main(int argc, char **argv){
 
  FILE *IN, *OUT;
  seqlist_t IDs;
  sequence_t *seq;

  int i, keep, excl;
  char *prog, *list, *f, *out, *id;
  /* todo convert to uint_64 */
  uint64_t n, total;

  /* default values */
  prog = basename(*argv);
  list = NULL;
  out = NULL;
  OUT = stdout; 
  excl = keep = 0;

  /* Check command line */
  while ((i = getopt(argc, argv, "hl:o:x")) != -1) {
    switch (i) {
    case 'h':
      usage(prog, EXIT_SUCCESS); break;
	 case 'l':
		list = optarg; break;
	 case 'o':
		out = optarg; break;
	 case 'x':
		excl = 1; break;
	 default:
		usage(prog, EXIT_FAILURE); 
	 }
  }
  /* some files to deal with ? */
  if (argc - optind < 1) { usage(prog,  EXIT_FAILURE); }

  /* get ids from list */
  if (list != NULL) {
	 if ((IN = fopen(list, "r")) == NULL) 
		err(EXIT_FAILURE, "%s", list);

	 seqlist_init(&IDs);
	 i = seqlist_populate(&IDs, IN);
	 if (i == 0) errx(EXIT_FAILURE, "file: %s no identifier found", list); 
	 
	 if (fclose(IN) == EOF) err(EXIT_FAILURE, "%s: close failed", list);

    seqlist_sort(&IDs); 
  }

  /* open output file */
  if ((out != NULL) && ((OUT = fopen(out, "w")) == NULL))
	 err(EXIT_FAILURE, "%s", out);

  total = n = 0;

  /* start filtering all argument file*/
  for (i = optind; i < argc; i++) {
    f = *(argv+i);
	 if ((IN = fopen(f, "r")) == NULL) err(EXIT_FAILURE, "%s", f);
	 
	 while ((seq = sequence_parse(IN, SEQFMT_FASTA)) != NULL) {
		total++;
		id = seq->nam;
		keep = 1;
		if (list != NULL) { keep = 1 - seqlist_chk(&IDs, id); }
		if (excl == 1)   { keep = 1 - keep; }
		if (keep == 1) { n++; sequence_print(OUT, seq, SEQFMT_FASTA); }
		sequence_free(seq);
	 }//end while
	 
	  /* check for parsing error vs EOF */
	 if ((feof(IN) == 0) || (total == 0)){
		errx(1, "%s: unable to read sequence %d", f, total+1);
	 }
	 
	 if (fclose(IN) == EOF) err(EXIT_FAILURE, "%s: close failed", f);
  }//end for files
  
  if ((out != NULL) && (fclose(OUT) == EOF)) err(EXIT_FAILURE, "%s", out);
  if (list != NULL) seqlist_fini(&IDs);
  
  /*display resume on stderr */
  fprintf(stderr, "file %s: Extracted %"PRI_U64" from %"PRI_U64" sequences (%.2f %%)\n" ,
          basename(f), n , total, (float)(n)/total*100.0);

  return EXIT_SUCCESS; 
}
Exemplo n.º 3
0
int two_states_coin_toss()
{
  model my_model;
  state model_states[2];
  double symbols_head_state[2]={1.0,0.0};
  double trans_prob_head_state[2]={0.5,0.5};
  double trans_prob_head_state_rev[2]={0.5,0.5};
  int trans_id_head_state[2]={0,1};
  double symbols_tail_state[2]={0.0,1.0};
  double trans_prob_tail_state[2]={0.5,0.5};
  double trans_prob_tail_state_rev[2]={0.5,0.5};
  int trans_id_tail_state[2]={0,1};
  sequence_t *my_output;
  double log_p_viterbi, log_p_forward;
  double **forward_alpha;
  double forward_scale[10];
  int *viterbi_path;
  int i;
  /* flags indicating whether a state is silent */
  int silent_array[2] =  {0,0}; 

  my_model.model_type = 0;
  /* initialise head state */
  model_states[0].pi = 0.5;
  model_states[0].b=symbols_head_state;
  model_states[0].out_states=2;
  model_states[0].out_a=trans_prob_head_state;
  model_states[0].out_id=trans_id_head_state;
  model_states[0].in_states=2;
  model_states[0].in_id=trans_id_head_state;
  model_states[0].in_a=trans_prob_head_state_rev;
  model_states[0].fix=1;

  /* initialise tail state */
  model_states[1].pi = 0.5;
  model_states[1].b=symbols_tail_state;
  model_states[1].out_states=2;
  model_states[1].out_id=trans_id_tail_state;
  model_states[1].out_a=trans_prob_tail_state;
  model_states[1].in_states=2;
  model_states[1].in_id=trans_id_tail_state;
  model_states[1].in_a=trans_prob_tail_state_rev;
  model_states[1].fix=1;

  /* initialise model */
  my_model.N=2;
  my_model.M=2;
  my_model.s=model_states;
  my_model.prior=-1;
  my_model.silent = silent_array;
  
  fprintf(stdout,"transition matrix:\n");
  model_A_print(stdout,&my_model,""," ","\n");
  fprintf(stdout,"observation symbol matrix:\n");
  model_B_print(stdout,&my_model,""," ","\n");

  my_output=model_generate_sequences(&my_model,0,10,10,100);
  sequence_print(stdout,my_output);

  /* try viterbi algorithm in a clear situation */
  viterbi_path=viterbi(&my_model,
		       my_output->seq[0],
		       my_output->seq_len[0],
		       &log_p_viterbi);
  if (viterbi_path==NULL)
    {fprintf(stderr,"viterbi failed!"); return 1;}

  fprintf(stdout,"viterbi:\n");
  
  for(i=0;i<my_output->seq_len[0];i++){
    printf(" %d, ", viterbi_path[i]);
  }
  printf("\n");

  fprintf(stdout,
	  "log-p of this sequence (viterbi algorithm): %f\n",
	  log_p_viterbi);

  /* allocate matrix for forward algorithm */
  fprintf(stdout,"applying forward algorithm to the sequence...");
  forward_alpha=stat_matrix_d_alloc(10,2);
  if (forward_alpha==NULL)
    {
      fprintf(stderr,"\n could not alloc forward_alpha matrix\n");
      return 1;
    }

  /* run foba_forward */
  if (foba_forward(&my_model,
		   my_output->seq[0],
		   my_output->seq_len[0],
		   forward_alpha,
		   forward_scale,
		   &log_p_forward))
    {
      fprintf(stderr,"foba_logp failed!");
      stat_matrix_d_free(&forward_alpha);
      return 1;
    }

  /* alpha matrix */
  fprintf(stdout,"Done.\nalpha matrix from forward algorithm:\n");
  matrix_d_print(stdout,forward_alpha,10,2,""," ","\n");
  fprintf(stdout,"log-p of this sequence (forward algorithm): %f\n",log_p_forward);
  
  /* clean up */
  sequence_free(&my_output);
  free(viterbi_path);
  stat_matrix_d_free(&forward_alpha);
  return 0;
}