Modify the srf utility to tar.bz2 the qseq files instead of the using
[htsworkflow.git] / htsworkflow / pipelines / srf.py
1 from glob import glob
2 import logging
3 import os
4
5 from htsworkflow.util import queuecommands
6
7 SOLEXA2SRF = 0
8 ILLUMINA2SRF10 = 1
9 ILLUMINA2SRF11 = 2
10
11 def pathname_to_run_name(base):
12   """
13   Convert a pathname to a base runfolder name
14   handle the case with a trailing /
15
16   >>> print pathname_to_run_name("/a/b/c/run")
17   run
18   >>> print pathname_to_run_name("/a/b/c/run/")
19   run
20   >>> print pathname_to_run_name("run")
21   run
22   >>> print pathname_to_run_name("run/")
23   run
24   >>> print pathname_to_run_name("../run")
25   run
26   >>> print pathname_to_run_name("../run/")
27   run
28   """
29   name = ""
30   while len(name) == 0:
31     base, name = os.path.split(base)
32     if len(base) == 0:
33       break
34   return name
35
36 def make_srf_commands(run_name, bustard_dir, lanes, site_name, destdir, cmdlevel=ILLUMINA2SRF11):
37   """
38   make a subprocess-friendly list of command line arguments to run solexa2srf
39   generates files like: 
40   woldlab:080514_HWI-EAS229_0029_20768AAXX:8.srf
41    site        run name                    lane
42              
43   run_name - most of the file name (run folder name is a good choice)
44   lanes - list of integers corresponding to which lanes to process
45   site_name - name of your "sequencing site" or "Individual"
46   destdir - where to write all the srf files
47   """
48   # clean up pathname
49   logging.info("run_name %s" % ( run_name, ))
50   
51   cmd_list = []
52   for lane in lanes:
53     name_prefix = '%s_%%l_%%t_' % (run_name,)
54     destname = '%s_%s_%d.srf' % (site_name, run_name, lane)
55     destdir = os.path.normpath(destdir)
56     dest_path = os.path.join(destdir, destname)
57     seq_pattern = 's_%d_*_seq.txt' % (lane,)
58
59     if cmdlevel == SOLEXA2SRF:
60         cmd = ['solexa2srf', 
61                '-N', name_prefix,
62                '-n', '%3x:%3y', 
63                '-o', dest_path, 
64                seq_pattern]
65     elif cmdlevel == ILLUMINA2SRF10:
66         cmd = ['illumina2srf', 
67                '-v1.0',
68                '-o', dest_path,
69                seq_pattern]
70     elif cmdlevel == ILLUMINA2SRF11:
71         seq_pattern = 's_%d_*_qseq.txt' % (lane,)
72         cmd = ['illumina2srf', 
73                '-o', dest_path,
74                seq_pattern]
75     else:
76         raise ValueError("Unrecognized run level %d" % (cmdlevel,))
77
78     logging.info("Generated command: " + " ".join(cmd))
79     cmd_list.append(" ".join(cmd))
80   return cmd_list
81
82 def create_qseq_patterns(bustard_dir):
83   """
84   Scan a bustard directory for qseq files and determine a glob pattern
85   """
86   # grab one tile for each lane.
87   qseqs = glob(os.path.join(bustard_dir, '*_0001_qseq.txt'))
88   qseqs = [ os.path.split(x)[-1] for x in qseqs ]
89   if len(qseqs[0].split('_')) == 4:
90     # single ended
91     return [(None,"s_%d_[0-9][0-9][0-9][0-9]_qseq.txt")]
92   elif len(qseqs[0].split('_')) == 5:
93     # more than 1 read
94     # build a dictionary of read numbers by lane
95     # ( just in case we didn't run all 8 lanes )
96     lanes = {}
97     for q in qseqs:
98       sample, lane, read, tile, extension = q.split('_')
99       lanes.setdefault(lane, []).append(read)
100     qseq_patterns = []
101     # grab a lane from the dictionary
102     # I don't think it matters which one.
103     k = lanes.keys()[0]
104     # build the list of patterns
105     for read in lanes[k]:
106       read = int(read)
107       qseq_patterns.append((read, 's_%d_' + '%d_[0-9][0-9][0-9][0-9]_qseq.txt' % (read,)))
108     return qseq_patterns
109   else:
110     raise RuntimeError('unrecognized qseq pattern, not a single or multiple read pattern')
111   
112 def make_qseq_commands(run_name, bustard_dir, lanes, site_name, destdir, cmdlevel=ILLUMINA2SRF11):
113   """
114   make a subprocess-friendly list of command line arguments to run solexa2srf
115   generates files like: 
116   woldlab:080514_HWI-EAS229_0029_20768AAXX:8.srf
117    site        run name                    lane
118              
119   run_name - most of the file name (run folder name is a good choice)
120   lanes - list of integers corresponding to which lanes to process
121   site_name - name of your "sequencing site" or "Individual"
122   destdir - where to write all the srf files
123   """
124   # clean up pathname
125   logging.info("run_name %s" % ( run_name, ))
126   
127   cmd_list = []
128   for lane in lanes:
129     name_prefix = '%s_%%l_%%t_' % (run_name,)
130     destdir = os.path.normpath(destdir)
131     qseq_patterns = create_qseq_patterns(bustard_dir)
132
133     for read, pattern in qseq_patterns:
134       if read is None:
135         destname = '%s_%s_l%d.tar.bz2' % (site_name, run_name, lane)
136         dest_path = os.path.join(destdir, destname)
137       else:
138         destname = '%s_%s_l%d_r%d.tar.bz2' % (site_name, run_name, lane, read)
139         dest_path = os.path.join(destdir, destname)
140         
141       cmd = " ".join(['tar', 'cjf', dest_path, pattern % (lane,) ])
142       logging.info("Generated command: " + cmd)
143       cmd_list.append(cmd)
144       
145   return cmd_list
146
147 def run_commands(new_dir, cmd_list, num_jobs):
148     logging.info("chdir to %s" % (new_dir,))
149     curdir = os.getcwd()
150     os.chdir(new_dir)
151     q = queuecommands.QueueCommands(cmd_list, num_jobs)
152     q.run()
153     os.chdir(curdir)
154     
155 def make_md5_commands(destdir):
156   """
157   Scan the cycle dir and create md5s for the contents
158   """
159   cmd_list = []
160   destdir = os.path.abspath(destdir)
161   bz2s = glob(os.path.join(destdir,"*.bz2"))
162   gzs = glob(os.path.join(destdir,"*gz"))
163   srfs = glob(os.path.join(destdir,"*.srf"))
164
165   file_list = bz2s + gzs + srfs
166
167   for f in file_list:
168       cmd = " ".join(['md5sum', f, '>', f+'.md5'])
169       logging.info('generated command: '+cmd)
170       cmd_list.append(cmd)
171
172   return cmd_list