|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +## summarise_pydamagebins.py |
| 4 | +## Originally written by James Fellows Yates and released under the MIT license. |
| 5 | +## See git repository (https://github.com/nf-core/mag) for full license text. |
| 6 | + |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import argparse |
| 10 | +import pandas as pd |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +def parse_args(args=None): |
| 15 | + parser = argparse.ArgumentParser() |
| 16 | + parser.add_argument( |
| 17 | + "-c", |
| 18 | + "--contig-to-bin-map", |
| 19 | + required=True, |
| 20 | + metavar="FILE", |
| 21 | + help="Input file of tab-separated list of <bin_id>\t<contig_id> mappings.", |
| 22 | + ) |
| 23 | + parser.add_argument( |
| 24 | + "-o", |
| 25 | + "--output", |
| 26 | + required=False, |
| 27 | + metavar="FILE", |
| 28 | + help="Path to output TSV file with all statistic values summarised with as a median value per bin.", |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + "-v", |
| 32 | + "--verbose", |
| 33 | + required=False, |
| 34 | + action=argparse.BooleanOptionalAction, |
| 35 | + help="Print to console more logging information", |
| 36 | + ) |
| 37 | + parser.add_argument("--version", action="version", version="%(prog)s 0.0.1") |
| 38 | + parser.add_argument( |
| 39 | + "pydamage_reports_input", |
| 40 | + nargs="+", |
| 41 | + help="List of pydamage report files for contigs.", |
| 42 | + ) |
| 43 | + |
| 44 | + return parser.parse_args(args) |
| 45 | + |
| 46 | + |
| 47 | +def main(args=None): |
| 48 | + argparse.ArgumentParser(description="summarise_pydamage.py") |
| 49 | + args = parse_args(args) |
| 50 | + |
| 51 | + if args.verbose: |
| 52 | + print( |
| 53 | + "[summarise_pydamagebins.py] PROCESSING: Loading " + args.contig_to_bin_map |
| 54 | + ) |
| 55 | + |
| 56 | + ## Load contig to bin map from Nextflow |
| 57 | + contig_to_bin_map = pd.read_csv( |
| 58 | + args.contig_to_bin_map, |
| 59 | + sep="\t", |
| 60 | + ) |
| 61 | + contig_to_bin_map.rename(columns={"contig_id": "reference"}, inplace=True) |
| 62 | + contig_to_bin_map["assembly_id"] = contig_to_bin_map["assembly_id"].astype(str) |
| 63 | + |
| 64 | + ## Repair contig names to match |
| 65 | + ## Some tools remove everything after first space, so pyDamage has truncated headers vs. raw contigs |
| 66 | + ## We strip this information from the raw contigs to meet at the simplest/lowest common denominator |
| 67 | + contig_to_bin_map["reference"] = contig_to_bin_map["reference"].str.extract( |
| 68 | + "([^ ]+)" |
| 69 | + ) |
| 70 | + |
| 71 | + ## Clean up pydamage reports |
| 72 | + pydamage_reports_dfs = [] |
| 73 | + |
| 74 | + for report in args.pydamage_reports_input: |
| 75 | + if args.verbose: |
| 76 | + print("[summarise_pydamagebins.py] PROCESSING: Loading " + report) |
| 77 | + assembly_id = os.path.basename(report).replace("_pydamage_results.csv", "") |
| 78 | + pydamage_df = pd.read_csv(report, sep=",") |
| 79 | + pydamage_df["assembly_id"] = assembly_id |
| 80 | + pydamage_df.insert(0, "assembly_id", pydamage_df.pop("assembly_id")) |
| 81 | + pydamage_reports_dfs.append(pydamage_df) |
| 82 | + |
| 83 | + pydamage_reports_all = pd.concat(pydamage_reports_dfs) |
| 84 | + pydamage_reports_all["assembly_id"] = pydamage_reports_all["assembly_id"].astype( |
| 85 | + str |
| 86 | + ) |
| 87 | + |
| 88 | + ## Merge contig_to_bin_map with pydamage reports |
| 89 | + if args.verbose: |
| 90 | + print( |
| 91 | + "[summarise_pydamagebins.py] MERGING: contig to bin map with pydamage reports" |
| 92 | + ) |
| 93 | + |
| 94 | + pydamage_contig_to_bin = pd.merge( |
| 95 | + left=contig_to_bin_map, |
| 96 | + right=pydamage_reports_all, |
| 97 | + on=["assembly_id", "reference"], |
| 98 | + ).sort_values(by=["bin_id", "assembly_id", "binner", "reference"]) |
| 99 | + |
| 100 | + ## Group by bin_id, save per-bin collection, and then summarise over median |
| 101 | + if args.verbose: |
| 102 | + print("[summarise_pydamagebins.py] GROUPING: by bin and calculating median") |
| 103 | + |
| 104 | + pydamage_bin_summary = pydamage_contig_to_bin.groupby(["bin_id"], as_index=False) |
| 105 | + |
| 106 | + for name, group in pydamage_bin_summary: |
| 107 | + filename = Path(name).stem + "_pydamage_bin_results.tsv" |
| 108 | + group.to_csv(filename, sep="\t", index=False) |
| 109 | + |
| 110 | + pydamage_bin_summary_median = pydamage_bin_summary.median( |
| 111 | + numeric_only=True |
| 112 | + ).sort_values(by=["bin_id"]) |
| 113 | + |
| 114 | + pydamage_bin_summary_median.to_csv( |
| 115 | + "pydamage_bins_summary.tsv", sep="\t", index=False |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + sys.exit(main()) |
0 commit comments