| 1 |
/* |
| 2 |
Copyright [2015-2024] EMBL-European Bioinformatics Institute |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifdef PERL_CAPI |
| 18 |
#define WIN32IO_IS_STDIO |
| 19 |
#endif |
| 20 |
#include "EXTERN.h" |
| 21 |
#include "perl.h" |
| 22 |
#include "XSUB.h" |
| 23 |
#ifdef FCGI |
| 24 |
#include <fcgi_stdio.h> |
| 25 |
#else |
| 26 |
#ifdef USE_SFIO |
| 27 |
#include <config.h> |
| 28 |
#else |
| 29 |
#include <stdio.h> |
| 30 |
#endif |
| 31 |
#include <perlio.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#define TRACEME(x) do { \ |
| 35 |
if (SvTRUE(perl_get_sv("Bio::DB::HTS::ENABLE_DEBUG", TRUE))) \ |
| 36 |
{ PerlIO_stdoutf (x); PerlIO_stdoutf ("\n"); } \ |
| 37 |
} while (0) |
| 38 |
|
| 39 |
#ifndef Newx |
| 40 |
# define Newx(v,n,t) New(0,v,n,t) |
| 41 |
#endif |
| 42 |
|
| 43 |
#ifndef Newxz |
| 44 |
# define Newxz(v,n,t) Newz(0,v,n,t) |
| 45 |
#endif |
| 46 |
|
| 47 |
#include <unistd.h> |
| 48 |
#include <math.h> |
| 49 |
#include <string.h> |
| 50 |
#include <zlib.h> |
| 51 |
|
| 52 |
#include "htslib/kseq.h" |
| 53 |
#include "htslib/hts.h" |
| 54 |
#include "htslib/hfile.h" |
| 55 |
#include "htslib/sam.h" |
| 56 |
#include "htslib/faidx.h" |
| 57 |
#include "htslib/tbx.h" |
| 58 |
#include "htslib/bgzf.h" |
| 59 |
#include "htslib/vcf.h" |
| 60 |
#include "htslib/vcfutils.h" |
| 61 |
#include "htslib/vcf_sweep.h" |
| 62 |
#include "htslib/synced_bcf_reader.h" |
| 63 |
|
| 64 |
#include "htslib/khash.h" |
| 65 |
KHASH_MAP_INIT_STR(vdict, bcf_idinfo_t) |
| 66 |
typedef khash_t(vdict) vdict_t; |
| 67 |
|
| 68 |
/* stolen from bam_aux.c */ |
| 69 |
#define BAM_MAX_REGION 1<<29 |
| 70 |
|
| 71 |
typedef htsFile* Bio__DB__HTSfile; |
| 72 |
typedef bam_hdr_t* Bio__DB__HTS__Header; |
| 73 |
typedef bam1_t* Bio__DB__HTS__Alignment; |
| 74 |
typedef hts_idx_t* Bio__DB__HTS__Index; |
| 75 |
typedef faidx_t* Bio__DB__HTS__Fai; |
| 76 |
typedef bam_pileup1_t* Bio__DB__HTS__Pileup; |
| 77 |
typedef tbx_t* Bio__DB__HTS__Tabix; |
| 78 |
typedef hts_itr_t* Bio__DB__HTS__Tabix__Iterator; |
| 79 |
typedef vcfFile* Bio__DB__HTS__VCFfile; |
| 80 |
typedef hts_itr_t* Bio__DB__HTS__VCF__Iterator; |
| 81 |
typedef bcf_hdr_t* Bio__DB__HTS__VCF__Header; |
| 82 |
typedef bcf_hdr_t* Bio__DB__HTS__VCF__HeaderPtr; |
| 83 |
typedef bcf1_t* Bio__DB__HTS__VCF__Row; |
| 84 |
typedef bcf1_t* Bio__DB__HTS__VCF__RowPtr; |
| 85 |
KSEQ_INIT(gzFile, gzread) |
| 86 |
typedef gzFile Bio__DB__HTS__Kseq; |
| 87 |
typedef kseq_t* Bio__DB__HTS__Kseq__Iterator; |
| 88 |
typedef kstream_t* Bio__DB__HTS__Kseq__Kstream; |
| 89 |
typedef kstring_t* Bio__DB__HTS__Kseq__Kstring; |
| 90 |
typedef bcf_sweep_t* Bio__DB__HTS__VCF__Sweep; |
| 91 |
|
| 92 |
typedef struct { |
| 93 |
SV* callback; |
| 94 |
SV* data; |
| 95 |
} fetch_callback_data; |
| 96 |
typedef fetch_callback_data *fetch_callback_dataptr; |
| 97 |
typedef struct { |
| 98 |
int start; |
| 99 |
int end; |
| 100 |
double width; |
| 101 |
int reads; |
| 102 |
int* bin; |
| 103 |
} coverage_graph; |
| 104 |
typedef coverage_graph *coverage_graph_ptr; |
| 105 |
|
| 106 |
static int MaxPileupCnt=8000; |
| 107 |
|
| 108 |
void XS_pack_charPtrPtr( SV * arg, char ** array, int count) { |
| 109 |
int i; |
| 110 |
AV * avref; |
| 111 |
avref = (AV*)sv_2mortal((SV*)newAV()); |
| 112 |
for (i=0; i<count; i++) { |
| 113 |
av_push(avref, newSVpv(array[i], strlen(array[i]))); |
| 114 |
} |
| 115 |
SvSetSV( arg, newRV((SV*)avref)); |
| 116 |
} |
| 117 |
|
| 118 |
static int invoke_sv_to_int_fun(SV *func, SV *arg) |
| 119 |
{ |
| 120 |
dSP; |
| 121 |
int count, ret; |
| 122 |
|
| 123 |
ENTER; |
| 124 |
SAVETMPS; |
| 125 |
|
| 126 |
PUSHMARK(SP); |
| 127 |
XPUSHs(arg); |
| 128 |
PUTBACK; |
| 129 |
|
| 130 |
count = call_sv(func, G_SCALAR); |
| 131 |
SPAGAIN; |
| 132 |
|
| 133 |
if (count != 1) return -1; |
| 134 |
|
| 135 |
ret = POPi; |
| 136 |
PUTBACK; |
| 137 |
FREETMPS; |
| 138 |
LEAVE; |
| 139 |
|
| 140 |
return ret; |
| 141 |
} |
| 142 |
|
| 143 |
int hts_fetch_fun (void *data, bam1_t *b) |
| 144 |
{ |
| 145 |
dSP; |
| 146 |
int count; |
| 147 |
|
| 148 |
fetch_callback_dataptr fcp; |
| 149 |
SV* callback; |
| 150 |
SV* callbackdata; |
| 151 |
SV* alignment_obj; |
| 152 |
bam1_t *b2; |
| 153 |
|
| 154 |
fcp = (fetch_callback_dataptr) data; |
| 155 |
callback = fcp->callback; |
| 156 |
callbackdata = fcp->data; |
| 157 |
|
| 158 |
/* The underlying bam1_t will be bam_destroy1()ed by alignment_obj's |
| 159 |
* destructor, so we need to duplicate it here. We could create the Perl SV |
| 160 |
* alongside the C bam1_t (cf bami_coverage), but note that a new b & b_sv |
| 161 |
* would be needed for each iteration, as some callback functions will |
| 162 |
* expect distinct references to distinct alignment objects each time. |
| 163 |
*/ |
| 164 |
b2 = bam_dup1(b); |
| 165 |
|
| 166 |
alignment_obj = sv_setref_pv(newSV(sizeof(bam1_t)),"Bio::DB::HTS::Alignment",(void*) b2); |
| 167 |
|
| 168 |
/* set up subroutine stack for the call */ |
| 169 |
ENTER; |
| 170 |
SAVETMPS; |
| 171 |
|
| 172 |
PUSHMARK(SP); |
| 173 |
XPUSHs(sv_2mortal(alignment_obj)); |
| 174 |
XPUSHs(callbackdata); |
| 175 |
PUTBACK; |
| 176 |
|
| 177 |
/* execute the call */ |
| 178 |
count = call_sv(callback,G_SCALAR|G_DISCARD); |
| 179 |
|
| 180 |
FREETMPS; |
| 181 |
LEAVE; |
| 182 |
|
| 183 |
return 1; |
| 184 |
} |
| 185 |
|
| 186 |
int invoke_pileup_callback_fun(uint32_t tid, |
| 187 |
uint32_t pos, |
| 188 |
int n, |
| 189 |
const bam_pileup1_t *pl, |
| 190 |
void *data) { |
| 191 |
dSP; |
| 192 |
int count,i; |
| 193 |
fetch_callback_dataptr fcp; |
| 194 |
SV* callback; |
| 195 |
SV* callbackdata; |
| 196 |
SV* p; |
| 197 |
AV* pileup; |
| 198 |
|
| 199 |
fcp = (fetch_callback_dataptr) data; |
| 200 |
callback = fcp->callback; |
| 201 |
callbackdata = fcp->data; |
| 202 |
|
| 203 |
/* turn the bam_pileup1_t into the appropriate object */ |
| 204 |
/* this causes a compiler warning -- ignore it */ |
| 205 |
pileup = newAV(); |
| 206 |
av_extend(pileup,n); |
| 207 |
for (i=0;i<n;i++) { |
| 208 |
p = newSV(sizeof(bam_pileup1_t)); |
| 209 |
sv_setref_pv(p,"Bio::DB::HTS::Pileup",(void*) &pl[i]); |
| 210 |
av_push(pileup,p); |
| 211 |
} |
| 212 |
|
| 213 |
/* set up subroutine stack for the call */ |
| 214 |
ENTER; |
| 215 |
SAVETMPS; |
| 216 |
|
| 217 |
PUSHMARK(SP); |
| 218 |
XPUSHs(sv_2mortal(newSViv(tid))); |
| 219 |
XPUSHs(sv_2mortal(newSViv(pos))); |
| 220 |
XPUSHs(sv_2mortal(newRV_noinc((SV*)pileup))); |
| 221 |
XPUSHs(callbackdata); |
| 222 |
PUTBACK; |
| 223 |
|
| 224 |
/* execute the call */ |
| 225 |
count = call_sv(callback,G_SCALAR|G_DISCARD); |
| 226 |
|
| 227 |
FREETMPS; |
| 228 |
LEAVE; |
| 229 |
|
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
/* |
| 234 |
Declarations to allow add_pileup_line to work |
| 235 |
Ported from samtoosl v1 setup. |
| 236 |
*/ |
| 237 |
|
| 238 |
/* start pileup support copy from bam.h in samtools */ |
| 239 |
/* but pileup functions are offered as bam_plp_auto_f in htslib */ |
| 240 |
|
| 241 |
typedef int (*bam_pileup_f)(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data); |
| 242 |
|
| 243 |
typedef struct |
| 244 |
{ |
| 245 |
bam_plp_t iter; |
| 246 |
bam_pileup_f func; |
| 247 |
void *data; |
| 248 |
} hts_plbuf_t; |
| 249 |
|
| 250 |
|
| 251 |
hts_plbuf_t *hts_plbuf_init(bam_pileup_f func, void *data) |
| 252 |
{ |
| 253 |
hts_plbuf_t *buf; |
| 254 |
buf = calloc(1, sizeof(hts_plbuf_t)); |
| 255 |
buf->iter = bam_plp_init(0, 0); |
| 256 |
buf->func = func; |
| 257 |
buf->data = data; |
| 258 |
return buf; |
| 259 |
} |
| 260 |
|
| 261 |
void hts_plbuf_destroy(hts_plbuf_t *buf) |
| 262 |
{ |
| 263 |
bam_plp_destroy(buf->iter); |
| 264 |
free(buf); |
| 265 |
} |
| 266 |
|
| 267 |
int hts_plbuf_push(const bam1_t *b, hts_plbuf_t *buf) |
| 268 |
{ |
| 269 |
int ret, n_plp, tid, pos; |
| 270 |
const bam_pileup1_t *plp; |
| 271 |
ret = bam_plp_push(buf->iter, b); |
| 272 |
if (ret < 0) return ret; |
| 273 |
while ((plp = bam_plp_next(buf->iter, &tid, &pos, &n_plp)) != 0) |
| 274 |
buf->func(tid, pos, n_plp, plp, buf->data); |
| 275 |
return 0; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
/* end pileup support copy from bam.h in samtools */ |
| 280 |
|
| 281 |
/** |
| 282 |
pileup support functions |
| 283 |
*/ |
| 284 |
int add_pileup_line (void *data, bam1_t *b) |
| 285 |
{ |
| 286 |
hts_plbuf_t *pileup = (hts_plbuf_t*) data; |
| 287 |
hts_plbuf_push(b,pileup); |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
int coverage_from_pileup_fun (uint32_t tid, |
| 294 |
uint32_t pos, |
| 295 |
int n, |
| 296 |
const bam_pileup1_t *pl, |
| 297 |
void *data) { |
| 298 |
coverage_graph_ptr cgp; |
| 299 |
int bin; |
| 300 |
int i; |
| 301 |
int valid; |
| 302 |
|
| 303 |
cgp = (coverage_graph_ptr) data; |
| 304 |
cgp->reads += n; |
| 305 |
|
| 306 |
valid = 0; |
| 307 |
for (i=0;i<n;i++) { |
| 308 |
if (!pl[i].is_del && !pl[i].is_refskip) |
| 309 |
valid++; |
| 310 |
} |
| 311 |
|
| 312 |
if (pos >= cgp->start && pos <= cgp->end) { |
| 313 |
bin = (pos-cgp->start)/cgp->width; |
| 314 |
cgp->bin[bin] += valid; |
| 315 |
} |
| 316 |
|
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
/** |
| 322 |
From bam_aux.c in samtools. Needed to allow pileup function to work. |
| 323 |
*/ |
| 324 |
int bam_parse_region(bam_hdr_t *header, const char *str, int *ref_id, int *beg, int *end) |
| 325 |
{ |
| 326 |
const char *name_lim = hts_parse_reg(str, beg, end); |
| 327 |
if (name_lim) { |
| 328 |
char *name = malloc(name_lim - str + 1); |
| 329 |
memcpy(name, str, name_lim - str); |
| 330 |
name[name_lim - str] = '\0'; |
| 331 |
*ref_id = bam_name2id(header, name); |
| 332 |
free(name); |
| 333 |
} |
| 334 |
else { |
| 335 |
// not parsable as a region, but possibly a sequence named "foo:a" |
| 336 |
*ref_id = bam_name2id(header, str); |
| 337 |
*beg = 0; *end = INT_MAX; |
| 338 |
} |
| 339 |
if (*ref_id == -1) return -1; |
| 340 |
return *beg <= *end? 0 : -1; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
From bam.c in samtools - these are wrappers that can be used OK here. |
| 345 |
*/ |
| 346 |
char *bam_format1(const bam_hdr_t *header, const bam1_t *b) |
| 347 |
{ |
| 348 |
kstring_t str; |
| 349 |
str.l = str.m = 0; str.s = NULL; |
| 350 |
sam_format1(header, b, &str); |
| 351 |
return str.s; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
void bam_view1(const bam_hdr_t *header, const bam1_t *b) |
| 356 |
{ |
| 357 |
char *s = bam_format1(header, b); |
| 358 |
puts(s); |
| 359 |
free(s); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
/** |
| 364 |
Get the file extension for a filename |
| 365 |
*/ |
| 366 |
int get_index_fmt_from_extension(const char * filename) |
| 367 |
{ |
| 368 |
char * ext = strrchr( filename, '.' ) ; |
| 369 |
if( strcmp(ext, ".cram")==0 ) |
| 370 |
{ |
| 371 |
return HTS_FMT_CRAI ; |
| 372 |
} |
| 373 |
if( strcmp(ext, ".bam")==0 ) |
| 374 |
{ |
| 375 |
return HTS_FMT_BAI ; //could also be HTS_FMT_CSI |
| 376 |
} |
| 377 |
return -1 ; |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
fetch function |
| 382 |
*/ |
| 383 |
int hts_fetch(htsFile *fp, const hts_idx_t *idx, int tid, int beg, int end, void *data, bam_plp_auto_f func) |
| 384 |
{ |
| 385 |
int ret; |
| 386 |
hts_itr_t *iter ; |
| 387 |
bam1_t *b ; |
| 388 |
|
| 389 |
iter = sam_itr_queryi(idx, tid, beg, end); |
| 390 |
b = bam_init1(); |
| 391 |
|
| 392 |
while((ret = sam_itr_next(fp, iter, b)) >= 0) |
| 393 |
{ |
| 394 |
func(data,b); |
| 395 |
} |
| 396 |
hts_itr_destroy(iter); |
| 397 |
bam_destroy1(b); |
| 398 |
return (ret == -1)? 0 : ret; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS |
| 403 |
|
| 404 |
SV* |
| 405 |
htslib_version(packname="Bio::DB::HTS") |
| 406 |
char * packname |
| 407 |
PROTOTYPE: $ |
| 408 |
CODE: |
| 409 |
RETVAL = newSVpv(hts_version(), 0); |
| 410 |
OUTPUT: |
| 411 |
RETVAL |
| 412 |
|
| 413 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::Fai PREFIX=fai_ |
| 414 |
|
| 415 |
Bio::DB::HTS::Fai |
| 416 |
fai_load(packname="Bio::DB::HTS::Fai", filename) |
| 417 |
char * packname |
| 418 |
char * filename |
| 419 |
PROTOTYPE: $$ |
| 420 |
CODE: |
| 421 |
RETVAL = fai_load(filename); |
| 422 |
OUTPUT: |
| 423 |
RETVAL |
| 424 |
|
| 425 |
void |
| 426 |
fai_DESTROY(fai) |
| 427 |
Bio::DB::HTS::Fai fai |
| 428 |
PROTOTYPE: $ |
| 429 |
CODE: |
| 430 |
fai_destroy(fai); |
| 431 |
|
| 432 |
SV* |
| 433 |
fai_fetch(fai,reg) |
| 434 |
Bio::DB::HTS::Fai fai |
| 435 |
const char *reg |
| 436 |
PROTOTYPE: $$$ |
| 437 |
PREINIT: |
| 438 |
char *seq; |
| 439 |
int len; |
| 440 |
CODE: |
| 441 |
seq = fai_fetch(fai,reg,&len); |
| 442 |
if (seq == NULL) |
| 443 |
XSRETURN_EMPTY; |
| 444 |
RETVAL = newSVpv(seq,len); |
| 445 |
free((void*)seq); |
| 446 |
OUTPUT: |
| 447 |
RETVAL |
| 448 |
|
| 449 |
|
| 450 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTSfile PREFIX=hts_ |
| 451 |
|
| 452 |
int |
| 453 |
max_pileup_cnt(packname,...) |
| 454 |
CODE: |
| 455 |
if (items > 1) |
| 456 |
MaxPileupCnt = SvIV(ST(1)); |
| 457 |
RETVAL = MaxPileupCnt; |
| 458 |
OUTPUT: |
| 459 |
RETVAL |
| 460 |
|
| 461 |
|
| 462 |
int |
| 463 |
hts_is_remote(packname, filename) |
| 464 |
char * packname |
| 465 |
char * filename |
| 466 |
PROTOTYPE: $$ |
| 467 |
CODE: |
| 468 |
RETVAL = hisremote(filename) ; |
| 469 |
OUTPUT: |
| 470 |
RETVAL |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
Bio::DB::HTSfile |
| 476 |
hts_open(packname, filename, mode="r") |
| 477 |
char * packname |
| 478 |
char * filename |
| 479 |
char * mode |
| 480 |
PROTOTYPE: $$$ |
| 481 |
CODE: |
| 482 |
RETVAL = hts_open(filename,mode); |
| 483 |
OUTPUT: |
| 484 |
RETVAL |
| 485 |
|
| 486 |
|
| 487 |
void |
| 488 |
hts_DESTROY(htsfile) |
| 489 |
Bio::DB::HTSfile htsfile |
| 490 |
PROTOTYPE: $ |
| 491 |
CODE: |
| 492 |
hts_close(htsfile); |
| 493 |
|
| 494 |
|
| 495 |
int |
| 496 |
hts_index_build(packname, filename) |
| 497 |
char * packname |
| 498 |
const char * filename |
| 499 |
CODE: |
| 500 |
RETVAL = sam_index_build(filename,0); //generate BAI for BAM files |
| 501 |
OUTPUT: |
| 502 |
RETVAL |
| 503 |
|
| 504 |
Bio::DB::HTS::Index |
| 505 |
hts_index_load(packname, htsfile) |
| 506 |
char * packname |
| 507 |
Bio::DB::HTSfile htsfile |
| 508 |
PROTOTYPE: $$ |
| 509 |
PREINIT: |
| 510 |
SV *htsfile_sv = SvRV(ST(1)); |
| 511 |
HV *assocfile = get_hv("Bio::DB::HTS::_associated_file", GV_ADD); |
| 512 |
CODE: |
| 513 |
RETVAL = sam_index_load(htsfile, htsfile->fn) ; |
| 514 |
|
| 515 |
/* For CRAM, it is important that this hts_idx_t is destroyed *before* |
| 516 |
* the associated htsFile. We hold on to a reference to the htsFile |
| 517 |
* (which we'll release in our destructor) to ensure it outlives us. |
| 518 |
*/ |
| 519 |
SvREFCNT_inc(htsfile_sv); |
| 520 |
hv_store(assocfile, (char *) &RETVAL, sizeof RETVAL, htsfile_sv, 0); |
| 521 |
OUTPUT: |
| 522 |
RETVAL |
| 523 |
|
| 524 |
Bio::DB::HTS::Header |
| 525 |
hts_header_read(htsfile) |
| 526 |
Bio::DB::HTSfile htsfile |
| 527 |
PROTOTYPE: $$ |
| 528 |
PREINIT: |
| 529 |
bam_hdr_t *bh; |
| 530 |
int64_t result ; |
| 531 |
const htsFormat *format ; |
| 532 |
CODE: |
| 533 |
format = hts_get_format( htsfile ) ; |
| 534 |
if( format->format == bam ) //enum value from htsExactFormat from hts.h |
| 535 |
result = bgzf_seek(htsfile->fp.bgzf,0,0) ; |
| 536 |
/* |
| 537 |
* https://github.com/Ensembl/Bio-DB-HTS/issues/54 |
| 538 |
* must seek at beginning of file for sam as well |
| 539 |
*/ |
| 540 |
else if ( format->format == sam ) { |
| 541 |
/* |
| 542 |
* Using hseek with htslib < 1.5 triggers segfault |
| 543 |
* and couldn't find a valid alternative. |
| 544 |
* |
| 545 |
* WARNING: we're tied to buggy behaviour for htslib <= 1.3.1 |
| 546 |
*/ |
| 547 |
if ( strcmp(hts_version(), "1.5") >= 0 ) |
| 548 |
result = hseek(htsfile->fp.hfile, 0, SEEK_SET); |
| 549 |
} |
| 550 |
|
| 551 |
bh = sam_hdr_read(htsfile); |
| 552 |
RETVAL = bh ; |
| 553 |
OUTPUT: |
| 554 |
RETVAL |
| 555 |
|
| 556 |
|
| 557 |
int |
| 558 |
hts_header_write(htsfile,header, ...) |
| 559 |
Bio::DB::HTSfile htsfile |
| 560 |
Bio::DB::HTS::Header header |
| 561 |
PREINIT: |
| 562 |
char *reference = ""; |
| 563 |
const htsFormat *format ; |
| 564 |
PROTOTYPE: $$ |
| 565 |
CODE: |
| 566 |
format = hts_get_format( htsfile ) ; |
| 567 |
if( format->format == cram ) |
| 568 |
{ |
| 569 |
if(items > 2) |
| 570 |
{ |
| 571 |
reference = (char *)SvPV_nolen(ST(2)); |
| 572 |
hts_set_fai_filename(htsfile, reference); |
| 573 |
} |
| 574 |
else |
| 575 |
{ |
| 576 |
croak("Error: need reference sequence file for writing CRAM file '%s'", htsfile->fn); |
| 577 |
} |
| 578 |
} |
| 579 |
RETVAL= sam_hdr_write(htsfile,header); |
| 580 |
if (RETVAL != 0) |
| 581 |
croak("Error %d while creating file '%s'", RETVAL, htsfile->fn); |
| 582 |
OUTPUT: |
| 583 |
RETVAL |
| 584 |
|
| 585 |
Bio::DB::HTS::Alignment |
| 586 |
hts_read1(htsfile,header) |
| 587 |
Bio::DB::HTSfile htsfile |
| 588 |
Bio::DB::HTS::Header header |
| 589 |
PROTOTYPE: $$ |
| 590 |
PREINIT: |
| 591 |
bam1_t *alignment; |
| 592 |
CODE: |
| 593 |
alignment = bam_init1(); |
| 594 |
if (sam_read1(htsfile,header,alignment) >= 0) { |
| 595 |
RETVAL = alignment ; |
| 596 |
} |
| 597 |
else { |
| 598 |
bam_destroy1(alignment); |
| 599 |
XSRETURN_EMPTY; |
| 600 |
} |
| 601 |
OUTPUT: |
| 602 |
RETVAL |
| 603 |
|
| 604 |
int |
| 605 |
hts_write1(htsfile,header,align) |
| 606 |
Bio::DB::HTSfile htsfile |
| 607 |
Bio::DB::HTS::Header header |
| 608 |
Bio::DB::HTS::Alignment align |
| 609 |
PROTOTYPE: $$ |
| 610 |
CODE: |
| 611 |
RETVAL = sam_write1(htsfile,header,align); |
| 612 |
OUTPUT: |
| 613 |
RETVAL |
| 614 |
|
| 615 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::Alignment PREFIX=bama_ |
| 616 |
|
| 617 |
Bio::DB::HTS::Alignment |
| 618 |
bama_new(package="Bio::DB::HTS::Alignment") |
| 619 |
char * package |
| 620 |
PROTOTYPE: $ |
| 621 |
CODE: |
| 622 |
RETVAL = bam_init1(); |
| 623 |
OUTPUT: |
| 624 |
RETVAL |
| 625 |
|
| 626 |
void |
| 627 |
bama_DESTROY(b) |
| 628 |
Bio::DB::HTS::Alignment b |
| 629 |
PROTOTYPE: $ |
| 630 |
CODE: |
| 631 |
bam_destroy1(b); |
| 632 |
|
| 633 |
int |
| 634 |
bama_tid(b,...) |
| 635 |
Bio::DB::HTS::Alignment b |
| 636 |
PROTOTYPE: $;$ |
| 637 |
CODE: |
| 638 |
if (items > 1) |
| 639 |
b->core.tid = SvIV(ST(1)); |
| 640 |
RETVAL=b->core.tid; |
| 641 |
OUTPUT: |
| 642 |
RETVAL |
| 643 |
|
| 644 |
int |
| 645 |
bama_pos(b,...) |
| 646 |
Bio::DB::HTS::Alignment b |
| 647 |
PROTOTYPE: $;$ |
| 648 |
CODE: |
| 649 |
if (items > 1) |
| 650 |
b->core.pos = SvIV(ST(1)); |
| 651 |
RETVAL=b->core.pos; |
| 652 |
OUTPUT: |
| 653 |
RETVAL |
| 654 |
|
| 655 |
int |
| 656 |
bama_calend(b) |
| 657 |
Bio::DB::HTS::Alignment b |
| 658 |
PROTOTYPE: $ |
| 659 |
CODE: |
| 660 |
RETVAL=bam_endpos(b); |
| 661 |
OUTPUT: |
| 662 |
RETVAL |
| 663 |
|
| 664 |
int |
| 665 |
bama_cigar2qlen(b) |
| 666 |
Bio::DB::HTS::Alignment b |
| 667 |
PROTOTYPE: $ |
| 668 |
CODE: |
| 669 |
RETVAL=bam_cigar2qlen(b->core.n_cigar,bam_get_cigar(b)); |
| 670 |
OUTPUT: |
| 671 |
RETVAL |
| 672 |
|
| 673 |
int |
| 674 |
bama_qual(b,...) |
| 675 |
Bio::DB::HTS::Alignment b |
| 676 |
PROTOTYPE: $;$ |
| 677 |
CODE: |
| 678 |
if (items > 1) |
| 679 |
b->core.qual = SvIV(ST(1)); |
| 680 |
RETVAL=b->core.qual; |
| 681 |
OUTPUT: |
| 682 |
RETVAL |
| 683 |
|
| 684 |
int |
| 685 |
bama_flag(b,...) |
| 686 |
Bio::DB::HTS::Alignment b |
| 687 |
PROTOTYPE: $;$ |
| 688 |
CODE: |
| 689 |
if (items > 1) |
| 690 |
b->core.flag = SvIV(ST(1)); |
| 691 |
RETVAL=b->core.flag; |
| 692 |
OUTPUT: |
| 693 |
RETVAL |
| 694 |
|
| 695 |
int |
| 696 |
bama_n_cigar(b,...) |
| 697 |
Bio::DB::HTS::Alignment b |
| 698 |
PROTOTYPE: $;$ |
| 699 |
CODE: |
| 700 |
if (items > 1) |
| 701 |
b->core.n_cigar = SvIV(ST(1)); |
| 702 |
RETVAL=b->core.n_cigar; |
| 703 |
OUTPUT: |
| 704 |
RETVAL |
| 705 |
|
| 706 |
int |
| 707 |
bama_l_qseq(b,...) |
| 708 |
Bio::DB::HTS::Alignment b |
| 709 |
PROTOTYPE: $;$ |
| 710 |
CODE: |
| 711 |
if (items > 1) |
| 712 |
b->core.l_qseq = SvIV(ST(1)); |
| 713 |
RETVAL=b->core.l_qseq; |
| 714 |
OUTPUT: |
| 715 |
RETVAL |
| 716 |
|
| 717 |
SV* |
| 718 |
bama_qseq(b) |
| 719 |
Bio::DB::HTS::Alignment b |
| 720 |
PROTOTYPE: $ |
| 721 |
PREINIT: |
| 722 |
char* seq; |
| 723 |
int i; |
| 724 |
CODE: |
| 725 |
Newxz(seq,b->core.l_qseq+1,char); |
| 726 |
for (i=0;i<b->core.l_qseq;i++) { |
| 727 |
seq[i]=seq_nt16_str[bam_seqi(bam_get_seq(b),i)]; |
| 728 |
} |
| 729 |
RETVAL = newSVpv(seq,b->core.l_qseq); |
| 730 |
Safefree(seq); |
| 731 |
OUTPUT: |
| 732 |
RETVAL |
| 733 |
|
| 734 |
SV* |
| 735 |
bama__qscore(b) |
| 736 |
Bio::DB::HTS::Alignment b |
| 737 |
PROTOTYPE: $ |
| 738 |
CODE: |
| 739 |
RETVAL = newSVpv((char *) bam_get_qual(b),b->core.l_qseq); |
| 740 |
OUTPUT: |
| 741 |
RETVAL |
| 742 |
|
| 743 |
int |
| 744 |
bama_mtid(b,...) |
| 745 |
Bio::DB::HTS::Alignment b |
| 746 |
PROTOTYPE: $;$ |
| 747 |
CODE: |
| 748 |
if (items > 1) |
| 749 |
b->core.mtid = SvIV(ST(1)); |
| 750 |
RETVAL=b->core.mtid; |
| 751 |
OUTPUT: |
| 752 |
RETVAL |
| 753 |
|
| 754 |
int |
| 755 |
bama_mpos(b,...) |
| 756 |
Bio::DB::HTS::Alignment b |
| 757 |
PROTOTYPE: $;$ |
| 758 |
CODE: |
| 759 |
if (items > 1) |
| 760 |
b->core.mpos = SvIV(ST(1)); |
| 761 |
RETVAL=b->core.mpos; |
| 762 |
OUTPUT: |
| 763 |
RETVAL |
| 764 |
|
| 765 |
int |
| 766 |
bama_isize(b,...) |
| 767 |
Bio::DB::HTS::Alignment b |
| 768 |
PROTOTYPE: $;$ |
| 769 |
CODE: |
| 770 |
if (items > 1) |
| 771 |
b->core.isize = SvIV(ST(1)); |
| 772 |
RETVAL=b->core.isize; |
| 773 |
OUTPUT: |
| 774 |
RETVAL |
| 775 |
|
| 776 |
int |
| 777 |
bama_l_aux(b,...) |
| 778 |
Bio::DB::HTS::Alignment b |
| 779 |
PROTOTYPE: $;$ |
| 780 |
CODE: |
| 781 |
RETVAL=SvIV(newSViv(bam_get_l_aux(b))); |
| 782 |
OUTPUT: |
| 783 |
RETVAL |
| 784 |
|
| 785 |
char* |
| 786 |
bama_aux(b) |
| 787 |
Bio::DB::HTS::Alignment b |
| 788 |
PREINIT: |
| 789 |
uint8_t *s; |
| 790 |
uint8_t type, key[2]; |
| 791 |
char str[8192]; |
| 792 |
CODE: |
| 793 |
s = bam_get_aux(b); |
| 794 |
str[0] = '\0'; |
| 795 |
|
| 796 |
int left = sizeof(str) - strlen(str); |
| 797 |
while (left > 0 && (s < b->data + b->l_data)) { |
| 798 |
char* d = str+strlen(str); |
| 799 |
|
| 800 |
key[0] = s[0]; |
| 801 |
key[1] = s[1]; |
| 802 |
left -= snprintf(d, left, "%c%c:", key[0], key[1]); |
| 803 |
|
| 804 |
d += 3; |
| 805 |
s += 2; |
| 806 |
type = *s++; |
| 807 |
|
| 808 |
if (left <= 0) continue; |
| 809 |
|
| 810 |
if (type == 'A') { left -= snprintf(d, left, "A:%c", *s); s++; } |
| 811 |
else if (type == 'C') { left -= snprintf(d, left, "i:%u", *s); s++; } |
| 812 |
else if (type == 'c') { left -= snprintf(d, left, "i:%d", *s); s++; } |
| 813 |
else if (type == 'S') { left -= snprintf(d, left, "i:%u", *(uint16_t*)s);s += 2; } |
| 814 |
else if (type == 's') { left -= snprintf(d, left, "i:%d", *(int16_t*)s); s += 2; } |
| 815 |
else if (type == 'I') { left -= snprintf(d, left, "i:%u", *(uint32_t*)s);s += 4; } |
| 816 |
else if (type == 'i') { left -= snprintf(d, left, "i:%d", *(int32_t*)s); s += 4; } |
| 817 |
else if (type == 'f') { left -= snprintf(d, left, "f:%g", *(float*)s); s += 4; } |
| 818 |
else if (type == 'd') { left -= snprintf(d, left, "d:%lg", *(double*)s); s += 8; } |
| 819 |
else if (type == 'Z' || type == 'H') { left -= snprintf(d, left, "%c:", type); |
| 820 |
strncat(d, (char *) s, left); |
| 821 |
while (*s++) {} |
| 822 |
left = sizeof(str) - strlen(str); |
| 823 |
} |
| 824 |
if (left <= 0) continue; |
| 825 |
strncat(d,"\t",left); |
| 826 |
left--; |
| 827 |
} |
| 828 |
str[strlen(str)-1] = '\0'; |
| 829 |
RETVAL = str; |
| 830 |
OUTPUT: |
| 831 |
RETVAL |
| 832 |
|
| 833 |
SV* |
| 834 |
bama_aux_get(b,tag) |
| 835 |
Bio::DB::HTS::Alignment b |
| 836 |
char* tag |
| 837 |
PROTOTYPE: $$ |
| 838 |
PREINIT: |
| 839 |
int type; |
| 840 |
uint8_t *s; |
| 841 |
CODE: |
| 842 |
s = bam_aux_get(b,tag); |
| 843 |
if (s==0) |
| 844 |
XSRETURN_EMPTY; |
| 845 |
type = *s++; |
| 846 |
switch (type) { |
| 847 |
case 'c': |
| 848 |
RETVAL = newSViv((int32_t)*(int8_t*)s); |
| 849 |
break; |
| 850 |
case 'C': |
| 851 |
RETVAL = newSViv((int32_t)*(uint8_t*)s); |
| 852 |
break; |
| 853 |
case 's': |
| 854 |
RETVAL = newSViv((int32_t)*(int16_t*)s); |
| 855 |
break; |
| 856 |
case 'S': |
| 857 |
RETVAL = newSViv((int32_t)*(uint16_t*)s); |
| 858 |
break; |
| 859 |
case 'i': |
| 860 |
RETVAL = newSViv(*(int32_t*)s); |
| 861 |
break; |
| 862 |
case 'I': |
| 863 |
RETVAL = newSViv((int32_t)*(uint32_t*)s); |
| 864 |
break; |
| 865 |
case 'f': |
| 866 |
RETVAL = newSVnv(*(float*)s); |
| 867 |
break; |
| 868 |
case 'Z': |
| 869 |
case 'H': |
| 870 |
RETVAL = newSVpv((char*)s,0); |
| 871 |
break; |
| 872 |
case 'A': |
| 873 |
RETVAL = newSVpv((char*)s,1); |
| 874 |
break; |
| 875 |
default: |
| 876 |
XSRETURN_EMPTY; |
| 877 |
} |
| 878 |
OUTPUT: |
| 879 |
RETVAL |
| 880 |
|
| 881 |
void |
| 882 |
bama_aux_keys(b) |
| 883 |
Bio::DB::HTS::Alignment b |
| 884 |
PROTOTYPE: $ |
| 885 |
PREINIT: |
| 886 |
uint8_t *s; |
| 887 |
uint8_t type; |
| 888 |
PPCODE: |
| 889 |
{ |
| 890 |
s = bam_get_aux(b); /* s is a khash macro */ |
| 891 |
while (s < b->data + b->l_data) { |
| 892 |
XPUSHs(sv_2mortal(newSVpv((char *) s, 2))); |
| 893 |
s += 2; |
| 894 |
type = *s++; |
| 895 |
if (type == 'A') { ++s; } |
| 896 |
else if (type == 'C') { ++s; } |
| 897 |
else if (type == 'c') { ++s; } |
| 898 |
else if (type == 'S') { s += 2; } |
| 899 |
else if (type == 's') { s += 2; } |
| 900 |
else if (type == 'I') { s += 4; } |
| 901 |
else if (type == 'i') { s += 4; } |
| 902 |
else if (type == 'f') { s += 4; } |
| 903 |
else if (type == 'Z' || type == 'H') { while (*s) ++(s); ++(s); } |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
SV* |
| 908 |
bama_data(b,...) |
| 909 |
Bio::DB::HTS::Alignment b |
| 910 |
PROTOTYPE: $;$ |
| 911 |
PREINIT: |
| 912 |
STRLEN len; |
| 913 |
CODE: |
| 914 |
if (items > 1) { |
| 915 |
b->data = (uint8_t *) SvPV(ST(1),len); |
| 916 |
b->l_data = len; |
| 917 |
} |
| 918 |
RETVAL=newSVpv((char *) b->data, b->l_data); |
| 919 |
OUTPUT: |
| 920 |
RETVAL |
| 921 |
|
| 922 |
int |
| 923 |
bama_data_len(b,...) |
| 924 |
Bio::DB::HTS::Alignment b |
| 925 |
PROTOTYPE: $;$ |
| 926 |
CODE: |
| 927 |
if (items > 1) |
| 928 |
b->l_data = SvIV(ST(1)); |
| 929 |
RETVAL=b->l_data; |
| 930 |
OUTPUT: |
| 931 |
RETVAL |
| 932 |
|
| 933 |
int |
| 934 |
bama_m_data(b,...) |
| 935 |
Bio::DB::HTS::Alignment b |
| 936 |
PROTOTYPE: $;$ |
| 937 |
CODE: |
| 938 |
if (items > 1) { |
| 939 |
b->m_data = SvIV(ST(1)); |
| 940 |
} |
| 941 |
RETVAL=b->m_data; |
| 942 |
OUTPUT: |
| 943 |
RETVAL |
| 944 |
|
| 945 |
SV* |
| 946 |
bama_qname(b) |
| 947 |
Bio::DB::HTS::Alignment b |
| 948 |
PROTOTYPE: $ |
| 949 |
CODE: |
| 950 |
RETVAL=newSVpv(bam_get_qname(b),0); |
| 951 |
OUTPUT: |
| 952 |
RETVAL |
| 953 |
|
| 954 |
int |
| 955 |
bama_paired(b) |
| 956 |
Bio::DB::HTS::Alignment b |
| 957 |
PROTOTYPE: $ |
| 958 |
CODE: |
| 959 |
RETVAL=(b->core.flag&BAM_FPAIRED) != 0; |
| 960 |
OUTPUT: |
| 961 |
RETVAL |
| 962 |
|
| 963 |
int |
| 964 |
bama_proper_pair(b) |
| 965 |
Bio::DB::HTS::Alignment b |
| 966 |
PROTOTYPE: $ |
| 967 |
CODE: |
| 968 |
RETVAL=(b->core.flag&BAM_FPROPER_PAIR) != 0; |
| 969 |
OUTPUT: |
| 970 |
RETVAL |
| 971 |
|
| 972 |
int |
| 973 |
bama_unmapped(b) |
| 974 |
Bio::DB::HTS::Alignment b |
| 975 |
PROTOTYPE: $ |
| 976 |
CODE: |
| 977 |
RETVAL=(b->core.flag&BAM_FUNMAP) != 0; |
| 978 |
OUTPUT: |
| 979 |
RETVAL |
| 980 |
|
| 981 |
int |
| 982 |
bama_munmapped(b) |
| 983 |
Bio::DB::HTS::Alignment b |
| 984 |
PROTOTYPE: $ |
| 985 |
CODE: |
| 986 |
RETVAL=(b->core.flag&BAM_FMUNMAP) != 0; |
| 987 |
OUTPUT: |
| 988 |
RETVAL |
| 989 |
|
| 990 |
int |
| 991 |
bama_reversed(b) |
| 992 |
Bio::DB::HTS::Alignment b |
| 993 |
PROTOTYPE: $ |
| 994 |
CODE: |
| 995 |
RETVAL=bam_is_rev(b); |
| 996 |
OUTPUT: |
| 997 |
RETVAL |
| 998 |
|
| 999 |
int |
| 1000 |
bama_mreversed(b) |
| 1001 |
Bio::DB::HTS::Alignment b |
| 1002 |
PROTOTYPE: $ |
| 1003 |
CODE: |
| 1004 |
RETVAL=bam_is_mrev(b); |
| 1005 |
OUTPUT: |
| 1006 |
RETVAL |
| 1007 |
|
| 1008 |
SV* |
| 1009 |
bama_cigar(b) |
| 1010 |
Bio::DB::HTS::Alignment b |
| 1011 |
PROTOTYPE: $ |
| 1012 |
PREINIT: |
| 1013 |
int i; |
| 1014 |
uint32_t *c; |
| 1015 |
AV *avref; |
| 1016 |
CODE: |
| 1017 |
avref = (AV*) sv_2mortal((SV*)newAV()); |
| 1018 |
c = bam_get_cigar(b); |
| 1019 |
for (i=0;i<b->core.n_cigar;i++) |
| 1020 |
av_push(avref, newSViv(c[i])); |
| 1021 |
RETVAL = (SV*) newRV((SV*)avref); |
| 1022 |
OUTPUT: |
| 1023 |
RETVAL |
| 1024 |
|
| 1025 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::Header PREFIX=bam_ |
| 1026 |
|
| 1027 |
Bio::DB::HTS::Header |
| 1028 |
bam_new(packname=Bio::DB::HTS::Header) |
| 1029 |
PROTOTYPE: $ |
| 1030 |
CODE: |
| 1031 |
RETVAL = bam_hdr_init(); |
| 1032 |
OUTPUT: |
| 1033 |
RETVAL |
| 1034 |
|
| 1035 |
int |
| 1036 |
bam_n_targets(bamh) |
| 1037 |
Bio::DB::HTS::Header bamh |
| 1038 |
PROTOTYPE: $ |
| 1039 |
CODE: |
| 1040 |
RETVAL = bamh->n_targets; |
| 1041 |
OUTPUT: |
| 1042 |
RETVAL |
| 1043 |
|
| 1044 |
SV* |
| 1045 |
bam_target_name(bamh) |
| 1046 |
Bio::DB::HTS::Header bamh |
| 1047 |
PROTOTYPE: $ |
| 1048 |
PREINIT: |
| 1049 |
int i; |
| 1050 |
AV * avref; |
| 1051 |
CODE: |
| 1052 |
avref = (AV*) sv_2mortal((SV*)newAV()); |
| 1053 |
for (i=0;i<bamh->n_targets;i++) |
| 1054 |
av_push(avref, newSVpv(bamh->target_name[i],0)); |
| 1055 |
RETVAL = (SV*) newRV((SV*)avref); |
| 1056 |
OUTPUT: |
| 1057 |
RETVAL |
| 1058 |
|
| 1059 |
SV* |
| 1060 |
bam_target_len(bamh) |
| 1061 |
Bio::DB::HTS::Header bamh |
| 1062 |
PROTOTYPE: $ |
| 1063 |
PREINIT: |
| 1064 |
int i; |
| 1065 |
AV * avref; |
| 1066 |
CODE: |
| 1067 |
avref = (AV*) sv_2mortal((SV*)newAV()); |
| 1068 |
for (i=0;i<bamh->n_targets;i++) |
| 1069 |
av_push(avref, newSViv(bamh->target_len[i])); |
| 1070 |
RETVAL = (SV*) newRV((SV*)avref); |
| 1071 |
OUTPUT: |
| 1072 |
RETVAL |
| 1073 |
|
| 1074 |
SV* |
| 1075 |
bam_text(bamh, ...) |
| 1076 |
Bio::DB::HTS::Header bamh |
| 1077 |
PREINIT: |
| 1078 |
char *newtext; |
| 1079 |
STRLEN n; |
| 1080 |
CODE: |
| 1081 |
/* in case text is not null terminated, we copy it */ |
| 1082 |
RETVAL = newSVpv(bamh->text,bamh->l_text); |
| 1083 |
if (items > 1) { |
| 1084 |
newtext = (char*) SvPV(ST(1),n); |
| 1085 |
strcpy(bamh->text,newtext); |
| 1086 |
bamh->l_text = n; |
| 1087 |
} |
| 1088 |
OUTPUT: |
| 1089 |
RETVAL |
| 1090 |
|
| 1091 |
|
| 1092 |
void |
| 1093 |
bam_parse_region(bamh,region) |
| 1094 |
Bio::DB::HTS::Header bamh |
| 1095 |
char* region |
| 1096 |
PROTOTYPE: $ |
| 1097 |
PREINIT: |
| 1098 |
int seqid,start,end; |
| 1099 |
PPCODE: |
| 1100 |
{ |
| 1101 |
bam_parse_region(bamh, |
| 1102 |
region, |
| 1103 |
&seqid, |
| 1104 |
&start, |
| 1105 |
&end); |
| 1106 |
if (seqid < 0) |
| 1107 |
XSRETURN_EMPTY; |
| 1108 |
else { |
| 1109 |
EXTEND(sp,3); |
| 1110 |
PUSHs(sv_2mortal(newSViv(seqid))); |
| 1111 |
PUSHs(sv_2mortal(newSViv(start))); |
| 1112 |
PUSHs(sv_2mortal(newSViv(end))); |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
void |
| 1117 |
bam_view1(bamh,alignment) |
| 1118 |
Bio::DB::HTS::Header bamh |
| 1119 |
Bio::DB::HTS::Alignment alignment |
| 1120 |
PROTOTYPE: $$ |
| 1121 |
CODE: |
| 1122 |
bam_view1(bamh,alignment); |
| 1123 |
|
| 1124 |
|
| 1125 |
void |
| 1126 |
bam_DESTROY(bamh) |
| 1127 |
Bio::DB::HTS::Header bamh |
| 1128 |
PROTOTYPE: $ |
| 1129 |
CODE: |
| 1130 |
bam_hdr_destroy(bamh); |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::Index PREFIX=bami_ |
| 1135 |
|
| 1136 |
int |
| 1137 |
bami_fetch(bai,hfp,ref,start,end,callback,callbackdata=&PL_sv_undef) |
| 1138 |
Bio::DB::HTS::Index bai |
| 1139 |
Bio::DB::HTSfile hfp |
| 1140 |
int ref |
| 1141 |
int start |
| 1142 |
int end |
| 1143 |
CV* callback |
| 1144 |
SV* callbackdata |
| 1145 |
PREINIT: |
| 1146 |
fetch_callback_data fcd; |
| 1147 |
CODE: |
| 1148 |
{ |
| 1149 |
fcd.callback = (SV*) callback; |
| 1150 |
fcd.data = callbackdata; |
| 1151 |
RETVAL = hts_fetch(hfp,bai,ref,start,end,&fcd,hts_fetch_fun); |
| 1152 |
} |
| 1153 |
OUTPUT: |
| 1154 |
RETVAL |
| 1155 |
|
| 1156 |
|
| 1157 |
void |
| 1158 |
bami_pileup(bai,hfp,ref,start,end,callback,callbackdata=&PL_sv_undef) |
| 1159 |
Bio::DB::HTS::Index bai |
| 1160 |
Bio::DB::HTSfile hfp |
| 1161 |
int ref |
| 1162 |
int start |
| 1163 |
int end |
| 1164 |
CV* callback |
| 1165 |
SV* callbackdata |
| 1166 |
PREINIT: |
| 1167 |
fetch_callback_data fcd; |
| 1168 |
hts_plbuf_t *pileup; |
| 1169 |
CODE: |
| 1170 |
fcd.callback = (SV*) callback; |
| 1171 |
fcd.data = callbackdata; |
| 1172 |
pileup = hts_plbuf_init(invoke_pileup_callback_fun,(void*)&fcd); |
| 1173 |
bam_plp_set_maxcnt(pileup->iter,MaxPileupCnt); |
| 1174 |
hts_fetch(hfp,bai,ref,start,end,(void*)pileup,add_pileup_line); |
| 1175 |
hts_plbuf_push(NULL,pileup); |
| 1176 |
hts_plbuf_destroy(pileup); |
| 1177 |
|
| 1178 |
AV* |
| 1179 |
bami_coverage(bai,hfp,ref,start,end,bins=0,maxcnt=8000,filter=NULL) |
| 1180 |
Bio::DB::HTS::Index bai |
| 1181 |
Bio::DB::HTSfile hfp |
| 1182 |
int ref |
| 1183 |
int start |
| 1184 |
int end |
| 1185 |
int bins |
| 1186 |
int maxcnt |
| 1187 |
SV* filter |
| 1188 |
PREINIT: |
| 1189 |
coverage_graph cg; |
| 1190 |
hts_plbuf_t *pileup; |
| 1191 |
AV* array; |
| 1192 |
int i, ret; |
| 1193 |
bam_hdr_t *bh; |
| 1194 |
hts_itr_t *iter; |
| 1195 |
const htsFormat *format ; |
| 1196 |
CODE: |
| 1197 |
{ |
| 1198 |
/* TODO:can we do away with this check by a move to CSI as the standard for BAM indices */ |
| 1199 |
if (end >= BAM_MAX_REGION) |
| 1200 |
{ |
| 1201 |
format = hts_get_format( hfp ) ; |
| 1202 |
if( format->format == bam ) //enum value from htsExactFormat from hts.h |
| 1203 |
{ |
| 1204 |
bgzf_seek(hfp->fp.bgzf,0,0); |
| 1205 |
bh = sam_hdr_read(hfp); |
| 1206 |
end = bh->target_len[ref]; |
| 1207 |
bam_hdr_destroy(bh); |
| 1208 |
} |
| 1209 |
} |
| 1210 |
if ((bins==0) || (bins > (end-start))) |
| 1211 |
bins = end-start; |
| 1212 |
|
| 1213 |
/* coverage graph used to communicate to our callback |
| 1214 |
the region we are sampling */ |
| 1215 |
cg.start = start; |
| 1216 |
cg.end = end; |
| 1217 |
cg.reads = 0; |
| 1218 |
cg.width = ((double)(end-start))/bins; |
| 1219 |
Newxz(cg.bin,bins+1,int); |
| 1220 |
|
| 1221 |
/* accumulate coverage into the coverage graph */ |
| 1222 |
pileup = hts_plbuf_init(coverage_from_pileup_fun,(void*)&cg); |
| 1223 |
if (items >= 7) |
| 1224 |
bam_plp_set_maxcnt(pileup->iter,maxcnt); |
| 1225 |
else |
| 1226 |
bam_plp_set_maxcnt(pileup->iter,MaxPileupCnt); |
| 1227 |
|
| 1228 |
iter = sam_itr_queryi(bai, ref, start, end); |
| 1229 |
|
| 1230 |
if (items >= 8 && SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVCV) |
| 1231 |
{ |
| 1232 |
bam1_t *b = bam_init1(); |
| 1233 |
SV *b_sv = sv_setref_pv(newSV(sizeof b), "Bio::DB::HTS::Alignment", b); |
| 1234 |
|
| 1235 |
while ((ret = sam_itr_next(hfp, iter, b)) >= 0) |
| 1236 |
if (invoke_sv_to_int_fun(filter, b_sv) != 0) |
| 1237 |
hts_plbuf_push(b, pileup); |
| 1238 |
|
| 1239 |
SvREFCNT_dec(b_sv); /* b_sv's destructor will call bam_destroy1(b) */ |
| 1240 |
} |
| 1241 |
else |
| 1242 |
{ |
| 1243 |
bam1_t *b = bam_init1(); |
| 1244 |
while ((ret = sam_itr_next(hfp, iter, b)) >= 0) |
| 1245 |
hts_plbuf_push(b, pileup); |
| 1246 |
bam_destroy1(b); |
| 1247 |
} |
| 1248 |
|
| 1249 |
hts_itr_destroy(iter); |
| 1250 |
|
| 1251 |
hts_plbuf_push(NULL,pileup); |
| 1252 |
hts_plbuf_destroy(pileup); |
| 1253 |
|
| 1254 |
/* now normalize to coverage/bp and convert into an array */ |
| 1255 |
array = newAV(); |
| 1256 |
av_extend(array,bins); |
| 1257 |
for (i=0;i<bins;i++) |
| 1258 |
av_store(array,i,newSVnv(((float)cg.bin[i])/cg.width)); |
| 1259 |
Safefree(cg.bin); |
| 1260 |
RETVAL = array; |
| 1261 |
sv_2mortal((SV*)RETVAL); /* this fixes a documented bug in perl typemap */ |
| 1262 |
} |
| 1263 |
OUTPUT: |
| 1264 |
RETVAL |
| 1265 |
|
| 1266 |
|
| 1267 |
void |
| 1268 |
bami_DESTROY(hts_idx) |
| 1269 |
Bio::DB::HTS::Index hts_idx |
| 1270 |
PREINIT: |
| 1271 |
HV *assocfile = get_hv("Bio::DB::HTS::_associated_file", GV_ADD); |
| 1272 |
CODE: |
| 1273 |
hts_idx_destroy(hts_idx) ; |
| 1274 |
// Now release our reference to the associated Bio::DB::HTSfile |
| 1275 |
hv_delete(assocfile, (char *) &hts_idx, sizeof hts_idx, 0); |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::Pileup PREFIX=pl_ |
| 1280 |
|
| 1281 |
int |
| 1282 |
pl_qpos(pl) |
| 1283 |
Bio::DB::HTS::Pileup pl |
| 1284 |
CODE: |
| 1285 |
RETVAL = pl->qpos; |
| 1286 |
OUTPUT: |
| 1287 |
RETVAL |
| 1288 |
|
| 1289 |
int |
| 1290 |
pl_pos(pl) |
| 1291 |
Bio::DB::HTS::Pileup pl |
| 1292 |
CODE: |
| 1293 |
RETVAL = pl->qpos+1; |
| 1294 |
OUTPUT: |
| 1295 |
RETVAL |
| 1296 |
|
| 1297 |
int |
| 1298 |
pl_indel(pl) |
| 1299 |
Bio::DB::HTS::Pileup pl |
| 1300 |
CODE: |
| 1301 |
RETVAL = pl->indel; |
| 1302 |
OUTPUT: |
| 1303 |
RETVAL |
| 1304 |
|
| 1305 |
int |
| 1306 |
pl_level(pl) |
| 1307 |
Bio::DB::HTS::Pileup pl |
| 1308 |
CODE: |
| 1309 |
RETVAL = pl->level; |
| 1310 |
OUTPUT: |
| 1311 |
RETVAL |
| 1312 |
|
| 1313 |
int |
| 1314 |
pl_is_del(pl) |
| 1315 |
Bio::DB::HTS::Pileup pl |
| 1316 |
CODE: |
| 1317 |
RETVAL = pl->is_del; |
| 1318 |
OUTPUT: |
| 1319 |
RETVAL |
| 1320 |
|
| 1321 |
int |
| 1322 |
pl_is_refskip(pl) |
| 1323 |
Bio::DB::HTS::Pileup pl |
| 1324 |
CODE: |
| 1325 |
RETVAL = pl->is_refskip; |
| 1326 |
OUTPUT: |
| 1327 |
RETVAL |
| 1328 |
|
| 1329 |
int |
| 1330 |
pl_is_head(pl) |
| 1331 |
Bio::DB::HTS::Pileup pl |
| 1332 |
CODE: |
| 1333 |
RETVAL = pl->is_head; |
| 1334 |
OUTPUT: |
| 1335 |
RETVAL |
| 1336 |
|
| 1337 |
int |
| 1338 |
pl_is_tail(pl) |
| 1339 |
Bio::DB::HTS::Pileup pl |
| 1340 |
CODE: |
| 1341 |
RETVAL = pl->is_tail; |
| 1342 |
OUTPUT: |
| 1343 |
RETVAL |
| 1344 |
|
| 1345 |
Bio::DB::HTS::Alignment |
| 1346 |
pl_b(pl) |
| 1347 |
Bio::DB::HTS::Pileup pl |
| 1348 |
CODE: |
| 1349 |
RETVAL = bam_dup1(pl->b); |
| 1350 |
OUTPUT: |
| 1351 |
RETVAL |
| 1352 |
|
| 1353 |
Bio::DB::HTS::Alignment |
| 1354 |
pl_alignment(pl) |
| 1355 |
Bio::DB::HTS::Pileup pl |
| 1356 |
CODE: |
| 1357 |
RETVAL = bam_dup1(pl->b); |
| 1358 |
OUTPUT: |
| 1359 |
RETVAL |
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::Tabix PREFIX = tabix_ |
| 1364 |
|
| 1365 |
Bio::DB::HTS::Tabix |
| 1366 |
tabix_tbx_open(fname) |
| 1367 |
char *fname |
| 1368 |
CODE: |
| 1369 |
RETVAL = tbx_index_load(fname); |
| 1370 |
OUTPUT: |
| 1371 |
RETVAL |
| 1372 |
|
| 1373 |
|
| 1374 |
void |
| 1375 |
tabix_tbx_close(t) |
| 1376 |
Bio::DB::HTS::Tabix t |
| 1377 |
CODE: |
| 1378 |
tbx_destroy(t); |
| 1379 |
|
| 1380 |
Bio::DB::HTS::Tabix::Iterator |
| 1381 |
tabix_tbx_query(t, region) |
| 1382 |
Bio::DB::HTS::Tabix t |
| 1383 |
char *region |
| 1384 |
CODE: |
| 1385 |
RETVAL = tbx_itr_querys(t, region); |
| 1386 |
OUTPUT: |
| 1387 |
RETVAL |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
SV* |
| 1392 |
tabix_tbx_header(fp, tabix) |
| 1393 |
Bio::DB::HTSfile fp |
| 1394 |
Bio::DB::HTS::Tabix tabix |
| 1395 |
PREINIT: |
| 1396 |
int num_header_lines = 0; |
| 1397 |
AV *av_ref; |
| 1398 |
kstring_t str = {0,0,0}; |
| 1399 |
CODE: |
| 1400 |
av_ref = newAV(); |
| 1401 |
while ( hts_getline(fp, KS_SEP_LINE, &str) >= 0 ) { |
| 1402 |
if ( ! str.l ) break; //no lines left so we are done |
| 1403 |
if ( str.s[0] != tabix->conf.meta_char ) break; |
| 1404 |
|
| 1405 |
//the line begins with a # so add it to the array |
| 1406 |
++num_header_lines; |
| 1407 |
av_push(av_ref, newSVpv(str.s, str.l)); |
| 1408 |
} |
| 1409 |
|
| 1410 |
if ( ! num_header_lines ) |
| 1411 |
XSRETURN_EMPTY; |
| 1412 |
|
| 1413 |
RETVAL = newRV_noinc((SV*) av_ref); |
| 1414 |
free(str.s); |
| 1415 |
OUTPUT: |
| 1416 |
RETVAL |
| 1417 |
|
| 1418 |
SV* |
| 1419 |
tabix_tbx_seqnames(t) |
| 1420 |
Bio::DB::HTS::Tabix t |
| 1421 |
PREINIT: |
| 1422 |
const char **names; |
| 1423 |
int i, num_seqs; |
| 1424 |
AV *av_ref; |
| 1425 |
CODE: |
| 1426 |
names = tbx_seqnames(t, &num_seqs); //call actual tabix method |
| 1427 |
|
| 1428 |
//blast all the values onto a perl array |
| 1429 |
av_ref = newAV(); |
| 1430 |
for (i = 0; i < num_seqs; ++i) { |
| 1431 |
SV *sv_ref = newSVpv(names[i], 0); |
| 1432 |
av_push(av_ref, sv_ref); |
| 1433 |
} |
| 1434 |
|
| 1435 |
free(names); |
| 1436 |
|
| 1437 |
//return a reference to our array |
| 1438 |
RETVAL = newRV_noinc((SV*)av_ref); |
| 1439 |
OUTPUT: |
| 1440 |
RETVAL |
| 1441 |
|
| 1442 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::Tabix::Iterator PREFIX = tabix_ |
| 1443 |
|
| 1444 |
SV* |
| 1445 |
tabix_tbx_iter_next(iter, fp, t) |
| 1446 |
Bio::DB::HTS::Tabix::Iterator iter |
| 1447 |
Bio::DB::HTSfile fp |
| 1448 |
Bio::DB::HTS::Tabix t |
| 1449 |
PREINIT: |
| 1450 |
kstring_t str = {0,0,0}; |
| 1451 |
CODE: |
| 1452 |
if (tbx_itr_next(fp, t, iter, &str) < 0) { |
| 1453 |
free(str.s); |
| 1454 |
XSRETURN_EMPTY; |
| 1455 |
} |
| 1456 |
|
| 1457 |
RETVAL = newSVpv(str.s, str.l); |
| 1458 |
free(str.s); |
| 1459 |
|
| 1460 |
OUTPUT: |
| 1461 |
RETVAL |
| 1462 |
|
| 1463 |
void |
| 1464 |
tabix_tbx_iter_free(iter) |
| 1465 |
Bio::DB::HTS::Tabix::Iterator iter |
| 1466 |
CODE: |
| 1467 |
tbx_itr_destroy(iter); |
| 1468 |
|
| 1469 |
|
| 1470 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::VCFfile PREFIX = vcf_file_ |
| 1471 |
|
| 1472 |
Bio::DB::HTS::VCFfile |
| 1473 |
vcf_file_open(packname, filename, mode="r") |
| 1474 |
char* packname |
| 1475 |
char* filename |
| 1476 |
char* mode |
| 1477 |
PROTOTYPE: $$$ |
| 1478 |
CODE: |
| 1479 |
RETVAL = bcf_open(filename, mode); |
| 1480 |
OUTPUT: |
| 1481 |
RETVAL |
| 1482 |
|
| 1483 |
Bio::DB::HTS::Tabix |
| 1484 |
vcf_file_tbx_index_load(packname, fname) |
| 1485 |
char* packname |
| 1486 |
char *fname |
| 1487 |
PROTOTYPE: $$ |
| 1488 |
CODE: |
| 1489 |
htsFile *fp = hts_open(fname,"r"); |
| 1490 |
if ( !fp ) croak("Could not read %s\n", fname); |
| 1491 |
enum htsExactFormat format = hts_get_format(fp)->format; |
| 1492 |
if ( hts_close(fp) ) croak("hts_close returned non-zero status: %s\n", fname); |
| 1493 |
|
| 1494 |
if ( format != vcf ) XSRETURN_UNDEF; |
| 1495 |
|
| 1496 |
RETVAL = tbx_index_load(fname); |
| 1497 |
OUTPUT: |
| 1498 |
RETVAL |
| 1499 |
|
| 1500 |
Bio::DB::HTS::Index |
| 1501 |
vcf_file_bcf_index_load(packname, filename) |
| 1502 |
char* packname |
| 1503 |
char* filename |
| 1504 |
PROTOTYPE: $$ |
| 1505 |
CODE: |
| 1506 |
htsFile *fp = hts_open(filename,"r"); |
| 1507 |
if ( !fp ) croak("Could not read %s\n", filename); |
| 1508 |
enum htsExactFormat format = hts_get_format(fp)->format; |
| 1509 |
if ( hts_close(fp) ) croak("hts_close returned non-zero status: %s\n", filename); |
| 1510 |
|
| 1511 |
if ( format != bcf ) XSRETURN_UNDEF; |
| 1512 |
|
| 1513 |
RETVAL = bcf_index_load(filename); |
| 1514 |
|
| 1515 |
OUTPUT: |
| 1516 |
RETVAL |
| 1517 |
|
| 1518 |
void |
| 1519 |
vcf_file_bcf_index_close(packname, bcf_idx) |
| 1520 |
char* packname |
| 1521 |
Bio::DB::HTS::Index bcf_idx |
| 1522 |
PROTOTYPE: $$ |
| 1523 |
CODE: |
| 1524 |
hts_idx_destroy(bcf_idx); |
| 1525 |
|
| 1526 |
|
| 1527 |
Bio::DB::HTS::VCF::Header |
| 1528 |
vcf_file_header_read(vfile) |
| 1529 |
Bio::DB::HTS::VCFfile vfile |
| 1530 |
PREINIT: |
| 1531 |
bcf_hdr_t* h; |
| 1532 |
CODE: |
| 1533 |
h = bcf_hdr_read(vfile); |
| 1534 |
RETVAL = h; |
| 1535 |
OUTPUT: |
| 1536 |
RETVAL |
| 1537 |
|
| 1538 |
|
| 1539 |
Bio::DB::HTS::VCF::Row |
| 1540 |
vcf_file_read1(vfile,header) |
| 1541 |
Bio::DB::HTS::VCFfile vfile |
| 1542 |
Bio::DB::HTS::VCF::Header header |
| 1543 |
PREINIT: |
| 1544 |
bcf1_t *rec; |
| 1545 |
CODE: |
| 1546 |
rec = bcf_init(); |
| 1547 |
if ( bcf_read(vfile, header, rec) == 0 ) |
| 1548 |
{ |
| 1549 |
bcf_unpack(rec, BCF_UN_ALL) ; |
| 1550 |
RETVAL = rec ; |
| 1551 |
} |
| 1552 |
else |
| 1553 |
{ |
| 1554 |
XSRETURN_EMPTY; |
| 1555 |
} |
| 1556 |
OUTPUT: |
| 1557 |
RETVAL |
| 1558 |
|
| 1559 |
|
| 1560 |
SV* |
| 1561 |
vcf_file_num_variants(packname,filename) |
| 1562 |
char* packname |
| 1563 |
char* filename |
| 1564 |
PROTOTYPE: $$$ |
| 1565 |
PREINIT: |
| 1566 |
int n_records = 0; |
| 1567 |
vcfFile* vfile; |
| 1568 |
bcf_hdr_t* h; |
| 1569 |
bcf1_t *rec; |
| 1570 |
CODE: |
| 1571 |
vfile = bcf_open(filename, "r"); |
| 1572 |
h = bcf_hdr_read(vfile); |
| 1573 |
rec = bcf_init(); |
| 1574 |
|
| 1575 |
//loop through all the lines but don't do anything with them |
| 1576 |
while(bcf_read(vfile, h, rec) == 0) |
| 1577 |
{ |
| 1578 |
++n_records; |
| 1579 |
} |
| 1580 |
bcf_destroy(rec); |
| 1581 |
bcf_hdr_destroy(h); |
| 1582 |
bcf_close(vfile) ; |
| 1583 |
RETVAL = newSViv(n_records); |
| 1584 |
OUTPUT: |
| 1585 |
RETVAL |
| 1586 |
|
| 1587 |
Bio::DB::HTS::VCF::Iterator |
| 1588 |
vcf_file_query(packname, region, ...) |
| 1589 |
char* packname |
| 1590 |
char* region |
| 1591 |
INIT: |
| 1592 |
if( items < 4 ) |
| 1593 |
croak("Missing arguments"); |
| 1594 |
|
| 1595 |
if( !(SvOK(ST(2)) && sv_isobject(ST(2)))) |
| 1596 |
croak("Invalid index argument"); |
| 1597 |
|
| 1598 |
if( !(SvOK(ST(3)) && sv_isobject(ST(3))) ) |
| 1599 |
croak("Invalid header argument"); |
| 1600 |
|
| 1601 |
CODE: |
| 1602 |
if ( sv_isa( ST(2), "Bio::DB::HTS::Tabix" ) ) { |
| 1603 |
RETVAL = tbx_itr_querys ( INT2PTR(tbx_t*, SvIV((SV *)SvRV(ST(2)))), region ); |
| 1604 |
} else if ( sv_isa( ST(2), "Bio::DB::HTS::Index" ) ) { |
| 1605 |
assert( sv_isa( ST(3), "Bio::DB::HTS::VCF::Header") ); |
| 1606 |
RETVAL = bcf_itr_querys ( INT2PTR(hts_idx_t*, SvIV((SV *)SvRV(ST(2)))), INT2PTR(bcf_hdr_t*, SvIV((SV *)SvRV(ST(3)))), region ); |
| 1607 |
} else |
| 1608 |
croak ( "Argument is not a valid index" ); |
| 1609 |
|
| 1610 |
if ( RETVAL == NULL ) XSRETURN_UNDEF; |
| 1611 |
|
| 1612 |
OUTPUT: |
| 1613 |
RETVAL |
| 1614 |
|
| 1615 |
|
| 1616 |
void |
| 1617 |
vcf_file_vcf_close(vfile) |
| 1618 |
Bio::DB::HTS::VCFfile vfile |
| 1619 |
CODE: |
| 1620 |
bcf_close(vfile); |
| 1621 |
|
| 1622 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::VCF::Iterator PREFIX = vcf_ |
| 1623 |
|
| 1624 |
Bio::DB::HTS::VCF::Row |
| 1625 |
vcf_iter_next(iter, fp, hdr, ...) |
| 1626 |
Bio::DB::HTS::VCF::Iterator iter |
| 1627 |
Bio::DB::HTS::VCFfile fp |
| 1628 |
Bio::DB::HTS::VCF::Header hdr |
| 1629 |
PREINIT: |
| 1630 |
kstring_t str = { 0, 0, 0 }; |
| 1631 |
bcf1_t *rec = bcf_init(); |
| 1632 |
int ret; |
| 1633 |
|
| 1634 |
INIT: |
| 1635 |
if ( items < 4 ) |
| 1636 |
croak("Missing arguments"); |
| 1637 |
|
| 1638 |
if( !(SvOK(ST(3)) && sv_isobject(ST(3))) ) |
| 1639 |
croak("Invalid index argument"); |
| 1640 |
|
| 1641 |
CODE: |
| 1642 |
if ( sv_isa( ST(3), "Bio::DB::HTS::Tabix" ) ) { |
| 1643 |
if (tbx_itr_next(fp, INT2PTR(tbx_t*, SvIV((SV *)SvRV(ST(3)))), iter, &str) < 0 || vcf_parse1(&str, hdr, rec) < 0) { |
| 1644 |
free(str.s); |
| 1645 |
bcf_destroy(rec); |
| 1646 |
XSRETURN_EMPTY; |
| 1647 |
} |
| 1648 |
|
| 1649 |
free(str.s); |
| 1650 |
|
| 1651 |
} else if ( sv_isa( ST(3), "Bio::DB::HTS::Index" ) ) { |
| 1652 |
if (bcf_itr_next(fp, iter, rec) < 0) { |
| 1653 |
bcf_destroy(rec); |
| 1654 |
XSRETURN_EMPTY; |
| 1655 |
} |
| 1656 |
} else |
| 1657 |
croak ( "VCF/BCF file does not have a valid index" ); |
| 1658 |
|
| 1659 |
bcf_unpack(rec, BCF_UN_ALL) ; |
| 1660 |
RETVAL = rec; |
| 1661 |
|
| 1662 |
OUTPUT: |
| 1663 |
RETVAL |
| 1664 |
|
| 1665 |
void |
| 1666 |
vcf_iter_free(iter) |
| 1667 |
Bio::DB::HTS::VCF::Iterator iter |
| 1668 |
CODE: |
| 1669 |
/* can call it also on a non-tabix index, |
| 1670 |
see HTSlib synced_bcf_reader.c: bcf_sr_destroy1, _reader_fill_buffer */ |
| 1671 |
tbx_itr_destroy(iter); |
| 1672 |
|
| 1673 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::VCF::Header PREFIX = vcfh_ |
| 1674 |
|
| 1675 |
void |
| 1676 |
vcfh_DESTROY(h) |
| 1677 |
Bio::DB::HTS::VCF::Header h |
| 1678 |
CODE: |
| 1679 |
bcf_hdr_destroy(h); |
| 1680 |
|
| 1681 |
SV* |
| 1682 |
vcfh_version(header) |
| 1683 |
Bio::DB::HTS::VCF::Header header |
| 1684 |
PREINIT: |
| 1685 |
CODE: |
| 1686 |
RETVAL = newSVpv(bcf_hdr_get_version(header),0) ; |
| 1687 |
OUTPUT: |
| 1688 |
RETVAL |
| 1689 |
|
| 1690 |
|
| 1691 |
int |
| 1692 |
vcfh_num_samples(header) |
| 1693 |
Bio::DB::HTS::VCF::Header header |
| 1694 |
PREINIT: |
| 1695 |
CODE: |
| 1696 |
RETVAL = bcf_hdr_nsamples(header) ; |
| 1697 |
OUTPUT: |
| 1698 |
RETVAL |
| 1699 |
|
| 1700 |
|
| 1701 |
SV* |
| 1702 |
vcfh_get_sample_names(header) |
| 1703 |
Bio::DB::HTS::VCF::Header header |
| 1704 |
PREINIT: |
| 1705 |
int nsamples = 0 ; |
| 1706 |
int i ; |
| 1707 |
AV *av_ref; |
| 1708 |
CODE: |
| 1709 |
av_ref = newAV(); |
| 1710 |
nsamples = bcf_hdr_nsamples(header) ; |
| 1711 |
for (i=0 ; i<nsamples ; i++) |
| 1712 |
{ |
| 1713 |
SV *sv_ref = newSVpv(header->samples[i], 0); |
| 1714 |
av_push(av_ref, sv_ref); |
| 1715 |
} |
| 1716 |
RETVAL = newRV_noinc((SV*)av_ref); |
| 1717 |
OUTPUT: |
| 1718 |
RETVAL |
| 1719 |
|
| 1720 |
int |
| 1721 |
vcfh_num_seqnames(header) |
| 1722 |
Bio::DB::HTS::VCF::Header header |
| 1723 |
PREINIT: |
| 1724 |
int nseq = 0 ; |
| 1725 |
CODE: |
| 1726 |
bcf_hdr_seqnames(header, &nseq); |
| 1727 |
RETVAL = nseq; |
| 1728 |
OUTPUT: |
| 1729 |
RETVAL |
| 1730 |
|
| 1731 |
|
| 1732 |
SV* |
| 1733 |
vcfh_get_seqnames(header) |
| 1734 |
Bio::DB::HTS::VCF::Header header |
| 1735 |
PREINIT: |
| 1736 |
int nseq = 0 ; |
| 1737 |
const char **seqnames ; |
| 1738 |
int i = 0 ; |
| 1739 |
AV *av_ref = newAV() ; |
| 1740 |
CODE: |
| 1741 |
seqnames = bcf_hdr_seqnames(header, &nseq); |
| 1742 |
for (i = 0; i < nseq; i++) |
| 1743 |
{ |
| 1744 |
SV *sv_ref = newSVpv(seqnames[i], 0); |
| 1745 |
av_push(av_ref, sv_ref); |
| 1746 |
} |
| 1747 |
free(seqnames) ; |
| 1748 |
RETVAL = newRV_noinc((SV*)av_ref); |
| 1749 |
OUTPUT: |
| 1750 |
RETVAL |
| 1751 |
|
| 1752 |
|
| 1753 |
SV* |
| 1754 |
vcfh_fmt_text(header) |
| 1755 |
Bio::DB::HTS::VCF::Header header |
| 1756 |
PREINIT: |
| 1757 |
int len, is_bcf = 0; /* discard IDX fields */ |
| 1758 |
CODE: |
| 1759 |
/* |
| 1760 |
* get header formatted text |
| 1761 |
* use deprecated (since 1.4) bcf_hdr_fmt_text to support |
| 1762 |
* older htslib versions |
| 1763 |
* NOTE: |
| 1764 |
* using bcf_hdr_format is claimed to be better (optimised for huge headers), |
| 1765 |
* but for htslib >= 1.4 bcf_hdr_fmt_text calls bcf_hdr_format underneath |
| 1766 |
* and for htslib 1.3.1 bcf_hdr_fmt_text execute the same statements |
| 1767 |
* as bcf_hdr_format |
| 1768 |
*/ |
| 1769 |
RETVAL = newSVpv(bcf_hdr_fmt_text(header, is_bcf, &len), 0); |
| 1770 |
OUTPUT: |
| 1771 |
RETVAL |
| 1772 |
|
| 1773 |
|
| 1774 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::VCF::Row PREFIX = vcfrow_ |
| 1775 |
|
| 1776 |
void |
| 1777 |
vcfrow_print(row,header) |
| 1778 |
Bio::DB::HTS::VCF::Row row |
| 1779 |
Bio::DB::HTS::VCF::Header header |
| 1780 |
PREINIT: |
| 1781 |
int i ; |
| 1782 |
CODE: |
| 1783 |
printf("\nVCF data line:\n"); |
| 1784 |
printf("chromosome:%s\t", bcf_hdr_id2name(header,row->rid)); |
| 1785 |
printf("position:%d\t", (row->pos+1)); |
| 1786 |
printf("QUAL:%f\t", row->qual); |
| 1787 |
printf("ID:%s\t", row->d.id ); |
| 1788 |
printf("REF:%s\n", row->d.als); |
| 1789 |
printf("Num Alleles:%d\n", row->n_allele-1); |
| 1790 |
for( i=1 ; i<row->n_allele ; i++ ) |
| 1791 |
{ |
| 1792 |
printf("ALT[%d]=%s\t", i, row->d.allele[i]); |
| 1793 |
} |
| 1794 |
printf("\nNum Filters:%d\n", row->d.n_flt); |
| 1795 |
|
| 1796 |
# printf("\nfilter:%s\t", row->d.id ); |
| 1797 |
# printf("\info:%s\n", row->d.als); |
| 1798 |
OUTPUT: |
| 1799 |
|
| 1800 |
|
| 1801 |
SV* |
| 1802 |
vcfrow_chromosome(row,header) |
| 1803 |
Bio::DB::HTS::VCF::Row row |
| 1804 |
Bio::DB::HTS::VCF::Header header |
| 1805 |
PREINIT: |
| 1806 |
CODE: |
| 1807 |
RETVAL = newSVpv(bcf_hdr_id2name(header,row->rid),0) ; |
| 1808 |
OUTPUT: |
| 1809 |
RETVAL |
| 1810 |
|
| 1811 |
|
| 1812 |
int |
| 1813 |
vcfrow_position(row) |
| 1814 |
Bio::DB::HTS::VCF::Row row |
| 1815 |
PREINIT: |
| 1816 |
CODE: |
| 1817 |
RETVAL = row->pos+1; |
| 1818 |
OUTPUT: |
| 1819 |
RETVAL |
| 1820 |
|
| 1821 |
float |
| 1822 |
vcfrow_quality(row) |
| 1823 |
Bio::DB::HTS::VCF::Row row |
| 1824 |
PREINIT: |
| 1825 |
CODE: |
| 1826 |
RETVAL = row->qual; |
| 1827 |
OUTPUT: |
| 1828 |
RETVAL |
| 1829 |
|
| 1830 |
|
| 1831 |
SV* |
| 1832 |
vcfrow_id(row) |
| 1833 |
Bio::DB::HTS::VCF::Row row |
| 1834 |
PREINIT: |
| 1835 |
CODE: |
| 1836 |
RETVAL = newSVpv(row->d.id, 0) ; |
| 1837 |
OUTPUT: |
| 1838 |
RETVAL |
| 1839 |
|
| 1840 |
SV* |
| 1841 |
vcfrow_reference(row) |
| 1842 |
Bio::DB::HTS::VCF::Row row |
| 1843 |
PREINIT: |
| 1844 |
CODE: |
| 1845 |
RETVAL = newSVpv(row->d.als, 0) ; |
| 1846 |
OUTPUT: |
| 1847 |
RETVAL |
| 1848 |
|
| 1849 |
|
| 1850 |
int |
| 1851 |
vcfrow_num_alleles(row) |
| 1852 |
Bio::DB::HTS::VCF::Row row |
| 1853 |
PREINIT: |
| 1854 |
CODE: |
| 1855 |
RETVAL = row->n_allele-1 ; |
| 1856 |
OUTPUT: |
| 1857 |
RETVAL |
| 1858 |
|
| 1859 |
|
| 1860 |
SV* |
| 1861 |
vcfrow_get_alleles(row) |
| 1862 |
Bio::DB::HTS::VCF::Row row |
| 1863 |
PREINIT: |
| 1864 |
int i; |
| 1865 |
AV *av_ref; |
| 1866 |
CODE: |
| 1867 |
av_ref = newAV(); |
| 1868 |
for (i = 1; i < row->n_allele; ++i) { |
| 1869 |
SV *sv_ref = newSVpv(row->d.allele[i], 0); |
| 1870 |
av_push(av_ref, sv_ref); |
| 1871 |
} |
| 1872 |
RETVAL = newRV_noinc((SV*)av_ref); |
| 1873 |
OUTPUT: |
| 1874 |
RETVAL |
| 1875 |
|
| 1876 |
int |
| 1877 |
vcfrow_num_filters(row) |
| 1878 |
Bio::DB::HTS::VCF::Row row |
| 1879 |
PREINIT: |
| 1880 |
CODE: |
| 1881 |
RETVAL = row->d.n_flt ; |
| 1882 |
OUTPUT: |
| 1883 |
RETVAL |
| 1884 |
|
| 1885 |
int |
| 1886 |
vcfrow_has_filter(row,header,filter) |
| 1887 |
Bio::DB::HTS::VCF::Row row |
| 1888 |
Bio::DB::HTS::VCF::Header header |
| 1889 |
char* filter |
| 1890 |
PREINIT: |
| 1891 |
CODE: |
| 1892 |
RETVAL = bcf_has_filter(header,row,filter) ; |
| 1893 |
OUTPUT: |
| 1894 |
RETVAL |
| 1895 |
|
| 1896 |
|
| 1897 |
int |
| 1898 |
vcfrow_is_snp(row) |
| 1899 |
Bio::DB::HTS::VCF::Row row |
| 1900 |
PREINIT: |
| 1901 |
CODE: |
| 1902 |
RETVAL = bcf_is_snp(row) ; |
| 1903 |
OUTPUT: |
| 1904 |
RETVAL |
| 1905 |
|
| 1906 |
|
| 1907 |
int |
| 1908 |
vcfrow_get_variant_type(row, allele_index) |
| 1909 |
Bio::DB::HTS::VCF::Row row |
| 1910 |
int allele_index |
| 1911 |
CODE: |
| 1912 |
RETVAL = bcf_get_variant_type(row, allele_index); |
| 1913 |
OUTPUT: |
| 1914 |
RETVAL |
| 1915 |
|
| 1916 |
|
| 1917 |
SV* |
| 1918 |
vcfrow_get_info_type(row,header,id) |
| 1919 |
Bio::DB::HTS::VCF::Row row |
| 1920 |
Bio::DB::HTS::VCF::Header header |
| 1921 |
char* id |
| 1922 |
PREINIT: |
| 1923 |
bcf_info_t* info ; |
| 1924 |
CODE: |
| 1925 |
info = bcf_get_info(header, row, id); |
| 1926 |
if( info == NULL ) |
| 1927 |
{ |
| 1928 |
RETVAL = newSVpv("",0); |
| 1929 |
} |
| 1930 |
else |
| 1931 |
{ |
| 1932 |
switch( info->type ) |
| 1933 |
{ |
| 1934 |
case BCF_BT_FLOAT: |
| 1935 |
RETVAL = newSVpv("Float",0); |
| 1936 |
break ; |
| 1937 |
case BCF_BT_NULL: |
| 1938 |
RETVAL = newSVpv("Flag",0); |
| 1939 |
break ; |
| 1940 |
case BCF_BT_CHAR: |
| 1941 |
RETVAL = newSVpv("String",0); |
| 1942 |
break ; |
| 1943 |
default: |
| 1944 |
RETVAL = newSVpv("Integer",0); |
| 1945 |
} |
| 1946 |
} |
| 1947 |
OUTPUT: |
| 1948 |
RETVAL |
| 1949 |
|
| 1950 |
SV* |
| 1951 |
vcfrow_get_info(row, header, ...) |
| 1952 |
Bio::DB::HTS::VCF::Row row |
| 1953 |
Bio::DB::HTS::VCF::Header header |
| 1954 |
|
| 1955 |
PREINIT: |
| 1956 |
bcf_info_t* info ; |
| 1957 |
int i = 0, avi; |
| 1958 |
int* buf_i = NULL; |
| 1959 |
float* buf_f = NULL; |
| 1960 |
char* buf_c = NULL; |
| 1961 |
int mbuf_i = 0; |
| 1962 |
int mbuf_f = 0; |
| 1963 |
int mbuf_c = 0; |
| 1964 |
int result; |
| 1965 |
|
| 1966 |
vdict_t *d; |
| 1967 |
khint_t k; |
| 1968 |
AV* row_ids; |
| 1969 |
HV* info_data; |
| 1970 |
|
| 1971 |
INIT: |
| 1972 |
if ( items < 2 ) |
| 1973 |
croak ( "Missing arguments" ); |
| 1974 |
|
| 1975 |
row_ids = newAV(); |
| 1976 |
if ( items > 2 ) { |
| 1977 |
if ( SvOK(ST(2)) && SvTYPE(ST(2)) == SVt_PV ) { |
| 1978 |
av_push ( row_ids, newSVpv ( SvPVX(ST(2)) , 0) ); |
| 1979 |
} else |
| 1980 |
croak ( "ID argument must be a valid string" ); |
| 1981 |
} else { |
| 1982 |
d = (vdict_t*)header->dict[BCF_DT_ID]; |
| 1983 |
if ( d == 0 ) croak ( "Couldn't get ID dict" ); |
| 1984 |
|
| 1985 |
for ( k = kh_begin(d); k != kh_end(d); ++k ) |
| 1986 |
if ( kh_exist(d, k) && bcf_get_info(header, row, kh_key(d, k)) != NULL ) |
| 1987 |
av_push ( row_ids, newSVpv ( kh_key(d, k), 0) ); |
| 1988 |
} |
| 1989 |
|
| 1990 |
CODE: |
| 1991 |
|
| 1992 |
info_data = newHV(); |
| 1993 |
|
| 1994 |
for ( avi = 0; avi <= AvFILL(row_ids); ++avi ) { |
| 1995 |
char* id = savepv( SvPV_nolen( *av_fetch ( row_ids, avi, 0 ) ) ) ; |
| 1996 |
|
| 1997 |
info = bcf_get_info(header, row, id); |
| 1998 |
|
| 1999 |
if( info == NULL ) { /* info null, nothing to return */ |
| 2000 |
hv_store( info_data, id, strlen(id), newSVpv("ID_NOT_FOUND", 0), 0 ); |
| 2001 |
} else { |
| 2002 |
AV* av_ref = newAV(); |
| 2003 |
|
| 2004 |
if( info->type == BCF_BT_NULL ) { |
| 2005 |
result = bcf_get_info_flag(header, row, id, &buf_i, &mbuf_i); |
| 2006 |
|
| 2007 |
if( result == 1 ) |
| 2008 |
av_push(av_ref, newSViv(1)); |
| 2009 |
else |
| 2010 |
av_push(av_ref, newSViv(0)); |
| 2011 |
} else if( info->type == BCF_BT_FLOAT ) { |
| 2012 |
result = bcf_get_info_float(header, row, id, &buf_f, &mbuf_f) ; |
| 2013 |
|
| 2014 |
for( i=0 ; i<result ; i++ ) |
| 2015 |
av_push(av_ref, newSVnv(buf_f[i])) ; |
| 2016 |
} else if( info->type == BCF_BT_CHAR ) { |
| 2017 |
result = bcf_get_info_string(header, row, id, &buf_c, &mbuf_c) ; |
| 2018 |
|
| 2019 |
av_push(av_ref, newSVpvn(buf_c, result)); |
| 2020 |
} else if( info->type == BCF_BT_INT8 || info->type == BCF_BT_INT16 || info->type == BCF_BT_INT32 ) { |
| 2021 |
result = bcf_get_info_int32(header, row, id, &buf_i, &mbuf_i) ; |
| 2022 |
|
| 2023 |
for( i=0 ; i<result ; i++ ) |
| 2024 |
av_push(av_ref, newSViv(buf_i[i])) ; |
| 2025 |
} |
| 2026 |
|
| 2027 |
hv_store ( info_data, id, strlen(id), newRV_noinc((SV*)av_ref), 0 ); |
| 2028 |
} |
| 2029 |
} |
| 2030 |
|
| 2031 |
if ( AvFILL(row_ids) == 0 ) { |
| 2032 |
STRLEN len; |
| 2033 |
char* key = SvPV(*av_fetch(row_ids, 0, 0), len); |
| 2034 |
|
| 2035 |
SV** svp = hv_fetch( info_data, key, len, 0); |
| 2036 |
if (svp != NULL) { |
| 2037 |
RETVAL = newSVsv(*svp); |
| 2038 |
|
| 2039 |
SvREFCNT_dec((SV*)info_data); |
| 2040 |
} else |
| 2041 |
croak ("Couldn't find key"); |
| 2042 |
|
| 2043 |
} else |
| 2044 |
RETVAL = newRV_noinc((SV*)info_data); |
| 2045 |
|
| 2046 |
SvREFCNT_dec((SV*)row_ids); |
| 2047 |
|
| 2048 |
free(buf_i); |
| 2049 |
free(buf_f); |
| 2050 |
free(buf_c); |
| 2051 |
|
| 2052 |
OUTPUT: |
| 2053 |
RETVAL |
| 2054 |
|
| 2055 |
|
| 2056 |
SV* |
| 2057 |
vcfrow_get_format_type(row,header,id) |
| 2058 |
Bio::DB::HTS::VCF::Row row |
| 2059 |
Bio::DB::HTS::VCF::Header header |
| 2060 |
char* id |
| 2061 |
PREINIT: |
| 2062 |
bcf_fmt_t* fmt ; |
| 2063 |
CODE: |
| 2064 |
fmt = bcf_get_fmt(header, row, id); |
| 2065 |
if( fmt == NULL ) |
| 2066 |
{ |
| 2067 |
RETVAL = newSVpv("",0); |
| 2068 |
} |
| 2069 |
else |
| 2070 |
{ |
| 2071 |
switch( fmt->type ) |
| 2072 |
{ |
| 2073 |
case BCF_BT_FLOAT: |
| 2074 |
RETVAL = newSVpv("Float",0); |
| 2075 |
break ; |
| 2076 |
case BCF_BT_CHAR: |
| 2077 |
RETVAL = newSVpv("String",0); |
| 2078 |
break ; |
| 2079 |
default: |
| 2080 |
RETVAL = newSVpv("Integer",0); |
| 2081 |
} |
| 2082 |
} |
| 2083 |
OUTPUT: |
| 2084 |
RETVAL |
| 2085 |
|
| 2086 |
SV* |
| 2087 |
vcfrow_get_format(row, header, ...) |
| 2088 |
Bio::DB::HTS::VCF::Row row |
| 2089 |
Bio::DB::HTS::VCF::Header header |
| 2090 |
|
| 2091 |
PREINIT: |
| 2092 |
bcf_fmt_t* fmt; |
| 2093 |
int i, avi; |
| 2094 |
int* buf_i = NULL; |
| 2095 |
float* buf_f = NULL; |
| 2096 |
char* buf_c = NULL; |
| 2097 |
int mbuf_i = 0; |
| 2098 |
int mbuf_f = 0; |
| 2099 |
int mbuf_c = 0; |
| 2100 |
|
| 2101 |
vdict_t *d; |
| 2102 |
khint_t k; |
| 2103 |
AV* row_ids; |
| 2104 |
HV* fmt_data; |
| 2105 |
|
| 2106 |
INIT: |
| 2107 |
if ( items < 2 ) |
| 2108 |
croak ( "Missing arguments" ); |
| 2109 |
|
| 2110 |
CODE: |
| 2111 |
|
| 2112 |
if ( items > 2 ) { |
| 2113 |
if ( SvOK(ST(2)) && SvTYPE(ST(2)) == SVt_PV ) { |
| 2114 |
// av_push ( row_ids, newSVpv ( SvPVX(ST(2)) , 0) ); |
| 2115 |
|
| 2116 |
fmt = bcf_get_fmt(header, row, SvPVX(ST(2))); |
| 2117 |
|
| 2118 |
if( fmt == NULL ) { /* fmt null, nothing to return */ |
| 2119 |
RETVAL = newSVpv("ID_NOT_FOUND", 0); |
| 2120 |
} else { |
| 2121 |
AV* av_ref = (AV*) sv_2mortal((SV*) newAV() ); |
| 2122 |
|
| 2123 |
if( fmt->type == BCF_BT_FLOAT ) { |
| 2124 |
int ndst = bcf_get_format_float(header, row, SvPVX(ST(2)), &buf_f, &mbuf_f) ; |
| 2125 |
if ( ndst < 0 ) |
| 2126 |
croak ("Couldn't read float format"); |
| 2127 |
|
| 2128 |
for( i=0 ; i<ndst ; i++ ) |
| 2129 |
av_push(av_ref, newSVnv(buf_f[i])); |
| 2130 |
|
| 2131 |
} else if( fmt->type == BCF_BT_CHAR ) { |
| 2132 |
int ndst = bcf_get_format_char(header, row, SvPVX(ST(2)), &buf_c, &mbuf_c) ; |
| 2133 |
if ( ndst < 0 ) |
| 2134 |
croak ("Couldn't read string format"); |
| 2135 |
|
| 2136 |
av_push(av_ref, newSVpv(buf_c, ndst+1)); |
| 2137 |
|
| 2138 |
} else if( fmt->type == BCF_BT_INT8 || fmt->type == BCF_BT_INT16 || fmt->type == BCF_BT_INT32 ) { |
| 2139 |
int ndst = bcf_get_format_int32(header, row, SvPVX(ST(2)), &buf_i, &mbuf_i) ; |
| 2140 |
if ( ndst < 0 ) |
| 2141 |
croak ("Couldn't read int format"); |
| 2142 |
|
| 2143 |
for( i=0 ; i<ndst ; i++ ) |
| 2144 |
av_push(av_ref, newSViv(buf_i[i])); |
| 2145 |
} |
| 2146 |
|
| 2147 |
RETVAL = newRV((SV*)av_ref); |
| 2148 |
} |
| 2149 |
} else |
| 2150 |
croak ( "ID argument must be a valid string" ); |
| 2151 |
|
| 2152 |
} else { |
| 2153 |
fmt_data = (HV*) sv_2mortal( (SV*) newHV() ); |
| 2154 |
|
| 2155 |
d = (vdict_t*)header->dict[BCF_DT_ID]; |
| 2156 |
if ( d == 0 ) croak ( "Couldn't get ID dict" ); |
| 2157 |
|
| 2158 |
for ( k = kh_begin(d); k != kh_end(d); ++k ) |
| 2159 |
if ( kh_exist(d, k) && (fmt = bcf_get_fmt(header, row, kh_key(d, k) ) ) != NULL ) { |
| 2160 |
|
| 2161 |
AV* av_ref = (AV*) sv_2mortal((SV*) newAV() ); |
| 2162 |
|
| 2163 |
if( fmt->type == BCF_BT_FLOAT ) { |
| 2164 |
int ndst = bcf_get_format_float(header, row, kh_key(d, k), &buf_f, &mbuf_f) ; |
| 2165 |
if ( ndst < 0 ) |
| 2166 |
croak ("Couldn't read float format"); |
| 2167 |
|
| 2168 |
for( i=0 ; i<ndst ; i++ ) |
| 2169 |
av_push(av_ref, newSVnv(buf_f[i])); |
| 2170 |
|
| 2171 |
} else if( fmt->type == BCF_BT_CHAR ) { |
| 2172 |
int ndst = bcf_get_format_char(header, row, kh_key(d, k), &buf_c, &mbuf_c) ; |
| 2173 |
if ( ndst < 0 ) |
| 2174 |
croak ("Couldn't read string format"); |
| 2175 |
|
| 2176 |
av_push(av_ref, newSVpv(buf_c, ndst+1)); |
| 2177 |
|
| 2178 |
} else if( fmt->type == BCF_BT_INT8 || fmt->type == BCF_BT_INT16 || fmt->type == BCF_BT_INT32 ) { |
| 2179 |
int ndst = bcf_get_format_int32(header, row, kh_key(d, k), &buf_i, &mbuf_i) ; |
| 2180 |
if ( ndst < 0 ) |
| 2181 |
croak ("Couldn't read int format"); |
| 2182 |
|
| 2183 |
for( i=0 ; i<ndst ; i++ ) |
| 2184 |
av_push(av_ref, newSViv(buf_i[i])); |
| 2185 |
} |
| 2186 |
|
| 2187 |
char* key = savepv((const char*) kh_key(d, k)); |
| 2188 |
hv_store ( fmt_data, key, (I32)strlen(key), newRV((SV*) av_ref), 0 ); |
| 2189 |
} |
| 2190 |
|
| 2191 |
RETVAL = newRV((SV*) fmt_data); |
| 2192 |
} |
| 2193 |
|
| 2194 |
free(buf_i); |
| 2195 |
free(buf_f); |
| 2196 |
free(buf_c); |
| 2197 |
|
| 2198 |
OUTPUT: |
| 2199 |
RETVAL |
| 2200 |
|
| 2201 |
|
| 2202 |
SV* |
| 2203 |
vcfrow_get_genotypes(row,header) |
| 2204 |
Bio::DB::HTS::VCF::Row row |
| 2205 |
Bio::DB::HTS::VCF::Header header |
| 2206 |
PREINIT: |
| 2207 |
int ngt ; |
| 2208 |
int* gt_arr = NULL ; |
| 2209 |
int ngt_arr = 0; |
| 2210 |
AV* av_ref; |
| 2211 |
int i=0 ; |
| 2212 |
CODE: |
| 2213 |
av_ref = newAV(); |
| 2214 |
/* Note the VCF header type treats this as a String but BCF treats as an int */ |
| 2215 |
ngt = bcf_get_genotypes(header, row, >_arr, &ngt_arr); |
| 2216 |
for( i=0 ; i<ngt_arr ; i++ ) |
| 2217 |
{ |
| 2218 |
av_push(av_ref, newSViv(gt_arr[i])) ; |
| 2219 |
} |
| 2220 |
free(gt_arr); |
| 2221 |
RETVAL = newRV_noinc((SV*)av_ref); |
| 2222 |
OUTPUT: |
| 2223 |
RETVAL |
| 2224 |
|
| 2225 |
void |
| 2226 |
vcfrow_DESTROY(row) |
| 2227 |
Bio::DB::HTS::VCF::Row row |
| 2228 |
CODE: |
| 2229 |
bcf_destroy(row); |
| 2230 |
|
| 2231 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::VCF::Sweep PREFIX = vcfs_ |
| 2232 |
|
| 2233 |
Bio::DB::HTS::VCF::Sweep |
| 2234 |
vcfs_sweep_open(filename) |
| 2235 |
char* filename |
| 2236 |
PREINIT: |
| 2237 |
bcf_sweep_t* sweep; |
| 2238 |
CODE: |
| 2239 |
sweep = bcf_sweep_init(filename); |
| 2240 |
RETVAL = sweep; |
| 2241 |
OUTPUT: |
| 2242 |
RETVAL |
| 2243 |
|
| 2244 |
Bio::DB::HTS::VCF::HeaderPtr |
| 2245 |
vcfs_header_read(sweep) |
| 2246 |
Bio::DB::HTS::VCF::Sweep sweep |
| 2247 |
PREINIT: |
| 2248 |
bcf_hdr_t* h; |
| 2249 |
CODE: |
| 2250 |
h = bcf_sweep_hdr(sweep); |
| 2251 |
RETVAL = h; |
| 2252 |
OUTPUT: |
| 2253 |
RETVAL |
| 2254 |
|
| 2255 |
Bio::DB::HTS::VCF::RowPtr |
| 2256 |
vcfs_sweep_next(sweep) |
| 2257 |
Bio::DB::HTS::VCF::Sweep sweep |
| 2258 |
PREINIT: |
| 2259 |
bcf1_t* line; |
| 2260 |
CODE: |
| 2261 |
line = bcf_sweep_fwd(sweep); |
| 2262 |
if( line ) |
| 2263 |
{ |
| 2264 |
RETVAL = line; |
| 2265 |
} |
| 2266 |
else |
| 2267 |
{ |
| 2268 |
XSRETURN_EMPTY ; |
| 2269 |
} |
| 2270 |
OUTPUT: |
| 2271 |
RETVAL |
| 2272 |
|
| 2273 |
Bio::DB::HTS::VCF::RowPtr |
| 2274 |
vcfs_sweep_previous(sweep) |
| 2275 |
Bio::DB::HTS::VCF::Sweep sweep |
| 2276 |
PREINIT: |
| 2277 |
bcf1_t* line; |
| 2278 |
CODE: |
| 2279 |
line = bcf_sweep_bwd(sweep); |
| 2280 |
if( line ) |
| 2281 |
{ |
| 2282 |
RETVAL = line; |
| 2283 |
} |
| 2284 |
else |
| 2285 |
{ |
| 2286 |
XSRETURN_EMPTY ; |
| 2287 |
} |
| 2288 |
OUTPUT: |
| 2289 |
RETVAL |
| 2290 |
|
| 2291 |
void |
| 2292 |
vcfs_sweep_close(sweep) |
| 2293 |
Bio::DB::HTS::VCF::Sweep sweep |
| 2294 |
CODE: |
| 2295 |
bcf_sweep_destroy(sweep); |
| 2296 |
OUTPUT: |
| 2297 |
|
| 2298 |
|
| 2299 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::Kseq PREFIX = kseq_ |
| 2300 |
|
| 2301 |
Bio::DB::HTS::Kseq |
| 2302 |
kseq_new(package, filename, mode="r") |
| 2303 |
char *package |
| 2304 |
char *filename |
| 2305 |
char *mode |
| 2306 |
PROTOTYPE: $$$ |
| 2307 |
CODE: |
| 2308 |
RETVAL = gzopen(filename, mode); |
| 2309 |
OUTPUT: |
| 2310 |
RETVAL |
| 2311 |
|
| 2312 |
Bio::DB::HTS::Kseq |
| 2313 |
kseq_newfh(pack, fh, mode="r") |
| 2314 |
char *pack |
| 2315 |
PerlIO* fh |
| 2316 |
char *mode |
| 2317 |
PROTOTYPE: $$$ |
| 2318 |
CODE: |
| 2319 |
RETVAL = gzdopen(PerlIO_fileno(fh), mode); |
| 2320 |
OUTPUT: |
| 2321 |
RETVAL |
| 2322 |
|
| 2323 |
Bio::DB::HTS::Kseq::Iterator |
| 2324 |
kseq_iterator(fp) |
| 2325 |
Bio::DB::HTS::Kseq fp |
| 2326 |
PROTOTYPE: $ |
| 2327 |
CODE: |
| 2328 |
RETVAL = kseq_init(fp); |
| 2329 |
OUTPUT: |
| 2330 |
RETVAL |
| 2331 |
|
| 2332 |
void |
| 2333 |
kseq_DESTROY(fp) |
| 2334 |
Bio::DB::HTS::Kseq fp |
| 2335 |
PROTOTYPE: $ |
| 2336 |
CODE: |
| 2337 |
gzclose(fp); |
| 2338 |
|
| 2339 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::Kseq::Kstream PREFIX=kstream_ |
| 2340 |
|
| 2341 |
Bio::DB::HTS::Kseq::Kstream |
| 2342 |
kstream_new(package, fh) |
| 2343 |
char *package |
| 2344 |
Bio::DB::HTS::Kseq fh |
| 2345 |
PROTOTYPE: $$ |
| 2346 |
CODE: |
| 2347 |
RETVAL = ks_init(fh); |
| 2348 |
OUTPUT: |
| 2349 |
RETVAL |
| 2350 |
|
| 2351 |
int |
| 2352 |
kstream_begin(kstr) |
| 2353 |
Bio::DB::HTS::Kseq::Kstream kstr |
| 2354 |
PROTOTYPE: $ |
| 2355 |
CODE: |
| 2356 |
RETVAL = kstr->begin; |
| 2357 |
OUTPUT: |
| 2358 |
RETVAL |
| 2359 |
|
| 2360 |
int |
| 2361 |
kstream_end(kstr) |
| 2362 |
Bio::DB::HTS::Kseq::Kstream kstr |
| 2363 |
PROTOTYPE: $ |
| 2364 |
CODE: |
| 2365 |
RETVAL = kstr->end; |
| 2366 |
OUTPUT: |
| 2367 |
RETVAL |
| 2368 |
|
| 2369 |
int |
| 2370 |
kstream_is_eof(kstr) |
| 2371 |
Bio::DB::HTS::Kseq::Kstream kstr |
| 2372 |
PROTOTYPE: $ |
| 2373 |
CODE: |
| 2374 |
RETVAL = kstr->is_eof; |
| 2375 |
OUTPUT: |
| 2376 |
RETVAL |
| 2377 |
|
| 2378 |
char * |
| 2379 |
kstream_buffer(kstr) |
| 2380 |
Bio::DB::HTS::Kseq::Kstream kstr |
| 2381 |
PROTOTYPE: $ |
| 2382 |
CODE: |
| 2383 |
RETVAL = (char *)kstr->buf; |
| 2384 |
OUTPUT: |
| 2385 |
RETVAL |
| 2386 |
|
| 2387 |
Bio::DB::HTS::Kseq |
| 2388 |
kstream_fh(kstr) |
| 2389 |
Bio::DB::HTS::Kseq::Kstream kstr |
| 2390 |
PROTOTYPE: $ |
| 2391 |
CODE: |
| 2392 |
RETVAL = kstr->f; |
| 2393 |
OUTPUT: |
| 2394 |
RETVAL |
| 2395 |
|
| 2396 |
void |
| 2397 |
kstream_DESTROY(kstr) |
| 2398 |
Bio::DB::HTS::Kseq::Kstream kstr |
| 2399 |
PROTOTYPE: $ |
| 2400 |
CODE: |
| 2401 |
ks_destroy(kstr); |
| 2402 |
|
| 2403 |
MODULE = Bio::DB::HTS PACKAGE = Bio::DB::HTS::Kseq::Iterator PREFIX=kseqit_ |
| 2404 |
|
| 2405 |
SV * |
| 2406 |
kseqit_next_seq_hash(it) |
| 2407 |
Bio::DB::HTS::Kseq::Iterator it |
| 2408 |
PROTOTYPE: $ |
| 2409 |
INIT: |
| 2410 |
HV * results; |
| 2411 |
CODE: |
| 2412 |
results = (HV *)sv_2mortal((SV *)newHV()); |
| 2413 |
if (kseq_read(it) >= 0) { |
| 2414 |
hv_stores(results, "name", newSVpvn(it->name.s, it->name.l)); |
| 2415 |
hv_stores(results, "desc", newSVpvn(it->comment.s, it->comment.l)); |
| 2416 |
hv_stores(results, "seq", newSVpvn(it->seq.s, it->seq.l)); |
| 2417 |
hv_stores(results, "qual", newSVpvn(it->qual.s, it->qual.l)); |
| 2418 |
RETVAL = newRV((SV *)results); |
| 2419 |
} else { |
| 2420 |
XSRETURN_UNDEF; |
| 2421 |
} |
| 2422 |
OUTPUT: |
| 2423 |
RETVAL |
| 2424 |
|
| 2425 |
SV * |
| 2426 |
kseqit_next_seq(it) |
| 2427 |
Bio::DB::HTS::Kseq::Iterator it |
| 2428 |
PROTOTYPE: $ |
| 2429 |
INIT: |
| 2430 |
HV * results; |
| 2431 |
HV * class_stash; |
| 2432 |
SV * ref; |
| 2433 |
CODE: |
| 2434 |
results = (HV *)sv_2mortal((SV *)newHV()); |
| 2435 |
class_stash = gv_stashpv("Bio::DB::HTS::Kseq::Record", 0); |
| 2436 |
if (kseq_read(it) >= 0) { |
| 2437 |
hv_stores(results, "name", newSVpvn(it->name.s, it->name.l)); |
| 2438 |
hv_stores(results, "desc", newSVpvn(it->comment.s, it->comment.l)); |
| 2439 |
hv_stores(results, "seq", newSVpvn(it->seq.s, it->seq.l)); |
| 2440 |
hv_stores(results, "qual", newSVpvn(it->qual.s, it->qual.l)); |
| 2441 |
ref = newRV((SV *)results); |
| 2442 |
sv_bless(ref, class_stash); |
| 2443 |
RETVAL = ref; |
| 2444 |
} else { |
| 2445 |
XSRETURN_UNDEF; |
| 2446 |
} |
| 2447 |
OUTPUT: |
| 2448 |
RETVAL |
| 2449 |
|
| 2450 |
int |
| 2451 |
kseqit_read(it) |
| 2452 |
Bio::DB::HTS::Kseq::Iterator it |
| 2453 |
PROTOTYPE: $ |
| 2454 |
INIT: |
| 2455 |
CODE: |
| 2456 |
RETVAL = kseq_read(it); |
| 2457 |
OUTPUT: |
| 2458 |
RETVAL |
| 2459 |
|
| 2460 |
void |
| 2461 |
kseqit_rewind(it) |
| 2462 |
Bio::DB::HTS::Kseq::Iterator it |
| 2463 |
PROTOTYPE: $ |
| 2464 |
CODE: |
| 2465 |
/* kseq_rewind() doesn't completely rewind the file, |
| 2466 |
just resets markers */ |
| 2467 |
kseq_rewind(it); |
| 2468 |
/* use zlib to do so */ |
| 2469 |
gzrewind(it->f->f); |
| 2470 |
|
| 2471 |
Bio::DB::HTS::Kseq::Kstream |
| 2472 |
kseqit_kstream(it) |
| 2473 |
Bio::DB::HTS::Kseq::Iterator it |
| 2474 |
PROTOTYPE: $ |
| 2475 |
CODE: |
| 2476 |
RETVAL = it->f; |
| 2477 |
OUTPUT: |
| 2478 |
RETVAL |
| 2479 |
|
| 2480 |
char * |
| 2481 |
kseqit_name(it) |
| 2482 |
Bio::DB::HTS::Kseq::Iterator it |
| 2483 |
PROTOTYPE: $ |
| 2484 |
CODE: |
| 2485 |
RETVAL = it->name.s; |
| 2486 |
OUTPUT: |
| 2487 |
RETVAL |
| 2488 |
|
| 2489 |
char * |
| 2490 |
kseqit_comment(it) |
| 2491 |
Bio::DB::HTS::Kseq::Iterator it |
| 2492 |
PROTOTYPE: $ |
| 2493 |
CODE: |
| 2494 |
RETVAL = it->comment.s; |
| 2495 |
OUTPUT: |
| 2496 |
RETVAL |
| 2497 |
|
| 2498 |
char * |
| 2499 |
kseqit_seq(it) |
| 2500 |
Bio::DB::HTS::Kseq::Iterator it |
| 2501 |
PROTOTYPE: $ |
| 2502 |
CODE: |
| 2503 |
RETVAL = it->seq.s; |
| 2504 |
OUTPUT: |
| 2505 |
RETVAL |
| 2506 |
|
| 2507 |
char * |
| 2508 |
kseqit_qual(it) |
| 2509 |
Bio::DB::HTS::Kseq::Iterator it |
| 2510 |
PROTOTYPE: $ |
| 2511 |
CODE: |
| 2512 |
RETVAL = it->qual.s; |
| 2513 |
OUTPUT: |
| 2514 |
RETVAL |
| 2515 |
|
| 2516 |
int |
| 2517 |
kseqit_last_char(it) |
| 2518 |
Bio::DB::HTS::Kseq::Iterator it |
| 2519 |
PROTOTYPE: $ |
| 2520 |
CODE: |
| 2521 |
RETVAL = it->last_char; |
| 2522 |
OUTPUT: |
| 2523 |
RETVAL |
| 2524 |
|
| 2525 |
void |
| 2526 |
kseqit_DESTROY(it) |
| 2527 |
Bio::DB::HTS::Kseq::Iterator it |
| 2528 |
PROTOTYPE: $ |
| 2529 |
CODE: |
| 2530 |
kseq_destroy(it); |