本文整理汇总了Python中pybedtools.Interval方法的典型用法代码示例。如果您正苦于以下问题:Python pybedtools.Interval方法的具体用法?Python pybedtools.Interval怎么用?Python pybedtools.Interval使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pybedtools
的用法示例。
在下文中一共展示了pybedtools.Interval方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __getitem__
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def __getitem__(self, idx):
"""Returns (pybedtools.Interval, labels)
"""
row = self.df.iloc[idx]
# TODO: use kipoiseq.dataclasses.interval instead of pybedtools
import pybedtools
interval = pybedtools.create_interval_from_list(
[to_scalar(x) for x in row.iloc[:self.bed_columns]])
if self.ignore_targets or self.n_tasks == 0:
labels = {}
else:
labels = row.iloc[self.bed_columns:].values.astype(
self.label_dtype)
return interval, labels
示例2: merged_interval_features
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def merged_interval_features(feature, bam_handle,find_svtype=False):
support_list = feature.name.split(",")
locations = sorted(map(int, support_list[0:-1:3]))
other_bp_ends = support_list[-1] if not find_svtype else ','.join(support_list[1::3])
num_unique_locations = len(set(locations))
count_str = ",".join(["%s,%s" % (i, c) for (i, c) in collections.Counter(locations).items()])
plus_support = len([i for i in support_list[2::3] if i == "+"])
minus_support = len(locations) - plus_support
locations_span = max(locations) - min(locations)
interval_readcount = bam_handle.count(reference=str(feature.chrom), start=feature.start, end=feature.end)
info = {"SC_PLUS_SUPPORT":plus_support, "SC_MINUS_SUPPORT":minus_support,
"SC_LOCATIONS_SPAN":locations_span, "SC_NUM_UNIQUE_LOCATIONS":num_unique_locations,
"SC_COUNT_STR": count_str, "SC_COVERAGE":interval_readcount, "SC_OTHER_BP_ENDS": other_bp_ends,
"SC_SC_BP_ENDS": "%s-%s"%(feature.start, feature.end)}
name = "%s,%s,0,SC" % (
base64.b64encode(json.dumps(info)), feature.fields[6].split(',')[0])
return pybedtools.Interval(feature.chrom, feature.start, feature.end, name=name, score=feature.score,
otherfields=[str(interval_readcount)]+feature.fields[6:])
示例3: get_full_interval
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def get_full_interval(feature, pad):
name_fields = feature.name.split(",")
info = json.loads(base64.b64decode(name_fields[0]))
other_bp_ends=info["SC_OTHER_BP_ENDS"]
start = feature.start
end = feature.end
sv_type = name_fields[1]
if "-" in other_bp_ends:
other_bp_start,other_bp_end=map(lambda x:int(x),other_bp_ends.split("-"))
if other_bp_start != 0 or other_bp_end != 0:
start = min((feature.start+feature.end)/2,(other_bp_start+other_bp_end)/2)
end = max((feature.start+feature.end)/2,(other_bp_start+other_bp_end)/2)
sv_len = 0 if sv_type == "INS" else max(end-start,0)
info["SOURCES"] = "%s-%d-%s-%d-%d-SoftClip" % (feature.chrom, start, feature.chrom, end, sv_len)
name = "%s,%s,%d,%s"%(base64.b64encode(json.dumps(info)),sv_type,sv_len,'SC')
return pybedtools.Interval(feature.chrom, start, end, name=name, score=feature.score,
otherfields=feature.fields[6:])
示例4: find_other_bp_interval
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def find_other_bp_interval(feature,pad):
name_fields = feature.name.split(",")
sv_type = name_fields[1]
sv_methods = name_fields[3]
sv_length = int(name_fields[2])
info = json.loads(base64.b64decode(name_fields[0]))
other_bps=map(lambda y: map(int,y['SC_OTHER_BP_ENDS'].split('-')),info["SC_SUBINTERVAL_INFOs"])
start_interval=min(map(lambda x:x[0],other_bps))
end_interval=max(map(lambda x:x[1],other_bps))
soft_clip_location = (start_interval+end_interval)/2
sc_bp=feature.start if abs(soft_clip_location-feature.start)>abs(soft_clip_location-feature.end) else feature.end
other_bp_field="%d-%d"%(max(sc_bp-pad,0),sc_bp+pad)
name="%d,%d,+,%s"%(soft_clip_location,sc_bp,other_bp_field)
return pybedtools.Interval(feature.chrom, start_interval, end_interval, name=name, score="1",
otherfields=[sv_type])
示例5: find_idp
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def find_idp(feature, wiggle):
n = len(feature.fields) / 2
if feature.chrom != feature.fields[n]:
return None
start_dup = feature.start
end_dup = feature.end
start_del = int(feature.fields[n + 1])
end_del = int(feature.fields[n + 2])
if abs(start_del - end_del) > (abs(start_dup - end_dup) - wiggle):
return None
dist_ends = [abs(start_del - start_dup), abs(end_del - end_dup)]
if min(dist_ends) > wiggle:
return None
del_pos = start_del if dist_ends[0] > dist_ends[1] else end_del
name = "%s,%s" % (feature.name, feature.fields[n + 3])
score = "%s,%s" % (feature.score, feature.fields[n + 4])
return pybedtools.Interval(feature.chrom, feature.start, feature.end,
name=name, score=score,
otherfields=["%d" % del_pos,
"%d-%d" % (start_del, end_del)])
示例6: filter_itxs
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def filter_itxs(feature):
n = len(feature.fields) / 2
del_interval_idp = map(int, feature.fields[7].split("-"))
del_interval_itx_1 = map(int,
feature.fields[n + 7].split(",")[0].split("-"))
del_interval_itx_2 = map(int,
feature.fields[n + 7].split(",")[1].split("-"))
if filter(lambda x: abs(x[0] - del_interval_idp[0]) + abs(
x[1] - del_interval_idp[1]) == 0,
[del_interval_itx_1, del_interval_itx_2]) and "LowQual" not in \
feature.fields[n + 4]:
return None
return pybedtools.Interval(feature.chrom, feature.start, feature.end,
name=feature.name,
score=feature.score,
otherfields=feature.fields[6:n])
示例7: to_bed_interval
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def to_bed_interval(self, sample_name):
if self.start <= 0:
return None
if self.sv_type not in ["DEL", "INS", "INV", "ITX", "CTX", "DUP"]: return None
# if not self.sub_intervals and list(self.sources)[0] == "HaplotypeCaller": return None
# if len(self.sources) == 1 and list(self.sources)[0] == "HaplotypeCaller": return None
if self.sv_type == "INS":
end = self.end + 1
elif self.sv_type in ["ITX","CTX"] :
end = self.start + 1
else:
end = self.end
info = self.get_info()
if self.sv_type in ["ITX", "CTX"]:
info["POS2"] = self.end
info["END"] = end
info["CHR2"] = self.chrom2
if not self.is_precise:
info.update({"IMPRECISE": True})
return pybedtools.Interval(self.chrom, self.start, end, name="%s,%s,%d,%s" % (
base64.b64encode(json.dumps(info)), self.sv_type, self.length,
";".join(self._get_svmethods())),
score=str(len(self.sources)))
示例8: add_weighted_score
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def add_weighted_score(in_bed, score_bed):
out_bed = in_bed.intersect(score_bed, wao=True).saveas(os.path.join(args.tmpdir, "score.bed"))
bed_array = []
last_interval = pybedtools.Interval("", 0, 0)
map_value = 0.0
for interval in out_bed:
if interval.chrom != last_interval.chrom or interval.start != last_interval.start or interval.end != last_interval.end:
if last_interval.chrom:
bed_array.append(tuple(last_interval.fields[:-5]) + (str(map_value),))
map_value = 0.0
last_interval = interval
if float(interval.fields[-1]) > 0:
map_value += float(interval.fields[-1]) * float(interval.fields[-2]) / float(interval.length)
if last_interval.chrom:
bed_array.append(tuple(last_interval.fields[:-5]) + (str(map_value),))
return pybedtools.BedTool(bed_array)
示例9: soft_clips_at_breakpoint
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def soft_clips_at_breakpoint(read, region):
"""
Returns True if the given pysam read maps up to the edge of the given
pybedtools.Interval, ``region`` and soft clips at that edge.
Two cases are possible:
1. The end of the read soft clips such that the read's reference end stops
at the breakpoint start and the end of the read is softclipped.
2. The beginning of the read soft clips such that the read's reference start
begins at the breakpoint end and the beginning of the read is softclipped.
In both cases, the read should be mapped in its best location in the
reference ("perfect mapping").
"""
return (
(read.reference_end == region.start and read.cigar[-1][0] == BAM_CSOFT_CLIP) or
(read.reference_start == region.end + 1 and read.cigar[0][0] == BAM_CSOFT_CLIP)
)
示例10: test_bed3
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def test_bed3(tmpdir):
bed_file = write_tmp('chr1\t1\t2\nchr1\t1\t3', tmpdir)
bt = BedDataset(bed_file)
assert bt.n_tasks == 0
assert len(bt) == 2
assert np.all(bt.df[0] == 'chr1')
assert bt[0] == (Interval("chr1", 1, 2), {})
assert bt[1] == (Interval("chr1", 1, 3), {})
示例11: test_bed3_labels
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def test_bed3_labels(tmpdir):
bed_file = write_tmp('chr1\t1\t2\t1\t0\nchr1\t1\t3\t0\t1', tmpdir)
bt = BedDataset(bed_file)
assert np.all(bt.get_targets() == np.array([[1, 0],
[0, 1]]))
assert len(bt) == 2
assert bt.n_tasks == 2
assert np.all(bt.df[0] == 'chr1')
assert bt[0][0] == Interval("chr1", 1, 2)
assert np.all(bt[0][1] == np.array([1, 0]))
assert bt[1][0] == Interval("chr1", 1, 3)
assert np.all(bt[1][1] == np.array([0, 1]))
assert len(bt) == 2
示例12: test_incl_excl_chromosomes
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def test_incl_excl_chromosomes(tmpdir):
bed_file = write_tmp(
'chr1\t1\t2\t1\t0\nchr2\t1\t3\t0\t1\nchr3\t1\t3\t0\t1', tmpdir)
bt = BedDataset(bed_file)
assert len(bt) == 3
bt = BedDataset(bed_file, incl_chromosomes=['chr1'])
assert len(bt) == 1
assert bt[0][0] == Interval("chr1", 1, 2)
bt = BedDataset(bed_file, excl_chromosomes=['chr1'])
assert len(bt) == 2
assert bt[0][0] == Interval("chr2", 1, 3)
示例13: test_tsvreader
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def test_tsvreader(tsv_file, num_chr, label_dtype):
ds = BedDataset(tsv_file, label_dtype=label_dtype, num_chr=num_chr)
interval, labels = ds[0]
assert isinstance(interval, Interval)
if not num_chr:
assert interval.chrom.startswith("chr")
assert isinstance(labels[0], label_dtype)
assert interval.start == 2
assert interval.end == 4
示例14: from_pybedtools
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def from_pybedtools(cls, interval):
"""Create the ranges object from `pybedtools.Interval`
# Arguments
interval: `pybedtools.Interval` instance
"""
return cls(chrom=interval.chrom,
start=interval.start,
end=interval.stop,
name=interval.name,
score=interval.score,
strand=interval.strand,
attrs=dict(interval.attrs or dict())
)
示例15: test_fasta_extractor_valid_intervals
# 需要导入模块: import pybedtools [as 别名]
# 或者: from pybedtools import Interval [as 别名]
def test_fasta_extractor_valid_intervals():
extractor = FastaExtractor("tests/data/fasta_test.fa")
intervals = [Interval("chr1", 0, 10), Interval("chr2", 0, 10)]
expected_data = np.array(
[
[
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
],
[
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[0.25, 0.25, 0.25, 0.25],
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[0.25, 0.25, 0.25, 0.25],
],
],
dtype=np.float32,
)
data = extractor(intervals)
assert (data == expected_data).all()