示例#1
0
void kill_child(JNIEnv *env, jclass clazz __attribute__((unused)), int id, int signal) {
  message *m;
  int send_error;
  struct cmd_signal_info *signal_info;
  
  m = create_message(get_sequence(&ctrl_seq, &ctrl_seq_lock),
                     sizeof(struct cmd_signal_info), CTRL_ID);
  
  if(!m) {
    LOGE("%s: cannot crate messages", __func__);
    return;
  }
  
  signal_info = (struct cmd_signal_info *) m->data;
  
  signal_info->cmd_action = CMD_SIGNAL;
  signal_info->id = id;
  signal_info->signal = signal;
  
  pthread_mutex_lock(&write_lock);
  send_error = send_message(sockfd, m);
  pthread_mutex_unlock(&write_lock);
  
  if(send_error) {
    LOGE("%s: cannot send messages", __func__);
  }
}
data_chunk deterministic_wallet::generate_public_key(
    size_t n, bool for_change) const
{
    hash_digest sequence = get_sequence(n, for_change);

    ssl_bignum x, y, z;
    BN_bin2bn(sequence.data(), sequence.size(), z);
    BN_bin2bn(master_public_key_.data(), 32, x);
    BN_bin2bn(master_public_key_.data() + 32, 32, y);

    // Create a point.
    ec_group group(EC_GROUP_new_by_curve_name(NID_secp256k1));
    ec_point mpk(EC_POINT_new(group));
    bn_ctx ctx(BN_CTX_new());
    EC_POINT_set_affine_coordinates_GFp(group, mpk, x, y, ctx);
    ec_point result(EC_POINT_new(group));

    // result pubkey_point = mpk_pubkey_point + z*curve.generator
    ssl_bignum one;
    BN_one(one);
    EC_POINT_mul(group, result, z, mpk, one, ctx);

    // Create the actual public key.
    EC_POINT_get_affine_coordinates_GFp(group, result, x, y, ctx);
    // 04 + x + y
    data_chunk raw_pubkey{0x04};
    extend_data(raw_pubkey, bignum_data(x));
    extend_data(raw_pubkey, bignum_data(y));
    return raw_pubkey;
}
示例#3
0
/**
 * @brief notify the command receiver that a chid printed a line on it's stderr.
 * @param c the child hat generated @p line
 * @param line the line received from the child stderr
 * @returns 0 on success, -1 on error.
 */
int on_cmd_stderr(child_node *c, char *line) {
  message *m;
  struct cmd_stderr_info *stderr_info;
  size_t len;
  uint16_t seq;
  
  seq = get_sequence(&(c->conn->ctrl_seq), &(c->conn->control.mutex));
  
  len = strlen(line) + 1;
  
  m = create_message(seq, sizeof(struct cmd_stderr_info) + len, CTRL_ID);
  
  if(!m) {
    print(ERROR, "cannot create messages");
    return -1;
  }
  
  stderr_info = (struct cmd_stderr_info *) m->data;
  stderr_info->cmd_action = CMD_STDERR;
  stderr_info->id = c->id;
  memcpy(stderr_info->line, line, len);
  
  if(enqueue_message(&(c->conn->outcoming), m)) {
    print(ERROR, "cannot enqueue messages");
    dump_message(m);
    free_message(m);
    return -1;
  }
  
  return 0;
}
示例#4
0
static int fetch_disc_read_callback(const bam1_t* alignment, void* data) {
    //    MEI_data* mei_data = static_cast<MEI_data*>(data);
    std::pair<MEI_data*, UserDefinedSettings*>* env = static_cast<std::pair<MEI_data*, UserDefinedSettings*>*>(data);
    MEI_data* mei_data = env->first;
    UserDefinedSettings* userSettings = env->second;
    if (!(alignment->core.flag & BAM_FUNMAP || alignment->core.flag & BAM_FMUNMAP) && // Both ends are mapped.
        !is_concordant(alignment, mei_data->current_insert_size) &&                   // Ends map discordantly.
        // Extra check for (very) large mapping distance.  This is done beside the check for read
        // discordance to speed up computation by ignoring signals from small structural variants.
        (alignment->core.tid != alignment->core.mtid ||
         abs(alignment->core.pos - alignment->core.mpos) > userSettings->MIN_DD_MAP_DISTANCE)) {
            
            // Save alignment as simple_read object.
            std::string read_name = enrich_read_name(bam1_qname(alignment), alignment->core.flag & BAM_FREAD1);
            char strand = bam1_strand(alignment)? Minus : Plus;
            char mate_strand = bam1_mstrand(alignment)? Minus : Plus;
            std::string read_group;
            get_read_group(alignment, read_group);
            std::string sample_name;
            get_sample_name(read_group, mei_data->sample_names, sample_name);
            
            simple_read* read = new simple_read(read_name, alignment->core.tid, alignment->core.pos, strand, sample_name,
                                                get_sequence(bam1_seq(alignment), alignment->core.l_qseq),
                                                alignment->core.mtid, alignment->core.mpos, mate_strand);
            mei_data->discordant_reads.push_back(read);
        }
    return 0;
}
示例#5
0
void get_sequences(FILE *input, int *nseq,char ***seqs,char ***names)
{ int    maxl, k;
  char **seqa, **seqn;
  char **namea, **namen;

  maxl  = 32;
  seqa = (char **) safe_malloc(maxl*sizeof(char *));
  namea = (char **) safe_malloc(maxl*sizeof(char *));
  firstget=1;
  k = 0;
  while (1)
    { for (; k < maxl; k++)
        { if (get_sequence(input,&(seqa[k]),&(namea[k]))== NULL) break;
        }
      if (k < maxl) break;
      seqn = (char **) safe_malloc(2*maxl*sizeof(char *));
      namen = (char **) safe_malloc(2*maxl*sizeof(char *));
      for (k = 0; k < maxl; k++){
        seqn[k] = seqa[k];
        namen[k] = namea[k];
      }
      safe_free(seqa);
      safe_free(namea);
      seqa = seqn;
      namea = namen;
      maxl *= 2;
    }

  *nseq = k-1;
  *seqs=seqa;
  *names=namea;
}
示例#6
0
int subsample(int nseqs, char *fastqfile, struct fastq *seqs[], int compressed) {


	// read the fastq file into a temporary hash
	struct fastq *allseqs[HASHSIZE] = {NULL};

	int read_seqs = 0;
	if (compressed)
		read_seqs = read_fastq_gz(fastqfile, allseqs);
	else
		read_seqs = read_fastq(fastqfile, allseqs);

	if (read_seqs < nseqs) {
		fprintf(stderr, "You requested %d sequences but there are only %d in the file!\n", nseqs, read_seqs);
		nseqs = read_seqs;
	}

	// get all the ids from the sequences
	char *ids[read_seqs];
	get_ids(ids, allseqs);

	// subsample those IDs 
	char **subsample = subsample_n(read_seqs, ids, nseqs);

	for (int i=0; i<nseqs; i++) {
		char *info = get_seq_information(subsample[i], seqs);
		char *seq = get_sequence(subsample[i], seqs);
		char *qua = get_quality(subsample[i], seqs);
	
	}
	return nseqs;
}
secret_parameter deterministic_wallet::generate_secret(
    size_t n, bool for_change)
{
    if (seed_.empty())
        return null_hash;

    ssl_bignum z;
    hash_digest sequence = get_sequence(n, for_change);
    BN_bin2bn(sequence.data(), sequence.size(), z);

    ec_group group(EC_GROUP_new_by_curve_name(NID_secp256k1));
    ssl_bignum order;
    bn_ctx ctx(BN_CTX_new());
    EC_GROUP_get_order(group, order, ctx);

    // secexp = (stretched_seed + z) % order
    ssl_bignum secexp;
    BN_bin2bn(stretched_seed_.data(), stretched_seed_.size(), secexp);
    BN_add(secexp, secexp, z);
    BN_mod(secexp, secexp, order, ctx);

    secret_parameter secret;
    BITCOIN_ASSERT(BN_num_bytes(secexp) == secret.size());
    BN_bn2bin(secexp, secret.data());
    return secret;
}
示例#8
0
文件: control.c 项目: GCrean/libxmp
int xmp_seek_time(xmp_context opaque, int time)
{
	struct context_data *ctx = (struct context_data *)opaque;
	struct player_data *p = &ctx->p;
	struct module_data *m = &ctx->m;
	int i, t;

	if (ctx->state < XMP_STATE_PLAYING)
		return -XMP_ERROR_STATE;

	for (i = m->mod.len - 1; i >= 0; i--) {
		int pat = m->mod.xxo[i];
		if (pat >= m->mod.pat) {
			continue;
		}
		if (get_sequence(ctx, i) != p->sequence) {
			continue;
		}
		t = m->xxo_info[i].time;
		if (time >= t) {
			set_position(ctx, i, 1);
			break;
		}
	}
	if (i < 0) {
		xmp_set_position(opaque, 0);
	}

	return p->pos < 0 ? 0 : p->pos;
}
secret_parameter deterministic_wallet::generate_secret(
    size_t n, bool for_change) const
{
    if (seed_.empty())
        return null_hash;

    ssl_bignum z;
    hash_digest sequence = get_sequence(n, for_change);
    BN_bin2bn(sequence.data(), sequence.size(), z);

    ec_group group(EC_GROUP_new_by_curve_name(NID_secp256k1));
    ssl_bignum order;
    bn_ctx ctx(BN_CTX_new());
    EC_GROUP_get_order(group, order, ctx);

    // secexp = (stretched_seed + z) % order
    ssl_bignum secexp;
    BN_bin2bn(stretched_seed_.data(), stretched_seed_.size(), secexp);
    BN_add(secexp, secexp, z);
    BN_mod(secexp, secexp, order, ctx);

    secret_parameter secret;
    int secexp_bytes_size = BN_num_bytes(secexp);
    BITCOIN_ASSERT(secexp_bytes_size >= 0 &&
        static_cast<size_t>(BN_num_bytes(secexp)) <= secret.size());
    // If bignum value begins with 0x00, then
    // SSL will skip to the first significant digit.
    size_t copy_offset = secret.size() - BN_num_bytes(secexp);
    BN_bn2bin(secexp, secret.data() + copy_offset);
    // Zero out beginning 0x00 bytes (if they exist).
    std::fill(secret.begin(), secret.begin() + copy_offset, 0x00);
    return secret;
}
示例#10
0
/* Get the set of key mappings for a single modifier combination.
   Mappings are appended to "head". Returns 0 on success and -1 on failure. */
