run_fastqc.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/python
  2. """This module runs the initial QC process."""
  3. import json
  4. import os
  5. import sys
  6. from contextlib import suppress
  7. BASE_COMMAND = "perl -- /FastQC/fastqc"
  8. MOUNT_PATHS = json.loads(os.environ.get("MOUNT_PATHS"))
  9. INPUT_FOLDER = MOUNT_PATHS["input"] + "/"
  10. # If a specific environment variable is set, appends the respective option.
  11. options = ""
  12. with suppress(Exception):
  13. adapters = MOUNT_PATHS["globals"]["ADAPTERS"]
  14. options += f" --adapters {adapters}/qc_adapters.txt"
  15. kmers = os.environ.get("KMERS")
  16. if kmers is not None:
  17. options += f" --kmers {kmers}"
  18. svg = os.environ.get("SVG")
  19. if svg is not None and svg == "true":
  20. options += " --svg"
  21. if not options:
  22. print("Running with default options.")
  23. else:
  24. print("Specified options:" + options)
  25. # Iterates over all sample directories and processes them conserving the directory structure.
  26. for root, dirs, files in os.walk(INPUT_FOLDER):
  27. if len(files) > 0:
  28. for file in files:
  29. if file.casefold().endswith(".fq.gz") or file.casefold().endswith(".fastq.gz"):
  30. file_input_path = os.path.join(root, file)
  31. folder_output_path = os.path.join(
  32. MOUNT_PATHS["output"],
  33. root.removeprefix(INPUT_FOLDER)
  34. )
  35. full_command = (f"{BASE_COMMAND}{options} "
  36. f"--outdir={folder_output_path} "
  37. f"{file_input_path}")
  38. os.makedirs(folder_output_path, exist_ok = True)
  39. exit_code = os.waitstatus_to_exitcode(os.system(full_command))
  40. if exit_code != 0:
  41. sys.exit(exit_code)