Try to make runfolder results extraction more robust
[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     parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
43                       default=False,
44                       help='turn on verbose mode')
45     parser.add_option('-s', '--summary', dest='summary', action='store_true',
46                       default=False,
47                       help='produce summary report')
48     parser.add_option('-a', '--archive', dest='archive', action='store_true',
49                       default=False,
50                       help='generate run configuration archive')
51     parser.add_option('--extract-results', action='store_true',
52            default=False,
53            help='extract result files out of runfolder into a simpler archive')
54     parser.add_option('-o', '--output-dir', default=None,
55            help="specify the default output directory for extract results")
56
57     parser.add_option('--run-xml', dest='run_xml',
58            default=None,
59            help='specify a run_<FlowCell>.xml file for summary reports')
60     
61
62     return parser
63
64 def main(cmdlist=None):
65     parser = make_parser()
66     opt, args = parser.parse_args(cmdlist)
67
68     logging.basicConfig()
69     if opt.verbose:
70         root_log = logging.getLogger()
71         root_log.setLevel(logging.INFO)
72
73     logging.info('Starting htsworkflow illumina runfolder processing tool.')
74     runs = []
75     if opt.run_xml:
76         # handle ~ shortcut
77         opt.run_xml = os.path.expanduser(opt.run_xml)
78         tree = ElementTree.parse(opt.run_xml).getroot()
79         runs.append(runfolder.PipelineRun(xml=tree))
80     for run_pattern in args:
81         # expand args on our own if needed
82         for run_dir in glob(run_pattern):
83             runs.extend(runfolder.get_runs(run_dir))
84
85     if len(runs) > 0:
86         if opt.summary:
87             print runfolder.summary_report(runs)
88         if opt.archive:
89             runfolder.extract_run_parameters(runs)
90         if opt.extract_results:
91             runfolder.extract_results(runs, opt.output_dir)
92
93     return 0
94
95 if __name__ == "__main__":
96   sys.exit(main(sys.argv[1:]))