Skip to content

Commit

Permalink
Implemented stranded option to tx2genome
Browse files Browse the repository at this point in the history
  • Loading branch information
tleonardi committed Feb 19, 2019
1 parent 417126e commit b43a17c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bedparse/bedline.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def introns(self):



def tx2genome(self, coord):
def tx2genome(self, coord, stranded=False):
""" Given a position in transcript coordinates returns the equivalent in genome coordinates.
The transcript coordinates are considered without regard to strand, i.e. 0 is the leftmost
position for both + and - strand transcripts."""
Expand All @@ -317,6 +317,9 @@ def tx2genome(self, coord):
exLens = [ int(i) for i in self.exLengths.split(',')if i!='' ]
nEx=self.nEx

if stranded:
coord = sum(exLens)-coord-1

# Throw an exception is the coordinate is invalid
if(coord<0 or coord>=sum(exLens)):
raise BEDexception("This coordinate doesn't exist in the transcript")
Expand Down
30 changes: 30 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,25 @@ class KnownValues(unittest.TestCase):
)
]

known_tx2genome_stranded=[
(
# Transcript
['chr1', 1000, 2000, 'Tx1', '0', '+', 1000, 1000, 0, 1, '1000,', '0,'],
# Tuple of tuples (txCoord, GenomeCoord)
((0, 1999), (500, 1499), (999, 1000)),
# Tuple of txCoord that should throw exception
(-10, -100, "a", 0.7, 2000, 10000)
),
(
# Transcript
["1", "25900101", "25906466", "ENST00000455785", "0", "-", "25901015", "25904676", "0", "5", "986,192,173,75,78,", "0,1389,3539,4562,6287,"],
# Tuple of tuples (txCoord, GenomeCoord)
((0, 25906465), (465, 25901542), (1503, 25900101)),
# Tuple of txCoord that should throw exception
(-10, -100, "a", 0.7, 1504, 10000)
)
]

def test_promoter100(self):
'''promoters() should return correct promoters100 with known input'''
for ((bed), (prom)) in self.known_promoters100:
Expand Down Expand Up @@ -246,5 +265,16 @@ def test_tx2genome(self):
self.assertEqual(res, genomeCoord)
for be in broken_examples:
self.assertRaises(bedparse.BEDexception, tx.tx2genome, be)

def test_tx2genome_stranded(self):
'''tx2genome should return corred coordinates for known cases'''
for tx, examples, broken_examples in self.known_tx2genome_stranded:
tx = bedparse.bedline(tx)
for (txCoord, genomeCoord) in examples:
res=tx.tx2genome(txCoord, stranded=True)
self.assertEqual(res, genomeCoord)
tx2genome_stranded = lambda x : tx.tx2genome(x, stranded=True)
for be in broken_examples:
self.assertRaises(bedparse.BEDexception, tx2genome_stranded, be)
if __name__ == '__main__':
unittest.main()

0 comments on commit b43a17c

Please sign in to comment.