-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from tcezard/EVA3263_shallow
EVA-3263 - Shallow validation
- Loading branch information
Showing
22 changed files
with
650 additions
and
378 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import argparse | ||
import os | ||
|
||
import yaml | ||
from ebi_eva_common_pyutils.logger import logging_config | ||
from eva_sub_cli.file_utils import open_gzip_if_required, fasta_iter | ||
|
||
logger = logging_config.get_logger(__name__) | ||
|
||
|
||
max_nb_lines = 10000 | ||
|
||
|
||
def trim_down_vcf(vcf_file, output_vcf): | ||
""" | ||
Produce a smaller vcf files containing a maximum of 10000 records | ||
""" | ||
with open_gzip_if_required(vcf_file) as vcf_in, open(output_vcf, 'w') as vcf_out: | ||
line_count = 0 | ||
ref_seq_names = set() | ||
for line in vcf_in: | ||
if line.startswith('#') or line_count < max_nb_lines: | ||
vcf_out.write(line) | ||
if not line.startswith('#'): | ||
line_count += 1 | ||
ref_seq_names.add(line.split('\t')[0]) | ||
else: | ||
break | ||
if line_count != max_nb_lines: | ||
logger.warning(f'Only {line_count} found in the source VCF {vcf_file} ') | ||
return line_count, ref_seq_names | ||
|
||
|
||
def trim_down_fasta(fasta_file, output_fasta, ref_seq_names): | ||
""" | ||
Produce a smaller fasta files containing only the reference sequences found in the VCF file | ||
""" | ||
found_sequences = set() | ||
with open(output_fasta, 'w') as fasta_out: | ||
for header, sequence in fasta_iter(fasta_file): | ||
name = header.split()[0] | ||
if name in ref_seq_names: | ||
found_sequences.add(name) | ||
print(f'>{header}', file=fasta_out) | ||
for i in range(0, len(sequence), 80): | ||
print(sequence[i:i+80], file=fasta_out) | ||
return found_sequences | ||
|
||
|
||
def main(): | ||
arg_parser = argparse.ArgumentParser( | ||
description=f'Take a VCF file and only keep {max_nb_lines} lines and remove unused fasta sequence from the ' | ||
f'associated reference genome') | ||
arg_parser.add_argument('--vcf_file', dest='vcf_file', required=True, | ||
help='Path to the vcf file to be trimmed down') | ||
arg_parser.add_argument('--output_vcf_file', dest='output_vcf_file', required=True, | ||
help='Path to the output vcf file') | ||
arg_parser.add_argument('--fasta_file', dest='fasta_file', required=True, | ||
help='Path to the fasta file to be trimmed down') | ||
arg_parser.add_argument('--output_fasta_file', dest='output_fasta_file', required=True, | ||
help='Path to the output fasta file') | ||
arg_parser.add_argument('--output_yaml_file', dest='output_yaml_file', required=True, | ||
help='Path to the yaml file containing the trim down metrics') | ||
|
||
args = arg_parser.parse_args() | ||
logging_config.add_stdout_handler() | ||
|
||
line_count, ref_sequence = trim_down_vcf(args.vcf_file, args.output_vcf_file) | ||
sequence_found = trim_down_fasta(args.fasta_file, args.output_fasta_file, ref_sequence) | ||
trim_down_metrics = {'trim_down_vcf_record': line_count, 'number_sequence_found': sequence_found, | ||
'trim_down_required': line_count == max_nb_lines} | ||
if len(sequence_found) != len(ref_sequence): | ||
logger.warning(f'Not all sequences were found in the fasta file. Cancelling trimming down of fasta file') | ||
os.link(args.fasta_file, args.output_fasta_file) | ||
trim_down_metrics.pop('number_sequence_found') | ||
with open(args.output_yaml_file) as open_file: | ||
yaml.safe_dump(trim_down_metrics, open_file) | ||
|
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
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 |
---|---|---|
|
@@ -32,4 +32,6 @@ | |
</div> | ||
{% endif %} | ||
|
||
|
||
|
||
{%- endmacro %} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
{% macro optional_shallow_validation_report(validation_results) -%} | ||
{% set results = validation_results.get('shallow_validation', {}) %} | ||
|
||
{% if results.get('required') %} | ||
<section> | ||
<div class="report-section fail collapsible"> <span class="expand_icon">▶</span> | ||
❌ <b>You requested to run the shallow validation, please run full validation before submitting the data</b> | ||
</div> | ||
<div class="no-show"> | ||
<table> | ||
<tr> | ||
<th><strong>VCF File</strong></th> | ||
<th><strong>Variant lines validated in VCF</strong></th> | ||
<th><strong>Entries used in Fasta</strong></th> | ||
</tr> | ||
{% for vcf_file in results.get('metrics') %} | ||
<tr> | ||
<td>{{ vcf_file }}</td> | ||
<td>{{ results.get('metrics').get(vcf_file).get('trim_down_vcf_record') }}</td> | ||
<td>{{ results.get('metrics').get(vcf_file).get('number_sequence_found') }}</td> | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
</div> | ||
</section> | ||
{% endif %} | ||
|
||
{%- endmacro %} |
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.