<commit_before>#!/usr/bin/env python

#Copyright (C) 2013 by <NAME>
#
#Released under the MIT license, see LICENSE.txt
#!/usr/bin/env python

import os
import sys
import logging
from pybedtools import BedTool, Interval

""" all track-data specific io code goes here.  Just BED implemented for now,
will eventually add WIG and maybe eventually bigbed / bigwig """

###########################################################################

def readTrackData(trackPath, chrom, start, end, **kwargs):
    """ read genome annotation track into python list of values.  a value
    is returned for every element in range (default value is None).  The
    type of file is detected from the extension"""
    data = None
    if not os.path.isfile(trackPath):
        sys.stderr.write("Warning: track file not found %s\n" %
                         trackPath)
        return None

    trackExt = os.path.splitext(trackPath)[1]
    if trackExt == ".bed":
        return readBedData(trackPath, chrom, start, end, **kwargs)
    else:
        sys.stderr.write("Warning: non-BED file skipped %s\n" %
                         trackPath)
    return None

###########################################################################

def readBedData(bedPath, chrom, start, end, **kwargs):

    valCol = None
    sort = False
    if kwargs is not None and "valCol" in kwargs:
        valCol = int(kwargs["valCol"])
    valMap = None
    if kwargs is not None and "valMap" in kwargs:
        valMap = kwargs["valMap"]
    defVal = None
    if valMap is not None:
        defVal = valMap.getMissingVal()
    updateMap = False
    if kwargs is not None and "updateValMap" in kwargs:
        updateMap = kwargs["updateValMap"]
    if kwargs is not None and "sort" in kwargs:
        sort = kwargs["sort"] == True

    data = [defVal] * (end - start)
    logging.debug("readBedData(%s, update=%s)" % (bedPath, updateMap))
    bedTool = BedTool(bedPath)
    if sort is True:
        logging.debug("sortBed(%s)" % bedPath)
        bedTool = bedTool.sort()
        <commit_msg>add support for wiggle<commit_after>#!/usr/bin/env python

#Copyright (C) 2013 by <NAME>
#
#Released under the MIT license, see LICENSE.txt
#!/usr/bin/env python

import os
import sys
import logging
from pybedtools import BedTool, Interval
from .common import runShellCommand

""" all track-data specific io code goes here.  Just BED implemented for now,
will eventually add WIG and maybe eventually bigbed / bigwig """

###########################################################################

def readTrackData(trackPath, chrom, start, end, **kwargs):
    """ read genome annotation track into python list of values.  a value
    is returned for every element in range (default value is None).  The
    type of file is detected from the extension"""
    data = None
    if not os.path.isfile(trackPath):
        sys.stderr.write("Warning: track file not found %s\n" %
                         trackPath)
        return None

    trackExt = os.path.splitext(trackPath)[1]
    tempPath = None
    if trackExt == ".bw" or trackExt == ".bigwig":
        #just writing in current directory.  something more principaled might
        #be safer / nicer eventually
        tempPath = os.path.splitext(os.path.basename(trackPath))[0] \
                   + "_temp.bed"
        logging.info("Extracting wig to temp bed %s. Make sure to erase"
                     " in event of crash" % os.path.abspath(tempPath)) 
        runShellCommand("bigWigToBedGraph %s %s -chrom=%s -start=%d -end=%d" %
                        (trackPath, tempPath, chrom, start, end))
        trackExt = ".bed"
        trackPath = tempPath
    if trackExt == ".bed":
        data = readBedData(trackPath, chrom, start, end, **kwargs)
    else:     
        sys.stderr.write("Warning: non-BED file skipped %s\n" %
                         trackPath)
        
    if tempPath is not None:
        runShellCommand("rm -f %s" % tempPath)
    return data

###########################################################################

def readBedData(bedPath, chrom, start, end, **kwargs):

    valCol = None
    sort = False
    if kwargs is not None and "valCol" in kwargs:
        valCol = int(kwargs["valCol"])
    valMap = None
    if kwargs is not None and "valMap" in kwargs:
        valMap = kwargs["valMap"]
    defVal = None
    if valMap is not None:
        defVal = valMap.getMissingVal()
    updateMap = False
    if kwargs is not None and "updateValMap" in kwargs:
        updateMap = kwargs["updateValMap"]
    if kwargs is not None and "sort" in kwargs:
        sort = kwargs["sort"] == True

    data = [defVal] * (end - start)
    logging.debug("readBedData(%s, update=%s)" % (bedPath, updateMap))
    bedTool = BedTool(bedPath)
    if sort is True:
        logging.debug("sortBed(%s)" % bedPath)
        bedTool = bedTool.sort()
        
{'fraction_non_alphanumeric': 0.15869431328269348, 'fraction_numerical': 0.0022582631903099977, 'mean_word_length': 2.7738187451587915, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '852184', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1380, 'n_words': 480}