annotate 0.3.0/bin/prepare_nanopore_fastq_dir.py @ 92:295c2597a475

"planemo upload"
author kkonganti
date Tue, 19 Jul 2022 10:07:24 -0400
parents
children
rev   line source
kkonganti@92 1 #!/usr/bin/env python3
kkonganti@92 2
kkonganti@92 3 import os
kkonganti@92 4 import re
kkonganti@92 5 import glob
kkonganti@92 6 import argparse
kkonganti@92 7 import logging
kkonganti@92 8
kkonganti@92 9 def main():
kkonganti@92 10 # READ IN ARGUMENTS
kkonganti@92 11 desc = """
kkonganti@92 12 Takes in a file with flowcell ID, one per line and creates soft links
kkonganti@92 13 to 'fastq_pass' directory at target location.
kkonganti@92 14
kkonganti@92 15 Ex:
kkonganti@92 16
kkonganti@92 17 prepare_nanopore_fastq_dir.py \
kkonganti@92 18 -o /hpc/scratch/Kranti.Konganti/np_test \
kkonganti@92 19 -f flowcells.txt
kkonganti@92 20
kkonganti@92 21 where flowcells.txt contains the following lines:
kkonganti@92 22
kkonganti@92 23 FAL11127
kkonganti@92 24 FAL11151
kkonganti@92 25
kkonganti@92 26 """
kkonganti@92 27 parser = argparse.ArgumentParser(prog='prepare_nanopore_fastq_dir.py',
kkonganti@92 28 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
kkonganti@92 29 description=desc)
kkonganti@92 30 required = parser.add_argument_group('required arguments')
kkonganti@92 31
kkonganti@92 32 required.add_argument("-f", dest='flowcells', required=True,
kkonganti@92 33 help="Path to a text file containing Nanopore flowcell IDs, one per line")
kkonganti@92 34 required.add_argument("-i", dest='inputdir',
kkonganti@92 35 required=False, action='append', nargs='*',
kkonganti@92 36 help="Path to search directory. This directory location is where" +
kkonganti@92 37 " the presence of 'fastq_pass' will be searched for each flowcell.")
kkonganti@92 38 required.add_argument("-o", dest='outputdir',
kkonganti@92 39 required=True,
kkonganti@92 40 help="Path to output directory. This directory is created by the script" +
kkonganti@92 41 " and new soft links (symlinks) are created in this directory.")
kkonganti@92 42
kkonganti@92 43 args = parser.parse_args()
kkonganti@92 44 flowcells = args.flowcells
kkonganti@92 45 output = args.outputdir
kkonganti@92 46 inputs = args.inputdir
kkonganti@92 47
kkonganti@92 48 logging.basicConfig(format='%(asctime)s - %(levelname)s => %(message)s', level=logging.DEBUG)
kkonganti@92 49
kkonganti@92 50 if not inputs:
kkonganti@92 51 inputs = ['/projects/nanopore/raw']
kkonganti@92 52 nanopore_machines = ['RazorCrest', 'Revolution', 'ObiWan', 'MinIT',
kkonganti@92 53 'Mayhem', 'CaptainMarvel', 'MinION', 'MinION_Padmini', 'RogueOne']
kkonganti@92 54 logging.info(f"Searching default path(s). Use -i option if custom path should be searched.")
kkonganti@92 55 else:
kkonganti@92 56 nanopore_machines = ['custom']
kkonganti@92 57
kkonganti@92 58 fastq_pass_found = {}
kkonganti@92 59 was_fastq_pass_found = []
kkonganti@92 60
kkonganti@92 61 for each_input in inputs:
kkonganti@92 62 for machine in nanopore_machines:
kkonganti@92 63 if ''.join(nanopore_machines) != 'custom':
kkonganti@92 64 input = os.path.join(each_input, machine)
kkonganti@92 65 else:
kkonganti@92 66 input = ''.join(each_input)
kkonganti@92 67
kkonganti@92 68 logging.info(f"Searching path: {input}")
kkonganti@92 69
kkonganti@92 70 if (os.path.exists(flowcells) and os.path.getsize(flowcells) > 0):
kkonganti@92 71 with open(flowcells, 'r') as fcells:
kkonganti@92 72 for flowcell in fcells:
kkonganti@92 73 if re.match('^\s*$', flowcell):
kkonganti@92 74 continue
kkonganti@92 75 flowcell = flowcell.strip()
kkonganti@92 76 fastq_pass_path = glob.glob(os.path.join(input, flowcell, f"**", f"*[!fast5]*", 'fastq_pass'))
kkonganti@92 77 # Try one more time since the flowcell user is trying to query may be the parent directory
kkonganti@92 78 # of fastq_pass
kkonganti@92 79 fastq_pass = fastq_pass_path if fastq_pass_path else glob.glob(os.path.join(input, f"**", f"*[!fast5]*", flowcell, 'fastq_pass'))
kkonganti@92 80 if not fastq_pass:
kkonganti@92 81 # logging.warning(f"Flowcell " +
kkonganti@92 82 # os.path.join(input, flowcell).strip() +
kkonganti@92 83 # f" does not seem to have a fastq_pass directory! Skipped!!")
kkonganti@92 84 if not flowcell in fastq_pass_found.keys():
kkonganti@92 85 fastq_pass_found[flowcell] = 0
kkonganti@92 86 else:
kkonganti@92 87 fastq_pass_found[flowcell] = 1
kkonganti@92 88 sym_link_dir = os.path.join(output, flowcell)
kkonganti@92 89 sym_link_dir_dest = os.path.join(sym_link_dir, 'fastq_pass')
kkonganti@92 90 if not os.path.exists(sym_link_dir):
kkonganti@92 91 os.makedirs(sym_link_dir)
kkonganti@92 92 os.symlink(
kkonganti@92 93 ''.join(fastq_pass),
kkonganti@92 94 sym_link_dir_dest, target_is_directory=True
kkonganti@92 95 )
kkonganti@92 96 logging.info(f"New soft link created: {sym_link_dir_dest}")
kkonganti@92 97 else:
kkonganti@92 98 logging.info(f"Soft link {sym_link_dir_dest} already exists! Skipped!!")
kkonganti@92 99 fcells.close()
kkonganti@92 100 else:
kkonganti@92 101 logging.error(f"File {flowcells} is empty or does not exist!\n")
kkonganti@92 102
kkonganti@92 103 for k,v in fastq_pass_found.items():
kkonganti@92 104 if not v:
kkonganti@92 105 was_fastq_pass_found.append(k)
kkonganti@92 106
kkonganti@92 107 if was_fastq_pass_found:
kkonganti@92 108 logging.warning("Did not find fastq_pass folder for the supplied flowcells: " +
kkonganti@92 109 ', '.join(was_fastq_pass_found))
kkonganti@92 110
kkonganti@92 111 if was_fastq_pass_found and len(was_fastq_pass_found) == len(fastq_pass_found):
kkonganti@92 112 logging.error(f"None of the supplied flowcells were found! The output directory, {output} may not have been created!")
kkonganti@92 113 else:
kkonganti@92 114 logging.info(f"NOTE: Now you can use {output} directory as --input to cpipes.\n")
kkonganti@92 115
kkonganti@92 116 if __name__ == "__main__":
kkonganti@92 117 main()