Skip to content

Commit

Permalink
Merge pull request #184 from JureZmrzlikar/bedgraph
Browse files Browse the repository at this point in the history
bedgraph: support additional track-configuring parameters
  • Loading branch information
tomazc committed Aug 20, 2018
2 parents 7a6dba8 + c272a15 commit 2978772
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
47 changes: 40 additions & 7 deletions iCount/files/bedgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
import logging
import re

import pybedtools

Expand All @@ -15,13 +16,14 @@
LOGGER = logging.getLogger(__name__)


def bed2bedgraph(bed, bedgraph, name='User Track', description='User Supplied Track'):
def bed2bedgraph(bed, bedgraph, name='User Track', description='User Supplied Track', visibility=None, priority=None,
color=None, alt_color=None, max_height_pixels=None):
"""
Convert from BED6 to bedGraph format.
For further explanation of ``name`` and ``description`` parameters
(and also others, that are not yet supported) see:
For further explanation of parameters see:
https://genome.ucsc.edu/goldenPath/help/customTrack.html#TRACK
https://genome.ucsc.edu/goldenpath/help/trackDb/trackDbHub.html
Parameters
----------
Expand All @@ -30,9 +32,24 @@ def bed2bedgraph(bed, bedgraph, name='User Track', description='User Supplied Tr
bedgraph : str
Output bedGraph file.
name : str
Track label. Can consist of up to 15 characters.
Track label. Should be shorter than 15 characters.
description : str
Track description. Can consist of up to 60 characters.
Track description. Should be shorter than 60 characters.
visibility: str
Define the initial display mode of the annotation track. Choose
among "hide", "dense", "full", "pack" and "squish". Default is
"dense".
priority : int
Defines the track's order relative to other tracks in same group.
color : str
Define the main color for the annotation track. The track color
consists of three comma-separated RGB values from 0-255, e.g
RRR,GGG,BBB. The default value is 0,0,0 (black).
alt_color : str
Allow a color range that varies from color to alt_color.
max_height_pixels : str
The limits of vertical viewing space for track, though it is
configurable by the user. Should be of the format <max:default:min>.
Returns
-------
Expand All @@ -42,13 +59,29 @@ def bed2bedgraph(bed, bedgraph, name='User Track', description='User Supplied Tr
iCount.log_inputs(LOGGER, level=logging.INFO) # pylint: disable=protected-access
LOGGER.info('Checking input parameters...')
assert bed.endswith(('.bed', '.bed.gz'))
assert bedgraph.endswith('.bedgraph')

header = [
'track',
'type=bedGraph',
'name="{}"'.format(name[:15]),
'description="{}"'.format(description[:60]),
'name="{}"'.format(name),
'description="{}"'.format(description),
]
if visibility:
assert re.fullmatch(r'hide|dense|full|pack|squish', visibility)
header.append('visibility={}'.format(visibility))
if priority:
assert re.fullmatch(r'\d+', str(priority))
header.append('priority={}'.format(priority))
if color:
assert re.fullmatch(r'\d{1,3},\d{1,3},\d{1,3}', color)
header.append('color={}'.format(color))
if alt_color:
assert re.fullmatch(r'\d{1,3},\d{1,3},\d{1,3}', alt_color)
header.append('altColor={}'.format(alt_color))
if max_height_pixels:
assert re.fullmatch(r'\d{1,3}:\d{1,3}:\d{1,3}', max_height_pixels)
header.append('maxHeightPixels={}'.format(max_height_pixels))

LOGGER.info('Converting %s to %s', bed, bedgraph)
with open(bedgraph, 'wt') as outfile:
Expand Down
9 changes: 7 additions & 2 deletions iCount/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,19 @@ def test_summary(self):

def test_bed2bedgraph(self):
command_basic = [
'iCount', 'bedgraph', self.cross_links, self.tmp1,
'iCount', 'bedgraph', self.cross_links, get_temp_file_name(extension='bedgraph'),
'-S', '40', # Supress lower than ERROR messages.
]

command_full = [
'iCount', 'bedgraph', self.cross_links, self.tmp1,
'iCount', 'bedgraph', self.cross_links, get_temp_file_name(extension='bedgraph'),
'--name', 'Name.',
'--description', 'Description.',
'--visibility', 'full',
'--priority', '20',
'--color', '256,0,0',
'--alt_color', '0,256,0',
'--max_height_pixels', '100:50:0',
'-S', '40', # Supress lower than ERROR messages.
]

Expand Down
18 changes: 13 additions & 5 deletions iCount/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,22 @@ def test_bed2bedgraph(self):

def test_bed2bedgraph_params(self):
"""
Test with custom ``name`` and ``description`` parameters.
Note that ``name`` is too long and is trimmed to 15 characters.
Test with custom parameters.
"""
iCount.files.bedgraph.bed2bedgraph(
self.bed, self.bedgraph, name='Longer than 15 chars.', description='Custom text.')
self.bed,
self.bedgraph,
name='Sample name',
description='A long and detailed description.',
visibility='full',
priority=20,
color='256,0,0',
alt_color='0,256,0',
max_height_pixels='100:50:0',
)
expected = [
['track type=bedGraph name="Longer than 15 " description="Custom text."'],
['track type=bedGraph name="Sample name" description="A long and detailed description."'
' visibility=full priority=20 color=256,0,0 altColor=0,256,0 maxHeightPixels=100:50:0'],
['1', '4', '5', '+5'],
['1', '5', '6', '+1'],
['1', '5', '6', '-1'],
Expand Down

0 comments on commit 2978772

Please sign in to comment.