Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
binfpy
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
opensource
binfpy
Commits
f5d19d0a
Commit
f5d19d0a
authored
Sep 04, 2017
by
Mikael Boden
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
prac4
parent
f5bf266d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
9 deletions
+46
-9
bed.py
bed.py
+18
-2
ival.py
ival.py
+28
-7
No files found.
bed.py
View file @
f5d19d0a
...
...
@@ -136,10 +136,14 @@ class BedEntry():
def
getInterval
(
self
):
return
ival
.
Interval
(
self
.
chromStart
,
self
.
chromEnd
)
def
dist
(
entry1
,
entry2
):
def
dist
(
entry1
,
entry2
,
signed
=
False
,
centre2centre
=
False
):
""" Calculate and return the BedEntry with the closest distance (from one end of the interval of this to the end of the interval of that).
If centre2centre is True, use the centre-to-centre distance instead.
If signed is True, the distance is negative if this interval is after the that.
"""
if
isinstance
(
entry1
,
BedEntry
)
and
isinstance
(
entry2
,
BedEntry
):
if
(
entry1
.
chrom
==
entry2
.
chrom
):
return
ival
.
dist
(
entry1
.
getInterval
(),
entry2
.
getInterval
())
return
ival
.
dist
(
entry1
.
getInterval
(),
entry2
.
getInterval
()
,
signed
,
centre2centre
)
return
None
class
BedFile
:
...
...
@@ -282,6 +286,18 @@ class BedFile:
else
:
return
None
else
:
return
None
def
getOneOfClosest
(
self
,
item
):
all
=
self
.
getClosest
(
item
)
if
all
==
None
:
return
None
else
:
return
next
(
iter
(
all
))
def
getOneOfOverlap
(
self
,
item
):
all
=
self
.
getOverlap
(
item
)
if
all
==
None
:
return
None
elif
len
(
all
)
==
0
:
return
None
else
:
return
next
(
iter
(
all
))
def
readBedFile
(
filename
,
format
=
'Limited'
):
""" Read a BED file.
format: specifies the format of the file,
...
...
ival.py
View file @
f5d19d0a
...
...
@@ -294,14 +294,31 @@ class Interval:
def
__sizeof__
(
self
):
return
self
.
max
-
self
.
min
def
dist
(
self
,
that
):
if
(
self
.
min
>
that
.
max
):
return
self
.
min
-
that
.
max
# that interval is BEFORE this
if
(
self
.
max
<
that
.
min
):
return
that
.
min
-
self
.
max
# that interval is AFTER this
return
0
def
dist
(
first
,
second
):
def
dist
(
self
,
that
,
signed
=
False
,
centre2centre
=
False
):
""" Calculate and return the closest distance (from one end of the interval of this to the end of the interval of that).
If centre2centre is True, use the centre-to-centre distance instead.
If signed is True, the distance is negative if this interval is after the that.
"""
if
not
centre2centre
:
if
not
signed
:
if
(
self
.
min
>
that
.
max
):
return
self
.
min
-
that
.
max
# that interval is BEFORE this
if
(
self
.
max
<
that
.
min
):
return
that
.
min
-
self
.
max
# that interval is AFTER this
else
:
# distance is signed
if
(
self
.
min
>
that
.
max
):
return
that
.
max
-
self
.
min
# that interval is BEFORE this
if
(
self
.
max
<
that
.
min
):
return
that
.
min
-
self
.
max
# that interval is AFTER this
return
0
else
:
thiscentre
=
(
self
.
max
-
self
.
min
)
/
2
+
self
.
min
thatcentre
=
(
that
.
max
-
that
.
min
)
/
2
+
that
.
min
return
thatcentre
-
thiscentre
if
signed
else
abs
(
thatcentre
-
thiscentre
)
def
dist
(
first
,
second
,
signed
=
False
,
centre2centre
=
False
):
""" Calculate and return the closest distance (from one end of the interval to the other).
If centre2centre is True, use the centre-to-centre distance instead.
If signed is True, the distance is negative if the first is after the second.
"""
if
isinstance
(
first
,
Interval
)
and
isinstance
(
second
,
Interval
):
return
first
.
dist
(
second
)
return
first
.
dist
(
second
,
signed
,
centre2centre
)
return
RuntimeError
def
union
(
first
,
second
):
...
...
@@ -347,6 +364,10 @@ if __name__ == '__main__':
k
=
Interval
(
24
,
24
)
l
=
Interval
(
52
,
55
)
print
(
'dist(b,a,signed=False,centre2centre=False)='
,
dist
(
b
,
a
,
signed
=
False
,
centre2centre
=
False
))
print
(
'dist(b,a,signed=True,centre2centre=False)='
,
dist
(
b
,
a
,
signed
=
True
,
centre2centre
=
False
))
print
(
'dist(b,a,signed=False,centre2centre=True)='
,
dist
(
b
,
a
,
signed
=
False
,
centre2centre
=
True
))
print
(
'dist(b,a,signed=True,centre2centre=True)='
,
dist
(
b
,
a
,
signed
=
True
,
centre2centre
=
True
))
t
=
IntervalTree
()
t
.
put
(
a
,
'A'
)
t
.
put
(
b
,
'B'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment