Example #1
0
def create_test_suite(module_names,
                      base_path,
                      overwrite=False,
                      printer=NullWriter()):
    """Creates a :py:class:`unittest.TestCase` for the ``main()`` method of
    a command-line python module, and write these to modules
    
    Parameters
    ----------
    module_names : list
        List of module names, as strings, to create :py:class:`~unittest.TestCase` s for
    
    base_path : str
        Path where test modules will be written
    
    overwrite : bool, optional
        If True, existing files will be overwritten (Default: False)
    
    printer : file-like, optional
        Logger to which output will be written
    """
    for module_name in module_names:
        printer.write(module_name)
        short_name = get_short_name(module_name, separator="\.", terminator="")
        output_str = TEST_TEMPLATE.replace("${MODULE}", module_name)
        output_str = output_str.replace("${CAP_SHORT_NAME}",
                                        short_name.capitalize())
        output_str = output_str.replace("${SHORT_NAME}", short_name)
        output_fn = os.path.join(base_path, "test_%s.py" % short_name)
        if overwrite or not os.path.exists(output_fn):
            printer.write("Writing %s" % output_fn)
            fout = open(output_fn, "w")
            fout.write(output_str)
            fout.close()
Example #2
0
def create_test_suite(module_names,base_path,overwrite=False,printer=NullWriter()):
    """Creates a :py:class:`unittest.TestCase` for the ``main()`` method of
    a command-line python module, and write these to modules
    
    Parameters
    ----------
    module_names : list
        List of module names, as strings, to create :py:class:`~unittest.TestCase` s for
    
    base_path : str
        Path where test modules will be written
    
    overwrite : bool, optional
        If True, existing files will be overwritten (Default: False)
    
    printer : file-like, optional
        Logger to which output will be written
    """
    for module_name in module_names:
        printer.write(module_name)
        short_name = get_short_name(module_name,separator="\.",terminator="")
        output_str = TEST_TEMPLATE.replace("${MODULE}",module_name)
        output_str = output_str.replace("${CAP_SHORT_NAME}",short_name.capitalize())
        output_str = output_str.replace("${SHORT_NAME}",short_name)
        output_fn  = os.path.join(base_path,"test_%s.py" % short_name)
        if overwrite or not os.path.exists(output_fn):
            printer.write("Writing %s" % output_fn)
            fout = open(output_fn,"w")
            fout.write(output_str)
            fout.close()
Example #3
0
def test_get_short_name():
    tests = [("test", "test", {}), ("test.py", "test", dict(terminator=".py")),
             ("/home/jdoe/test.py", "test", dict(terminator=".py")),
             ("/home/jdoe/test.py.py", "test.py", dict(terminator=".py")),
             ("/home/jdoe/test.py.2", "test.py.2", {}),
             ("/home/jdoe/test.py.2", "test.py.2", dict(terminator=".py")),
             ("plastid.bin.test", "test", dict(separator="\.", terminator=""))]
    for inp, expected, kwargs in tests:
        found = get_short_name(inp, **kwargs)
        msg = "test_get_short_name(): failed on input '%s'. Expected '%s'. Got '%s'" % (
            inp, expected, found)
        yield assert_equal, expected, found, msg
Example #4
0
def get_short_samplename(inp):
    """Creates a sample legend label from a sample filename by removing everything including and after ``".txt"``

    Parameters
    ----------
    inp : str
        filename
        
    Returns
    -------
    str
    """
    return get_short_name(inp, separator=os.path.sep, terminator=".txt")
Example #5
0
def test_get_short_name():
    tests = [("test","test",{}),
             ("test.py","test",dict(terminator=".py")),
             ("/home/jdoe/test.py","test",dict(terminator=".py")),
             ("/home/jdoe/test.py.py","test.py",dict(terminator=".py")),
             ("/home/jdoe/test.py.2","test.py.2",{}),
             ("/home/jdoe/test.py.2","test.py.2",dict(terminator=".py")),
             ("plastid.bin.test","test",dict(separator="\.",terminator=""))
             ]
    for inp, expected, kwargs in tests:
        found = get_short_name(inp,**kwargs)
        msg = "test_get_short_name(): failed on input '%s'. Expected '%s'. Got '%s'" % (inp,expected,found)
        yield assert_equal, expected, found, msg
Example #6
0
import glob
import numpy as np

from plastid.genomics.genome_array import BAMGenomeArray
from plastid.util.io.openers import get_short_name
from plastid.util.io.filters import NameDateWriter
from plastid.util.scriptlib.help_formatters import format_module_docstring
from plastid.genomics.roitools import SegmentChain
from plastid.readers.bed import BED_Reader

# ignore and do not print any occurences of matching warnings
warnings.simplefilter("ignore")

# A stream to which stderr-like info can be written
# when printed in terminal, includes the script name, time & date
printer = NameDateWriter(get_short_name(inspect.stack()[-1][1]))


# create list of tuples [(SegmentChain, counts)]
def getCountVectorData(bedfile, bamList, bamIDs):

    # read BED file to iterator of SegmentChain objects
    bed_segmentChains = list(BED_Reader(open(bedfile)))

    # create list of tuples (SegmentChain, [count_vectors/BAM])
    countVectorData = []

    # iterate through ROIs
    for index, segment in enumerate(bed_segmentChains):

        # prints in terminal progress every 100 ROIs processed
found in a `GFF3`_ file. Features with multiple parents are sorted into a category
called `Multiple`.
"""
from plastid.readers.gff import GFF3_Reader
from plastid.util.io.filters import NameDateWriter
from plastid.util.io.openers import get_short_name, opener, argsopener
from plastid.util.scriptlib.help_formatters import format_module_docstring
from plastid.util.scriptlib.argparsers import BaseParser
from collections import Counter
import argparse
import sys
import inspect
import warnings

warnings.simplefilter("once")
printer = NameDateWriter(get_short_name(inspect.stack()[-1][1]))

def main(argv=sys.argv[1:]):
	"""Command-line program
	
	Parameters
	----------
	argv : list, optional
		A list of command-line arguments, which will be processed
		as if the script were called from the command line if
		:py:func:`main` is called directly.

		Default: `sys.argv[1:]`. The command-line arguments, if the script is
		invoked from the command line
	"""
	bp = BaseParser()