37a335a6392cfdeceb8ccf1117f901262eb4a663
scripts/manuela_hotspots/Test_Script
| ... | ... | @@ -0,0 +1,67 @@ |
| 1 | +import pandas as pd |
|
| 2 | +import os |
|
| 3 | + |
|
| 4 | +# Config |
|
| 5 | +input_file = "hotspot_amino_acids_full_chrpos.tsv" |
|
| 6 | +output_dir = "per_gene_markdown" |
|
| 7 | + |
|
| 8 | +# Create output directory |
|
| 9 | +os.makedirs(output_dir, exist_ok=True) |
|
| 10 | + |
|
| 11 | +# Read TSV file |
|
| 12 | +df = pd.read_csv(input_file, sep="\t") |
|
| 13 | + |
|
| 14 | +# Lymphopedia format: select and rename columns |
|
| 15 | +df = df.rename(columns={ |
|
| 16 | + "Start_Position": "Coordinate", |
|
| 17 | + "HGVSp_Short": "HGVS" |
|
| 18 | +})[["Hugo_Symbol", "Chromosome", "Coordinate", "DLBCL", "FL", "BL", "HGVS"]] |
|
| 19 | + |
|
| 20 | +# Containers for index and combined file |
|
| 21 | +gene_files = [] |
|
| 22 | +toc_entries = [] |
|
| 23 | +all_md_content = "# All Gene Mutation Tables\n\n## Table of Contents\n\n" |
|
| 24 | + |
|
| 25 | +# TOC entries |
|
| 26 | + |
|
| 27 | +for gene in sorted(df["Hugo_Symbol"].unique()): |
|
| 28 | + all_md_content += f"- [{gene}](#{gene.lower()})\n" |
|
| 29 | + |
|
| 30 | +#divider |
|
| 31 | +all_md_content += "\n---\n\n" |
|
| 32 | + |
|
| 33 | +# group by Hugo_Symbol --> per gene |
|
| 34 | +for gene, group in df.groupby("Hugo_Symbol"): |
|
| 35 | + # md table header |
|
| 36 | + md_table = "| " + " | ".join(group.columns) + " |\n" |
|
| 37 | + md_table += "| " + " | ".join(["---"] * len(group.columns)) + " |\n" |
|
| 38 | + for _, row in group.iterrows(): |
|
| 39 | + md_table += "| " + " | ".join(map(str, row.values)) + " |\n" |
|
| 40 | + |
|
| 41 | + # per gene md file |
|
| 42 | + file_name = f"{gene}.md" |
|
| 43 | + output_path = os.path.join(output_dir, file_name) |
|
| 44 | + with open(output_path, "w", encoding="utf-8") as f: |
|
| 45 | + f.write(f"# {gene}\n\n") |
|
| 46 | + f.write(md_table) |
|
| 47 | + |
|
| 48 | + |
|
| 49 | +# add section divider for all tables in ALL_GENES.md |
|
| 50 | +all_md_content += "\n---\n\n" |
|
| 51 | + |
|
| 52 | +# add each gene's section to combined file |
|
| 53 | +for gene, group in df.groupby("Hugo_Symbol"): |
|
| 54 | + md_table = "| " + " | ".join(group.columns) + " |\n" |
|
| 55 | + md_table += "| " + " | ".join(["---"] * len(group.columns)) + " |\n" |
|
| 56 | + for _, row in group.iterrows(): |
|
| 57 | + md_table += "| " + " | ".join(map(str, row.values)) + " |\n" |
|
| 58 | + all_md_content += f"## {gene}\n\n{md_table}\n\n" |
|
| 59 | + |
|
| 60 | + |
|
| 61 | +# create ALL_GENES.md (with TOC) |
|
| 62 | +all_path = os.path.join(output_dir, "ALL_GENES.md") |
|
| 63 | +with open(all_path, "w", encoding="utf-8") as f: |
|
| 64 | + f.write(all_md_content) |
|
| 65 | + |
|
| 66 | +print(f" Markdown files and ALL_GENES.md created in: {output_dir}/") |
|
| 67 | + |