mark the example submission rule files as being raw, so the escapes dont get confused
[htsworkflow.git] / scripts / htsw-srf
1 #!/usr/bin/python
2
3 import logging
4 import optparse
5 import os
6 import sys
7
8 from htsworkflow.pipelines import runfolder
9 from htsworkflow.pipelines.srf import make_srf_commands, make_qseq_commands, \
10                                       run_commands, pathname_to_run_name
11 from htsworkflow.pipelines.srf import ILLUMINA2SRF10, ILLUMINA2SRF11, SOLEXA2SRF
12
13 def make_parser():
14   usage = '%prog: [options] runfolder -l 1,2,3 [runfolder -l 5,6 ...]'
15
16   parser = optparse.OptionParser(usage)
17   parser.add_option('--dry-run', action='store_true',
18                     help='print what would be done',
19                     default=False)
20
21   parser.add_option('-d', '--dest-dir', dest='dest_dir',
22                     help='location to write srf files to',
23                     default='.')
24   parser.add_option('-s', '--site',
25                     help='site name',
26                     default='Individual')
27   parser.add_option('-l', '--lanes', dest='lanes', action="append",
28          default=[],
29          help='comma seperated list of lanes to add to srf'
30   )
31   parser.add_option('-j', '--jobs', default=1, type='int',
32                     help='how many jobs to run simultaneously')
33   parser.add_option('-r', '--runfolder-version', default=ILLUMINA2SRF11, type='int',
34                     help='Which class of srf file should we attempt to create\n'
35                          '0 = Solexa pipeline 0.2.6 - 0.3\n'
36                          '1 = illumina pipeline 1.0\n'
37                          '2 = illumina pipeline 1.1rc1 and later \n')
38                      
39   parser.add_option('-v', '--verbose', dest='verbose',
40                     default=False, action='store_true',
41                     help='report more about internals (INFO)')
42   parser.add_option('--debug', dest='debug',
43                     default=False, action='store_true',
44                     help='report even more about internals (DEBUG)')
45  
46   return parser
47
48 def parse_lane_arg(lane_arg):
49     """
50     Convert comma sperated list of lane ids to a list of integers
51     """
52     lanes = []
53     for lane in lane_arg.split(','):
54         try:
55             lane = int(lane)
56             if lane < 1 or lane > 8:
57                 parser.error('Lanes must be in range [1..8]')
58             lanes.append(lane)
59         except ValueError:
60             parser.error('Lane selections must be integers')
61     return lanes
62
63 def main(cmdline=None):
64     parser = make_parser()
65     opts, args = parser.parse_args(cmdline)
66    
67     if opts.debug: 
68         logging.basicConfig(level=logging.DEBUG)
69     elif opts.verbose:
70         logging.basicConfig(level=logging.INFO)
71     else:
72         logging.basicConfig(level=logging.WARNING)
73
74     if len(args) == 0:
75         parser.error('need runfolder arguments')
76
77     # parse lane arguemnts
78     lanes_list = []
79     if len(opts.lanes) == 0:
80         lanes_list = [[1,2,3,4,5,6,7,8]] * len(args)
81     elif len(opts.lanes) == len(args):
82         for lane_arg in opts.lanes:
83             lanes_list.append(parse_lane_arg(lane_arg))
84     else:
85         parser.error(
86           "Number of lane arguments must match number of runfolders"
87         )
88
89     make_commands = make_qseq_commands
90     # build list of commands
91     cmds = {}
92     for runfolder_path, lanes in zip(args, lanes_list):
93         # normalize paths, either relative to home dirs or current dir
94         runfolder_path = os.path.abspath(runfolder_path)
95         run_name = pathname_to_run_name(runfolder_path)
96         # so any bustard directories?
97         runs = runfolder.get_runs(runfolder_path)
98         # give up if there are anything other than 1 run
99         if len(runs) > 1:
100           print 'ERROR: Too many run directories in %s' %(runfolder_path,)
101           return 1
102         elif len(runs) == 1:
103           bustard_dir = runs[0].bustard.pathname
104           cmds[bustard_dir] = make_commands(run_name,
105                                             bustard_dir,
106                                             lanes,
107                                             opts.site,
108                                             opts.dest_dir,
109                                             opts.runfolder_version)
110         else:
111           print "ERROR: Couldn't find a bustard directory in", runfolder_path
112           return 1
113
114     if not opts.dry_run:
115       for cwd, cmd_list in cmds.items():
116         run_commands(cwd, cmd_list, opts.jobs)
117     else:
118       for cwd, cmd_list in cmds.items():
119         print cwd
120         print cmd_list
121         print 'jobs: ', opts.jobs
122
123     return 0
124
125 if __name__ == "__main__":
126     sys.exit(main(sys.argv[1:]))