|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +VERSION = "V1.0.0" |
| 4 | + |
| 5 | +DESCRIPTION = """ |
| 6 | +------------------------------------- |
| 7 | + Abnormal Contamination Check |
| 8 | + Version = {VERSION} |
| 9 | +------------------------------------- |
| 10 | +Written by James Torrance |
| 11 | +Modified by Eerik Aunin |
| 12 | +Modified by Damon-Lee Pointon |
| 13 | +------------------------------------- |
| 14 | +
|
| 15 | +Script for determining if there is |
| 16 | +enough contamination found by FCS-GX |
| 17 | +to warrant an abnormal contamination |
| 18 | +report alarm. Partially based on code |
| 19 | +written by James Torrance |
| 20 | +------------------------------------- |
| 21 | +
|
| 22 | +""" |
| 23 | + |
| 24 | +import general_purpose_functions as gpf |
| 25 | +import sys |
| 26 | +import os.path |
| 27 | +import pathlib |
| 28 | +import argparse |
| 29 | +import textwrap |
| 30 | + |
| 31 | + |
| 32 | +def parse_args(): |
| 33 | + parser = argparse.ArgumentParser( |
| 34 | + prog="Abnormal Contamination Check", |
| 35 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 36 | + description=textwrap.dedent(DESCRIPTION), |
| 37 | + ) |
| 38 | + parser.add_argument("assembly", type=str, help="Path to the fasta assembly file") |
| 39 | + parser.add_argument("summary_path", type=str, help="Path to the tiara summary file") |
| 40 | + parser.add_argument("-v", "--version", action="version", version=VERSION) |
| 41 | + return parser.parse_args() |
| 42 | + |
| 43 | + |
| 44 | +def get_sequence_lengths(assembly_fasta_path): |
| 45 | + """ |
| 46 | + Gets sequence lengths of a FASTA file and returns them as a dictionary |
| 47 | + """ |
| 48 | + seq_lengths_dict = dict() |
| 49 | + fasta_data = gpf.read_fasta_in_chunks(assembly_fasta_path) |
| 50 | + for header, seq in fasta_data: |
| 51 | + seq_len = len(seq) |
| 52 | + seq_lengths_dict[header] = dict() |
| 53 | + seq_lengths_dict[header]["seq_len"] = seq_len |
| 54 | + return seq_lengths_dict |
| 55 | + |
| 56 | + |
| 57 | +def load_fcs_gx_results(seq_dict, fcs_gx_and_tiara_summary_path): |
| 58 | + """ |
| 59 | + Loads FCS-GX actions from the FCS-GX and Tiara results summary file, adds them to the dictionary that contains sequence lengths |
| 60 | + """ |
| 61 | + fcs_gx_and_tiara_summary_data = gpf.l(fcs_gx_and_tiara_summary_path) |
| 62 | + fcs_gx_and_tiara_summary_data = fcs_gx_and_tiara_summary_data[1 : len(fcs_gx_and_tiara_summary_data)] |
| 63 | + for line in fcs_gx_and_tiara_summary_data: |
| 64 | + split_line = line.split(",") |
| 65 | + assert len(split_line) == 5 |
| 66 | + seq_name = split_line[0] |
| 67 | + fcs_gx_action = split_line[1] |
| 68 | + seq_dict[seq_name]["fcs_gx_action"] = fcs_gx_action |
| 69 | + return seq_dict |
| 70 | + |
| 71 | + |
| 72 | +def main(): |
| 73 | + args = parse_args() |
| 74 | + if os.path.isfile(args.summary_path) is False: |
| 75 | + sys.stderr.write( |
| 76 | + f"The FCS-GX and Tiara results file was not found at the expected location ({args.summary_path})\n" |
| 77 | + ) |
| 78 | + sys.exit(1) |
| 79 | + |
| 80 | + if os.path.isfile(args.assembly) is False: |
| 81 | + sys.stderr.write(f"The assembly FASTA file was not found at the expected location ({args.assembly})\n") |
| 82 | + sys.exit(1) |
| 83 | + |
| 84 | + seq_dict = get_sequence_lengths(args.assembly) |
| 85 | + seq_dict = load_fcs_gx_results(seq_dict, args.summary_path) |
| 86 | + |
| 87 | + total_assembly_length = 0 |
| 88 | + lengths_removed = list() |
| 89 | + scaffolds_removed = 0 |
| 90 | + scaffold_count = len(seq_dict) |
| 91 | + |
| 92 | + for seq_name in seq_dict: |
| 93 | + seq_len = seq_dict[seq_name]["seq_len"] |
| 94 | + if seq_dict[seq_name]["fcs_gx_action"] == "EXCLUDE": |
| 95 | + lengths_removed.append(seq_len) |
| 96 | + scaffolds_removed += 1 |
| 97 | + total_assembly_length += seq_len |
| 98 | + |
| 99 | + alarm_threshold_for_parameter = { |
| 100 | + "TOTAL_LENGTH_REMOVED": 1e7, |
| 101 | + "PERCENTAGE_LENGTH_REMOVED": 3, |
| 102 | + "LARGEST_SCAFFOLD_REMOVED": 1.8e6, |
| 103 | + } |
| 104 | + |
| 105 | + report_dict = { |
| 106 | + "TOTAL_LENGTH_REMOVED": sum(lengths_removed), |
| 107 | + "PERCENTAGE_LENGTH_REMOVED": 100 * sum(lengths_removed) / total_assembly_length, |
| 108 | + "LARGEST_SCAFFOLD_REMOVED": max(lengths_removed, default=0), |
| 109 | + "SCAFFOLDS_REMOVED": scaffolds_removed, |
| 110 | + "PERCENTAGE_SCAFFOLDS_REMOVED": 100 * scaffolds_removed / scaffold_count, |
| 111 | + } |
| 112 | + |
| 113 | + for param in report_dict: |
| 114 | + sys.stderr.write(f"{param}: {report_dict[param]}\n") |
| 115 | + |
| 116 | + fcs_gx_alarm_indicator_path = f"fcs-gx_alarm_indicator_file.txt" |
| 117 | + pathlib.Path(fcs_gx_alarm_indicator_path).unlink(missing_ok=True) |
| 118 | + |
| 119 | + alarm_list = [] |
| 120 | + stage1_decon_pass_flag = True |
| 121 | + for param in alarm_threshold_for_parameter: |
| 122 | + param_value = report_dict[param] |
| 123 | + alarm_threshold = alarm_threshold_for_parameter[param] |
| 124 | + |
| 125 | + # IF CONTAMINATING SEQ FOUND FILL FILE WITH ABNORMAL CONTAM |
| 126 | + if param_value > alarm_threshold_for_parameter[param]: |
| 127 | + stage1_decon_pass_flag = False |
| 128 | + alarm_list.append( |
| 129 | + f"YES_ABNORMAL_CONTAMINATION: Stage 1 decon alarm triggered for {param}: the value for this parameter in this assembly is {param_value} | alarm threshold is {alarm_threshold}\n" |
| 130 | + ) |
| 131 | + |
| 132 | + # Seperated out to ensure that the file is written in one go and doesn't confuse Nextflow |
| 133 | + with open(fcs_gx_alarm_indicator_path, "a") as f: |
| 134 | + f.write("".join(alarm_list)) |
| 135 | + |
| 136 | + # IF NO CONTAM FILL FILE WITH NO CONTAM |
| 137 | + if stage1_decon_pass_flag is True: |
| 138 | + alarm_message = f"NO_ABNORMAL_CONTAMINATION: No scaffolds were tagged for removal by FCS-GX\n" |
| 139 | + with open(fcs_gx_alarm_indicator_path, "a") as f: |
| 140 | + f.write(alarm_message) |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + main() |
0 commit comments