@@ -29,7 +29,8 @@ def docker_call(tool,
2929 """
3030 Calls Docker, passing along parameters and tool.
3131
32- :param str tool: Name of the Docker image to be used (e.g. quay.io/ucsc_cgl/samtools)
32+ :param str OR list[str] tool: Single str or list of names of the Docker images and order to be used
33+ (e.g. quay.io/ucsc_cgl/samtools)
3334 :param list[str] parameters: Command line arguments to be passed to the tool
3435 :param str work_dir: Directory to mount into the container via `-v`. Destination convention is /data
3536 :param bool rm: Set to True to pass `--rm` flag.
@@ -83,21 +84,62 @@ def docker_call(tool,
8384 if env :
8485 for e , v in env .iteritems ():
8586 base_docker_call .extend (['-e' , '{}={}' .format (e , v )])
87+
8688 if docker_parameters :
8789 base_docker_call += docker_parameters
88-
89- _log .debug ("Calling docker with %s." % " " .join (base_docker_call + [tool ] + parameters ))
90-
91- docker_call = base_docker_call + [tool ] + parameters
92-
90+
91+ docker_call = []
92+
93+ # Pipe functionality
94+ shell_flag = False # Flag for running subprocess with string command or list command
95+ if '|' in parameters :
96+ command_list = []
97+ if isinstance (tool , list ):
98+ # If tool is a list of docker tool strings
99+ # Parse pipes if they exist into list of commands
100+ bar_indices = [idx for idx , arg in enumerate (parameters ) if arg == '|' ]
101+ command = parameters [0 :bar_indices [0 ]]
102+ command [0 ] = "'" + command [0 ]
103+ command [len (command )- 1 ] += "'"
104+ command_list .append (command )
105+ prev_idx = bar_indices [0 ]
106+ for idx in bar_indices [1 :]:
107+ command = parameters [prev_idx + 1 :idx ]
108+ command_list .append (command )
109+ prev_idx = idx
110+
111+ command = parameters [prev_idx + 1 :]
112+ command_list .append (command )
113+ for i in len (tool ):
114+ docker_call .extend (base_docker_call + ['--entrypoint /bin/bash' , tool [i ], '-c' ] + command_list [i ])
115+
116+ docker_call = " " .join (docker_call )
117+ shell_flag = True
118+ _log .debug ("Calling docker with %s." % docker_call )
119+
120+ elif isinstance (tool , str ):
121+ # If tool is a string representing the docker tool name
122+ # Set pipes as a single command run under a single docker container
123+ parameters [0 ] = "'" + parameters [0 ]
124+ parameters [len (parameters )- 1 ] += "'"
125+ docker_call .extend (base_docker_call + ['--entrypoint /bin/bash' , tool , '-c' ] + parameters )
126+ docker_call = " " .join (docker_call )
127+ shell_flag = True
128+ _log .debug ("Calling docker with %s." % docker_call )
129+
130+ else :
131+ # Default behavior
132+ docker_call = base_docker_call + [tool ] + parameters
133+ _log .debug ("Calling docker with %s." % " " .join (docker_call ))
134+
93135 try :
94136 if outfile :
95- subprocess .check_call (docker_call , stdout = outfile )
137+ subprocess .check_call (docker_call , stdout = outfile , shell = shell_flag )
96138 else :
97139 if check_output :
98- return subprocess .check_output (docker_call )
140+ return subprocess .check_output (docker_call , shell = shell_flag )
99141 else :
100- subprocess .check_call (docker_call )
142+ subprocess .check_call (docker_call , shell = shell_flag )
101143 # Fix root ownership of output files
102144 except :
103145 # Panic avoids hiding the exception raised in the try block
0 commit comments