Skip to content

Commit

Permalink
Fixed API issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kcleal committed Oct 24, 2024
1 parent 96dcd00 commit a15ffaf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
43 changes: 35 additions & 8 deletions dysgu/map_set_utils.pyx
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
#cython: language_level=3

import click
import numpy as np
cimport numpy as np
import cython
import time
import logging
import gzip
from libcpp.vector cimport vector as cpp_vector
from libcpp.pair cimport pair as cpp_pair

from libc.stdlib cimport abs as c_abs
from libc.math cimport fabs as c_fabs

# from pysam.libcalignmentfile cimport AlignmentFile
# from pysam.libcalignedsegment cimport AlignedSegment
# from pysam.libchtslib cimport bam1_t, BAM_CIGAR_SHIFT, BAM_CIGAR_MASK
from libc.stdint cimport uint32_t, uint16_t, int16_t, int32_t

import math

ctypedef cpp_pair[int, int] cpp_item
ctypedef cpp_pair[long, int] cpp_long_item

Expand Down Expand Up @@ -100,6 +93,40 @@ def merge_intervals(intervals, srt=True, pad=0, add_indexes=False):
merged.append(list(higher)[:3] + [[higher[3]]])
return merged

def load_bed(filepath):
"""
Load the first 3 columns of a BED file into a list of tuples.
Handles both regular and gzip compressed files.
Args:
filepath (str): Path to the BED file
Returns:
list: List of tuples in format [(chrom, start, end), ...]
"""
# Determine if file is gzip compressed based on extension
is_gzip = filepath.endswith('.gz')
bed_regions = []
open_func = gzip.open if is_gzip else open
mode = 'rt' if is_gzip else 'r'
with open_func(filepath, mode) as f:
for line in f:
if line.startswith('#'):
continue
if not line.strip():
continue
fields = line.strip().split('\t')
if len(fields) >= 3: # Ensure we have at least 3 columns
try:
chrom = fields[0]
start = int(fields[1])
end = int(fields[2])
bed_regions.append((chrom, start, end))
except ValueError:
continue

return bed_regions


cdef class Py_BasicIntervalTree:
def __cinit__(self):
Expand Down
2 changes: 1 addition & 1 deletion dysgu/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from dysgu import post_call as post_call_metrics
from dysgu.cluster import pipe1
from dysgu.io_funcs import to_vcf
from dysgu.map_set_utils import to_dict
from dysgu.map_set_utils import to_dict, merge_intervals, load_bed
from dysgu.merge_svs import merge_events
from dysgu.view import dotdict, set_numeric, vcf_to_df

Expand Down

0 comments on commit a15ffaf

Please sign in to comment.