Source code

Download source archive ↓

upstream-dependencies/galaxy-sequence-utils/galaxy_utils/sequence/scripts/fastq_groomer.py

1# Dan Blankenberg
2
3import argparse
4
5from galaxy_utils.sequence.fastq import (
6    fastqAggregator,
7    fastqReader,
8    fastqVerboseErrorReader,
9    fastqWriter,
10)
11
12
13class Groomer:
14
15    def __init__(self, file_handle=None):
16        self.file_handle = file_handle
17        args = self._get_args()
18        self.input_filename = args.input_filename
19        self.input_type = args.input_type
20        self.output_filename = args.output_filename
21        self.output_type = args.output_type
22        self.force_quality_encoding = args.force_quality_encoding
23        self.summarize_input = args.summarize_input == 'summarize_input'
24        self.fix_id = args.fix_id
25
26        if self.force_quality_encoding == 'None':
27            self.force_quality_encoding = None
28
29    def run(self):
30        aggregator = fastqAggregator()
31        reader_class = fastqReader
32        if self.summarize_input:
33            reader_class = fastqVerboseErrorReader
34        read_count = None
35
36        writer = fastqWriter(
37            path=self.output_filename,
38            format=self.output_type,
39            force_quality_encoding=self.force_quality_encoding)
40        reader = reader_class(
41            fh=self.file_handle,
42            path=self.input_filename,
43            format=self.input_type,
44            apply_galaxy_conventions=True,
45            fix_id=self.fix_id)
46        with writer, reader:
47            for read_count, fastq_read in enumerate(reader):
48                if self.summarize_input:
49                    aggregator.consume_read(fastq_read)
50                writer.write(fastq_read)
51
52        self._print_output(read_count, aggregator)
53
54    def _print_output(self, read_count, aggregator):
55        if read_count is not None:
56            print(f"Groomed {read_count + 1:d} {self.input_type} reads into {self.output_type} reads.")
57            if self.input_type != self.output_type and 'solexa' in [self.input_type, self.output_type]:
58                print("Converted between Solexa and PHRED scores.")
59            if self.summarize_input:
60                print(
61                    "Based upon quality and sequence, the input data is valid for:",
62                    ", ".join(aggregator.get_valid_formats()) or "None"
63                )
64                ascii_range = aggregator.get_ascii_range()
65                decimal_range = aggregator.get_decimal_range()
66                # print using repr, since \x00 (null) causes info truncation in galaxy when printed
67                print(
68                    f"Input ASCII range: {repr(ascii_range[0])}({ord(ascii_range[0]):d}) - {repr(ascii_range[1])}({ord(ascii_range[1]):d})"
69                )
70                print(f"Input decimal range: {decimal_range[0]:d} - {decimal_range[1]:d}")
71        else:
72            print("No valid FASTQ reads were provided.")
73
74    def _get_args(self):
75        types = ['solexa', 'illumina', 'sanger', 'cssanger']
76        type_choices = [t + ext for t in types for ext in ['', '.gz', '.bz2']]
77        fqe_choices = ['None', 'ascii', 'decimal']
78        si_choices = ['summarize_input', 'dont_summarize_input']  # Should be yes/no (leave as is for now)
79
80        p = argparse.ArgumentParser()
81        p.add_argument('input_filename', help='file to groom')
82        p.add_argument('input_type', choices=type_choices, help='input FASTQ quality scores type')
83        p.add_argument('output_filename', help='groomed output file')
84        p.add_argument('output_type', choices=type_choices, help='input FASTQ quality scores type')
85        p.add_argument('force_quality_encoding', choices=fqe_choices, help='force quality score encoding')
86        p.add_argument('summarize_input', choices=si_choices, help='summarize input data')
87
88        fi_group = p.add_mutually_exclusive_group()
89        fi_group.add_argument(
90            '--fix-id', dest='fix_id', action='store_true', help='fix inconsistent identifiers')
91        fi_group.add_argument(
92            '--no-fix-id', dest='fix_id', action='store_false', help='do not fix inconsistent identifiers')
93        p.set_defaults(fix_id=True)
94
95        return p.parse_args()
96
97
98def main():
99    Groomer().run()
100
101
102if __name__ == "__main__":
103    main()