Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Move pysam index to external process #11558

Draft
wants to merge 1 commit into
base: release_21.01
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions lib/galaxy/datatypes/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)
from galaxy.datatypes.metadata import DictParameter, ListParameter, MetadataElement, MetadataParameter
from galaxy.datatypes.sniff import build_sniff_from_prefix
from galaxy.datatypes.util.generic_util import call_pysam_index
from galaxy.util import nice_size, sqlite
from galaxy.util.checkers import is_bz2, is_gzip
from . import data, dataproviders
Expand Down Expand Up @@ -488,13 +489,8 @@ def dataset_content_needs_grooming(self, file_name):
# If pysam fails to index a file it will write to stderr,
# and this causes the set_meta script to fail. So instead
# we start another process and discard stderr.
if index_flag == '-b':
# IOError: No such file or directory: '-b' if index_flag is set to -b (pysam 0.15.4)
cmd = ['python', '-c', f"import pysam; pysam.set_verbosity(0); pysam.index('{file_name}', '{index_name}')"]
else:
cmd = ['python', '-c', f"import pysam; pysam.set_verbosity(0); pysam.index('{index_flag}', '{file_name}', '{index_name}')"]
with open(os.devnull, 'w') as devnull:
subprocess.check_call(cmd, stderr=devnull, shell=False)
call_pysam_index(file_name, index_name, index_flag=index_flag,
stderr=os.devnull)
needs_sorting = False
except subprocess.CalledProcessError:
needs_sorting = True
Expand All @@ -516,11 +512,7 @@ def set_meta(self, dataset, overwrite=True, **kwd):
index_file = dataset.metadata.bam_csi_index
if not index_file:
index_file = dataset.metadata.spec[spec_key].param.new_file(dataset=dataset)
if index_flag == '-b':
# IOError: No such file or directory: '-b' if index_flag is set to -b (pysam 0.15.4)
pysam.index(dataset.file_name, index_file.file_name)
else:
pysam.index(index_flag, dataset.file_name, index_file.file_name)
call_pysam_index(dataset.file_name, index_file.file_name, index_flag=index_flag)
dataset.metadata.bam_index = index_file

def sniff(self, file_name):
Expand Down Expand Up @@ -691,7 +683,7 @@ def get_cram_version(self, filename):

def set_index_file(self, dataset, index_file):
try:
pysam.index(dataset.file_name, index_file.file_name)
call_pysam_index(dataset.file_name, index_file.file_name)
return True
except Exception as exc:
log.warning('%s, set_index_file Exception: %s', self, exc)
Expand Down
18 changes: 18 additions & 0 deletions lib/galaxy/datatypes/util/generic_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ def count_special_lines(word, filename, invert=False):
except commands.CommandLineException:
return 0
return int(out)


def call_pysam_index(self, file_name, index_name, index_flag=None, stderr=None):
"""
The pysam.index call can block the GIL, which can pause all threads, including
the heartbeat thread. Therefore, start it as an external process.
"""
if index_flag == '-b' or not index_flag:
# IOError: No such file or directory: '-b' if index_flag is set to -b (pysam 0.15.4)
cmd = ['python', '-c', f"import pysam; pysam.set_verbosity(0); pysam.index('{file_name}', '{index_name}')"]
else:
cmd = ['python', '-c',
f"import pysam; pysam.set_verbosity(0); pysam.index('{index_flag}', '{file_name}', '{index_name}')"]
if stderr:
with open(stderr, 'w') as stderr:
subprocess.check_call(cmd, stderr=stderr, shell=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use

def execute(cmds, input=None):
?

Copy link
Member Author

@nuwang nuwang Mar 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing. The original code has a specific comment saying that stderr needs to be discarded:

# we start another process and discard stderr.

and execute doesn't seem to support stderr redirection?

Copy link
Member

@mvdbeek mvdbeek Mar 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what stderr=subprocess.PIPE does (not exactly, but this good enough. the only important thing is that stderr of the externalize pysam call doesn't end up in the outer stderr, which was? a failure reason for the metadata script)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah I see, the piped stderr is being ignored. Sure, seems fine, can do.

else:
subprocess.check_call(cmd, shell=False)