static int getkeys(name_mapping_t *keys, int max, int mod) {
  sequence_t *current, *new_seq;
  int i;

  for (i = 0; i < max; i++) {
    /* Check if the key is blocked from the command line. */
    if (blocked_keys != NULL) {
      name_mapping_t key_mod[2] = {keys[i], modifiers[mod]};
      if (lfind(&key_mod, blocked_keys, &blocked_keys_fill, sizeof(char *),
                (int (*)(const void *, const void *))check_key) != NULL) {
        printf("Skipping blocked key %s%s\n", modifiers[mod].name, keys[i].name);
        continue;
      }
    }

    printf("%s%s ", modifiers[mod].name, keys[i].name);
    fflush(stdout);

#ifndef NO_AUTOLEARN
    /* When auto learning, just send the appropriate events to the terminal. */
    if (option_auto_learn) {
      send_event(keys[i].keysym, modifiers[mod].keysym);
    }
#endif

    /* Retrieve key sequence. */
    new_seq = get_sequence();
    if (new_seq == NULL) {
      printf("\n");
      continue;
    } else if (new_seq == (void *)-1) {
      return -1;
    }
    printf("%s ", new_seq->seq);
    current = head;
    /* Check for duplicate sequences, and set the duplicate field if one is found. */
    while (current != NULL) {
      if (strcmp(current->seq, new_seq->seq) == 0 && current->duplicate == NULL) {
        printf("(duplicate for %s%s)", current->modifiers->name, current->keynames->name);
        new_seq->duplicate = current;
        break;
      } else {
        current = current->next;
      }
    }

    new_seq->modifiers = modifiers + mod;
    new_seq->keynames = keys + i;
    new_seq->next = head;
    head = new_seq;
    printf("\n");
  }
  return 0;
}
示例#11
0
/**
 * @brief send handlers definitions
 * @param conn the ::connection to send these definitions
 * @returns 0 on success, -1 on error.
 */
int send_handlers_list(conn_node *conn) {
    handler *h;
    message *m;
    struct hndl_list_info *handlers_info;
    struct hndl_info *handler_info;
    int ret;
    size_t array_size;

    ret=-1;
    array_size=0;

    for(h=(handler *) handlers.head; h; h=(handler *) h->next) {
        array_size += sizeof(struct hndl_info);
        array_size += strlen(h->name) +1;
    }

    m = create_message(get_sequence(&(conn->ctrl_seq), &(conn->control.mutex)),
                       sizeof(struct hndl_list_info) + array_size,
                       CTRL_ID);

    if(!m) {
        print( ERROR, "cannot create messages" );
        goto exit;
    }

    handlers_info = (struct hndl_list_info *) m->data;
    handlers_info->hndl_code = HNDL_LIST;

    handler_info = handlers_info->list;

    for(h=(handler *) handlers.head; h; h=(handler *) h->next) {
        handler_info->id = h->id;
        handler_info->have_stdin = h->have_stdin;
        handler_info->have_stdout = h->have_stdout;

        strcpy(handler_info->name, h->name);

        handler_info = (struct hndl_info *) (
                           ((char *) handler_info) + sizeof(struct hndl_info) + strlen(h->name) + 1);
    }

    if(enqueue_message(&(conn->outcoming), m)) {
        print( ERROR, "cannot enqueue message" );
        dump_message(m);
        free_message(m);
    } else {
        ret = 0;
    }

exit:

    return ret;
}
示例#12
0
文件: control.c 项目: GCrean/libxmp
static void set_position(struct context_data *ctx, int pos, int dir)
{
	struct player_data *p = &ctx->p;
	struct module_data *m = &ctx->m;
	struct xmp_module *mod = &m->mod;
	struct flow_control *f = &p->flow;
	int seq, start;

	/* If dir is 0, we can jump to a different sequence */
	if (dir == 0) {
		seq = get_sequence(ctx, pos);
	} else {
		seq = p->sequence;
	}

	if (seq == 0xff)
		return;

	start = m->seq_data[seq].entry_point;

	if (seq >= 0) {
		p->sequence = seq;

		if (pos >= 0) {
			if (mod->xxo[pos] == 0xff) {
				return;
			}

			while (mod->xxo[pos] == 0xfe && pos > start) {
				if (dir < 0) {
					pos--;
				} else {
					pos++;
				}
			}

			if (pos > p->scan[seq].ord) {
				f->end_point = 0;
			} else {
				f->num_rows = mod->xxp[mod->xxo[p->ord]]->rows;
				f->end_point = p->scan[seq].num;
			}
		}

		if (pos < m->mod.len) {
			if (pos == 0) {
				p->pos = -1;
			} else {
				p->pos = pos;
			}
		}
	}
}
示例#13
0
static inline void	treat_aknowledgment(edcl_packet_t*	packet,
					    unsigned int*	offset,
					    unsigned int*	size,
					    unsigned int*	sequence,
					    unsigned int*	nb_ack)

{
	/* We've been aknowledge, keep sending */
	if (get_ack(packet->header)) {
		*offset += min(*size, MAX_DATA_SIZE);
		*size -= min(*size, MAX_DATA_SIZE);
		++(*sequence);
		++(*nb_ack);
	}
	/* The sequence number was wrong, fix it */
	else {
		if (config.verbose)
			fprintf(stderr,
				"  Wrong sequence number (%d) should be: %d.\n",
				*sequence, get_sequence(packet->header));
		*sequence = get_sequence(packet->header);
	}
}
示例#14
0
alignment_block array_next_alignment(maf_array_parser parser){
   if(parser->curr_block==parser->size) return NULL;
   ++parser->curr_block;
   int check= fseek(parser->maf_file,
        parser->alignment_blocks[parser->curr_block],SEEK_SET);
   if(check !=0){
      fprintf(stderr,"File seek error: %s\n",strerror(errno));
      return NULL;
   }
   char buffer[4096];
//First read 'a' line and initialize alignment struct
   alignment_block new_align=malloc(sizeof(*new_align));
   assert(new_align != NULL);
   new_align->sequences = malloc(2*sizeof(*new_align->sequences));
   assert(new_align->sequences != NULL);
   new_align->size=new_align->curr_seq=0;
   new_align->max=2;
   char *fc = fgets(buffer,4096,parser->maf_file);
   if(ferror(parser->maf_file) != 0){
          fprintf(stderr, "File stream error: %s\nError: %s",
             parser->filename,strerror(errno));
          return NULL;
   }
//***HANDLE SCORE/PASS/DATA here***
   new_align->data = NULL;
   seq new_seq;
   while(!feof(parser->maf_file)){
      char *fc = fgets(buffer,4096,parser->maf_file);
      if(ferror(parser->maf_file) != 0){
          fprintf(stderr, "File stream error: %s\nError: %s",
             parser->filename,strerror(errno));
          return NULL;
      }
      if(buffer[0]!='s')break;
      new_seq = get_sequence(buffer);
      if(new_seq == NULL){
        fprintf(stderr, "Invalid sequence entry %s\n",buffer);
        return NULL;
      }
      if(new_align->size ==new_align->max){
          new_align->sequences=realloc(new_align->sequences,
             2*new_align->max*sizeof(seq));
          assert(new_align->sequences!=NULL);
          new_align->max *=2;
      }new_align->sequences[new_align->size++]=new_seq;
   }
   return new_align;
}
示例#15
0
/**
 * @brief notify the command receiver that a child has exited
 * @param c the terminated child
 * @param status the child status returned by waitpid()
 * @returns 0 on success, -1 on error.
 */
