Skip to content

Commit

Permalink
summarise: Add --num-decimal-places.
Browse files Browse the repository at this point in the history
  • Loading branch information
wwood committed Dec 9, 2024
1 parent 8e99d16 commit 272f714
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions singlem/condense.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,8 @@ def each_sample_wise(io):

for row in reader:
(sample, coverage, taxonomy) = row
if float(coverage) == 0:
continue
if sample != current_sample:
if current_sample is not None:
yield CondensedCommunityProfile(current_sample, current_root)
Expand Down
8 changes: 6 additions & 2 deletions singlem/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ def add_less_common_pipe_arguments(argument_group):
summarise_taxonomic_profile_output_args.add_argument('--output-species-by-site-level', help="Output site by species level to this file. Requires --output-species-by-site-relative-abundance.", choices=['species','genus','family','order','class','phylum','domain'], default='species')
summarise_taxonomic_profile_output_args.add_argument('--output-species-by-site-relative-abundance-prefix', metavar='PATH_PREFIX', help="Output site by species relative abundance to this file prefix. One file will be written for each taxonomic level.")
summarise_taxonomic_profile_output_args.add_argument('--output-filled-taxonomic-profile', metavar='FILE', help="Output a taxonomic profile where the coverage of each taxon includes the coverage of each of its descendent taxons e.g. the d__Bacteria entry includes the p__Patescibacteria entry.")
summarise_taxonomic_profile_output_args.add_argument('--output-taxonomic-profile-with-extras', metavar='FILE', help="Output a taxonomic profile with extra information (coverage, 'filled' coverage, relative abundance, taxonomy level). ")
summarise_taxonomic_profile_output_args.add_argument('--output-taxonomic-profile-with-extras', metavar='FILE', help="Output a taxonomic profile with extra information (coverage, 'filled' coverage, relative abundance, taxonomy level).")
summarise_taxonomic_profile_output_args.add_argument('--num-decimal-places', metavar='INT', type=int, help="Number of decimal places to report in the coverage column of the --output-taxonomic-profile-with-extras [default: 2].")
summarise_taxonomic_profile_output_args.add_argument('--output-taxonomic-level-coverage', metavar='FILE', help="Output summary of how much coverage has been assigned to each taxonomic level in a taxonomic profile to a TSV file.")

summarise_otu_table_input_args = summarise_parser.add_argument_group('OTU table input')
Expand Down Expand Up @@ -844,6 +845,8 @@ def get_min_taxon_coverage(args, subparser='pipe'):
raise Exception("--output-filled-taxonomic-profile requires --input-taxonomic-profiles to be defined")
if args.output_taxonomic_profile_with_extras and not args.input_taxonomic_profiles:
raise Exception("--output-taxonomic-profile-with-extras requires --input-taxonomic-profiles to be defined")
if args.num_decimal_places and not args.output_taxonomic_profile_with_extras:
raise Exception("--num-decimal-places currently requires --output-taxonomic-profile-with-extras to be defined")

if args.stream_inputs or args.unaligned_sequences_dump_file:
from singlem.otu_table_collection import StreamingOtuTableCollection
Expand Down Expand Up @@ -1034,7 +1037,8 @@ def get_min_taxon_coverage(args, subparser='pipe'):
with open(args.output_taxonomic_profile_with_extras, 'w') as f:
Summariser.write_taxonomic_profile_with_extras(
input_taxonomic_profile_files = args.input_taxonomic_profiles,
output_taxonomic_profile_extras_io = f)
output_taxonomic_profile_extras_io = f,
num_decimal_places = args.num_decimal_places)
else:
raise Exception("Expected --output-taxonomic-profile-krona or --output-site-by-species-relative-abundance or --output-taxonomic-level-coverage to be defined, since --input-taxonomic-profiles was defined")

Expand Down
9 changes: 6 additions & 3 deletions singlem/summariser.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,11 @@ def write_filled_taxonomic_profile(**kwargs):
def write_taxonomic_profile_with_extras(**kwargs):
input_taxonomic_profile_files = kwargs.pop('input_taxonomic_profile_files')
output_io = kwargs.pop('output_taxonomic_profile_extras_io')
num_decimal_places = kwargs.pop('num_decimal_places') # Default to 2 below
if len(kwargs) > 0:
raise Exception("Unexpected arguments detected: %s" % kwargs)
if num_decimal_places is None:
num_decimal_places = 2

logging.info("Writing taxonomic profile with extras")

Expand All @@ -642,9 +645,9 @@ def write_taxonomic_profile_with_extras(**kwargs):
full_coverage = wn.get_full_coverage()
print("\t".join([
profile.sample,
str(wn.coverage),
str(round(full_coverage, 2)),
str(round(full_coverage / total_coverage * 100, 2)),
str(round(wn.coverage, num_decimal_places)),
str(round(full_coverage, num_decimal_places)),
str(round(full_coverage / total_coverage * 100, num_decimal_places)),
str(levels[level]),
'; '.join(wn.get_taxonomy())
]), file=output_io)
Expand Down

0 comments on commit 272f714

Please sign in to comment.