Classes | Functions | Variables

bbcflib::btrack Namespace Reference

Classes

class  Track
class  FeatureStream

Functions

def track
def convert
def concat_fields
def map_chromosomes
def split_field
def score_threshold
def strand_to_int
def int_to_strand
def format_float
def format_int
def ucsc_to_ensembl
def ensembl_to_ucsc

Variables

list __all__
dictionary _track_map

Detailed Description

Examples::

import btrack as track

track.convert("data/test.bed","test0.sql",chrmeta='mm9')

Open a bed track & copy its info into another sql track::

chrmeta = {'chr2':{'length':4000000}}
info = {'datatype':'features'}
track_in = track.track("data/test.bed",chrmeta=chrmeta)
track_out = track.track("test1.sql",fields=['start','end','name'],chrmeta=chrmeta,info=info)
track_out.write(track_in.read())
track_out.close()
track_in.close()

Copy a selection of a bed track into a wig track::

track_in = track.track("data/HoxD13_4C_FB.sql")
track_out = track.track("test2.wig")
selection = [{'chr':'chr1','start':(7568000,9607000)},{'chr':'chr2','end':(3907400,4302000)}]
track_out.write(track_in.read(selection=selection),mode='overwrite')
track_out.close()
track_in.close()

Read a track (see :func:`FeatureStream <bbcflib.btrack.FeatureStream>`)::

track_in = track.track("data/Gene_TxS_chr2.bed.gz",chrmeta='mm9',format='bed')
for x in track_in.read():
    print x  #('chr2', 3030497, 3032496, 'ENSMUST00000072955_txS')
    break

Split field::

for x in track.split_field(track_in.read(),['name','extension'],'name','_'):
    print x  #['chr2', 3030497, 3032496, 'ENSMUST00000072955', 'txS']
    break

Random track::

from bFlatMajor.common import shuffled
for n,x in enumerate(shuffled(track_in.read('chr2'),chrlen=chrmeta['chr2']['length'])):
    print x
    if n>10: break

Do something with the scores of a signal track only if the location is present in another features track::

selection = {'chr':'chr2','start':(7540000,75650000)}
track_features = track.track("data/Bricks_HoxD4_FB_05_chr2.bed")
track_scores = track.track("data/HoxD13_4C_FB.sql",readonly=True)
score = float()
length = float()
for x in track_scores.read(selection=track_features.read(selection=selection),
                           fields=['start','end','score']):
    score += x[2]*(x[1]-x[0])
    length += (x[1]-x[0])
track_features.close()
track_scores.close()
print score/length


Function Documentation

def bbcflib::btrack::concat_fields (   stream,
  infields,
  outfield = 'name',
  separator = '|',
  as_tuple = False 
)
Concatenate fields of a stream. Ex.:

('chr1', 12, 'aa', 'bb') -> ('chr1', 12, 'aa|bb')     # as_tuple=False
('chr1', 12, 'aa', 'bb') -> ('chr1', 12, ('aa','bb')) # as_tuple=True

:param stream: FeatureStream object.
:param infields: (list of str) list of fields to concatenate.
:param outfield: (str) name of the new field created by concatenation of *infields*
    (can be an already existing one).
:param separator: (str) char to add between entries from concatenated fields.
:param as_tuple: (bool) join concatenated field entries in a tuple instead of a
    separator in a single string.
:rtype: FeatureStream object.
def bbcflib::btrack::convert (   source,
  target,
  chrmeta = None,
  info = None 
)
Converts a file from one format to another. Format can be explicitly specified::

    convert(('file1','bed'), ('file2','sql')) ,

otherwise it is guessed first from file extension::

    convert('file1.bed', 'file2.sql')

or in the worst case, by reading the first lines of the file.

