diff --git a/singlem/condense.py b/singlem/condense.py index b043a963..efc431c8 100644 --- a/singlem/condense.py +++ b/singlem/condense.py @@ -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) diff --git a/singlem/main.py b/singlem/main.py index 7884e19c..0627a6c3 100755 --- a/singlem/main.py +++ b/singlem/main.py @@ -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') @@ -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 @@ -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") diff --git a/singlem/summariser.py b/singlem/summariser.py index 8315e3bb..613db4a5 100644 --- a/singlem/summariser.py +++ b/singlem/summariser.py @@ -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") @@ -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)