-
Notifications
You must be signed in to change notification settings - Fork 10
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
Allow different (multiple) inputs #106
Closed
+158
−48
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -156,14 +156,6 @@ def sanity_check_config(): | |||||||||||||||||
print("-"*80, file=sys.stderr) | ||||||||||||||||||
raise InvalidConfigError("No config") | ||||||||||||||||||
|
||||||||||||||||||
assert LOCAL_INGEST or S3_SRC, "The config must define either 's3_src' or 'local_ingest'" | ||||||||||||||||||
# NOTE: we could relax the following exclusivity of S3_SRC and LOCAL_INGEST | ||||||||||||||||||
# if we want to use `--config local_ingest=gisaid` overrides. | ||||||||||||||||||
assert not (S3_SRC and LOCAL_INGEST), "The config defined both 'local_ingest' and 's3_src', which are mutually exclusive" | ||||||||||||||||||
if S3_SRC: | ||||||||||||||||||
assert isinstance(S3_SRC, dict) and all([k in S3_SRC for k in ("name", "sequences", "metadata")]), \ | ||||||||||||||||||
"Config 's3_src' must be a dict with 'name', 'sequences' and 'metadata' keys" | ||||||||||||||||||
|
||||||||||||||||||
sanity_check_config() | ||||||||||||||||||
|
||||||||||||||||||
def as_list(x): | ||||||||||||||||||
|
@@ -242,48 +234,115 @@ rule files: | |||||||||||||||||
files = rules.files.params | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
rule download_sequences: | ||||||||||||||||||
rule download_s3_sequences: | ||||||||||||||||||
output: | ||||||||||||||||||
sequences = f"data/{S3_SRC.get('name', None)}/sequences_{{segment}}.fasta", | ||||||||||||||||||
metadata = "data/{input_name}/sequences_{segment}.fasta", | ||||||||||||||||||
params: | ||||||||||||||||||
address=lambda w: S3_SRC.get('sequences', None).format(segment=w.segment), | ||||||||||||||||||
no_sign_request=lambda w: "--no-sign-request" if S3_SRC.get('sequences', "").startswith(NEXTSTRAIN_PUBLIC_BUCKET) else "" | ||||||||||||||||||
address=lambda w: input_by_name(w.input_name, segment=w.segment).format(segment=w.segment), | ||||||||||||||||||
no_sign_request=lambda w: "--no-sign-request" if input_by_name(w.input_name, segment=w.segment).startswith(NEXTSTRAIN_PUBLIC_BUCKET) else "" | ||||||||||||||||||
shell: | ||||||||||||||||||
""" | ||||||||||||||||||
aws s3 cp {params.no_sign_request:q} {params.address:q} - | zstd -d > {output.sequences} | ||||||||||||||||||
aws s3 cp {params.no_sign_request:q} {params.address:q} - | zstd -d > {output.metadata} | ||||||||||||||||||
""" | ||||||||||||||||||
|
||||||||||||||||||
rule download_metadata: | ||||||||||||||||||
rule download_s3_metadata: | ||||||||||||||||||
output: | ||||||||||||||||||
metadata = f"data/{S3_SRC.get('name', None)}/metadata.tsv", | ||||||||||||||||||
metadata = "data/{input_name}/metadata.tsv", | ||||||||||||||||||
params: | ||||||||||||||||||
address=S3_SRC.get('metadata', None), | ||||||||||||||||||
no_sign_request=lambda w: "--no-sign-request" if S3_SRC.get('metadata', "").startswith(NEXTSTRAIN_PUBLIC_BUCKET) else "" | ||||||||||||||||||
address=lambda w: input_by_name(w.input_name, metadata=True), | ||||||||||||||||||
no_sign_request=lambda w: "--no-sign-request" if input_by_name(w.input_name, metadata=True).startswith(NEXTSTRAIN_PUBLIC_BUCKET) else "" | ||||||||||||||||||
shell: | ||||||||||||||||||
""" | ||||||||||||||||||
aws s3 cp {params.no_sign_request:q} {params.address:q} - | zstd -d > {output.metadata} | ||||||||||||||||||
""" | ||||||||||||||||||
|
||||||||||||||||||
def _segment_input(info, segment): | ||||||||||||||||||
address = info.get('sequences', None) | ||||||||||||||||||
if address is None: | ||||||||||||||||||
return address | ||||||||||||||||||
elif isinstance(address, str): | ||||||||||||||||||
return address | ||||||||||||||||||
elif isinstance(address, dict): | ||||||||||||||||||
return address.get(segment, None) | ||||||||||||||||||
raise InvalidConfigError(f"Invalid structure for one or more provided (additional) input sequences") | ||||||||||||||||||
|
||||||||||||||||||
def input_metadata(wildcards): | ||||||||||||||||||
if S3_SRC: | ||||||||||||||||||
return f"data/{S3_SRC['name']}/metadata.tsv", | ||||||||||||||||||
elif LOCAL_INGEST: | ||||||||||||||||||
return f"ingest/{LOCAL_INGEST}/results/metadata.tsv", | ||||||||||||||||||
raise Exception() # already caught by `sanity_check_config` above, this is just being cautious | ||||||||||||||||||
def input_by_name(name, metadata=False, segment=False): | ||||||||||||||||||
assert (metadata and not segment) or (segment and not metadata), "Workflow bug" | ||||||||||||||||||
info = next((c for c in [*config['inputs'], *config.get('additional_inputs', [])] if c['name']==name)) | ||||||||||||||||||
return info.get('metadata', None) if metadata else _segment_input(info, segment) | ||||||||||||||||||
|
||||||||||||||||||
def input_sequences(wildcards): | ||||||||||||||||||
if S3_SRC: | ||||||||||||||||||
return f"data/{S3_SRC['name']}/sequences_{wildcards.segment}.fasta", | ||||||||||||||||||
elif LOCAL_INGEST: | ||||||||||||||||||
return f"ingest/{LOCAL_INGEST}/results/sequences_{wildcards.segment}.fasta" | ||||||||||||||||||
raise Exception() # already caught by `sanity_check_config` above, this is just being cautious | ||||||||||||||||||
def collect_inputs(metadata=None, segment=None, augur_merge=False): | ||||||||||||||||||
""" | ||||||||||||||||||
This function is pure - subsequent calls will return the same results. | ||||||||||||||||||
""" | ||||||||||||||||||
assert (metadata and not segment) or (segment and not metadata), "Workflow bug" | ||||||||||||||||||
|
||||||||||||||||||
inputs = [] | ||||||||||||||||||
for source in [*config['inputs'], *config.get('additional_inputs', [])]: | ||||||||||||||||||
name = source['name'] | ||||||||||||||||||
|
||||||||||||||||||
address = source.get('metadata', None) if metadata else _segment_input(source, segment) | ||||||||||||||||||
if address is None: | ||||||||||||||||||
continue # inputs can define (e.g.) only metadata, or only sequences for some segments etc | ||||||||||||||||||
|
||||||||||||||||||
# addresses may be a remote filepath or a local file | ||||||||||||||||||
if address.startswith('s3://'): | ||||||||||||||||||
download_path = f"data/{source['name']}/metadata.tsv" \ | ||||||||||||||||||
if metadata \ | ||||||||||||||||||
else f"data/{source['name']}/sequences_{segment}.fasta" | ||||||||||||||||||
inputs.append((name, download_path)) | ||||||||||||||||||
continue | ||||||||||||||||||
elif address.startswith(r'http[s]:\/\/'): | ||||||||||||||||||
raise InvalidConfigError("Workflow cannot yet handle HTTP[S] inputs") | ||||||||||||||||||
inputs.append((name, resolve_config_path(address, {'segment':segment}))) | ||||||||||||||||||
|
||||||||||||||||||
if not inputs: | ||||||||||||||||||
raise InvalidConfigError("No inputs provided with defined metadata") | ||||||||||||||||||
|
||||||||||||||||||
if augur_merge: | ||||||||||||||||||
return " ".join([f"{x[0]}={x[1]}" for x in inputs]) | ||||||||||||||||||
return [x[1] for x in inputs] | ||||||||||||||||||
|
||||||||||||||||||
rule merge_metadata: | ||||||||||||||||||
""" | ||||||||||||||||||
This rule should only be invoked if there are multiple defined metadata inputs | ||||||||||||||||||
(config.inputs + config.additional_inputs) | ||||||||||||||||||
""" | ||||||||||||||||||
input: | ||||||||||||||||||
metadata = collect_inputs(metadata=True) | ||||||||||||||||||
params: | ||||||||||||||||||
metadata = collect_inputs(metadata=True, augur_merge=True) | ||||||||||||||||||
output: | ||||||||||||||||||
metadata = "results/metadata_merged.tsv" | ||||||||||||||||||
shell: | ||||||||||||||||||
r""" | ||||||||||||||||||
augur merge \ | ||||||||||||||||||
--metadata {params.metadata} \ | ||||||||||||||||||
--source-columns 'input_{{NAME}}' \ | ||||||||||||||||||
--output-metadata {output.metadata} | ||||||||||||||||||
""" | ||||||||||||||||||
|
||||||||||||||||||
rule merge_sequences: | ||||||||||||||||||
""" | ||||||||||||||||||
Placeholder rule while we want for `augur merge` to support sequences | ||||||||||||||||||
""" | ||||||||||||||||||
input: | ||||||||||||||||||
metadata = lambda w: collect_inputs(segment=w.segment) | ||||||||||||||||||
output: | ||||||||||||||||||
metadata = "results/sequences_merged_{segment}.fasta" | ||||||||||||||||||
Comment on lines
+329
to
+332
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming nitpick:
Suggested change
|
||||||||||||||||||
shell: | ||||||||||||||||||
r""" | ||||||||||||||||||
cat {input.metadata} > {output.metadata} | ||||||||||||||||||
victorlin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
""" | ||||||||||||||||||
|
||||||||||||||||||
rule filter_sequences_by_subtype: | ||||||||||||||||||
input: | ||||||||||||||||||
sequences = input_sequences, | ||||||||||||||||||
metadata = input_metadata, | ||||||||||||||||||
sequences = lambda w: collect_inputs(segment=w.segment)[0] | ||||||||||||||||||
if len(collect_inputs(segment=w.segment))==1 | ||||||||||||||||||
else f"results/sequences_merged_{w.segment}.fasta", | ||||||||||||||||||
metadata = collect_inputs(metadata=True)[0] | ||||||||||||||||||
if len(collect_inputs(metadata=True))==1 | ||||||||||||||||||
else "results/metadata_merged.tsv" | ||||||||||||||||||
output: | ||||||||||||||||||
sequences = "results/{subtype}/{segment}/sequences.fasta", | ||||||||||||||||||
params: | ||||||||||||||||||
|
@@ -299,7 +358,9 @@ rule filter_sequences_by_subtype: | |||||||||||||||||
|
||||||||||||||||||
rule filter_metadata_by_subtype: | ||||||||||||||||||
input: | ||||||||||||||||||
metadata = input_metadata, | ||||||||||||||||||
metadata = collect_inputs(metadata=True)[0] | ||||||||||||||||||
if len(collect_inputs(metadata=True))==1 | ||||||||||||||||||
else "results/metadata_merged.tsv" | ||||||||||||||||||
output: | ||||||||||||||||||
metadata = "results/{subtype}/metadata.tsv", | ||||||||||||||||||
params: | ||||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo