Source code

Download source archive ↓

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

1# Dan Blankenberg
2
3import importlib.util
4import os
5import shutil
6import sys
7
8from galaxy_utils.sequence.fastq import fastqReader, fastqWriter
9
10
11def main():
12    # Read command line arguments
13    input_filename = sys.argv[1]
14    script_filename = sys.argv[2]
15    output_filename = sys.argv[3]
16    additional_files_path = sys.argv[4]
17    input_type = sys.argv[5] or 'sanger'
18
19    # Save script file for debugging/verification info later
20    os.makedirs(additional_files_path, exist_ok=True)
21    new_script_path = os.path.join(additional_files_path, 'script.py')
22    shutil.copy(script_filename, new_script_path)
23
24    spec = importlib.util.spec_from_file_location(
25        "fastq_manipulator",
26        new_script_path,
27    )
28    assert spec
29    fastq_manipulator = importlib.util.module_from_spec(spec)
30    assert spec.loader
31    spec.loader.exec_module(fastq_manipulator)
32    i = None
33    reads_manipulated = 0
34
35    writer = fastqWriter(path=output_filename, format=input_type)
36    reader = fastqReader(path=input_filename, format=input_type)
37    with writer, reader:
38        for i, fastq_read in enumerate(reader):
39            new_read = fastq_manipulator.match_and_manipulate_read(fastq_read)
40            if new_read:
41                writer.write(new_read)
42            if new_read != fastq_read:
43                reads_manipulated += 1
44
45    if i is None:
46        print("Your file contains no valid FASTQ reads.")
47    else:
48        print(f'Manipulated {reads_manipulated} of {i + 1} reads ({float(reads_manipulated) / float(i + 1) * 100.0:.2f}%).')
49
50
51if __name__ == "__main__":
52    main()