int on_child_done(child_node *c, int status) {
  struct message *msg;
  struct cmd_end_info *end_info;
  struct cmd_died_info *died_info;
  uint16_t seq;
  
  seq = get_sequence(&(c->conn->ctrl_seq), &(c->conn->control.mutex));
  
  if(WIFEXITED(status)) {
    msg = create_message(seq, sizeof(struct cmd_end_info), CTRL_ID);
  } else {
    msg = create_message(seq, sizeof(struct cmd_died_info), CTRL_ID);
  }
  
  if(!msg) {
    print( ERROR, "cannot create messages" );
    return -1;
  }
  
  if(WIFEXITED(status)) {
    end_info = (struct cmd_end_info *) msg->data;
    end_info->cmd_action = CMD_END;
    end_info->id = c->id;
    end_info->exit_value = WEXITSTATUS(status);
  } else {
    died_info = (struct cmd_died_info *) msg->data;
    died_info->cmd_action = CMD_DIED;
    died_info->id = c->id;
    
    if(WIFSIGNALED(status)) {
      died_info->signal = WTERMSIG(status);
    } else {
      print(ERROR, "child exited unexpectly. status=%0*X", (int) sizeof(int), status);
      died_info->signal = 0;
    }
  }
  
  if(enqueue_message(&(c->conn->outcoming), msg)) {
    print( ERROR, "cannot enqueue messages" );
    dump_message(msg);
    free_message(msg);
    return -1;
  }
  
  return 0;
}
示例#16
0
文件: ztmd.c 项目: elambert/honeycomb
int nl2_initialize(const char* my_address)
{
    time_t now;
    time(&now);
    srandom((unsigned int)now);
    seq_init = random();

    sequence = get_sequence();

    if (my_address == NULL)     /* Pretending to send */
        return 0;

    if ((sock = setup_multicast(my_address, &my_addr)) < 0)
        return -1;

    return 0;
}
示例#17
0
hash_alignment_block get_next_alignment_hash(maf_linear_parser parser){
   hash_alignment_block new_align = NULL;
   int in_block=0;
   int hc=0;
   long bytesread;
   int sizeLeftover=0;
   int bLoopCompleted = 0;
   char *temp;
   char *datum;
   char *npos;
   ENTRY *ret_val;
   do{
      if(parser->fill_buf){
         bytesread = fread(parser->buf+sizeLeftover, 1,
            sizeof(parser->buf)-1-sizeLeftover, parser->maf_file);
         if (bytesread<1){
            bLoopCompleted = 1;
            bytesread  = 0;
            continue;
         }
        if(ferror(parser->maf_file) != 0){
                fprintf(stderr, "File stream error: %s\nError: %s",
                   parser->filename,strerror(errno));
                return NULL;
         }
         parser->buf[sizeLeftover+bytesread]=0;
         parser->curr_pos=0;
         parser->pos=parser->buf;
         --parser->fill_buf;
     }
     npos = strchr(parser->pos,'\n');

     if(npos==NULL){
        sizeLeftover = strlen(parser->pos);
        memmove(parser->buf,parser->buf+(sizeof(parser->buf))-sizeLeftover-1,sizeLeftover);
        ++parser->fill_buf;
        continue;
     }
     *npos=0;
     datum = parser->pos;
     parser->pos = npos+1;
//If we've yet to enter an alignment block, and the first character
//of the line isn't 'a', then skip over it.
      if(!in_block && datum[0]!='a') continue;
//***HANDLE SCORE/PASS/DATA here**i
      else if(datum[0]=='a'){
//If we find an 'a' after entering a block, then this is a new block
//so rewind the file pointer and break out of read loop.
         if(in_block){
            *npos='\n';
            parser->pos = datum;
            break;
         }
//Else we're starting a new alignment block, initialize the data
//structure and set in_block to true.
         new_align=malloc(sizeof(*new_align));
         assert(new_align != NULL);
	 new_align->species = malloc(256*sizeof(char *));
         assert(new_align->species != NULL);
         new_align->sequences = calloc(1,sizeof(struct hsearch_data));
	 assert(new_align->sequences != NULL);
         hc = hcreate_r(256,new_align->sequences);
         if(hc == 0){
           fprintf(stderr,"Failed to create hash table: %s\n", strerror(errno));
           exit(1);
         }
	 new_align->size=0;
         new_align->max=128;
         new_align->data = NULL;
         in_block=1;
         continue;
      }
//If in a block and find 's', then it's a sequence to add to the
//current alignment block, parse it, reallocate alignment block's
//sequence array if necessary, and store the new sequence.
      else if(datum[0]=='s'){
         seq new_seq = get_sequence(datum);
         new_align->seq_length = new_seq->size;
         if(new_seq == NULL){
           fprintf(stderr, "Invalid sequence entry %s\n",datum);
           return NULL;
         }
         if(new_align->size >= new_align->max){
            fprintf(stderr, "WARNING: Alignment block hash table over half full"
		            "consider increasing max alignment hash size.\n"
			    "Current size: %d\nMax size: %d\n",new_align->size,
			    new_align->max);
         }temp = strdup(new_seq->src);
         assert(temp!=NULL);
         char *species_name=strtok(temp,".");
         ENTRY new_ent={species_name,new_seq};
         hc = hsearch_r(new_ent,ENTER,&ret_val,new_align->sequences);
         if(hc == 0){
           fprintf(stderr,"Failed to insert into hash table: %s\n", strerror(errno));
           exit(1);
         }if(ret_val->data != new_ent.data){
           fprintf(stderr, "Entry for species %s already present\n",species_name);
           continue;
	 }
//         printf("Entry inserted: %s\n", genome_names[i]);
         new_align->species[new_align->size++] = species_name;
         continue;
      }
//If we hit a character other than 'a' or 's', then we've exited
//the current alignment block, break out of the read loop and return
//the current alignment block.
      else break;
   }while(!bLoopCompleted);
   return new_align;
}
示例#18
0
int start_command(JNIEnv *env, jclass clazz __attribute__((unused)), jstring jhandler, jstring jcmd, jobjectArray jenv) {
  const char *utf;
  handler *h;
  int id;
  struct cmd_start_info *start_info;
  message *m;
  child_node *c;
  
  id = -1;
  m=NULL;
  c=NULL;
  utf=NULL;
  
  if(!authenticated()) {
    LOGE("%s: not authenticated", __func__);
    return -1;
  }
  
  if(!jhandler) {
    LOGE("%s: handler cannot be null", __func__);
    return -1;
  }
  
  utf = (*env)->GetStringUTFChars(env, jhandler, NULL);
  
  if(!utf) {
    LOGE("%s: cannot get handler name", __func__);
    goto jni_error;
  }
  
  h = get_handler_by_name(utf);
  
  if(!h) {
    LOGE("%s: handler \"%s\" not found", __func__, utf);
    goto exit;
  }
  
  (*env)->ReleaseStringUTFChars(env, jhandler, utf);
  utf=NULL;
  
  m = create_message(get_sequence(&ctrl_seq, &ctrl_seq_lock),
                      sizeof(struct cmd_start_info), CTRL_ID);
  
  if(!m) {
    LOGE("%s: cannot create messages", __func__);
    goto exit;
  }
  
  start_info = (struct cmd_start_info *) m->data;
  start_info->cmd_action = CMD_START;
  start_info->hid = h->id;
  
  if(jcmd && parse_cmd(env, h, jcmd, m)) {
    LOGE("%s: cannot parse command", __func__);
    goto exit;
  }
  
  if(jenv && parse_env(env, jenv, m)) {
    LOGE("%s: cannot parse environment", __func__);
    goto exit;
  }
  
  // create child
  
  c = create_child(m->head.seq);
  
  if(!c) {
    LOGE("%s: cannot craete child", __func__);
    goto exit;
  }
  
  c->handler = h;
  
  // add child to list
  
  pthread_mutex_lock(&(children.control.mutex));
  list_add(&(children.list), (node *) c);
  pthread_mutex_unlock(&(children.control.mutex));
  
  // send message to cSploitd
  
  pthread_mutex_lock(&write_lock);
  // OPTIMIZATION: use id to store return value for later check
  id = send_message(sockfd, m);
  pthread_mutex_unlock(&write_lock);
  
  if(id) {
    LOGE("%s: cannot send messages", __func__);
    // mark it as failed
    c->id = CTRL_ID;
    c->seq = 0;
  }
  
  id=-1;
  
  // wait for CMD_STARTED or CMD_FAIL
  
  pthread_mutex_lock(&(children.control.mutex));
  
  while(c->seq && children.control.active)
    pthread_cond_wait(&(children.control.cond), &(children.control.mutex));
  
  if(c->id == CTRL_ID || c->seq) { // command failed
    list_del(&(children.list), (node *) c);
  } else {
    id = c->id;
  }
  
  c->pending = 0;
  
  pthread_mutex_unlock(&(children.control.mutex));
  
  pthread_cond_broadcast(&(children.control.cond));
  
  if(id != -1) {
    LOGI("%s: child #%d started", __func__, id);
  } else if(c->seq) {
    LOGW("%s: pending child cancelled", __func__);
  } else {
    LOGW("%s: cannot start command", __func__);
  }
  
  goto exit;
  
  jni_error:
  if((*env)->ExceptionCheck(env)) {
    (*env)->ExceptionDescribe(env);
    (*env)->ExceptionClear(env);
  }
  
  exit:
  
  if(m)
    free_message(m);
  
  if(utf)
    (*env)->ReleaseStringUTFChars(env, jhandler, utf);
  
  return id;
}
示例#19
0
int main(int argc, char *argv[]) {
	// this file has 5 sequences in it so it should be easy to read!
	char *filename = "../example_data/small.fastq.gz";
	struct fastq *seqs[HASHSIZE] = {NULL};
	int nseqs = read_fastq_gz(filename, seqs);
	int error = 0;
	if (nseqs != ESEQS) {
		fprintf(stderr, "There was an error reading %s. We retrieved %d sequences but should have found %d sequences\n", filename, nseqs, ESEQS);
		error = 1;
	}

	/* now test the get sequence methods */
	int nnseqs = number_of_sequences(seqs);
	if (nnseqs != ESEQS) {
		printf("There was an error checking the number of sequences. We found %d using number_of_sequences but it should be %d\n", nnseqs, ESEQS);
		error++;
	}

	/* test getting the ids */
	char *ids[nnseqs];
	int nnseqsids = get_ids(ids, seqs);
	if (nnseqsids != nnseqs) {
		printf("There was an error when we got the ids. This returned %d ids but it should be %d\n", nnseqsids, nnseqs);
		error++;
	}
	printf("Got %d ids\n", nnseqsids);
	for (int i=0; i<nnseqsids; i++)
		printf("ID %d: %s\n", i, ids[i]);
	

	char *sq = get_sequence("@GFH7CG303FQFE8/1", seqs);
	char *sqans = "AAATACGGGTAGATATACCGCCATGTCTCGCAAACAGCCTCATCATGCCGCGCCTGCCCACTTTTTTGCATCAGTATCCCGGCATTGCGCTGGAACTTTGTAGTAGCGATCGACAGGTCGATTTACTACGGGAAGATTTCGACTGCGTGGTGCGCACAGAGCCGCTACATGCGCCAGGGATACTGACGCGCCCGCTTGGCAAACTGAGAAGAGTGAACTGCGCCAGCCCACAATACCTGGCGCGCTTTGGGTAATCCAGAAAAACCTTGACGATCTCGCCTCACATGCGGTGGTGCATTATTCATTGACCCCGGGAGTTTCTTCACCGGGTTTTGCCTTTGAAACTCCCACGGTATGCAGTGGTAAAAAACCGGCGGAAATGCTAACGGTAAACAGTACGAGACTTGGCATACAGCCTG";
	char *quans = "@@@DDDDDFFFIIIIIIIIIIIIIIIIIIIIHHHIIFDDHHHHHHHHHHHHHFFFFFFFFF666666FFFFFFFFFFFFFCCADDDFDDCDDDDDD???6388CFFCCCCCDFFFFFDFFFF??8638<FF?AA>DFFFAA<<<FFFFFAACCFF???CFDDDDBBBBBBA<<889;888<<===BBBBB=;;A688<<9...3<4434443<<AAAAA??A==<>8624/2642..028>=<9989<<;4/..6;;<:,,,--..<<==:9<<<A<4444<=<<733224000<><8::CGGDDDDB::99400/0444322576;622....266666<68899444:8A?\?\?=>>>:3//.------55--266...3777666646...6297...269766///3:<><>;;93";
	if (sq == NULL) {
		fprintf(stderr, "Bugger, no sequence for @GFH7CG303FQFE8/1\n");
		error++;
	}

	if (strcmp(sq, sqans)) {
		error++;
		printf("The sequence we retrieved for @GFH7CG303FQFE8/1 appeared to be wrong:\n%s\n", sq);
	}

	sq = get_sequence("NOSEQ", seqs);
	if (sq != NULL) {
		fprintf(stderr, "Using ID NOSEQ we retrieved %s\n", sq);
		error++;
	}


	char *qual = get_quality("@GFH7CG303FQFE8/1", seqs);
	if (strcmp(qual, quans)) {
		fprintf(stderr, "The quality scores for @GFH7CG303FQFE8/1 do not appear correct\n");
		error++;
	}

	qual = get_quality("NOSEQ", seqs);
	if (qual != NULL) {
		fprintf(stderr, "Got %s as quality scores for NOSEQ but shouldn't find any!\n", qual);
		error++;
	}

	char *info = get_seq_information("@GFH7CG303FQFE8/1", seqs);
	if (strcmp("@GFH7CG303FQFE8/1 length=419 xy=2234_0770 region=3 run=R_2010_04_12_12_22_54_", info)) {
		printf("TEST: The information for @GFH7CG303FQFE8/1 was unexpected: '%s'\n", info);
		error++;
	}



	
	if (error) 
		fprintf(stderr, "There were %d errors when running the tests\n", error);
	else
		fprintf(stderr, "All tests passed\n");
	
	exit(error);

}
示例#20
0
/**
 * Send an edcl packet and fragment it if max_packet_size < data_size
 */
