-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gene coverage columns during ingest workflow #36
- Loading branch information
Showing
3 changed files
with
138 additions
and
10 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
ingest/bin/calculate-gene-converage-from-nextclade-translation.py
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,54 @@ | ||
#! /usr/bin/env python3 | ||
|
||
import argparse | ||
from sys import stderr | ||
|
||
from Bio import SeqIO | ||
import re | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser( | ||
description="Calculate gene coverage from amino acid sequence. The output will be in TSV format to stdout.", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
parser.add_argument( | ||
"--fasta", | ||
type=str, | ||
help="FASTA file of CDS translations from Nextclade.", | ||
) | ||
parser.add_argument( | ||
"--out-col", | ||
type=str, | ||
default="gene_coverage", | ||
help="Output column name.", | ||
) | ||
return parser.parse_args() | ||
|
||
|
||
def calculate_gene_coverage_from_nextclade_cds(fasta, out_col): | ||
""" | ||
Calculate gene coverage from amino acid sequence in gene translation FASTA file from Nextclade. | ||
""" | ||
print(f"genbank_accession\t{out_col}") | ||
# Iterate over the sequences in the FASTA file | ||
for record in SeqIO.parse(fasta, "fasta"): | ||
sequence_id = record.id | ||
sequence = str(record.seq) | ||
|
||
# Calculate gene coverage | ||
results = re.findall(r"([ACDEFGHIKLMNPQRSTVWY])", sequence.upper()) | ||
gene_coverage = round(len(results) / len(sequence), 3) | ||
|
||
# Print the results | ||
print(f"{sequence_id}\t{gene_coverage}") | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
|
||
calculate_gene_coverage_from_nextclade_cds(args.fasta, args.out_col) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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