Python pkg_resources.load_entry_point() Examples

The following are 23 code examples of pkg_resources.load_entry_point(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pkg_resources , or try the search function .
Example #1
Source File: test_cmds.py    From oldvdf-competition with Apache License 2.0 6 votes vote down vote up
def invoke_tool(self, cmd_line):

        # capture io
        stdout_buffer = io.StringIO()
        stderr_buffer = io.StringIO()

        old_stdout = sys.stdout
        old_stderr = sys.stderr

        sys.stdout = stdout_buffer
        sys.stderr = stderr_buffer

        args = shlex.split(cmd_line)
        v = pkg_resources.load_entry_point('inkfish', 'console_scripts',
                                           args[0])(args)

        sys.stdout = old_stdout
        sys.stderr = old_stderr

        return v, stdout_buffer.getvalue(), stderr_buffer.getvalue() 
Example #2
Source File: test_process.py    From coveragepy with Apache License 2.0 6 votes vote down vote up
def test_bug_862(self):
        if env.WINDOWS:
            self.skipTest("Windows can't make symlinks")
        # This simulates how pyenv and pyenv-virtualenv end up creating the
        # coverage executable.
        self.make_file("elsewhere/bin/fake-coverage", """\
            #!{executable}
            import sys, pkg_resources
            sys.exit(pkg_resources.load_entry_point('coverage', 'console_scripts', 'coverage')())
            """.format(executable=sys.executable))
        os.chmod("elsewhere/bin/fake-coverage", stat.S_IREAD | stat.S_IEXEC)
        os.symlink("elsewhere", "somewhere")
        self.make_file("foo.py", "print('inside foo')")
        self.make_file("bar.py", "import foo")
        out = self.run_command("somewhere/bin/fake-coverage run bar.py")
        self.assertEqual("inside foo\n", out) 
Example #3
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_serialise_external(self):

        base = pkg_resources.resource_filename("Mikado.tests", "test_external")
        external_conf = os.path.join(base, "mikado.configuration.testds.yaml")
        external_scores = os.path.join(base, "annotation_run1.metrics.testds.txt")
        fasta = os.path.join(base, "mikado_prepared.testds.fasta")

        for procs in (1, 3):
            with self.subTest(procs=procs):
                dir = tempfile.TemporaryDirectory(suffix="test_serialise_external")
                log = "serialise.log"
                sys.argv = [str(_) for _ in ["mikado", "serialise", "--json-conf", external_conf,
                                             "--transcripts", fasta, "-od", dir.name,
                                             "-l", log,
                                             "--external-scores", external_scores,
                                             "--seed", 10, "--procs", procs, "mikado.db"]]
                log = os.path.join(dir.name, log)
                pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()
                conn = sqlite3.connect(os.path.join(dir.name, "mikado.db"))
                total = conn.execute("SELECT count(*) FROM external").fetchone()[0]
                self.assertEqual(total, 190)
                tot_sources = conn.execute("SELECT count(*) FROM external_sources").fetchone()[0]
                self.assertEqual(tot_sources, 95) 
Example #4
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_convert_from_problematic(self):
        probl = pkg_resources.resource_filename("Mikado.tests", "Chrysemys_picta_bellii_problematic.gff3")
        for outp in ("gtf", "bed12"):
            with self.subTest(outp=outp):
                outfile = tempfile.NamedTemporaryFile(mode="wt")
                outfile.close()
                sys.argv = ["", "util", "convert", "-of", outp, probl, outfile.name]
                # with self.assertRaises(SystemExit):
                pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()
                self.assertGreater(os.stat(outfile.name).st_size, 0)
                lines = [_ for _ in open(outfile.name)]
                self.assertTrue(any(["rna-NC_023890.1:71..1039" in line for line in lines]))
                self.assertTrue(any(["rna-NC_023890.1:1040..1107" in line for line in lines]))
                self.assertTrue(any(["gene-LOC112059550" in line for line in lines]))
                self.assertTrue(any(["id-LOC112059311" in line for line in lines]))


# @mark.slow 
Example #5
Source File: __init__.py    From kansha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def SearchEngine(engine='dummy', **config):
    entry = pkg_resources.load_entry_point(
        'kansha', 'search.engines', engine)
    return entry(**config) 
Example #6
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_problem_grep(self):
        fname = pkg_resources.resource_filename("Mikado.tests", "Chrysemys_picta_bellii_problematic.gff3")
        flist = pkg_resources.resource_filename("Mikado.tests", "Chrysemys_picta_bellii_problematic.list.txt")

        for flag in ("", "-v"):
            with self.subTest(flag=flag):
                form = os.path.splitext(fname)[1]
                outfile = tempfile.NamedTemporaryFile("wt", suffix=form)
                outfile.close()
                self.assertFalse(os.path.exists(outfile.name))
                if flag:
                    sys.argv = ["mikado", "util", "grep", flag, flist, fname, outfile.name]
                else:
                    sys.argv = ["mikado", "util", "grep", flist, fname, outfile.name]
                print(*sys.argv)
                pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()
                self.assertTrue(os.path.exists(outfile.name))
                found = set()

                others = ["NC_023890.1:1..16875"]
                if flag != "-v":
                    for line in pkg_resources.resource_stream("Mikado.tests",
                                                              "Chrysemys_picta_bellii_problematic.list.txt"):
                        rec = line.decode().rstrip().split()[0]
                        print(line, rec)
                        others.append(rec)

                with to_gff(outfile.name, input_format=form[1:]) as stream:
                    for record in stream:
                        if record.feature in ("exon", "CDS"):
                            continue
                        if record.is_transcript:
                            found.add(record.transcript)
                        elif record.feature in ("pseudogene", "region"):
                            found.add(record.id)

                self.assertEqual(len(found), len(others))
                self.assertEqual(found, set(others)) 
Example #7
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_v_grep(self):
        files = [pkg_resources.resource_filename("Mikado.tests", fname) for fname in ["trinity.gtf", "trinity.gff3"]]
        with io.TextIOWrapper(pkg_resources.resource_stream("Mikado.tests", "trinity.ids")) as id_file:
            ids = [tuple(line.rstrip().split("\t")) for line in id_file]

        id_temp_file = tempfile.NamedTemporaryFile("wt", suffix=".txt")
        to_write = [ids[_] for _ in np.random.choice(len(ids), 10, replace=False)]
        others = [_ for _ in ids if _ not in to_write]
        [print(*idline, sep="\t", file=id_temp_file) for idline in to_write]
        id_temp_file.flush()

        for fname in files:
            with self.subTest(fname=fname):
                form = os.path.splitext(fname)[1]
                outfile = tempfile.NamedTemporaryFile("wt", suffix=form)
                outfile.close()
                self.assertFalse(os.path.exists(outfile.name))
                sys.argv = ["mikado", "util", "grep", "-v", id_temp_file.name, fname, outfile.name]
                pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()
                self.assertTrue(os.path.exists(outfile.name))
                found = set()
                with to_gff(outfile.name, input_format=form[1:]) as stream:
                    for record in stream:
                        if record.is_transcript:
                            found.add(record.transcript)
                self.assertEqual(len(found), len(others))
                self.assertEqual(found, set(_[0] for _ in others)) 
Example #8
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_grep(self):
        files = [pkg_resources.resource_filename("Mikado.tests", fname) for fname in ["trinity.gtf", "trinity.gff3"]]
        with io.TextIOWrapper(pkg_resources.resource_stream("Mikado.tests", "trinity.ids")) as id_file:
            ids = [tuple(line.rstrip().split("\t")) for line in id_file]

        id_temp_file = tempfile.NamedTemporaryFile("wt", suffix=".txt")
        to_write = [ids[_] for _ in np.random.choice(len(ids), 10, replace=False)]
        [print(*idline, sep="\t", file=id_temp_file) for idline in to_write]
        id_temp_file.flush()

        for fname in files:
            with self.subTest(fname=fname):
                form = os.path.splitext(fname)[1]
                outfile = tempfile.NamedTemporaryFile("wt", suffix=form)
                outfile.close()
                self.assertFalse(os.path.exists(outfile.name))
                sys.argv = ["mikado", "util", "grep", id_temp_file.name, fname, outfile.name]
                pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()
                self.assertTrue(os.path.exists(outfile.name))
                found = set()
                with to_gff(outfile.name, input_format=form[1:]) as stream:
                    for record in stream:
                        if record.is_transcript:
                            found.add(record.transcript)
                self.assertEqual(len(found), 10)
                self.assertEqual(found, set(_[0] for _ in to_write)) 
Example #9
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_different_scoring(self):

        dir = tempfile.TemporaryDirectory()
        self.json_conf["pick"]["files"]["output_dir"] = os.path.abspath(dir.name)
        self.json_conf["pick"]["files"]["input"] = pkg_resources.resource_filename("Mikado.tests",
                                                                                   "mikado_prepared.gtf")

        self.json_conf["pick"]["files"]["loci_out"] = "mikado.test_diff.loci.gff3"
        self.json_conf["pick"]["files"]["subloci_out"] = "mikado.test_diff.subloci.gff3"
        self.json_conf["pick"]["files"]["monoloci_out"] = "mikado.test_diff.monoloci.gff3"
        self.json_conf["pick"]["files"]["log"] = "mikado.test_diff.log"
        self.json_conf["pick"]["alternative_splicing"]["pad"] = False
        self.json_conf["log_settings"]["log_level"] = "DEBUG"

        self.assertEqual(os.path.basename(self.json_conf["pick"]["scoring_file"]),
                         "plant.yaml")
        shutil.copy(pkg_resources.resource_filename("Mikado.tests", "mikado.db"),
                    os.path.join(self.json_conf["pick"]["files"]["output_dir"], "mikado.db"))
        self.json_conf["db_settings"]["db"] = os.path.join(self.json_conf["pick"]["files"]["output_dir"],
                                                           "mikado.db")
        json_file = os.path.join(self.json_conf["pick"]["files"]["output_dir"], "mikado.yaml")
        with open(json_file, "wt") as json_handle:
            sub_configure.print_config(yaml.dump(self.json_conf, default_flow_style=False), json_handle)
        sys.argv = ["mikado", "pick", "--json-conf", json_file, "--single", "--seed", "1078"]
        with self.assertRaises(SystemExit):
            pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()

        import csv
        with open(os.path.join(self.json_conf["pick"]["files"]["output_dir"], "mikado.test_diff.loci.scores.tsv")) as tsv:
            reader = csv.DictReader(tsv, delimiter="\t")
            score_names = [_ for _ in self.json_conf["scoring"]]
            score_header = [_ for _ in reader.fieldnames if _ not in
                            ("tid", "alias", "parent", "score", "source_score")]
            self.assertEqual(sorted(score_names), sorted(score_header))
        dir.cleanup() 
Example #10
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_convert_from_bam(self):

        bam_inp = pkg_resources.resource_filename("Mikado.tests", "test_mRNA.bam")
        for outp in ("gff3", "gtf", "bed12"):
            with self.subTest(outp=outp):
                outfile = tempfile.NamedTemporaryFile(mode="wt")
                outfile.close()
                sys.argv = ["", "util", "convert", "-of", outp, bam_inp, outfile.name]
                # with self.assertRaises(SystemExit):
                pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()
                self.assertGreater(os.stat(outfile.name).st_size, 0)
                lines = [_ for _ in open(outfile.name)]
                self.assertTrue(any(["TraesCS2B02G055500.1" in line for line in lines])) 
Example #11
Source File: runtests.py    From python-xlib with GNU Lesser General Public License v2.1 5 votes vote down vote up
def tests_run(display, authfile=None):
    pid = os.fork()
    if pid != 0:
        return pid
    if authfile is None:
        authfile = os.devnull
    os.environ['DISPLAY'] = display
    os.environ['XAUTHORITY'] = authfile
    cmd = [
        sys.executable,
        '-c', textwrap.dedent(
            '''
            from pkg_resources import load_entry_point
            sys.exit(load_entry_point(
                'nose', 'console_scripts', 'nosetests',
            )())
            '''
        ).lstrip(),
        '--exe', '--with-xunit', '--verbosity=3',
    ]
    has_custom_tests = False
    for arg in sys.argv[1:]:
        if not arg.startswith('-'):
            has_custom_tests = True
        cmd.append(arg)
    if not has_custom_tests:
        cmd.extend(('test/', 'examples/run_examples.py'))
    print('running tests: `{0}`'.format(' '.join(cmd)))
    sys.argv = cmd
    try:
        load_entry_point('nose', 'console_scripts', 'nosetests')()
    except SystemExit as err:
        code = err.code
    else:
        code = 0
    os._exit(code) 
Example #12
Source File: test_rt.py    From qopen with MIT License 5 votes vote down vote up
def test_script(self):
        self.script = load_entry_point('qopen', 'console_scripts', 'qopen-rt')
        with tempdir():
            with quiet():
                self.cmd('calc 1600 500 -t 5 -r 1000')
                self.cmd('calc 1600 500 -t 5 -r 1000 -a 5000')
                self.cmd('calc-direct 1600 500 -t 5')
                with warnings.catch_warnings():
                    warnings.simplefilter('ignore')
                    self.cmd('plot-t 1600 500 -r 1000')
                    self.cmd('plot-t 1600 500 -r 1000 --no-direct')
                    self.cmd('plot-r 1600 500 -t 0.5 --type rt2d') 
Example #13
Source File: test_xcore.py    From qopen with MIT License 5 votes vote down vote up
def test_entry_point(self):
        script = load_entry_point('qopen', 'console_scripts', 'qopen')
        with quiet():
            try:
                script(['-h'])
            except SystemExit:
                pass 
Example #14
Source File: testzodburi.py    From db with MIT License 5 votes vote down vote up
def parse(uri):
    resolve_uri = pkg_resources.load_entry_point(
        'newt.db', 'zodburi.resolvers', 'newt')
    factory, dbkw = resolve_uri(uri)
    with mock.patch("newt.db.storage") as storage:
        factory()
        (dsn,), options = storage.call_args
        return dsn, options, dbkw 
Example #15
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 4 votes vote down vote up
def test_subprocess(self):
                
        self.json_conf["pick"]["files"]["input"] = pkg_resources.resource_filename("Mikado.tests",
                                                                                   "mikado_prepared.gtf")
        dir = tempfile.TemporaryDirectory()
        self.json_conf["pick"]["files"]["output_dir"] = dir.name
        self.json_conf["pick"]["files"]["loci_out"] = "mikado.subproc.loci.gff3"
        self.json_conf["pick"]["files"]["subloci_out"] = "mikado.subproc.subloci.gff3"
        self.json_conf["pick"]["files"]["monoloci_out"] = "mikado.subproc.monoloci.gff3"
        self.json_conf["pick"]["alternative_splicing"]["pad"] = False
        self.json_conf["pick"]["files"]["log"] = "mikado.subproc.log"
        self.json_conf["db_settings"]["db"] = str(pkg_resources.resource_filename("Mikado.tests", "mikado.db"))
        self.json_conf["log_settings"]["log_level"] = "WARNING"

        for num in (1, 2):
            with self.subTest(num=num):
                self.json_conf["pick"]["threads"] = num
                self.json_conf["pick"]["run_options"]["single_thread"] = (num == 1)
                json_file = os.path.join(dir.name, "mikado.yaml")

                # Printing out would crash without removing these compiled bits
                self.json_conf["requirements"].pop("compiled", None)
                self.json_conf["as_requirements"].pop("compiled", None)
                self.json_conf["not_fragmentary"].pop("compiled", None)

                with open(json_file, "wt") as json_handle:
                    sub_configure.print_config(yaml.dump(self.json_conf, default_flow_style=False), json_handle)

                sys.argv = ["mikado", "pick", "--json-conf", json_file, "--seed", "1078"]
                with self.assertRaises(SystemExit):
                    pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()

                self.assertTrue(os.path.exists(os.path.join(dir.name, "mikado.subproc.loci.gff3")))
                with to_gff(os.path.join(dir.name, "mikado.subproc.loci.gff3")) as inp_gff:
                    lines = [_ for _ in inp_gff if not _.header is True]
                    self.assertGreater(len(lines), 0)
                    self.assertGreater(len([_ for _ in lines if _.is_transcript is True]), 0)
                    self.assertGreater(len([_ for _ in lines if _.feature == "mRNA"]), 0)
                    self.assertGreater(len([_ for _ in lines if _.feature == "CDS"]), 0)
                [os.remove(_) for _ in glob.glob(os.path.join(dir.name, "mikado.subproc.") + "*")]

        dir.cleanup() 
Example #16
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 4 votes vote down vote up
def test_subprocess_shm(self):
        self.json_conf["pick"]["files"]["input"] = pkg_resources.resource_filename("Mikado.tests",
                                                                                   "mikado_prepared.gtf")
        dir = tempfile.TemporaryDirectory()
        self.json_conf["pick"]["files"]["output_dir"] = dir.name
        self.json_conf["pick"]["files"]["loci_out"] = "mikado.subproc.loci.gff3"
        self.json_conf["pick"]["files"]["subloci_out"] = "mikado.subproc.subloci.gff3"
        self.json_conf["pick"]["files"]["monoloci_out"] = "mikado.subproc.monoloci.gff3"
        self.json_conf["pick"]["alternative_splicing"]["pad"] = False
        self.json_conf["pick"]["files"]["log"] = "mikado.subproc.log"
        self.json_conf["db_settings"]["db"] = str(pkg_resources.resource_filename("Mikado.tests", "mikado.db"))
        self.json_conf["log_settings"]["log_level"] = "WARNING"

        for num, shm in itertools.product((1, 2), (True,)):
            with self.subTest(num=num, shm=shm):

                self.json_conf["pick"]["threads"] = num
                self.json_conf["pick"]["run_options"]["single_thread"] = (num == 1)
                json_file = os.path.join(dir.name, "mikado.yaml")

                # Printing out would crash without removing these compiled bits
                self.json_conf["requirements"].pop("compiled", None)
                self.json_conf["as_requirements"].pop("compiled", None)
                self.json_conf["not_fragmentary"].pop("compiled", None)

                with open(json_file, "wt") as json_handle:
                    sub_configure.print_config(yaml.dump(self.json_conf, default_flow_style=False), json_handle)

                log = "pick.log"
                if os.path.exists(os.path.join(dir.name, log)):
                    os.remove(os.path.join(dir.name, log))
                sys.argv = ["mikado", "pick", "--json-conf", json_file, "--seed", "1078", "--log", log]
                if shm is True:
                    sys.argv.append("--shm")
                with self.assertRaises(SystemExit):
                    pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()

                self.assertTrue(os.path.exists(os.path.join(dir.name, "mikado.subproc.loci.gff3")))
                with to_gff(os.path.join(dir.name, "mikado.subproc.loci.gff3")) as inp_gff:
                    lines = [_ for _ in inp_gff if not _.header is True]
                    self.assertGreater(len(lines), 0)
                    self.assertGreater(len([_ for _ in lines if _.is_transcript is True]), 0)
                    self.assertGreater(len([_ for _ in lines if _.feature == "mRNA"]), 0)
                    self.assertGreater(len([_ for _ in lines if _.feature == "CDS"]), 0)
                with open(os.path.join(dir.name, log)) as hlog:
                    log_lines = [_.rstrip() for _ in hlog]
                if shm is True:
                    self.assertTrue(any("Copying Mikado database into a SHM db" in _ for _ in log_lines))

                [os.remove(_) for _ in glob.glob(os.path.join(dir.name, "mikado.subproc.") + "*")]

        dir.cleanup() 
Example #17
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 4 votes vote down vote up
def test_subprocess_single(self):

        xml = pkg_resources.resource_filename("Mikado.tests", "chunk-001-proteins.xml.gz")
        transcripts = pkg_resources.resource_filename("Mikado.tests", "mikado_prepared.fasta")
        junctions = pkg_resources.resource_filename("Mikado.tests", "junctions.bed")
        orfs = pkg_resources.resource_filename("Mikado.tests", "transcripts.fasta.prodigal.gff3")
        uniprot = pkg_resources.resource_filename("Mikado.tests", "uniprot_sprot_plants.fasta.gz")
        mobjects = 300  # Let's test properly the serialisation for BLAST

        dir = tempfile.TemporaryDirectory()
        json_file = os.path.join(dir.name, "mikado.yaml")
        db = os.path.join(dir.name, "mikado.db")
        log = os.path.join(dir.name, "serialise.log")
        uni_out = os.path.join(dir.name, "uniprot_sprot_plants.fasta")
        with gzip.open(uniprot, "rb") as uni, open(uni_out, "wb") as uni_out_handle:
            uni_out_handle.write(uni.read())

        with open(json_file, "wt") as json_handle:
            sub_configure.print_config(yaml.dump(self.json_conf, default_flow_style=False),
                                                      json_handle)
        # Set up the command arguments
        for procs in (1,):
            with self.subTest(proc=procs):
                sys.argv = [str(_) for _ in ["mikado", "serialise", "--json-conf", json_file,
                            "--transcripts", transcripts, "--blast_targets", uni_out,
                            "--orfs", orfs, "--junctions", junctions, "--xml", xml, "-od", dir.name,
                            "-p", procs, "-mo", mobjects, "--log", os.path.basename(log), "--seed", "1078",
                                             os.path.basename(db)]]
                pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()
                logged = [_.rstrip() for _ in open(log)]

                self.assertTrue(os.path.exists(db))
                conn = sqlite3.connect(db)
                cursor = conn.cursor()
                self.assertEqual(cursor.execute("select count(*) from hit").fetchall()[0][0], 562, logged)
                self.assertEqual(cursor.execute("select count(*) from hsp").fetchall()[0][0], 669)
                self.assertEqual(cursor.execute("select count(distinct(query_id)) from hsp").fetchall()[0][0], 71)
                self.assertEqual(cursor.execute("select count(distinct(query_id)) from hit").fetchall()[0][0], 71)
                self.assertEqual(cursor.execute("select count(distinct(target_id)) from hsp").fetchall()[0][0], 32)
                self.assertEqual(cursor.execute("select count(distinct(target_id)) from hit").fetchall()[0][0], 32)
                self.assertEqual(cursor.execute("select count(*) from junctions").fetchall()[0][0], 372)
                self.assertEqual(cursor.execute("select count(distinct(chrom_id)) from junctions").fetchall()[0][0], 2)
                self.assertEqual(cursor.execute("select count(*) from orf").fetchall()[0][0], 168,
                                 "\n".join(logged))
                self.assertEqual(cursor.execute("select count(distinct(query_id)) from orf").fetchall()[0][0], 81)
                os.remove(db)
        dir.cleanup() 
Example #18
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 4 votes vote down vote up
def test_subprocess_multi(self):

        xml = pkg_resources.resource_filename("Mikado.tests", "chunk-001-proteins.xml.gz")
        transcripts = pkg_resources.resource_filename("Mikado.tests", "mikado_prepared.fasta")
        junctions = pkg_resources.resource_filename("Mikado.tests", "junctions.bed")
        orfs = pkg_resources.resource_filename("Mikado.tests", "transcripts.fasta.prodigal.gff3")
        uniprot = pkg_resources.resource_filename("Mikado.tests", "uniprot_sprot_plants.fasta.gz")
        mobjects = 300  # Let's test properly the serialisation for BLAST

        # Set up the command arguments
        for procs in (1, 3,):
            with self.subTest(proc=procs):
                dir = tempfile.TemporaryDirectory(suffix="test_subprocess_multi_{}".format(procs))
                json_file = os.path.join(dir.name, "mikado.yaml")
                db = os.path.join(dir.name, "mikado.db")
                log = os.path.join(dir.name, "serialise.log")
                uni_out = os.path.join(dir.name, "uniprot_sprot_plants.fasta")
                with gzip.open(uniprot, "rb") as uni, open(uni_out, "wb") as uni_out_handle:
                    uni_out_handle.write(uni.read())

                with open(json_file, "wt") as json_handle:
                    sub_configure.print_config(yaml.dump(self.json_conf, default_flow_style=False),
                                               json_handle)
                sys.argv = [str(_) for _ in ["mikado", "serialise", "--json-conf", json_file,
                            "--transcripts", transcripts, "--blast_targets", uni_out,
                            "--orfs", orfs, "--junctions", junctions, "--xml", xml, "-od", dir.name,
                            "-p", procs, "-mo", mobjects, "--log", os.path.basename(log),
                                             "--seed", "1078", os.path.basename(db)]]
                pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()
                logged = [_.rstrip() for _ in open(log)]

                self.assertTrue(os.path.exists(db))
                conn = sqlite3.connect(db)
                cursor = conn.cursor()
                self.assertEqual(cursor.execute("select count(*) from hit").fetchall()[0][0], 562, logged)
                self.assertEqual(cursor.execute("select count(*) from hsp").fetchall()[0][0], 669)
                self.assertEqual(cursor.execute("select count(distinct(query_id)) from hsp").fetchall()[0][0], 71)
                self.assertEqual(cursor.execute("select count(distinct(query_id)) from hit").fetchall()[0][0], 71)
                self.assertEqual(cursor.execute("select count(distinct(target_id)) from hsp").fetchall()[0][0], 32)
                self.assertEqual(cursor.execute("select count(distinct(target_id)) from hit").fetchall()[0][0], 32)
                self.assertEqual(cursor.execute("select count(*) from junctions").fetchall()[0][0], 372)
                self.assertEqual(cursor.execute("select count(distinct(chrom_id)) from junctions").fetchall()[0][0], 2)
                self.assertEqual(cursor.execute("select count(*) from orf").fetchall()[0][0], 168,
                                 "\n".join(logged))
                self.assertEqual(cursor.execute("select count(distinct(query_id)) from orf").fetchall()[0][0], 81)
                os.remove(db)
                dir.cleanup() 
Example #19
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 4 votes vote down vote up
def test_xml_vs_tsv(self):
        xml = pkg_resources.resource_filename("Mikado.tests", os.path.join("blast_data", "diamond.0.9.30.xml.gz"))
        tsv = pkg_resources.resource_filename("Mikado.tests", os.path.join("blast_data", "diamond.0.9.30.tsv.gz"))
        queries = pkg_resources.resource_filename("Mikado.tests", os.path.join("blast_data", "transcripts.fasta"))
        prots = pkg_resources.resource_filename("Mikado.tests", "uniprot_sprot_plants.fasta.gz")
        logs = dict()
        dbs = dict()
        base = tempfile.TemporaryDirectory()
        for name, blast in zip(["xml", "tsv"], [xml, tsv]):
            db = "{}.db".format(name)
            log = "{}.log".format(name)
            sys.argv = [str(_) for _ in ["mikado", "serialise", "-od", base.name,
                                         "--transcripts", queries, "--blast_targets", prots,
                                         "--xml", xml, "-mo", 1000, "--log", log, "--seed", "1078",
                                         db]]
            pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()
            dbs[name] = os.path.join(base.name, db)
            logged = [_.rstrip() for _ in open(os.path.join(base.name, log))]
            logs[name] = logged

        def prep_dbs(name):
            hsp, hit, query, target = [pd.read_sql(table, name) for table in ["hsp", "hit", "query", "target"]]
            hit = hit.join(target.set_index("target_id"), on=["target_id"], how="inner").join(
                query.set_index("query_id"), on=["query_id"], how="inner")
            hsp = hsp.join(target.set_index("target_id"), on=["target_id"], how="inner").join(
                query.set_index("query_id"), on=["query_id"], how="inner")
            hsp.set_index(["query_name", "target_name", "counter"], inplace=True)
            hit.set_index(["query_name", "target_name"], inplace=True)
            return hit, hsp

        xml_hit, xml_hsp = prep_dbs("sqlite:///" + dbs["xml"])
        tsv_hit, tsv_hsp = prep_dbs("sqlite:///" + dbs["tsv"])
        hit = pd.merge(xml_hit, tsv_hit, left_index=True, right_index=True, suffixes=("_xml", "_tsv"))
        hsp = pd.merge(xml_hsp, tsv_hsp, left_index=True, right_index=True, suffixes=("_xml", "_tsv"))
        self.assertTrue(hit.shape[0] == xml_hit.shape[0] == tsv_hit.shape[0] > 0)
        self.assertTrue(hsp.shape[0] == xml_hsp.shape[0] == tsv_hsp.shape[0] > 0)
        # Get the columns
        hitcols, hspcols = dict(), dict()
        for d, df in zip([hitcols, hspcols], [hit, hsp]):
            for col in df.columns:
                name = col[:-4]
                if name not in d:
                    d[name] = []
                d[name].append(col)
            failed = []
            for col in d:
                if col in ("query_id", "target_id"):
                    continue
                catch = df[d[col]].apply(lambda row: row[0] == row[1] or
                                                     np.isclose(row[0], row[1], atol=.01, rtol=.01), axis=1)
                if not (catch).all():
                    failed.append(col)
            self.assertEqual(len(failed), 0, failed) 
Example #20
Source File: test_system_calls.py    From mikado with GNU Lesser General Public License v3.0 4 votes vote down vote up
def test_subprocess_multi_empty_orfs(self):

        xml = pkg_resources.resource_filename("Mikado.tests", "chunk-001-proteins.xml.gz")
        transcripts = pkg_resources.resource_filename("Mikado.tests", "mikado_prepared.fasta")
        junctions = pkg_resources.resource_filename("Mikado.tests", "junctions.bed")
        # orfs = pkg_resources.resource_filename("Mikado.tests", "transcripts.fasta.prodigal.gff3")
        tmp_orf = tempfile.NamedTemporaryFile(suffix=".bed12")
        tmp_orf.write(b"#track\n")
        tmp_orf.write(
            b"cufflinks_star_at.23553.1\t0\t1733\tID=1_1;partial=01;start_type=ATG\t0\t+\t312\t1733\t0,0,0\t1\t1733\t0\n")
        tmp_orf.flush()
        uniprot = pkg_resources.resource_filename("Mikado.tests", "uniprot_sprot_plants.fasta.gz")
        mobjects = 300  # Let's test properly the serialisation for BLAST

        # Set up the command arguments
        for procs in (3, 1):
            with self.subTest(procs=procs):
                dir = tempfile.TemporaryDirectory(prefix="has_to_fail")
                json_file = os.path.join(dir.name, "mikado.yaml")
                db = os.path.join(dir.name, "mikado.db")
                log = "failed_serialise.log"
                uni_out = os.path.join(dir.name, "uniprot_sprot_plants.fasta")
                self.json_conf["serialise"]["files"]["log"] = os.path.basename(log)
                self.json_conf["multiprocessing_method"] = "fork"
                with gzip.open(uniprot, "rb") as uni, open(uni_out, "wb") as uni_out_handle:
                    uni_out_handle.write(uni.read())

                with open(json_file, "wt") as json_handle:
                    sub_configure.print_config(yaml.dump(self.json_conf, default_flow_style=False), json_handle)
                with self.subTest(proc=procs):
                    sys.argv = [str(_) for _ in ["mikado", "serialise", "--json-conf", json_file,
                                                 "--transcripts", transcripts, "--blast_targets", uni_out,
                                                 "--log", log,
                                                 "-od", dir.name,
                                                 "--orfs", tmp_orf.name, "--junctions", junctions, "--xml", xml,
                                                 "-p", procs, "-mo", mobjects, db,
                                                 "--seed", "1078"]]
                    log = os.path.join(dir.name, log)
                    with self.assertRaises(SystemExit):
                        pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()
                    self.assertTrue("failed" in log)
                    self.assertTrue(os.path.exists(log), log)
                    self.assertTrue(os.stat(log).st_size > 0, log)
                    logged = [_.rstrip() for _ in open(log)]
                    self.assertGreater(len(logged), 0)
                    self.assertFalse(os.path.exists(db), logged)
                    self.assertTrue(any(
                        "Mikado serialise failed due to problems with the input data. Please check the logs." in line
                        for line in logged))
                    self.assertTrue(any(
                        "The provided ORFs do not match the transcripts provided and already present in the database."
                        in line for line in logged),
                    print("\n".join(logged)))
                dir.cleanup() 
Example #21
Source File: util.py    From Flask-P2P with MIT License 4 votes vote down vote up
def load_class(uri, default="gunicorn.workers.sync.SyncWorker",
        section="gunicorn.workers"):
    if inspect.isclass(uri):
        return uri
    if uri.startswith("egg:"):
        # uses entry points
        entry_str = uri.split("egg:")[1]
        try:
            dist, name = entry_str.rsplit("#", 1)
        except ValueError:
            dist = entry_str
            name = default

        try:
            return pkg_resources.load_entry_point(dist, section, name)
        except:
            exc = traceback.format_exc()
            msg = "class uri %r invalid or not found: \n\n[%s]"
            raise RuntimeError(msg % (uri, exc))
    else:
        components = uri.split('.')
        if len(components) == 1:
            while True:
                if uri.startswith("#"):
                    uri = uri[1:]

                if uri in SUPPORTED_WORKERS:
                    components = SUPPORTED_WORKERS[uri].split(".")
                    break

                try:
                    return pkg_resources.load_entry_point("gunicorn",
                                section, uri)
                except:
                    exc = traceback.format_exc()
                    msg = "class uri %r invalid or not found: \n\n[%s]"
                    raise RuntimeError(msg % (uri, exc))

        klass = components.pop(-1)

        try:
            mod = import_module('.'.join(components))
        except:
            exc = traceback.format_exc()
            msg = "class uri %r invalid or not found: \n\n[%s]"
            raise RuntimeError(msg % (uri, exc))
        return getattr(mod, klass) 
Example #22
Source File: __main__.py    From pifpaf with Apache License 2.0 4 votes vote down vote up
def get_command(self, ctx, name):
        params = [click.Argument(["command"], nargs=-1)]
        plugin = pkg_resources.load_entry_point(
            "pifpaf", "pifpaf.daemons", name)
        params.extend(map(lambda kw: click.Option(**kw), plugin.get_options()))

        def _run_cb(*args, **kwargs):
            return self._run(name, plugin, ctx, *args, **kwargs)

        return click.Command(name=name, callback=_run_cb, params=params) 
Example #23
Source File: util.py    From jbox with MIT License 4 votes vote down vote up
def load_class(uri, default="gunicorn.workers.sync.SyncWorker",
        section="gunicorn.workers"):
    if inspect.isclass(uri):
        return uri
    if uri.startswith("egg:"):
        # uses entry points
        entry_str = uri.split("egg:")[1]
        try:
            dist, name = entry_str.rsplit("#", 1)
        except ValueError:
            dist = entry_str
            name = default

        try:
            return pkg_resources.load_entry_point(dist, section, name)
        except:
            exc = traceback.format_exc()
            msg = "class uri %r invalid or not found: \n\n[%s]"
            raise RuntimeError(msg % (uri, exc))
    else:
        components = uri.split('.')
        if len(components) == 1:
            while True:
                if uri.startswith("#"):
                    uri = uri[1:]

                if uri in SUPPORTED_WORKERS:
                    components = SUPPORTED_WORKERS[uri].split(".")
                    break

                try:
                    return pkg_resources.load_entry_point("gunicorn",
                                section, uri)
                except:
                    exc = traceback.format_exc()
                    msg = "class uri %r invalid or not found: \n\n[%s]"
                    raise RuntimeError(msg % (uri, exc))

        klass = components.pop(-1)

        try:
            mod = import_module('.'.join(components))
        except:
            exc = traceback.format_exc()
            msg = "class uri %r invalid or not found: \n\n[%s]"
            raise RuntimeError(msg % (uri, exc))
        return getattr(mod, klass)