931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
|
# File 'ext/htslib_native/native_bam.c', line 931
static VALUE native_mpileup_open(VALUE klass, VALUE files, VALUE headers, VALUE region,
VALUE beg, VALUE end, VALUE maxcnt, VALUE overlaps) {
ruby_mpileup_t *value;
VALUE object;
int index, count;
Check_Type(files, T_ARRAY);
Check_Type(headers, T_ARRAY);
count = RARRAY_LEN(files);
if (count <= 0 || RARRAY_LEN(headers) != count) rb_raise(rb_eArgError, "invalid mpileup inputs");
object = TypedData_Make_Struct(klass, ruby_mpileup_t, &mpileup_type, value);
memset(value, 0, sizeof(*value));
value->count = count;
value->files = files;
value->headers = headers;
RB_OBJ_WRITE(object, &value->files, files);
RB_OBJ_WRITE(object, &value->headers, headers);
value->inputs = ALLOC_N(ruby_mplp_input_t, count);
value->input_data = ALLOC_N(void *, count);
value->depths = ALLOC_N(int, count);
value->entries = ALLOC_N(const bam_pileup1_t *, count);
memset(value->inputs, 0, sizeof(ruby_mplp_input_t) * count);
for (index = 0; index < count; index++) {
ruby_bam_file_t *file = get_file(rb_ary_entry(files, index), 0);
sam_hdr_t *header = get_header(rb_ary_entry(headers, index))->pointer;
value->inputs[index].file = file;
value->inputs[index].header = header;
if (!NIL_P(region)) {
if (!file->index) { mpileup_release(value); rb_raise(rb_eRuntimeError, "index file is required to use region mpileup"); }
if (NIL_P(beg) && NIL_P(end))
value->inputs[index].iterator = sam_itr_querys(file->index, header, StringValueCStr(region));
else
value->inputs[index].iterator = sam_itr_queryi(file->index, sam_hdr_name2tid(header, StringValueCStr(region)), NUM2LL(beg), NUM2LL(end));
if (!value->inputs[index].iterator) { mpileup_release(value); rb_raise(rb_eRuntimeError, "failed to query mpileup region"); }
}
value->input_data[index] = &value->inputs[index];
}
value->pileup = bam_mplp_init(count, native_mpileup_read, value->input_data);
if (!value->pileup) { mpileup_release(value); rb_raise(rb_eNoMemError, "bam_mplp_init failed"); }
if (!NIL_P(maxcnt)) bam_mplp_set_maxcnt(value->pileup, NUM2INT(maxcnt));
if (RTEST(overlaps) && bam_mplp_init_overlaps(value->pileup) < 0) {
mpileup_release(value);
rb_raise(rb_eRuntimeError, "bam_mplp_init_overlaps failed");
}
return object;
}
|