annotate seal-galaxy-cc1b1911/seal/split_demux_output.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 import logging
244073d9abc1 Uploaded
crs4
parents:
diff changeset
23 import os
244073d9abc1 Uploaded
crs4
parents:
diff changeset
24 import sys
244073d9abc1 Uploaded
crs4
parents:
diff changeset
25
244073d9abc1 Uploaded
crs4
parents:
diff changeset
26 import pydoop.hdfs as phdfs
244073d9abc1 Uploaded
crs4
parents:
diff changeset
27
244073d9abc1 Uploaded
crs4
parents:
diff changeset
28 from hadoop_galaxy.pathset import FilePathset
244073d9abc1 Uploaded
crs4
parents:
diff changeset
29
244073d9abc1 Uploaded
crs4
parents:
diff changeset
30 Debug = os.environ.get('DEBUG', None)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
31 logging.basicConfig(level=logging.DEBUG if Debug else logging.INFO)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
32
244073d9abc1 Uploaded
crs4
parents:
diff changeset
33 def usage_error(msg=None):
244073d9abc1 Uploaded
crs4
parents:
diff changeset
34 if msg:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
35 print >> sys.stderr, msg
244073d9abc1 Uploaded
crs4
parents:
diff changeset
36 print >> sys.stderr, "Usage: %s OUTPUT_ID DEMUX_OUTPUT_PATHSET NEW_FILE_DIR" % os.path.basename(sys.argv[0])
244073d9abc1 Uploaded
crs4
parents:
diff changeset
37 sys.exit(1)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
38
244073d9abc1 Uploaded
crs4
parents:
diff changeset
39
244073d9abc1 Uploaded
crs4
parents:
diff changeset
40 class PathsetWriter(object):
244073d9abc1 Uploaded
crs4
parents:
diff changeset
41 # The format is dictated by the Galaxy documentation for tools that produce a variable
244073d9abc1 Uploaded
crs4
parents:
diff changeset
42 # number of output files: http://wiki.g2.bx.psu.edu/Admin/Tools/Multiple%20Output%20Files
244073d9abc1 Uploaded
crs4
parents:
diff changeset
43 # We fix the file_type to 'pathset'.
244073d9abc1 Uploaded
crs4
parents:
diff changeset
44 Galaxy_output_name_template = "primary_%s_%s_visible_pathset"
244073d9abc1 Uploaded
crs4
parents:
diff changeset
45
244073d9abc1 Uploaded
crs4
parents:
diff changeset
46 def __init__(self, output_dir, output_id, data_type):
244073d9abc1 Uploaded
crs4
parents:
diff changeset
47 self.output_dir = output_dir
244073d9abc1 Uploaded
crs4
parents:
diff changeset
48 self.output_id = output_id
244073d9abc1 Uploaded
crs4
parents:
diff changeset
49 self.data_type = data_type
244073d9abc1 Uploaded
crs4
parents:
diff changeset
50
244073d9abc1 Uploaded
crs4
parents:
diff changeset
51 def write_pathset(self, dataset_path, name):
244073d9abc1 Uploaded
crs4
parents:
diff changeset
52 """
244073d9abc1 Uploaded
crs4
parents:
diff changeset
53 dataset_path: the path of the dataset to which the new pathset needs to refer
244073d9abc1 Uploaded
crs4
parents:
diff changeset
54 name: name of dataset to appear in Galaxy
244073d9abc1 Uploaded
crs4
parents:
diff changeset
55 """
244073d9abc1 Uploaded
crs4
parents:
diff changeset
56 if not name:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
57 raise RuntimeError("Blank dataset name")
244073d9abc1 Uploaded
crs4
parents:
diff changeset
58 sanitized_name = name.replace('_', '-') # replace _ with - or galaxy won't like the name
244073d9abc1 Uploaded
crs4
parents:
diff changeset
59 opathset = FilePathset(dataset_path)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
60 opathset.set_datatype(self.data_type)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
61 opath = os.path.join(self.output_dir, self.Galaxy_output_name_template % (self.output_id, sanitized_name))
244073d9abc1 Uploaded
crs4
parents:
diff changeset
62 logging.debug("writing dataset path %s to pathset file %s", dataset_path, opath)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
63 with open(opath, 'w') as f:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
64 opathset.write(f)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
65 return self # to allow chaining
244073d9abc1 Uploaded
crs4
parents:
diff changeset
66
244073d9abc1 Uploaded
crs4
parents:
diff changeset
67
244073d9abc1 Uploaded
crs4
parents:
diff changeset
68
244073d9abc1 Uploaded
crs4
parents:
diff changeset
69 def main():
244073d9abc1 Uploaded
crs4
parents:
diff changeset
70 if len(sys.argv) != 4:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
71 usage_error("Wrong number of arguments")
244073d9abc1 Uploaded
crs4
parents:
diff changeset
72
244073d9abc1 Uploaded
crs4
parents:
diff changeset
73 output_id, demux_data, dest_dir = sys.argv[1:]
244073d9abc1 Uploaded
crs4
parents:
diff changeset
74 logging.debug("input args: output_id, demux_data, dest_dir = %s", sys.argv[1:])
244073d9abc1 Uploaded
crs4
parents:
diff changeset
75
244073d9abc1 Uploaded
crs4
parents:
diff changeset
76 ipathset = FilePathset.from_file(demux_data)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
77 logging.debug("input path set: %s", ipathset)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
78
244073d9abc1 Uploaded
crs4
parents:
diff changeset
79 writer = PathsetWriter(dest_dir, output_id, ipathset.datatype)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
80
244073d9abc1 Uploaded
crs4
parents:
diff changeset
81 # ipathset points to the output directory given to demux. Inside it
244073d9abc1 Uploaded
crs4
parents:
diff changeset
82 # we should find all the project/sample subdirectories, plus 'unknown' (if there
244073d9abc1 Uploaded
crs4
parents:
diff changeset
83 # were any reads not attributable to a sample). So, we list the output
244073d9abc1 Uploaded
crs4
parents:
diff changeset
84 # dir and collect sample names and their paths. In theory, the pathset
244073d9abc1 Uploaded
crs4
parents:
diff changeset
85 # we receive as input should only contains the output from one demux; thus
244073d9abc1 Uploaded
crs4
parents:
diff changeset
86 # a sample should only occur once.
244073d9abc1 Uploaded
crs4
parents:
diff changeset
87 if len(ipathset) != 1:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
88 raise RuntimeError("Unexpected demux output pathset size of %d. Expected 1 (the demux output path)" % len(ipathset))
244073d9abc1 Uploaded
crs4
parents:
diff changeset
89
244073d9abc1 Uploaded
crs4
parents:
diff changeset
90 project_paths = \
244073d9abc1 Uploaded
crs4
parents:
diff changeset
91 filter(lambda p: os.path.basename(p)[0] not in ('_', '.'), # filter hadoop and regular hidden files
244073d9abc1 Uploaded
crs4
parents:
diff changeset
92 phdfs.ls(iter(ipathset).next()) # List the contents of the pathset. ls produces absolute paths
244073d9abc1 Uploaded
crs4
parents:
diff changeset
93 )
244073d9abc1 Uploaded
crs4
parents:
diff changeset
94 # Each project_path points to a directory containing the data from one project.
244073d9abc1 Uploaded
crs4
parents:
diff changeset
95 # There may also be a directory 'unknown'
244073d9abc1 Uploaded
crs4
parents:
diff changeset
96 for project_path in project_paths:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
97 if os.path.basename(project_path).lower() == 'unknown':
244073d9abc1 Uploaded
crs4
parents:
diff changeset
98 writer.write_pathset(project_path, 'unknown')
244073d9abc1 Uploaded
crs4
parents:
diff changeset
99 else:
244073d9abc1 Uploaded
crs4
parents:
diff changeset
100 for project_sample_path in phdfs.ls(project_path):
244073d9abc1 Uploaded
crs4
parents:
diff changeset
101 # take the last two elements of the path -- should be project, sample
244073d9abc1 Uploaded
crs4
parents:
diff changeset
102 complete_sample_name = "%s.%s" % tuple(project_sample_path.split(os.path.sep)[-2:])
244073d9abc1 Uploaded
crs4
parents:
diff changeset
103 writer.write_pathset(project_sample_path, complete_sample_name)
244073d9abc1 Uploaded
crs4
parents:
diff changeset
104
244073d9abc1 Uploaded
crs4
parents:
diff changeset
105 if __name__ == '__main__':
244073d9abc1 Uploaded
crs4
parents:
diff changeset
106 main()