Coverage for corgi/camisim.py: 97.50%

40 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-28 00:24 +0000

1"""Command to prepare labels for evaluating Corgi on CAMISIM benchmark data.""" 

2 

3from multitax import NcbiTx 

4import pandas as pd 

5from pathlib import Path 

6import typer 

7from .data import SeqIODataloader 

8from .refseq import DNAType 

9 

10app = typer.Typer() 

11 

12 

13@app.command() 

14def camisim( 

15 mapping: Path = typer.Argument(..., help="The path to the GSA mapping file (gsa_mapping.tsv.gz)"), 

16 genomes_dir: Path = typer.Argument(..., help="The directory containing the genomes"), 

17 output_csv: Path = typer.Argument(..., help="The output CSV file path"), 

18): 

19 """Convert GSA mapping to a CSV file with Corgi and Tiara labels as columns.""" 

20 df = pd.read_csv(mapping, sep="\t", compression="gzip") 

21 df['corgi_category'] = "" 

22 df['tiara_category'] = "" 

23 

24 print('Loading taxonomy...') 

25 taxonomy = NcbiTx() 

26 

27 dataloader = SeqIODataloader(genomes_dir) 

28 for _, record in dataloader.iter_records(): 

29 dna_type = DNAType.from_description(record.description) 

30 row = df['contig_id'] == record.id 

31 if not row.any(): 

32 continue 

33 if dna_type == DNAType.MITOCHONDRION: 

34 df.loc[row, 'corgi_category'] = "Mitochondrion" 

35 df.loc[row, 'tiara_category'] = "mit" 

36 elif dna_type == DNAType.PLASTID: 

37 df.loc[row, 'corgi_category'] = "Plastid" 

38 df.loc[row, 'tiara_category'] = "pla" 

39 elif dna_type == DNAType.PLASMID: 

40 df.loc[row, 'corgi_category'] = "Plasmid" 

41 df.loc[row, 'tiara_category'] = "unk2" 

42 else: 

43 # bac/arc/unk/unk1 

44 taxon_id = df[row]['taxon_id'].values[0] 

45 superkingdom = taxonomy.parent_rank(taxon_id, "superkingdom") 

46 df.loc[row, 'corgi_category'] = f"Nuclear/{superkingdom}" 

47 tiara_category = superkingdom.lower()[:3] 

48 if tiara_category not in ["bac", "arc", "euk"]: 

49 tiara_category = "unk1" 

50 df.loc[row, 'tiara_category'] = tiara_category 

51 

52 print(f"Writing to {output_csv}") 

53 df.to_csv(output_csv, index=False) 

54 

55 

56if __name__ == "__main__": 

57 app()