Mercurial > repos > sanbi-uwc > confil
comparison confil/kraken.py @ 12:5ec97cccb3fe draft
planemo upload for repository https://github.com/COMBAT-TB/confil commit b1a96c1b50cea70a20d4e606100879da8b6ed1b2
author | sanbi-uwc |
---|---|
date | Mon, 11 Mar 2019 07:18:34 -0400 |
parents | |
children | bbf9ab2ebee7 |
comparison
equal
deleted
inserted
replaced
11:72cb19a32d8b | 12:5ec97cccb3fe |
---|---|
1 import distutils.spawn | |
2 import os | |
3 import re | |
4 from shlex import split | |
5 from subprocess import PIPE, Popen | |
6 | |
7 import click | |
8 | |
9 from .report import parse_report | |
10 | |
11 OUT_DIR = os.path.abspath(os.curdir) | |
12 | |
13 | |
14 def kraken_installed(): | |
15 # check if `kraken2` is in path | |
16 installed = distutils.spawn.find_executable("kraken2") | |
17 if not installed: | |
18 raise OSError("kraken2 is not installed.") | |
19 return installed | |
20 | |
21 | |
22 def run_kraken(db, threads, cutoff, paired, seqfiles): | |
23 # Using the sample name to track report | |
24 seq_name = [os.path.splitext(os.path.basename(seq))[0] | |
25 for seq in seqfiles][0] | |
26 # remove _ and numbers | |
27 seq_name = re.sub('_[0-9]+$', '', seq_name) | |
28 # building cmd | |
29 cmd = "kraken2 --threads {threads} --db {db} --output {seq_name}.out --report {seq_name}.tab ".format( | |
30 threads=threads, db=db, seq_name=seq_name) | |
31 if paired: | |
32 cmd += "--paired --classified-out {}_cseqs#.fq ".format(seq_name) | |
33 cmd += "{seqfiles}".format(seqfiles=' '.join(seqfiles)) | |
34 click.secho("Executing kraken2: \n{}\n".format( | |
35 split(cmd)), fg='bright_yellow') | |
36 | |
37 # TODO: remove | |
38 # test_file = "https://raw.githubusercontent.com/COMBAT-TB/confil/master/test/test_data/test_file.tab" | |
39 # out_file = os.path.join(OUT_DIR, "{}.tab".format(seq_name)) | |
40 # mock_cmd = 'wget {} -O {}'.format(test_file, out_file) | |
41 # cmd = mock_cmd | |
42 # click.secho("Executing mock_cmd: \n{}\n".format(split(cmd)), fg='red') | |
43 | |
44 p = Popen(split(cmd), stdout=PIPE, stderr=PIPE, close_fds=True) | |
45 while True: | |
46 output = p.stdout.readline() | |
47 if output == '' and p.poll() is not None: | |
48 break | |
49 if output: | |
50 click.echo(output) | |
51 returncode = p.poll() | |
52 if returncode != 0: | |
53 error = p.stderr.readline() | |
54 raise OSError("Kraken2 launch error:\n{}\n".format(error)) | |
55 # parse kraken report | |
56 report_file = os.path.join(OUT_DIR, "{}.tab".format(seq_name)) | |
57 parse_report(report_file=report_file, cutoff=cutoff) | |
58 return returncode |