Skip to content

Commit

Permalink
fix(retropath2): adapted for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-gricourt committed Nov 16, 2022
1 parent daa032a commit 1fd3d8e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 43 deletions.
23 changes: 15 additions & 8 deletions retropath2_wrapper/RetroPath2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@description: Python wrapper to run RetroPath2.0 KNIME workflow
"""
import os
from os import (
mkdir as os_mkdir,
path as os_path,
Expand Down Expand Up @@ -116,6 +117,13 @@ def set_vars(
kpath = kexec[:kexec.rfind('/')]
kinstall = kpath[:kpath.rfind('/')]

workflow = os_path.join(
here, 'workflows', f'RetroPath2.0_{rp2_version}.knwf'
)

if sys_platform == 'win32':
workflow = "/".join(workflow.split(os.sep))

# Build a dict to store KNIME vars
return {
'kexec' : kexec,
Expand All @@ -124,11 +132,7 @@ def set_vars(
'kpath' : kpath,
'kinstall' : kinstall,
'kpkg_install' : kpkg_install,
'workflow' : os_path.join(
here,
'workflows',
f'RetroPath2.0_{rp2_version}.knwf'
)
'workflow' : workflow,
}


Expand Down Expand Up @@ -466,7 +470,7 @@ def install_knime(

logger.info(' |--url: '+kurl)
logger.info(' |--install_dir: '+kinstall)


def gunzip_to_csv(filename: str, indir: str) -> str:
"""
Expand Down Expand Up @@ -547,6 +551,9 @@ def format_files_for_knime(
copyfile(files[key], new_f)
files[key] = new_f

if sys_platform == 'win32':
for key, value in files.items():
files[key] = "/".join(value.split(os.sep))
return files


Expand Down Expand Up @@ -581,7 +588,7 @@ def install_knime_pkgs(
args = [kexec]
args += ['-application', 'org.eclipse.equinox.p2.director']
args += ['-nosplash']
args += ['-consolelog']
args += ['-consoleLog']
args += ['-r', 'http://update.knime.org/community-contributions/trunk,' \
+ 'http://update.knime.com/community-contributions/trusted/'+kver[:3]+',' \
+ 'http://update.knime.com/analytics-platform/'+kver[:3]]
Expand Down Expand Up @@ -626,7 +633,7 @@ def call_knime(
StreamHandler.terminator = ""
logger.info('{attr1}Running KNIME...{attr2}'.format(attr1=attr('bold'), attr2=attr('reset')))

args = ' -nosplash -nosave -reset --launcher.suppressErrors -application org.knime.product.KNIME_BATCH_APPLICATION ' \
args = ' -nosplash -nosave -reset -consoleLog --launcher.suppressErrors -application org.knime.product.KNIME_BATCH_APPLICATION ' \
+ ' -workflowFile=' + kvars['workflow'] \
+ ' -workflow.variable=input.dmin,"' + str(params['dmin']) + '",int' \
+ ' -workflow.variable=input.dmax,"' + str(params['dmax']) + '",int' \
Expand Down
20 changes: 10 additions & 10 deletions tests/functional/test_install_knime.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ def setUp(self):
self.tempdir = tempfile.mkdtemp()

def tearDown(self):
try:
os.remove(self.tempdir)
except:
pass
shutil.rmtree(self.tempdir, ignore_errors=True)

def test_install_knime(self):
def install_knime(self):
install_knime(
kinstall=self.tempdir,
kver=DEFAULT_KNIME_VERSION,
Expand All @@ -34,11 +31,14 @@ def test_install_knime(self):
"-consolelog",
"-help",
]
for x in glob.glob(os.path.join(self.tempdir, "**", "knime"), recursive=True):
ret = subprocess.run([x] + args)
if ret.returncode == 0:
kexec = x
break
for x in glob.glob(os.path.join(self.tempdir, "**", "knime*"), recursive=True):
try:
ret = subprocess.run([x] + args)
if ret.returncode == 0:
kexec = x
break
except:
pass
self.assertIsNot(kexec, None)
return kexec

Expand Down
46 changes: 27 additions & 19 deletions tests/functional/test_retropath2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import filecmp
import os
import shutil
import tempfile

from retropath2_wrapper.__main__ import create_logger
Expand All @@ -18,27 +19,34 @@ def setUp(self):
self.logger = create_logger(__name__, 'DEBUG')

def test_src_in_sink(self):
with tempfile.TemporaryDirectory() as tmpdir:
r_code, result = retropath2(
sink_file=self.lycopene_sink_csv,
source_file=self.source_mnxm790_csv,
rules_file=self.rules_csv,
outdir=tmpdir,
logger=self.logger,
)
self.assertEqual(r_code, RETCODES['SrcInSink'])
tmpdir = tempfile.mkdtemp()
r_code, result = retropath2(
sink_file=self.lycopene_sink_csv,
source_file=self.source_mnxm790_csv,
rules_file=self.rules_csv,
outdir=tmpdir,
logger=self.logger,
)
self.assertEqual(r_code, RETCODES['SrcInSink'])
shutil.rmtree(tmpdir, ignore_errors=True)

def test_lycopene(self):
with tempfile.TemporaryDirectory() as tempd:
r_code, result = retropath2(
sink_file=self.lycopene_sink_csv,
source_file=self.lycopene_source_csv,
rules_file=self.rulesd12_csv,
outdir=tempd,
)
self.assertTrue(
filecmp.cmp(os.path.join(result['outdir'], result['results']), self.lycopene_r20220104_results_csv)
)
tmpdir = tempfile.mkdtemp()
r_code, result = retropath2(
sink_file=self.lycopene_sink_csv,
source_file=self.lycopene_source_csv,
rules_file=self.rulesd12_csv,
outdir=tmpdir,
logger=self.logger,
)
# Compare number of lines to have a robust test accross platforms
with open(result['outdir'] + "/" + result['results']) as fid:
result_lines = fid.read().splitlines()
with open(self.lycopene_r20220104_results_csv) as fid:
theorical_lines = fid.read().splitlines()

self.assertEqual(len(result_lines), len(theorical_lines))
shutil.rmtree(tmpdir, ignore_errors=True)

"""
# Set attributes
Expand Down
1 change: 0 additions & 1 deletion tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@ class Main_test(unittest.TestCase):
lycopene_r20220104_results_csv = os.path.join(dataset_path, "lycopene", "out", "r20220104", "results.csv")
lycopene_r20220104_source_csv = os.path.join(dataset_path, "lycopene", "out", "r20220104", "source-in-sink.csv")
lycopene_r20220104_target_csv = os.path.join(dataset_path, "lycopene", "out", "r20220104", "target_scope.csv")

12 changes: 7 additions & 5 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@author: Joan Hérisson
"""
import os
import tempfile

from retropath2_wrapper.Args import RETCODES
Expand Down Expand Up @@ -31,8 +32,9 @@ def test_check_inchi_from_file(self):
with open(self.inchi_csv) as ifh:
inchis = ifh.read().splitlines()
for inchi in inchis:
tmpfile = tempfile.NamedTemporaryFile()
with open(tmpfile.name, "w") as fod:
fod.write('"Name","InChI"\n')
fod.write(f'"target","{inchi}"')
self.assertNotEqual(check_inchi_from_file(tmpfile.name), "")
fod = tempfile.NamedTemporaryFile(delete=False)
fod.write(bytes('"Name","InChI"\n', "utf8"))
fod.write(bytes('"target","%s"' % (inchi,), "utf8"))
fod.close()
self.assertNotEqual(check_inchi_from_file(fod.name), "")
os.remove(fod.name)

0 comments on commit 1fd3d8e

Please sign in to comment.