Skip to content

Commit 3c0a8eb

Browse files
committed
Adding new script to update CWL dockerpull key
1 parent 423bfed commit 3c0a8eb

5 files changed

Lines changed: 208 additions & 5 deletions

File tree

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,95 @@ bioconductor-customprodb 1.28.0,1.26.0,1.24.0,1.22.0,1.14.0 Generate customized
149149
...
150150
```
151151
152+
### bioconda2cwldocker
153+
154+
This script reads a conda env yaml file and replace, for each package in the environment, the images defined
155+
in the CWL or Yaml files inside the directory pass in the option **cwl_path**
156+
157+
#### Example
158+
159+
We would like to use the CWLs defined in the repo https://github.com/ncbi/cwl-ngs-workflows-cbb with the versions
160+
defined in this conda env:
161+
162+
###### Conda env file
163+
164+
```yaml
165+
name: rnaseq
166+
channels:
167+
- conda-forge
168+
- bioconda
169+
- defaults
170+
dependencies:
171+
- bedtools=2.29.2
172+
- fastqc=0.11.9
173+
- sra-tools=2.10.8
174+
- star=2.7.5a
175+
```
176+
177+
###### Cloning the repo
178+
179+
```bash
180+
$ git clone https://github.com/ncbi/cwl-ngs-workflows-cbb
181+
Cloning into 'cwl-ngs-workflows-cbb'...
182+
remote: Enumerating objects: 94, done.
183+
remote: Counting objects: 100% (94/94), done.
184+
remote: Compressing objects: 100% (69/69), done.
185+
remote: Total 1924 (delta 47), reused 50 (delta 25), pack-reused 1830
186+
Receiving objects: 100% (1924/1924), 319.82 KiB | 3.48 MiB/s, done.
187+
Resolving deltas: 100% (1216/1216), done.
188+
```
189+
190+
###### Print defined images for the tools in the CWLs
191+
192+
```bash
193+
$ cat cwl-ngs-workflows-cbb/tools/bedtools/bedtools.yml
194+
class: DockerRequirement
195+
dockerPull: quay.io/biocontainers/bedtools:2.28.0--hdf88d34_0
196+
197+
$ cat cwl-ngs-workflows-cbb/tools/fastqc/fastqc.yml
198+
class: DockerRequirement
199+
dockerPull: quay.io/biocontainers/fastqc:0.11.8--1
200+
201+
$ cat cwl-ngs-workflows-cbb/tools/sra-toolkit/sra-toolkit.yml
202+
class: DockerRequirement
203+
dockerPull: quay.io/biocontainers/sra-tools:2.10.7--pl526haddd2b5_1
204+
205+
$ cat cwl-ngs-workflows-cbb/tools/star/star.yml
206+
class: DockerRequirement
207+
dockerPull: quay.io/biocontainers/star:2.7.5a--0
208+
```
209+
210+
###### Running bioconda2cwldocker
211+
212+
```bash
213+
$ bioconda2cwldocker --conda_env_file conda-env.yaml --cwl_path cwl-ngs-workflows-cbb/
214+
bedtools with version 2.29.2 update image to: quay.io/biocontainers/bedtools:2.29.2--hc088bd4_0
215+
cwl-ngs-workflows-cbb/tools/bedtools/bedtools.yml with old image replaced: quay.io/biocontainers/bedtools:2.28.0--hdf88d34_0
216+
fastqc with version 0.11.9 update image to: quay.io/biocontainers/fastqc:0.11.9--0
217+
cwl-ngs-workflows-cbb/tools/fastqc/fastqc.yml with old image replaced: quay.io/biocontainers/fastqc:0.11.8--1
218+
sra-tools with version 2.10.8 update image to: quay.io/biocontainers/sra-tools:2.10.8--pl526haddd2b5_0
219+
cwl-ngs-workflows-cbb/tools/sra-toolkit/sra-toolkit.yml with old image replaced: quay.io/biocontainers/sra-tools:2.10.7--pl526haddd2b5_1
220+
```
221+
222+
##### Print new defined images for the tools in the CWLs
223+
224+
```bash
225+
$ cat cwl-ngs-workflows-cbb/tools/bedtools/bedtools.yml
226+
class: DockerRequirement
227+
dockerPull: quay.io/biocontainers/bedtools:2.29.2--hc088bd4_0
228+
229+
$ cat cwl-ngs-workflows-cbb/tools/fastqc/fastqc.yml
230+
class: DockerRequirement
231+
dockerPull: quay.io/biocontainers/fastqc:0.11.9--0
232+
233+
$ cat cwl-ngs-workflows-cbb/tools/sra-toolkit/sra-toolkit.yml
234+
class: DockerRequirement
235+
dockerPull: quay.io/biocontainers/sra-tools:2.10.8--pl526haddd2b5_0
236+
237+
$ cat cwl-ngs-workflows-cbb/tools/star/star.yml
238+
class: DockerRequirement
239+
dockerPull: quay.io/biocontainers/star:2.7.5a--0
240+
```
152241
153242
## Install
154243

requirements/base.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
requests~=2.24.0
1+
requests~=2.24.0
2+
PyYAML~=5.3.1

setup.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def readme():
1717
'': 'src',
1818
},
1919
data_files=[('', ['README.md'])],
20-
version='0.0.2',
21-
description='Find biocontainer images for tools',
20+
version='0.0.3',
21+
description='Tools to synchronize bioconda packages and versions with Biocontainer images',
2222
long_description=readme(),
2323
long_description_content_type='text/markdown',
2424
license='Public Domain',
@@ -27,7 +27,8 @@ def readme():
2727
maintainer='Vera Alvarez, Roberto',
2828
maintainer_email='veraalva' '@' 'ncbi.nlm.nih.gov',
2929
url='https://github.com/BioContainers/bioconda2biocontainer',
30-
install_requires=['requests'],
30+
install_requires=['requests',
31+
'PyYAML'],
3132
classifiers=[
3233
'Development Status :: 3 - Alpha',
3334
'Intended Audience :: Science/Research',
@@ -52,7 +53,8 @@ def readme():
5253
entry_points={
5354
'console_scripts': [
5455
'bioconda2biocontainer = bioconda2biocontainer.entry_point:main',
55-
'biocontainers-search = bioconda2biocontainer.entry_point_search:main'
56+
'biocontainers-search = bioconda2biocontainer.entry_point_search:main',
57+
'bioconda2cwldocker = bioconda2biocontainer.entry_point_update_cwl_docker:main'
5658
],
5759
}
5860
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
4+
import yaml
5+
6+
from bioconda2biocontainer.update_cwl_docker_image import update_cwl_docker_from_tool_name
7+
8+
9+
def main():
10+
parser = argparse.ArgumentParser(
11+
description='Replace Docker image in CWL from conda env yaml file')
12+
13+
parser.add_argument('--conda_env_file', help='Conda env yaml file',
14+
required=True)
15+
parser.add_argument('--cwl_path', help='Path to the CWL directory',
16+
required=True)
17+
args = parser.parse_args()
18+
19+
with open(args.conda_env_file) as fin:
20+
conda_env = yaml.load(fin, Loader=yaml.FullLoader)
21+
if 'dependencies' in conda_env:
22+
for d in conda_env['dependencies']:
23+
update_cwl_docker_from_tool_name(d, args.cwl_path)
24+
25+
26+
if __name__ == '__main__':
27+
main()
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import os
2+
3+
import yaml
4+
5+
from bioconda2biocontainer.biocontainer import find_latest_image
6+
7+
PRINT_HEADER = True
8+
9+
10+
def __replace_docker_image(f, old, new, package_name, package_version):
11+
global PRINT_HEADER
12+
if PRINT_HEADER:
13+
print('{} with version {} update image to: {}'.format(
14+
package_name, package_version,
15+
new))
16+
PRINT_HEADER = False
17+
print('\t{} with old image replaced: {}'.format(f, old))
18+
with open(f) as fin:
19+
list_of_lines = fin.readlines()
20+
with open(f, 'w') as fout:
21+
for line in list_of_lines:
22+
if old in line:
23+
line = line.replace(old, new)
24+
fout.write(line)
25+
26+
27+
def __load_cwl(f, package_name, package_version, image_name):
28+
with open(f) as fin:
29+
try:
30+
y = yaml.load(fin, Loader=yaml.FullLoader)
31+
return y
32+
except yaml.scanner.ScannerError:
33+
pass
34+
return None
35+
36+
37+
def __replace_in_cwl(f, package_name, package_version, image_name):
38+
y = __load_cwl(f, package_name, package_version, image_name)
39+
if y:
40+
if 'hints' in y and 'DockerRequirement' in y['hints'] and \
41+
'dockerPull' in y['hints']['DockerRequirement'] and \
42+
y['hints']['DockerRequirement']['dockerPull'].split(':')[0] == \
43+
image_name.split(':')[0] and \
44+
y['hints']['DockerRequirement']['dockerPull'] != image_name:
45+
__replace_docker_image(f,
46+
y['hints']['DockerRequirement']['dockerPull'], image_name,
47+
package_name, package_version)
48+
49+
50+
def __replace_in_yml(f, package_name, package_version, image_name):
51+
y = __load_cwl(f, package_name, package_version, image_name)
52+
if y:
53+
if 'dockerPull' in y and y['dockerPull'].split(':')[0] == image_name.split(':')[0] and \
54+
y['dockerPull'] != image_name:
55+
__replace_docker_image(f,
56+
y['dockerPull'], image_name,
57+
package_name, package_version)
58+
59+
60+
def update_cwl_docker_from_biocontainers(package_name, package_version, cwl_path):
61+
biocontainer_image = find_latest_image(package_name, package_version, False,
62+
False, False, 'Docker', None)
63+
if isinstance(biocontainer_image, dict):
64+
for root, dirs, files in os.walk(cwl_path):
65+
for f in files:
66+
f = os.path.join(root, f)
67+
if f.endswith('.cwl'):
68+
__replace_in_cwl(f, package_name, package_version,
69+
biocontainer_image['image_name'])
70+
elif f.endswith('.yml') or f.endswith('.yaml'):
71+
__replace_in_yml(f, package_name, package_version,
72+
biocontainer_image['image_name'])
73+
else:
74+
print('There is not biocontainer image for {} version {}'.format(
75+
package_name, package_version))
76+
77+
78+
def update_cwl_docker_from_tool_name(tool, cwl_path):
79+
global PRINT_HEADER
80+
PRINT_HEADER = True
81+
if isinstance(tool, str) and '=' in tool:
82+
tool_version = tool.split('=')
83+
update_cwl_docker_from_biocontainers(
84+
tool_version[0], tool_version[1], cwl_path)

0 commit comments

Comments
 (0)