Skip to content

Commit

Permalink
added in checks and tb-profiler installation
Browse files Browse the repository at this point in the history
  • Loading branch information
kristyhoran committed Aug 7, 2023
1 parent 04d5e6d commit 5329ee2
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 14 deletions.
17 changes: 4 additions & 13 deletions tbtamr/RunProfiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections import namedtuple
from tbtamr.CustomLog import logger
from tbtamr.TbTamr import Tbtamr

from tbtamr.TbTamr_Utils import check

class RunProfiler(Tbtamr):
"""
Expand Down Expand Up @@ -77,18 +77,9 @@ def _batch_collate(self, input_data):
return cmd

def _check_tbprofiler(self):
version_pat_3 = re.compile(r'\bv?(?P<major>[0-9]+)\.(?P<minor>[0-9]+)(?:\.(?P<release>[0-9]+)*)?(?:\.(?P<build>[0-9]+)*)?\b')
p = subprocess.run(f"tb-profiler version", capture_output=True, encoding = "utf-8", shell = True)
p = p.stdout
v = version_pat_3.search(p.strip())
if v:
v = v.group(0)
logger.info(f"TB Profiler version {v} detected.")
else:
logger.critical(f"It seems something is not quite right with your TB profiler installation. Please check your installation and try again.")
raise SystemExit
return True


return check()


def _run(self):
"""
Expand Down
64 changes: 64 additions & 0 deletions tbtamr/TbTamr_Utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

import pathlib, subprocess, re,json
from collections import namedtuple
from tbtamr.CustomLog import logger
# from tbtamr.RunProfiler import RunProfiler

# version_pat_3 =

def _run_cmd(cmd):
p = subprocess.run(f"{cmd}", capture_output=True, encoding = "utf-8", shell = True)
return p

def version_pattern():
return re.compile(r'\bv?(?P<major>[0-9]+)\.(?P<minor>[0-9]+)(?:\.(?P<release>[0-9]+)*)?(?:\.(?P<build>[0-9]+)*)?\b')

def _run_check(_input, sft):
p = _run_cmd(cmd=_input)
_str = p.stdout
v = version_pattern().search(_str.strip())
if v:
v = v.group(0)
logger.info(f"{sft} version {v} detected.")
elif sft == "snpEff" and p.returncode == 0:
logger.info(f"{' '.join(p.stdout.split())} detected.")
elif sft == "tb-profiler":
logger.warning(f"It seems that {sft} is not installed correctly.")
res = input("Would you like to install it now? (y/n): ")
if res.lower() == 'y':
_install_tbprofiler()
elif res.lower() == 'n':
logger.critical(f"{sft} is required - please run 'tbtamr setup' now to proceed.")
raise SystemExit
else:
logger.critical(f"{sft} is not installed.")
raise SystemExit

def check_deps():

with open(f"{pathlib.Path(__file__).parent / 'dep_config.json'}", "r") as j:

software = json.load(j)
logger.info(f"Checking that dependencies are installed and recording version.")

for sft in software:
_run_check(_input =software[sft], sft = sft)

return True

def _install_tbprofiler():

pp = _run_cmd(cmd="pip3 install git+https://github.com/MDU-PHL/pathogen-profiler")
if pp.returncode == 0:
logger.info(f"pathogen-profiler from https://github.com/MDU-PHL/ installed.")
tbp = _run_cmd(cmd = "pip3 install git+https://github.com/MDU-PHL/TBProfiler")
if tbp.returncode == 0:
logger.info(f"TB-profiler https://github.com/MDU-PHL/ installed. You should now be good to go.")

def check():

check_deps()
return True

def install():
_install_tbprofiler()
12 changes: 12 additions & 0 deletions tbtamr/dep_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"tb-profiler": "tb-profiler version 2>&1",
"samtools": "samtools --version 2>&1",
"bwa":"bwa 2>&1",
"snpEff":"snpEff -version 2>&1",
"samclip":"samclip --version 2>&1",
"freebayes":"freebayes --version 2>&1",
"bcftools":"bcftools --version 2>&1",
"bedtools":"bedtools --version 2>&1",
"delly":"delly --version 2>&1"
}

12 changes: 11 additions & 1 deletion tbtamr/tbtamr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
from tbtamr.AmrSetup import AmrSetup
from tbtamr.RunProfiler import RunProfiler
from tbtamr.Collate import Inferrence, Parse, Mdu

from tbtamr.TbTamr_Utils import check,install
from tbtamr.version import __version__

"""
tbtamr is designed to implement TB-profiler and parse the results compatible for MDU use. It may also be used for other purposes where the format of output is compatible
"""

def install_deps(args):
install()

def check_deps(args):
check()

def collate_results(args):

to_collate = Parse(args)
Expand Down Expand Up @@ -186,10 +192,14 @@ def set_parsers():
default= 80
)

parser_setup_deps = subparsers.add_parser('setup', help='Install tbtamr dependencies.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser_check_deps = subparsers.add_parser('check', help='Check tbtamr dependencies.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser_sub_run.set_defaults(func=run_pipeline)
parser_sub_collate.set_defaults(func = collate_results)
parser_sub_mdu.set_defaults(func = mdu)
parser_setup_deps.set_defaults(func = install_deps)
parser_check_deps.set_defaults(func = check_deps)
args = parser.parse_args(args=None if sys.argv[1:] else ['--help'])
return args

Expand Down

0 comments on commit 5329ee2

Please sign in to comment.