|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# version = '1.0.0' |
| 4 | + |
| 5 | +# Modified from script https://github.com/CDPHE-bioinformatics/CDPHE-influenza/blob/main/scripts/calc_percent_cov.py |
| 6 | + |
| 7 | +# import python modules |
| 8 | +import pandas as pd |
| 9 | +from datetime import date |
| 10 | +from Bio import SeqIO |
| 11 | +from Bio.SeqRecord import SeqRecord |
| 12 | + |
| 13 | +import sys |
| 14 | +import argparse |
| 15 | +import subprocess |
| 16 | + |
| 17 | +### Segment length dictionary |
| 18 | +ref_len_dict = { |
| 19 | + "A_MP": 982, |
| 20 | + "A_NP": 1497, |
| 21 | + "A_NS": 863, |
| 22 | + "A_PA": 2151, |
| 23 | + "A_PB1": 2274, |
| 24 | + "A_PB2": 2280, |
| 25 | + "A_HA_H1": 1704, |
| 26 | + "A_HA_H10": 1686, |
| 27 | + "A_HA_H11": 1698, |
| 28 | + "A_HA_H12": 1695, |
| 29 | + "A_HA_H13": 1701, |
| 30 | + "A_HA_H14": 1707, |
| 31 | + "A_HA_H15": 1713, |
| 32 | + "A_HA_H16": 1698, |
| 33 | + "A_HA_H2": 1689, |
| 34 | + "A_HA_H3": 1704, |
| 35 | + "A_HA_H4": 1695, |
| 36 | + "A_HA_H5": 1707, |
| 37 | + "A_HA_H6": 1704, |
| 38 | + "A_HA_H7": 1713, |
| 39 | + "A_HA_H8": 1701, |
| 40 | + "A_HA_H9": 1683, |
| 41 | + "A_NA_N1": 1413, |
| 42 | + "A_NA_N2": 1410, |
| 43 | + "A_NA_N3": 1410, |
| 44 | + "A_NA_N4": 1413, |
| 45 | + "A_NA_N5": 1422, |
| 46 | + "A_NA_N6": 1413, |
| 47 | + "A_NA_N7": 1416, |
| 48 | + "A_NA_N8": 1413, |
| 49 | + "A_NA_N9": 1413, |
| 50 | + "B_HA": 1758, |
| 51 | + "B_MP": 1139, |
| 52 | + "B_NA": 1408, |
| 53 | + "B_NP": 1683, |
| 54 | + "B_NS": 1034, |
| 55 | + "B_PA": 2181, |
| 56 | + "B_PB1": 2263, |
| 57 | + "B_PB2": 2313, |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +#### FUNCTIONS ##### |
| 62 | +def getOptions(): |
| 63 | + parser = argparse.ArgumentParser(description="Parses command.") |
| 64 | + parser.add_argument("fasta_files", help="Path to the fasta file") |
| 65 | + parser.add_argument("meta_id", help="Meta ID") |
| 66 | + options = parser.parse_args() |
| 67 | + return options |
| 68 | + |
| 69 | + |
| 70 | +def get_fasta_file_basename(fasta_file_path): |
| 71 | + basename = fasta_file_path.split("/")[-1] # strip directories |
| 72 | + return basename |
| 73 | + |
| 74 | + |
| 75 | +def get_segment_name(fasta_file_path): |
| 76 | + basename = fasta_file_path.split("/")[-1] # strip directories |
| 77 | + segment_name = basename.split(".")[0] # remove file extension |
| 78 | + return segment_name |
| 79 | + |
| 80 | + |
| 81 | +def get_gene_name(fasta_file_path): |
| 82 | + basename = fasta_file_path.split("/")[-1] # strip directories |
| 83 | + segment_name = basename.split(".")[0] # remove file extension |
| 84 | + gene_name = segment_name.split("_")[1] # extract gene name |
| 85 | + return gene_name |
| 86 | + |
| 87 | + |
| 88 | +def get_seq_length(fasta_file_path): |
| 89 | + # read in fasta file |
| 90 | + record = SeqIO.read(fasta_file_path, "fasta") |
| 91 | + |
| 92 | + # get length of non ambigous bases |
| 93 | + seq = record.seq |
| 94 | + seq_length = seq.count("A") + seq.count("C") + seq.count("G") + seq.count("T") |
| 95 | + |
| 96 | + return seq_length |
| 97 | + |
| 98 | + |
| 99 | +def calc_percent_cov(seq_length, ref_len_dict, segment_name): |
| 100 | + # calcuate per cov based on expected ref length |
| 101 | + expected_length = ref_len_dict[segment_name] |
| 102 | + percent_coverage = round(((seq_length / expected_length) * 100), 2) |
| 103 | + |
| 104 | + return percent_coverage |
| 105 | + |
| 106 | + |
| 107 | +def create_output(meta_id, segment_name, seq_length, percent_coverage, reference_length): |
| 108 | + df = pd.DataFrame() |
| 109 | + df["Sample"] = [meta_id] |
| 110 | + df["segment_name"] = [segment_name] |
| 111 | + df["reference_length"] = [reference_length] |
| 112 | + df["seq_length"] = [seq_length] |
| 113 | + df["percent_coverage"] = [percent_coverage] |
| 114 | + |
| 115 | + # Construct the output filename header |
| 116 | + output_header = "Sample\tsegment_name\treference_length\tseq_length\tpercent_coverage" |
| 117 | + |
| 118 | + # Construct the output filename |
| 119 | + output_filename = f"{meta_id}.{segment_name}.perc_cov_results.tsv" |
| 120 | + |
| 121 | + # Write the dataframe to the output file |
| 122 | + with open(output_filename, "w") as f: |
| 123 | + f.write(output_header + "\n") |
| 124 | + df.to_csv(f, sep="\t", index=False, header=False) |
| 125 | + |
| 126 | + |
| 127 | +#### MAIN #### |
| 128 | +if __name__ == "__main__": |
| 129 | + options = getOptions() |
| 130 | + fasta_file_path = options.fasta_files |
| 131 | + meta_id = options.meta_id |
| 132 | + |
| 133 | + basename = get_fasta_file_basename(fasta_file_path=fasta_file_path) |
| 134 | + |
| 135 | + segment_name = get_segment_name(fasta_file_path=fasta_file_path) |
| 136 | + gene_name = get_gene_name(fasta_file_path=fasta_file_path) |
| 137 | + |
| 138 | + seq_length = get_seq_length(fasta_file_path=fasta_file_path) |
| 139 | + percent_coverage = calc_percent_cov(seq_length=seq_length, ref_len_dict=ref_len_dict, segment_name=segment_name) |
| 140 | + |
| 141 | + reference_length = ref_len_dict[segment_name] |
| 142 | + |
| 143 | + create_output( |
| 144 | + meta_id=meta_id, |
| 145 | + segment_name=segment_name, |
| 146 | + seq_length=seq_length, |
| 147 | + percent_coverage=percent_coverage, |
| 148 | + reference_length=reference_length, |
| 149 | + ) |
0 commit comments