Source code

Download source archive ↓

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

1# Dan Blankenberg
2
3
4import sys
5
6from galaxy_utils.sequence.fastq import (
7    fastqAggregator,
8    fastqReader,
9)
10
11VALID_NUCLEOTIDES = ['A', 'C', 'G', 'T', 'N']
12VALID_COLOR_SPACE = list(map(str, range(7))) + ['.']
13SUMMARY_STAT_ORDER = ['read_count', 'min_score', 'max_score', 'sum_score', 'mean_score', 'q1', 'med_score', 'q3', 'iqr', 'left_whisker', 'right_whisker']
14
15
16def main():
17    input_filename = sys.argv[1]
18    output_filename = sys.argv[2]
19    input_type = sys.argv[3] or 'sanger'
20
21    aggregator = fastqAggregator()
22    num_reads = None
23    fastq_read = None
24    with fastqReader(path=input_filename, format=input_type) as reader:
25        for num_reads, fastq_read in enumerate(reader):
26            aggregator.consume_read(fastq_read)
27    out = open(output_filename, 'w')
28    valid_nucleotides = VALID_NUCLEOTIDES
29    if fastq_read:
30        if fastq_read.sequence_space == 'base':
31            out.write('#column\tcount\tmin\tmax\tsum\tmean\tQ1\tmed\tQ3\tIQR\tlW\trW\toutliers\tA_Count\tC_Count\tG_Count\tT_Count\tN_Count\tother_bases\tother_base_count\n')
32        else:
33            out.write('#column\tcount\tmin\tmax\tsum\tmean\tQ1\tmed\tQ3\tIQR\tlW\trW\toutliers\t0_Count\t1_Count\t2_Count\t3_Count\t4_Count\t5_Count\t6_Count\t._Count\tother_bases\tother_base_count\n')
34            valid_nucleotides = VALID_COLOR_SPACE
35    for i in range(aggregator.get_max_read_length()):
36        column_stats = aggregator.get_summary_statistics_for_column(i)
37        out.write(f'{i + 1:d}\t')
38        out.write("{:d}\t{:d}\t{:d}\t{:d}\t{:f}\t{:f}\t{:f}\t{:f}\t{:f}\t{:d}\t{:d}\t".format(*(column_stats[key] for key in SUMMARY_STAT_ORDER)))  # noqa: SFS201
39        out.write(f"{','.join(map(str, column_stats['outliers']))}\t")
40        base_counts = aggregator.get_base_counts_for_column(i)
41        for nuc in valid_nucleotides:
42            out.write(f"{base_counts.get(nuc, 0)}\t")
43        extra_nucs = sorted(nuc for nuc in base_counts.keys() if nuc not in valid_nucleotides)
44        out.write(f"{','.join(extra_nucs)}\t{','.join(str(base_counts[nuc]) for nuc in extra_nucs)}\n")
45    out.close()
46    if num_reads is None:
47        print("No valid FASTQ reads could be processed.")
48    else:
49        print(f"{num_reads + 1:d} FASTQ reads were processed.")
50        print(
51            "Based upon quality values and sequence characters, the input data is valid for:",
52            ", ".join(aggregator.get_valid_formats()) or "None"
53        )
54        ascii_range = aggregator.get_ascii_range()
55        decimal_range = aggregator.get_decimal_range()
56        print(f"Input ASCII range: {repr(ascii_range[0])}({ord(ascii_range[0]):d}) - {repr(ascii_range[1])}({ord(ascii_range[1]):d})")  # print(using repr, since \x00 (null) causes info truncation in galaxy when printed)
57        print(f"Input decimal range: {decimal_range[0]:d} - {decimal_range[1]:d}")
58
59
60if __name__ == "__main__":
61    main()