dcdaebefa1b64bd3cb174191bdfa11240cc22a52
[htsworkflow.git] / scripts / runfolder
1 #!/usr/bin/env python
2 """
3 Runfolder.py can generate a xml file capturing all the 'interesting' parameters from a finished pipeline run. (using the -a option). The information currently being captured includes:
4
5   * Flowcell ID
6   * run dates
7   * start/stop cycle numbers
8   * Firecrest, bustard, gerald version numbers
9   * Eland analysis types, and everything in the eland configuration file.
10   * cluster numbers and other values from the Summary.htm 
11     LaneSpecificParameters table. 
12   * How many reads mapped to a genome from an eland file
13
14 The ELAND "mapped reads" counter will also check for eland squashed file
15 that were symlinked from another directory. This is so I can track how 
16 many reads landed on the genome of interest and on the spike ins. 
17
18 Basically my subdirectories something like:
19
20 genomes/hg18
21 genomes/hg18/chr*.2bpb <- files for hg18 genome
22 genomes/hg18/chr*.vld  
23 genomes/hg18/VATG.fa.2bp <- symlink to genomes/spikeins
24 genomes/spikein 
25
26 runfolder.py can also spit out a simple summary report (-s option) 
27 that contains the per lane post filter cluster numbers and the mapped 
28 read counts. (The report isn't currently very pretty)
29 """
30 from glob import glob
31 import logging
32 import optparse
33 import os
34 import sys
35
36 from htsworkflow.pipelines import runfolder
37 from htsworkflow.pipelines.runfolder import ElementTree
38         
39 def make_parser():
40     usage = 'usage: %prog [options] runfolder_root_dir'
41     parser = optparse.OptionParser(usage)
42
43     parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
44                       default=False,
45                       help='turn on verbose mode')
46     commands = optparse.OptionGroup(parser, 'Commands')
47
48     commands.add_option('-s', '--summary', dest='summary', action='store_true',
49                         default=False,
50                         help='produce summary report')
51     commands.add_option('-a', '--archive', dest='archive', action='store_true',
52                         default=False,
53                         help='generate run configuration archive')
54     commands.add_option('--extract-results', action='store_true',
55            default=False,
56            help='create run-xml summary, compress the eland result files, and '
57                 'copy them and the Summary.htm file into archival directory.')
58     parser.add_option_group(commands)
59
60     parser.add_option('-o', '--output-dir', default=None,
61            help="specify the default output directory for extract results")
62
63     parser.add_option('-u', '--use-run', dest='use_run', default=None,
64                       help='Specify which run to use instead of autoscanning '
65                            'the runfolder. You do this by providing the final '
66                            ' GERALD directory, and it assumes the parent '
67                            'directories are the bustard and image processing '
68                            'directories.')
69                     
70     parser.add_option('--run-xml', dest='run_xml',
71            default=None,
72            help='specify a run_<FlowCell>.xml file for summary reports')
73     
74
75     return parser
76
77 def main(cmdlist=None):
78     parser = make_parser()
79     opt, args = parser.parse_args(cmdlist)
80
81     logging.basicConfig()
82     if opt.verbose:
83         root_log = logging.getLogger()
84         root_log.setLevel(logging.INFO)
85
86     logging.info('Starting htsworkflow illumina runfolder processing tool.')
87     runs = []
88     if opt.run_xml:
89         # handle ~ shortcut
90         opt.run_xml = os.path.expanduser(opt.run_xml)
91         tree = ElementTree.parse(opt.run_xml).getroot()
92         runs.append(runfolder.PipelineRun(xml=tree))
93
94     # look for manually specified run
95     if opt.use_run is not None:
96         runs.append(runfolder.get_specific_run(opt.use_run))
97
98     # scan runfolders for runs
99     for run_pattern in args:
100         # expand args on our own if needed
101         for run_dir in glob(run_pattern):
102             runs.extend(runfolder.get_runs(run_dir))
103
104     if len(runs) > 0:
105         command_run = False
106         if opt.summary:
107             print runfolder.summary_report(runs)
108             command_run = True
109         if opt.archive:
110             runfolder.extract_run_parameters(runs)
111             command_run = True
112         if opt.extract_results:
113             runfolder.extract_results(runs, opt.output_dir)
114             command_run = True
115         if command_run == False:
116             print "You need to specify a command."+os.linesep
117             parser.print_help()
118     else:
119         print "You need to specify some run folders to process..."+os.linesep
120         parser.print_help()
121
122     return 0
123
124 if __name__ == "__main__":
125   sys.exit(main(sys.argv[1:]))