static int		send_fragmented(void)
{
	unsigned int		nb_ack_packet = 0;
	unsigned int		address_offset = 0;
	unsigned int		nb_sent_packet = 0;
	unsigned int		local_data_size = config.data_size;
	edcl_packet_t*		packet = NULL;

	/* Allocating the packet of the packet */
	packet = calloc(1, min(local_data_size, MAX_DATA_SIZE) + sizeof (edcl_header_t));
	if (packet == NULL)
		goto error_malloc;

	do {
		/* Filling up the edcl header */
		clear_header(packet->header);
		set_operation(packet->header, WRITE_OP);
		set_sequence(packet->header, config.sequence_number);
		set_address(packet->header, config.memory_address + address_offset);
		set_length(packet->header, min(local_data_size, MAX_DATA_SIZE));

		if (config.verbose)
			fprintf(stderr,
				" [%d] Trying to write at 0x%08x.",
				config.sequence_number,
				config.memory_address + address_offset);

		/* Copying the data in the packet */
		memcpy(packet->data,
		       config.data_c + address_offset,
		       min(local_data_size, MAX_DATA_SIZE));

		/* Sending the packet to the ethernet IP */
		if (sendto(config.socket,
			   packet,
			   min(local_data_size, MAX_DATA_SIZE) + sizeof (edcl_header_t),
			   0, config.serv_info->ai_addr, config.serv_info->ai_addrlen)
		    == -1)
			goto error_send;

		/* Waiting for the aknowledgment */
		if (recvfrom(config.socket,  packet,
			     sizeof (edcl_header_t) + min(local_data_size, MAX_DATA_SIZE),
			     0, NULL, 0) == -1)
			goto error_recvfrom;

		/* We've been aknowledge, keep sending */
		if (get_ack(packet->header)) {
			if (config.verbose)
				fprintf(stderr,
					"\t[OK]\n");
			address_offset += min(local_data_size, MAX_DATA_SIZE);
			local_data_size -= min(local_data_size, MAX_DATA_SIZE);
			++config.sequence_number;
			++nb_ack_packet;
		}
		/* The sequence number was wrong, fix it */
		else {
			if (config.verbose)
				fprintf(stderr,
					"\t[Failed]\n  Wrong sequence number (%d) should be: %d. Fixed.\n",
					config.sequence_number, get_sequence(packet->header));
			config.sequence_number = get_sequence(packet->header);
		}

		++nb_sent_packet;
	} while (local_data_size > 0);

	if (config.verbose) {
		fprintf(stderr,
			"The datas have been fragmented in %d packets.\n", nb_ack_packet);
		fprintf(stderr,
			"\t %d packets have been sent (%d have been lost).\n",
			nb_sent_packet,
			nb_sent_packet - nb_ack_packet);
	}

	/* Releasing ressources */
	free(packet);

	return (0);

 error_recvfrom:
 error_send:
	if (config.verbose)
		fprintf(stderr,
			"Error while sending the packet.\n");
	free(packet);
	return (errno);
 error_malloc:
	if (config.verbose)
		fprintf(stderr,
			"Unable to allocate the packet.\n");
	return (errno);
}
示例#21
0
int start_command(JNIEnv *env, jclass clazz __attribute__((unused)), jstring jhandler, jstring jcmd) {
  char status;
  char *pos, *start, *end, *rpos, *wpos;
  const char *utf;
  jstring *utf_parent;
  handler *h;
  uint32_t msg_size; // big enought to check for uint16_t overflow
  int id;
  size_t arg_len, escapes;
  struct cmd_start_info *start_info;
  message *m;
  child_node *c;
  
  id = -1;
  m=NULL;
  c=NULL;
  
  if(!authenticated()) {
    LOGE("%s: not authenticated", __func__);
    return -1;
  }
  
  utf = (*env)->GetStringUTFChars(env, jhandler, NULL);
  utf_parent = &jhandler;
  
  if(!utf) {
    LOGE("%s: cannot get handler name", __func__);
    goto jni_error;
  }
  
  arg_len = (*env)->GetStringUTFLength(env, jhandler);
  
  if(!arg_len) {
    LOGE("%s: empty handler name", __func__);
    goto jni_error;
  }
  
  arg_len++; // test even the '\0'
  
  for(h=(handler *) handlers.list.head;h && strncmp(utf, h->name, arg_len);h=(handler *) h->next);
  
  if(!h) {
    LOGE("%s: handler \"%s\" not found", __func__, utf);
    goto exit;
  }
  
  (*env)->ReleaseStringUTFChars(env, jhandler, utf);
  utf = (*env)->GetStringUTFChars(env, jcmd, NULL);
  utf_parent = &jcmd;
  
  if(!utf) {
    LOGE("%s: cannot get command string", __func__);
    goto jni_error;
  }
  
  LOGD("%s: parsing \"%s\"", __func__, utf);
  
  msg_size = sizeof(struct cmd_start_info);
  m = create_message(get_sequence(&ctrl_seq, &ctrl_seq_lock),
                      msg_size, CTRL_ID);
  
  if(!m) {
    LOGE("%s: cannot create messages", __func__);
    goto exit;
  }
  
  start_info = (struct cmd_start_info *) m->data;
  start_info->cmd_action = CMD_START;
  start_info->hid = h->id;
  
  status = 0;
  arg_len = 0;
  escapes = 0;
  start = end = NULL;
  
  for(pos=(char *) utf;!(status & END_OF_STRING);pos++) {
    
    // string status parser
    switch (*pos) {
      case '"':
        if(status & ESCAPE_FOUND) {
          escapes++;
        } else if(status & (INSIDE_SINGLE_QUOTE)) {
          // copy it as a normal char
        } else if(status & INSIDE_DOUBLE_QUOTE) {
          status &= ~INSIDE_DOUBLE_QUOTE;
          end = pos;
        } else {
          status |= INSIDE_DOUBLE_QUOTE;
        }
        break;
      case '\'':
        if(status & ESCAPE_FOUND) {
          escapes++;
        } else if(status & INSIDE_DOUBLE_QUOTE) {
          // copy it as a normal char
        } else if(status & INSIDE_SINGLE_QUOTE) {
          status &= ~INSIDE_SINGLE_QUOTE;
          end = pos;
        } else {
          status |= INSIDE_SINGLE_QUOTE;
        }
        break;
      case '\\':
        if(status & ESCAPE_FOUND) {
          // copy it as normal char
          escapes++;
        } else {
          status |= ESCAPE_FOUND;
          continue;
        }
        break;
      case ' ': // if(isspace(*pos))
      case '\t':
        if(status & ESCAPE_FOUND) {
          escapes++;
        } else if(!status && start) {
          end=pos;
        }
        break;
      case '\0':
        status |= END_OF_STRING;
        end=pos;
        break;
      default:
        if(!start)
          start=pos;
    }
    
    status &= ~ESCAPE_FOUND;
    
    // copy the arg if found
    if(start && end) {
      
      LOGD("%s: argument found: start=%d, end=%d", __func__, (start-utf), (end-utf));
      arg_len=(end-start);
      arg_len-= escapes;
      
      msg_size+=arg_len + 1;
      
      if(msg_size > UINT16_MAX) {
        LOGW("%s: command too long: \"%s\"", __func__, utf);
        goto exit;
      }
      
      m->data = realloc(m->data, msg_size);
      
      if(!m->data) {
        LOGE("%s: realloc: %s", __func__, strerror(errno));
        goto exit;
      }
      
      wpos = m->data + m->head.size;
      for(rpos=start;rpos<end;rpos++) {
        if(status & ESCAPE_FOUND) {
          status &= ~ESCAPE_FOUND;
          if( *rpos != '\\' &&
              *rpos != '"' &&
              *rpos != '\'' &&
              *rpos != ' ' &&
              *rpos != '\t') {
            // unrecognized escape sequence, copy the backslash as it is.
            *wpos = '\\';
            wpos++;
          }
        } else if(*rpos == '\\') {
          status |= ESCAPE_FOUND;
          continue;
        }
        *wpos=*rpos;
        wpos++;
      }
      *(m->data + msg_size -1) = '\0';
      
      m->head.size = msg_size;
      
      start = end = NULL;
      escapes = 0;
    }
  }
  
  // create child
  
  c = create_child(m->head.seq);
  
  if(!c) {
    LOGE("%s: cannot craete child", __func__);
    goto exit;
  }
  
  c->handler = h;
  
  // add child to list
  
  pthread_mutex_lock(&(children.control.mutex));
  list_add(&(children.list), (node *) c);
  pthread_mutex_unlock(&(children.control.mutex));
  
  // send message to dSploitd
  
  pthread_mutex_lock(&write_lock);
  // OPTIMIZATION: use escapes to store return value for later check
  escapes = send_message(sockfd, m);
  pthread_mutex_unlock(&write_lock);
  
  if(escapes) {
    LOGE("%s: cannot send messages", __func__);
    goto exit;
  }
  
  // wait for CMD_STARTED or CMD_FAIL
  
  pthread_mutex_lock(&(children.control.mutex));
  
  while(c->seq && children.control.active)
    pthread_cond_wait(&(children.control.cond), &(children.control.mutex));
  
  if(c->id == CTRL_ID || c->seq) { // command failed
    list_del(&(children.list), (node *) c);
  } else {
    id = c->id;
  }
  
  c->pending = 0;
  
  pthread_mutex_unlock(&(children.control.mutex));
  
  pthread_cond_broadcast(&(children.control.cond));
  
  if(id != -1) {
    LOGI("%s: child #%d started", __func__, id);
  } else if(c->seq) {
    LOGW("%s: pending child cancelled", __func__);
  } else {
    LOGW("%s: cannot start command", __func__);
  }
  
  goto exit;
  
  jni_error:
  if((*env)->ExceptionCheck(env)) {
    (*env)->ExceptionDescribe(env);
    (*env)->ExceptionClear(env);
  }
  
  exit:
  if(c && id==-1) {
    pthread_mutex_lock(&(children.control.mutex));
    list_del(&(children.list), (node *) c);
    pthread_mutex_unlock(&(children.control.mutex));
    pthread_cond_broadcast(&(children.control.cond));
    free_child(c);
  }
  
  if(m)
    free_message(m);
  
  if(utf)
    (*env)->ReleaseStringUTFChars(env, *utf_parent, utf);
  
  return id;
}
示例#22
0
文件: main.c 项目: madmarks/SoftUni
int main(int argc, char** argv) 
{
    size_t i;
    
    char * text = NULL;
    size_t text_buf_size;
    ssize_t text_len;
    
    if(-1 == (text_len = get_line(&text, &text_buf_size, stdin)))
    {
        error("Error", false);
        return (EXIT_FAILURE);
    }
    
    const char * delimiters = "|";
    size_t num_of_blocks;

    char ** blocks = string_to_arr_of_strings_by_delimiters(text, delimiters, 
            &num_of_blocks);

    if(NULL == blocks)
    {
        error("Error", false);
        return (EXIT_FAILURE);
    }
    
    // filter invalid curly bracket sequences inside a single block
    if(0 != arr_of_strings_filtered_by_condition(&blocks, &num_of_blocks, 
            &is_valid_curly_brackets_sequence))
    {
        return (EXIT_FAILURE);
    }
    
    size_t resulting_string_total_len = BUFFER_LENGTH;
    char * resulting_string = (char *)malloc(resulting_string_total_len);
    
    if(NULL == resulting_string)
    {
        error("Error", false);
        return (EXIT_FAILURE);
    }
    resulting_string[0] = '\0';
    
    char * sequence = NULL;
    size_t sequence_len;
    size_t resulting_string_current_len = 0;
    for(i = 0; i < num_of_blocks; i++)
    {
        sequence = get_sequence(blocks[i]);
        if(NULL == sequence)
        {
            return (EXIT_FAILURE);
        }
        
        sequence_len = strlen(sequence);
        
        if((resulting_string_current_len + sequence_len) >= (resulting_string_total_len - 1))
        {
            resulting_string_total_len += sequence_len;
            char *new_resulting_string = (char *)realloc(resulting_string, resulting_string_total_len);
            if(NULL == new_resulting_string)
            {
                error("Error when realloc!", false);
                return (EXIT_FAILURE);
            }
            
            resulting_string = new_resulting_string;            
        }
            
        strcat(resulting_string, sequence);
        resulting_string_current_len += sequence_len;
    }    
    
    char * command = NULL;
    size_t command_buf_size = 0;
    ssize_t command_len;
    char * end_command = "end";
    
    struct CommandStruct command_struct;
    
    while(-1 != (command_len = get_line(&command, &command_buf_size, stdin)))
    {
        int ret = get_command_parameters(command, &command_struct);
        
        if(ret == 0 && strcmp(command_struct.command, end_command) == 0)
            break;
        
        if(ret == -3 || ret == -4 || ret == -5)
        {
            printf("Invalid command parameters\n");
            continue;
        }

        if(ret == 0 && (command_struct.positionA < 0 
                || command_struct.positionB < 0 || command_struct.size < 0 
                || (command_struct.positionA + command_struct.size) > strlen(resulting_string)
                || (command_struct.positionB + command_struct.size) > strlen(resulting_string)
                || (command_struct.positionA + command_struct.size) > command_struct.positionB))
        {
            printf("Invalid command parameters\n");
            continue;
        }

        swap((char *)(resulting_string + command_struct.positionA), 
                (char *)(resulting_string + command_struct.positionB), 
                command_struct.size);
    }  
    
    /* Print output */
    
    printf("%s\n", resulting_string);
    
    /* Free allocated resources */
    
    if(command)
        free(command);
    
    if(resulting_string)
        free(resulting_string);
    
    for(i = 0; i < num_of_blocks; i++)
    {
        if(blocks[i])
            free(blocks[i]);
    }
    
    if(blocks)
        free(blocks);
    
    if(text)
        free(text);
    
    return (EXIT_SUCCESS);
}
示例#23
0
Haplotype* generate_haplotype(Region& str_region, int32_t max_ref_flank_len, std::string& chrom_seq,
			      std::vector< std::vector<Alignment> >& alignments, std::vector<std::string>& vcf_alleles,
			      StutterModel* stutter_model, bool search_bams_for_alleles,
			      std::vector<HapBlock*>& blocks, std::vector<bool>& call_sample, std::ostream& logger){
  assert(blocks.size() == 0);
  assert(call_sample.size() == 0);

  std::map<std::string, int> seqs;

  // Determine the minimum and maximum alignment boundaries
  int32_t min_start = INT_MAX, max_stop = INT_MIN;
  for (auto vec_iter = alignments.begin(); vec_iter != alignments.end(); vec_iter++){
    for (auto aln_iter = vec_iter->begin(); aln_iter != vec_iter->end(); aln_iter++){
      min_start = std::min(min_start, aln_iter->get_start());
      max_stop  = std::max(max_stop,  aln_iter->get_stop());
      seqs[aln_iter->get_sequence()]++;
    }
  }
  
  logger << "# Seqs = " << seqs.size() << std::endl;
			 
  // Extract candidate STR sequences (use some padding to ensure indels near STR ends are included)
  std::vector<std::string> str_seqs;
  int32_t left_padding = 5, right_padding = 5;
  int32_t rep_region_start = str_region.start() < left_padding ? 0 : str_region.start()-left_padding;
  int32_t rep_region_end   = str_region.stop() + right_padding;
  std::string ref_seq      = uppercase(chrom_seq.substr(rep_region_start, rep_region_end-rep_region_start));
  int ideal_min_length     = 3*str_region.period(); // Would ideally have at least 3 repeat units in each allele after trimming

  // TO DO: Use frequency of deletions not contained within window to 
  //   i) Identify problematic regions
  //  ii) Retry with increased window padding?
  int32_t min_del_start, max_del_stop, flank_del_start, flank_del_stop, min_ins_pos, max_ins_pos;
  std::vector<bool> flank_del_call_sample, flank_ins_call_sample;
  int num_ext_del   = check_deletion_bounds(alignments,     rep_region_start, rep_region_end, min_del_start,   max_del_stop,   call_sample);
  int num_flank_del = check_flanking_deletions(alignments,  rep_region_start, rep_region_end, flank_del_start, flank_del_stop, flank_del_call_sample);
  int num_flank_ins = check_flanking_insertions(alignments, rep_region_start, rep_region_end, min_ins_pos,     max_ins_pos,    flank_ins_call_sample);
  int nfail         = std::max(num_ext_del, std::max(num_flank_del, num_flank_ins));

  // Recheck deletions after expanding the STR window
  if(1.0*nfail/alignments.size() <= MAX_FRAC_SAMPLE_DEL_FAIL){
    logger << "PASS SAMPLE DEL STATS: " << rep_region_start << "\t" << rep_region_end << "\t" << num_ext_del
	    << "\t" << min_del_start << "\t" << max_del_stop << "\t" << std::endl;
  }
  else {
    logger << "FAIL SAMPLE DEL STATS: " << rep_region_start << "\t" << rep_region_end << "\t" << num_ext_del
	   << "\t" << min_del_start << "\t" << max_del_stop << "\t" << std::endl;

    int32_t min_s = std::min(min_del_start, std::min(flank_del_start, min_ins_pos));
    int32_t max_e = std::max(max_del_stop,  std::max(flank_del_stop,  max_ins_pos));

    call_sample.clear();
    flank_del_call_sample.clear();
    flank_ins_call_sample.clear();
    rep_region_start  = std::min(rep_region_start, std::max(rep_region_start-10, min_s-5));
    rep_region_end    = std::max(rep_region_end,   std::min(rep_region_end+10,   max_e+5));
    left_padding      = str_region.start() - rep_region_start;
    right_padding     = rep_region_end     - str_region.stop();
    ref_seq           = uppercase(chrom_seq.substr(rep_region_start, rep_region_end-rep_region_start));
    num_ext_del       = check_deletion_bounds(alignments,    rep_region_start,  rep_region_end, min_del_start, max_del_stop, call_sample);
    num_flank_del     = check_flanking_deletions(alignments,  rep_region_start, rep_region_end, flank_del_start, flank_del_stop, flank_del_call_sample);
    num_flank_ins     = check_flanking_insertions(alignments, rep_region_start, rep_region_end, min_ins_pos, max_ins_pos, flank_ins_call_sample);
    
      /*
      if(1.0*sample_fail_count/alignments.size() <= MAX_FRAC_SAMPLE_DEL_FAIL){
	logger << "REDONE_PASS SAMPLE DEL STATS: " << rep_region_start << "\t" << rep_region_end << "\t" << sample_fail_count
		  << "\t" << min_del_start << "\t" << max_del_stop << "\t" << std::endl;
      }
      else {
	logger << "REDONE_FAIL SAMPLE DEL STATS: " << rep_region_start << "\t" << rep_region_end << "\t" << sample_fail_count
		  << "\t" << min_del_start << "\t" << max_del_stop << "\t" << std::endl;
      }
      */
  }

  /*
  logger << "# Flank deletions = " << num_flank_del << ", # Flank insertions = " << num_flank_ins << std::endl;
  for (unsigned int i = 0; i < call_sample.size(); i++)
    call_sample[i] = call_sample[i] && flank_del_call_sample[i] && flank_ins_call_sample[i];
  */

  // Trim boundaries so that the reference flanks aren't too long
  if (rep_region_start > max_ref_flank_len)
    min_start = std::max(rep_region_start-max_ref_flank_len, min_start);
  max_stop = std::min(rep_region_end+max_ref_flank_len, max_stop);

  // Extend each VCF allele by padding size
  std::vector<std::string> ext_vcf_alleles;
  if (vcf_alleles.size() != 0){
    std::string lflank = uppercase(chrom_seq.substr(rep_region_start,  str_region.start()-rep_region_start));
    std::string rflank = uppercase(chrom_seq.substr(str_region.stop(), rep_region_end-str_region.stop())); 
    for (unsigned int i = 0; i < vcf_alleles.size(); i++){
      ext_vcf_alleles.push_back(lflank + vcf_alleles[i] + rflank);
      logger << ext_vcf_alleles.back() << std::endl;
    }
    logger << ext_vcf_alleles[0] << std::endl << ref_seq << std::endl;
    assert(ext_vcf_alleles[0].compare(ref_seq) == 0);
  }
  generate_candidate_str_seqs(ref_seq, chrom_seq, left_padding, right_padding, ideal_min_length, alignments, 
			      ext_vcf_alleles, search_bams_for_alleles, rep_region_start, rep_region_end, str_seqs, logger);
  
  // Create a set of haplotype regions, consisting of STR sequence block flanked by two reference sequence stretches
  assert(rep_region_start > min_start && rep_region_end < max_stop);
  assert(str_seqs[0].compare(uppercase(chrom_seq.substr(rep_region_start, rep_region_end-rep_region_start))) == 0);
  blocks.clear();
  blocks.push_back(new HapBlock(min_start, rep_region_start, uppercase(chrom_seq.substr(min_start, rep_region_start-min_start))));    // Ref sequence preceding STRS
  blocks.push_back(new RepeatBlock(rep_region_start, rep_region_end, 
				   uppercase(chrom_seq.substr(rep_region_start, rep_region_end-rep_region_start)), str_region.period(), stutter_model));
  blocks.push_back(new HapBlock(rep_region_end, max_stop, uppercase(chrom_seq.substr(rep_region_end, max_stop-rep_region_end))));  // Ref sequence following STRs
  for (unsigned int j = 1; j < str_seqs.size(); j++)
    blocks[1]->add_alternate(str_seqs[j]);

  // Initialize each block's data structures, namely the homopolymer length information
  for (unsigned int i = 0; i < blocks.size(); i++)
    blocks[i]->initialize();
 
  logger << "Constructing haplotype" << std::endl;
  Haplotype* haplotype = new Haplotype(blocks);
  haplotype->print_block_structure(30, 100, logger);
  return haplotype;
}
示例#24
0
void bam_parser(opt_t *opt) {
    samFile *in = sam_open(opt->in_name, "r");
    if(in == NULL) die("bam_parser: fail to open file '%s'", opt->in_name);
    // if output file exists but not force to overwrite
    if(access(opt->out_name, F_OK)!=-1 && opt->f==false) die("bam_parser: %s exists, use opetion -f to overwrite", opt->out_name);
    bam_hdr_t *header = sam_hdr_read(in);
    bam1_t *aln = bam_init1();
    int8_t *p;
    int32_t n;
    int ret;
    while((ret=sam_read1(in, header, aln)) >= 0)
        printf("name=%s\nflag=%d\nseq=%s\nqual=%s\nlane_id=%d\n", get_read_name(aln), aln->core.flag, get_sequence(aln), get_qualities(aln), get_lane_id(aln));

    bam_destroy1(aln);
    sam_close(in);
}
示例#25
0
alignment_block linear_next_alignment_buffer(maf_linear_parser parser){
   alignment_block new_align = NULL;
   int in_block=0;

   long bytesread; 
   int sizeLeftover=0; 
   int bLoopCompleted = 0; 
   char *datum;
   int init=1;
   char *npos;
   int first=1;
   do{
      if(parser->fill_buf){
         bytesread = fread(parser->buf+sizeLeftover, 1, 
            sizeof(parser->buf)-1-sizeLeftover, parser->maf_file);
         if (bytesread<1){ 
            bLoopCompleted = 1; 
            bytesread  = 0;
            continue; 
         }       
        if(ferror(parser->maf_file) != 0){
                fprintf(stderr, "File stream error: %s\nError: %s",
                   parser->filename,strerror(errno));
                return NULL;
         }
         parser->buf[sizeLeftover+bytesread]=0;
         parser->curr_pos=0;
         parser->pos=parser->buf;
         --parser->fill_buf;
         init=1;
     }
     npos = strchr(parser->pos,'\n');
     
     if(npos==NULL){
        sizeLeftover = strlen(parser->pos);
        memmove(parser->buf,parser->buf+(sizeof(parser->buf))-sizeLeftover-1,sizeLeftover);
        ++parser->fill_buf;
        continue;
     }
     *npos=0;
     datum = parser->pos;
     parser->pos = npos+1;
//If we've yet to enter an alignment block, and the first character
//of the line isn't 'a', then skip over it.
      if(!in_block && datum[0]!='a') continue;
//***HANDLE SCORE/PASS/DATA here**i
      else if(datum[0]=='a'){
//If we find an 'a' after entering a block, then this is a new block
//so rewind the file pointer and break out of read loop.
         if(in_block){
            *npos='\n';
            parser->pos = datum;
            break;
         }
//Else we're starting a new alignment block, initialize the data
//structure and set in_block to true.
         new_align=malloc(sizeof(*new_align));
         assert(new_align != NULL);
         new_align->sequences = malloc(16*sizeof(*new_align->sequences));
         assert(new_align->sequences != NULL);
         new_align->size=new_align->curr_seq=0;
         new_align->max=16;
         new_align->data = NULL;
         in_block=1;
         continue;
      }
//If in a block and find 's', then it's a sequence to add to the
//current alignment block, parse it, reallocate alignment block's
//sequence array if necessary, and store the new sequence.
      else if(datum[0]=='s'){
         seq new_seq = get_sequence(datum);
         if(new_seq == NULL){
           fprintf(stderr, "Invalid sequence entry %s\n",datum);
           return NULL;
         }
         if(first){
            new_align->seq_length=strlen(new_seq->sequence);
            first = 0;
         }
         if(new_align->size ==new_align->max){
             new_align->sequences=realloc(new_align->sequences,
                2*new_align->max*sizeof(seq));
             assert(new_align->sequences!=NULL);
             new_align->max *=2;
         }new_align->sequences[new_align->size++]=new_seq;
         continue;
      }
//If we hit a character other than 'a' or 's', then we've exited
//the current alignment block, break out of the read loop and return
//the current alignment block.
      else break;
   }while(!bLoopCompleted);
   return new_align;
}
示例#26
0
alignment_block linear_next_alignment(maf_linear_parser parser){
   char buffer[4096];
   alignment_block new_align = NULL;
   int in_block=0;
   int file_pos;
   while(!feof(parser->maf_file)){
      file_pos = ftell(parser->maf_file);
      char *fc = fgets(buffer,4096,parser->maf_file);
      if(ferror(parser->maf_file) != 0){
             fprintf(stderr, "File stream error: %s\nError: %s",
                parser->filename,strerror(errno));
             return NULL;
      }
//If we've yet to enter an alignment block, and the first character
//of the line isn't 'a', then skip over it.
      if(!in_block && buffer[0]!='a') continue;
//***HANDLE SCORE/PASS/DATA here**i
      else if(buffer[0]=='a'){
//If we find an 'a' after entering a block, then this is a new block
//so rewind the file pointer and break out of read loop.
         if(in_block){
            int check= fseek(parser->maf_file,file_pos,SEEK_SET);
            if(check !=0){
               fprintf(stderr,"File seek error: %s\n",strerror(errno));
               return NULL;
            }
            break;
         }
//Else we're starting a new alignment block, initialize the data
//structure and set in_block to true.
         new_align=malloc(sizeof(*new_align));
         assert(new_align != NULL);
         new_align->sequences = malloc(16*sizeof(*new_align->sequences));
         assert(new_align->sequences != NULL);
         new_align->size=new_align->curr_seq=0;
         new_align->max=16;
         new_align->data = NULL;
         in_block=1;
         continue;
      }
//If in a block and find 's', then it's a sequence to add to the
//current alignment block, parse it, reallocate alignment block's
//sequence array if necessary, and store the new sequence.
      else if(buffer[0]=='s'){
         seq new_seq = get_sequence(buffer);
         if(new_seq == NULL){
           fprintf(stderr, "Invalid sequence entry %s\n",buffer);
           return NULL;
         }
         if(new_align->size ==new_align->max){
             new_align->sequences=realloc(new_align->sequences,
                2*new_align->max*sizeof(seq));
             assert(new_align->sequences!=NULL);
             new_align->max *=2;
         }new_align->sequences[new_align->size++]=new_seq;
         continue;
      }
//If we hit a character other than 'a' or 's', then we've exited
//the current alignment block, break out of the read loop and return
//the current alignment block.
      else break;
   }
   return new_align;
}
int main(int argc, const char* argv[])
{
    argc--;
    argv++;
    srand (time(NULL));
    
    db_ranking db;
    
    int change_rate = 10;
    if ( argc > 1 ){
        sscanf(argv[1], "%d", &change_rate);
    }
    
    diagnosis_phase_detector::phase phases[4] = {
        diagnosis_phase_detector::diagnosis_plain,
        diagnosis_phase_detector::diagnosis_green,
        diagnosis_phase_detector::diagnosis_hinselmann,
        diagnosis_phase_detector::diagnosis_schiller
    };
    
    map<diagnosis_phase_detector::phase, string> names;
    names[diagnosis_phase_detector::diagnosis_plain] = "plain";
    names[diagnosis_phase_detector::diagnosis_green] = "green";
    names[diagnosis_phase_detector::diagnosis_hinselmann] = "hinselmann";
    names[diagnosis_phase_detector::diagnosis_schiller] = "schiller";
    
    int current_phase_index = 0;
    diagnosis_phase_detector::phase cphase = phases[current_phase_index];
    
    // Parse the videos specified in the input file
    ifstream fin(argv[0]);
    string next_seq;
    //int counter = 5;
    int vindex = 0;
    
    while ( getline(fin, next_seq) != 0 /*&& counter-- > 0*/ ){
        cout << next_seq << endl;
        // Filaname
        filenames.push_back(next_seq);
        
        // Index
        v_index.push_back(db.get_video_index(next_seq));
        
        // Sequence of frames
        map<diagnosis_phase_detector::phase, vector<int> > next_frames;
        get_sequence(next_seq.c_str(), next_frames); 
        frames.push_back(next_frames);
        
        // Frame generator
        map<diagnosis_phase_detector::phase, layer_generator> next_generator;
        get_generator(next_frames, next_generator);
        generators.push_back(next_generator);
        
        // Annotated instance
        anonadado::instance* next_instance = new anonadado::instance();
        next_instance->read(next_seq.c_str());
        instances.push_back(next_instance);
        cout << "Video " << vindex++ << " done." << endl;
    }
    
    fin.close();
    
    bool has = true;
    bool exit = false;
    bool go_to_next_phase = false;
    
    while ( !exit && has ){
        int remaining = change_rate;
        go_to_next_phase = false;
        cout << "\n\n\n NEEEEXT!\n\n\n";
        cout << current_phase_index << " " << cphase << endl;
        cout << endl << endl;
        
        for ( int level = 0; has && !exit && !go_to_next_phase; level++ )
        {
            cout << "LEVEL " << level << endl;
            //boost::this_thread::sleep( boost::posix_time::seconds(1) );
            
            vector< pair<int, pair< pair<int, int>,
                                    pair<int, int>
                                  >
                        >
                  > pairs;
            
            // Generate pairs <(video, frame), (video, frame)> for this level
            for ( size_t va = 0; va < instances.size(); va++ ){
                vector<int> framesl_a;
                generators[va][cphase].get_next(framesl_a, level);
                
                for ( size_t fa = 0; fa < framesl_a.size(); fa++){
                    for ( size_t vb = 0; vb < va; vb++ ){
                        vector<int> framesl_b;
                        generators[vb][cphase].get_next(framesl_b, level);
                        
                        for ( size_t fb = 0; fb < framesl_b.size(); fb++ ){
                            if ( va == vb && framesl_a[fa] == framesl_b[fb] ){
                                continue;
                            }

                            int priority = 
                                min(db.num_annotated_frames(v_index[va]),
                                    db.num_annotated_frames(v_index[vb]));

                            pairs.push_back(
                                make_pair(priority,
                                          make_pair(make_pair(va,
                                                              framesl_a[fa]),
                                                    make_pair(vb,
                                                              framesl_b[fb])
                                                   )
                                         )
                            );
                        }
                    }
                }
            }
            
            if ( pairs.size() == 0 ){
                has = false;
                break;
            } else {
                has = true;
            }
            
            // Randomly sort these pairs
            vector<int> change_priorities;
            sort(pairs.begin(), pairs.end());
            
            change_priorities.push_back(0);
            for (size_t i = 1; i < pairs.size(); i++){
                if ( pairs[i].first != pairs[i - 1].first ){
                    change_priorities.push_back(i);
                }
            }
            change_priorities.push_back(pairs.size());
            
            for (size_t i = 0; i + 1 < change_priorities.size(); i++){
                random_shuffle(pairs.begin() + change_priorities[i],
                               pairs.begin() + change_priorities[i + 1]);
            }

            // Eval these pairs
            for ( size_t i = 0; i < pairs.size() && !go_to_next_phase; i++ ){
                int va = pairs[i].second.first.first;
                int fa = pairs[i].second.first.second;
                
                int vb = pairs[i].second.second.first;
                int fb = pairs[i].second.second.second;
                
                cout << filenames[va] << " " << filenames[vb] << endl;
                cout << "(" << va << ":" << fa << ") "
                     << "(" << vb << ":" << fb << ") " << endl;
                
                if ( db.exists(cphase,
                               v_index[va], frames[va][cphase][fa],
                               v_index[vb], frames[vb][cphase][fb])
                   )
                {
                    continue;
                }
                
                better = -1;
                bool save = true;
                
                while ( true ){
                    Mat a, b, dst;
                    instances[va]->get_frame(frames[va][cphase][fa], a);
                    instances[vb]->get_frame(frames[vb][cphase][fb], b);
                    draw_ui(a, b, dst);
                    
                    imshow(names[cphase], dst);
                    
                    int key = waitKey(0) % 0x100;
                    if ( key == 81 ){
                        better = 1;
                    } else if ( key == 83 ){
                        better = 0;
                    } else if ( key == 32 ){
                        break;
                    } else if ( key == 27 ){
                        exit = true;
                        has = false;
                        break;
                    } else if ( key == 115 ){       // Skip
                       save = false;
                       break;
                    } else {
                        better = -1;
                    }
                }
                
                if ( exit ){
                    break;
                }
                
                // Save the annotation
                if ( save ){
                    db.insert_annotation(cphase,
                                         v_index[va], frames[va][cphase][fa],
                                         v_index[vb], frames[vb][cphase][fb],
                                         better);
                }
                
                cout << "remaining " << remaining << endl;
                
                remaining--;
                if ( remaining <= 0 ){
                    go_to_next_phase = true;
                    break;
                }
            }
        }
        cout << "go to next\n";
        cvDestroyWindow(names[cphase].c_str());
        current_phase_index = (current_phase_index + 1) % 4;
        cphase = phases[current_phase_index];
        
    }
    
    cout << "Bye!\n";
    return 0;
}