annotate seal-galaxy-cc1b1911/seal/recab_table_galaxy.py @ 0:244073d9abc1 draft default tip

Uploaded
author crs4
date Wed, 15 Oct 2014 09:41:10 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
244073d9abc1 Uploaded
crs4
parents:
diff changeset
1 #!/usr/bin/env python
244073d9abc1 Uploaded
crs4
parents:
diff changeset
2
244073d9abc1 Uploaded
crs4
parents:
diff changeset
3 # Copyright (C) 2011-2014 CRS4.
244073d9abc1 Uploaded
crs4
parents:
diff changeset
4 #
244073d9abc1 Uploaded
crs4
parents:
diff changeset
5 # This file is part of Seal.
244073d9abc1 Uploaded
crs4
parents:
diff changeset
6 #
244073d9abc1 Uploaded
crs4
parents:
diff changeset
7 # Seal is free software: you can redistribute it and/or modify it
244073d9abc1 Uploaded
crs4
parents:
diff changeset
8 # under the terms of the GNU General Public License as published by the Free
244073d9abc1 Uploaded
crs4
parents:
diff changeset
9 # Software Foundation, either version 3 of the License, or (at your option)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
10 # any later version.
244073d9abc1 Uploaded
crs4
parents:
diff changeset
11 #
244073d9abc1 Uploaded
crs4
parents:
diff changeset
12 # Seal is distributed in the hope that it will be useful, but
244073d9abc1 Uploaded
crs4
parents:
diff changeset
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
244073d9abc1 Uploaded
crs4
parents:
diff changeset
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
244073d9abc1 Uploaded
crs4
parents:
diff changeset
15 # for more details.
244073d9abc1 Uploaded
crs4
parents:
diff changeset
16 #
244073d9abc1 Uploaded
crs4
parents:
diff changeset
17 # You should have received a copy of the GNU General Public License along
244073d9abc1 Uploaded
crs4
parents:
diff changeset
18 # with Seal. If not, see <http://www.gnu.org/licenses/>.
244073d9abc1 Uploaded
crs4
parents:
diff changeset
19
244073d9abc1 Uploaded
crs4
parents:
diff changeset
20
244073d9abc1 Uploaded
crs4
parents:
diff changeset
21
244073d9abc1 Uploaded
crs4
parents:
diff changeset
22 """
244073d9abc1 Uploaded
crs4
parents:
diff changeset
23 Calls the Seal RecabTable tool. Then, it calls recab_table_fetch to
244073d9abc1 Uploaded
crs4
parents:
diff changeset
24 concatenate all the partial tables and create a single csv file.
244073d9abc1 Uploaded
crs4
parents:
diff changeset
25 """
244073d9abc1 Uploaded
crs4
parents:
diff changeset
26
244073d9abc1 Uploaded
crs4
parents:
diff changeset
27
244073d9abc1 Uploaded
crs4
parents:
diff changeset
28 # parameters:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
29 # INPUT_DATA
244073d9abc1 Uploaded
crs4
parents:
diff changeset
30 # OUTPUT
244073d9abc1 Uploaded
crs4
parents:
diff changeset
31 # VCF
244073d9abc1 Uploaded
crs4
parents:
diff changeset
32 # NUM_REDUCERS
244073d9abc1 Uploaded
crs4
parents:
diff changeset
33 # [OTHER]
244073d9abc1 Uploaded
crs4
parents:
diff changeset
34
244073d9abc1 Uploaded
crs4
parents:
diff changeset
35 import os
244073d9abc1 Uploaded
crs4
parents:
diff changeset
36 import sys
244073d9abc1 Uploaded
crs4
parents:
diff changeset
37
244073d9abc1 Uploaded
crs4
parents:
diff changeset
38 import hadoop_galaxy.pathset as pathset
244073d9abc1 Uploaded
crs4
parents:
diff changeset
39 import subprocess
244073d9abc1 Uploaded
crs4
parents:
diff changeset
40 import tempfile
244073d9abc1 Uploaded
crs4
parents:
diff changeset
41 import pydoop.hdfs as phdfs
244073d9abc1 Uploaded
crs4
parents:
diff changeset
42
244073d9abc1 Uploaded
crs4
parents:
diff changeset
43 # XXX: add --append-python-path to the possible arguments?
244073d9abc1 Uploaded
crs4
parents:
diff changeset
44
244073d9abc1 Uploaded
crs4
parents:
diff changeset
45 def usage_error(msg=None):
244073d9abc1 Uploaded
crs4
parents:
diff changeset
46 if msg:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
47 print >> sys.stderr, msg
244073d9abc1 Uploaded
crs4
parents:
diff changeset
48 print >> sys.stderr, os.path.basename(sys.argv[0]), "INPUT_DATA OUTPUT VCF NUM_REDUCERS [OTHER]"
244073d9abc1 Uploaded
crs4
parents:
diff changeset
49 sys.exit(1)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
50
244073d9abc1 Uploaded
crs4
parents:
diff changeset
51
244073d9abc1 Uploaded
crs4
parents:
diff changeset
52 def run_recab(input_path, output_path, vcf, num_red, other_args):
244073d9abc1 Uploaded
crs4
parents:
diff changeset
53 mydir = os.path.abspath(os.path.dirname(__file__))
244073d9abc1 Uploaded
crs4
parents:
diff changeset
54 cmd = [
244073d9abc1 Uploaded
crs4
parents:
diff changeset
55 'hadoop_galaxy',
244073d9abc1 Uploaded
crs4
parents:
diff changeset
56 '--input', input_path,
244073d9abc1 Uploaded
crs4
parents:
diff changeset
57 '--output', output_path,
244073d9abc1 Uploaded
crs4
parents:
diff changeset
58 '--executable', 'seal',
244073d9abc1 Uploaded
crs4
parents:
diff changeset
59 'recab_table',
244073d9abc1 Uploaded
crs4
parents:
diff changeset
60 '--vcf-file', vcf,
244073d9abc1 Uploaded
crs4
parents:
diff changeset
61 '--num-reducers', num_red
244073d9abc1 Uploaded
crs4
parents:
diff changeset
62 ]
244073d9abc1 Uploaded
crs4
parents:
diff changeset
63
244073d9abc1 Uploaded
crs4
parents:
diff changeset
64 if other_args:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
65 cmd.extend(other_args)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
66
244073d9abc1 Uploaded
crs4
parents:
diff changeset
67 # now execute the hadoop job
244073d9abc1 Uploaded
crs4
parents:
diff changeset
68 subprocess.check_call(cmd)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
69
244073d9abc1 Uploaded
crs4
parents:
diff changeset
70 def collect_table(pset, output_path):
244073d9abc1 Uploaded
crs4
parents:
diff changeset
71 # finally, fetch the result into the final output file
244073d9abc1 Uploaded
crs4
parents:
diff changeset
72 cmd = ['seal', 'recab_table_fetch']
244073d9abc1 Uploaded
crs4
parents:
diff changeset
73 cmd.extend(pset.get_paths())
244073d9abc1 Uploaded
crs4
parents:
diff changeset
74 cmd.append(output_path)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
75 try:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
76 # remove the file that galaxy creates. recab_table_fetch refuses to
244073d9abc1 Uploaded
crs4
parents:
diff changeset
77 # overwrite it
244073d9abc1 Uploaded
crs4
parents:
diff changeset
78 os.unlink(output_path)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
79 except IOError:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
80 pass
244073d9abc1 Uploaded
crs4
parents:
diff changeset
81 subprocess.check_call(cmd)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
82
244073d9abc1 Uploaded
crs4
parents:
diff changeset
83 def cleanup(out_pathset):
244073d9abc1 Uploaded
crs4
parents:
diff changeset
84 # clean-up job output
244073d9abc1 Uploaded
crs4
parents:
diff changeset
85 for path in out_pathset:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
86 try:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
87 print >> sys.stderr, "Deleting output path", path
244073d9abc1 Uploaded
crs4
parents:
diff changeset
88 phdfs.rmr(path)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
89 except StandardError as e:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
90 print >> sys.stderr, "Error!", str(e)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
91
244073d9abc1 Uploaded
crs4
parents:
diff changeset
92 def main(args):
244073d9abc1 Uploaded
crs4
parents:
diff changeset
93 if len(args) < 5:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
94 usage_error()
244073d9abc1 Uploaded
crs4
parents:
diff changeset
95
244073d9abc1 Uploaded
crs4
parents:
diff changeset
96 input_data = args[0]
244073d9abc1 Uploaded
crs4
parents:
diff changeset
97 final_output = args[1]
244073d9abc1 Uploaded
crs4
parents:
diff changeset
98 vcf = args[2]
244073d9abc1 Uploaded
crs4
parents:
diff changeset
99 num_reducers = args[3]
244073d9abc1 Uploaded
crs4
parents:
diff changeset
100 other = args[4:]
244073d9abc1 Uploaded
crs4
parents:
diff changeset
101
244073d9abc1 Uploaded
crs4
parents:
diff changeset
102 # Create a temporary pathset to reference the recab_table
244073d9abc1 Uploaded
crs4
parents:
diff changeset
103 # output directory
244073d9abc1 Uploaded
crs4
parents:
diff changeset
104 with tempfile.NamedTemporaryFile(mode='rwb') as tmp_pathset_file:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
105 try:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
106 run_recab(input_data, tmp_pathset_file.name, vcf, num_reducers, other)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
107 tmp_pathset_file.seek(0)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
108 out_paths = pathset.FilePathset.from_file(tmp_pathset_file)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
109 collect_table(out_paths, final_output)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
110 finally:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
111 cleanup(out_paths)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
112
244073d9abc1 Uploaded
crs4
parents:
diff changeset
113 if __name__ == "__main__":
244073d9abc1 Uploaded
crs4
parents:
diff changeset
114 main(sys.argv[1:])
244073d9abc1 Uploaded
crs4
parents:
diff changeset
115
244073d9abc1 Uploaded
crs4
parents:
diff changeset
116 # vim: et ai ts=2 sw=2