:param source: (str or tuple) path to the source file, or tuple of the form (path, format).
:param target: (str or tuple) path to the target file, or tuple of the form (path, format).
def bbcflib::btrack::ensembl_to_ucsc (   start  ) 
Substract 1 to start coordinates going from Ensembl to UCSC annotation.
def bbcflib::btrack::format_float (   f = float()  ) 
Return a formatted string from a float or a string representing a float.
Limit to 4 decimals after the comma.
def bbcflib::btrack::format_int (   i = int()  ) 
Return a formatted string from an integer or a string representing an integer.
def bbcflib::btrack::int_to_strand (   num = 0  ) 
Convert 1/-1 into +/- notation for DNA strands.
def bbcflib::btrack::map_chromosomes (   stream,
  assembly,
  keep = False 
)
Translate the chromosome identifiers in *stream* into chromosome names of the type 'chr5'.

:param stream: FeatureStream object.
:param assembly: genrep.Assembly object.
:param keep: (bool) keep all features (True) or only those which chromosome identifier
    is recognized (False) [False].
def bbcflib::btrack::score_threshold (   source,
  threshold = 0.0,
  lower = False,
  fields = 'score' 
)
Filter the features of a track which score is above or below a certain threshold.

:param source: Track instance (or a subclass), or a list/tuple of them.
:param threshold: (float) threshold above which features are not retained (?)
:param lower: (bool) higher (False) or lower (True) bound.
:param fields: (str or list of str) names of the fields to apply the filter to.
def bbcflib::btrack::split_field (   stream,
  outfields,
  infield = 'name',
  separator = ';',
  header_split = None,
  strip_input = False 
)
Split one field of a stream containing multiple information, into multiple fields. Ex.:

('chr1', 12, 'aa;bb;cc') -> ('chr1', 12, 'aa', 'bb', 'cc')

:param stream: FeatureStream object.
:param outfields: (list of str) list of new fields to be created.
:param infield: (str) name of the field to be splitted.
:param separator: (str) char separating the information in *infield*'s entries [';'].
:param header_split: ?
:param strip_input: (bool) ?
def bbcflib::btrack::strand_to_int (   strand = None  ) 
Convert +/- into 1/-1 notation for DNA strands.
def bbcflib::btrack::track (   path,
  format = None,
  kwargs 
)
Guess file format.
Return a Track object of the corresponding subclass (e.g. BedTrack).

:param path: (str) path to a track-like file.
:param format: (str) format of the file.
:param **kwargs: (dict) parameters of the Track subclass' constructor.
def bbcflib::btrack::ucsc_to_ensembl (   start  ) 
Add +1 to start coordinates going from UCSC to Ensembl annotation.

Variable Documentation

list bbcflib::btrack::__all__
Initial value:
00001 ['Track','track','FeatureStream','convert',
00002            'strand_to_int','int_to_strand','format_float','format_int',
00003            'ucsc_to_ensembl','ensembl_to_ucsc',]
dictionary bbcflib::btrack::_track_map
Initial value:
00001 {
00002     'sql': ('bbcflib.btrack.sql','SqlTrack'),
00003     'db':  ('bbcflib.btrack.sql','SqlTrack'),
00004     'text':('bbcflib.btrack.text','TextTrack'),
00005     'txt': ('bbcflib.btrack.text','TextTrack'),
00006     'bed': ('bbcflib.btrack.text','BedTrack'),
00007     'bedGraph': ('bbcflib.btrack.text','BedGraphTrack'),
00008     'bedgraph': ('bbcflib.btrack.text','BedGraphTrack'),
00009     'sga': ('bbcflib.btrack.text','SgaTrack'),
00010     'wig': ('bbcflib.btrack.text','WigTrack'),
00011     'gff': ('bbcflib.btrack.text','GffTrack'),
00012     'gtf': ('bbcflib.btrack.text','GffTrack'),
00013     'bigWig': ('bbcflib.btrack.bin','BigWigTrack'),
00014     'bigwig': ('bbcflib.btrack.bin','BigWigTrack'),
00015     'bw':  ('bbcflib.btrack.bin','BigWigTrack'),
00016     'bam': ('bbcflib.btrack.bin','BamTrack'),
00017 }
 All Classes Namespaces Functions