8 Copyright (C) The Arvados Authors. All rights reserved.
10 SPDX-License-Identifier: CC-BY-SA-3.0
13 h2. Cancel a container request
15 {% codeblock as python %}
17 arvados.api().container_requests().update(uuid=container_request_uuid, body={"priority": 0}).execute()
20 h2. Cancel all container requests
22 {% codeblock as python %}
25 result = api.container_requests().list(filters=[["state", "=", "Committed"], ["priority", ">", "0"]]).execute()["items"]
26 for container_request in result:
27 api.container_requests().update(uuid=container_request["uuid"], body={"priority": 0}).execute()
30 h2. List completed container requests
32 {% codeblock as python %}
35 result = api.container_requests().list(filters=[["name", "like", name], ["state", "=", "Final"]]).execute()["items"]
36 container_uuids = [cr["container_uuid"] for cr in result]
37 containers = api.containers().list(filters=[["uuid", "in", container_uuids]]).execute()["items"]
38 container_dict = {c["uuid"]: c for c in containers}
40 for container_request in result:
41 container = container_dict[container_request["container_uuid"]]
42 print("%s, %s, %s" % (container_request["uuid"], container_request["name"], "Success" if container["exit_code"] == 0 else "Failed"))
45 h2. Get input of a CWL workflow
47 {% codeblock as python %}
50 container_request_uuid="qr1hi-xvhdp-zzzzzzzzzzzzzzz"
51 container_request = api.container_requests().get(uuid=container_request_uuid).execute()
52 print(container_request["mounts"]["/var/lib/cwl/cwl.input.json"])
55 h2. Get output of a CWL workflow
57 {% codeblock as python %}
59 import arvados.collection
61 container_request_uuid="qr1hi-xvhdp-zzzzzzzzzzzzzzz"
62 container_request = api.container_requests().get(uuid=container_request_uuid).execute()
63 collection = arvados.collection.CollectionReader(container_request["output_uuid"])
64 print(collection.open("cwl.output.json").read())
67 h2. Get state of a CWL workflow
69 {% codeblock as python %}
71 def get_cr_state(cr_uuid):
73 cr = api.container_requests().get(uuid=cr_uuid).execute()
74 if cr['container_uuid'] is None:
76 c = api.containers().get(uuid=cr['container_uuid']).execute()
77 if cr['state'] == 'Final' and c['state'] != 'Complete':
79 elif c['state'] in ['Locked', 'Queued']:
80 if c['priority'] == 0:
84 elif c['state'] == 'Complete' and c['exit_code'] != 0
86 elif c['state'] == 'Running':
87 if c['runtime_status'].get('error', None):
89 elif c['runtime_status'].get('warning', None):
92 container_request_uuid = 'qr1hi-xvhdp-zzzzzzzzzzzzzzz'
93 print(get_cr_state(container_request_uuid))
96 h2. List input of child requests
98 {% codeblock as python %}
101 parent_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz"
102 namefilter = "bwa%" # the "like" filter uses SQL pattern match syntax
103 container_request = api.container_requests().get(uuid=parent_request_uuid).execute()
104 parent_container_uuid = container_request["container_uuid"]
105 child_requests = api.container_requests().list(filters=[
106 ["requesting_container_uuid", "=", parent_container_uuid],
107 ["name", "like", namefilter]]).execute()
108 for c in child_requests["items"]:
109 print("%s" % c["name"])
110 for m in c["mounts"].values():
111 if "portable_data_hash" in m:
112 print(" %s" % m["portable_data_hash"])
115 h2. List output of child requests
117 {% codeblock as python %}
120 parent_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz"
121 namefilter = "bwa%" # the "like" filter uses SQL pattern match syntax
122 container_request = api.container_requests().get(uuid=parent_request_uuid).execute()
123 parent_container_uuid = container_request["container_uuid"]
124 child_requests = api.container_requests().list(filters=[
125 ["requesting_container_uuid", "=", parent_container_uuid],
126 ["name", "like", namefilter]]).execute()
127 output_uuids = [c["output_uuid"] for c in child_requests["items"]]
128 collections = api.collections().list(filters=[["uuid", "in", output_uuids]]).execute()
129 uuid_to_pdh = {c["uuid"]: c["portable_data_hash"] for c in collections["items"]}
130 for c in child_requests["items"]:
131 print("%s -> %s" % (c["name"], uuid_to_pdh[c["output_uuid"]]))
134 h2. List failed child requests
136 {% codeblock as python %}
139 parent_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz"
140 container_request = api.container_requests().get(uuid=parent_request_uuid).execute()
141 parent_container_uuid = container_request["container_uuid"]
142 child_requests = api.container_requests().list(filters=[
143 ["requesting_container_uuid", "=", parent_container_uuid]], limit=1000).execute()
144 child_containers = {c["container_uuid"]: c for c in child_requests["items"]}
145 cancelled_child_containers = api.containers().list(filters=[
146 ["exit_code", "!=", "0"],
147 ["uuid", "in", child_containers.keys()]], limit=1000).execute()
148 for c in cancelled_child_containers["items"]:
149 print("%s (%s)" % (child_containers[c["uuid"]]["name"], child_containers[c["uuid"]]["uuid"]))
152 h2. Get log of a child request
154 {% codeblock as python %}
156 import arvados.collection
158 container_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz"
159 container_request = api.container_requests().get(uuid=container_request_uuid).execute()
160 collection = arvados.collection.CollectionReader(container_request["log_uuid"])
162 print(collection.open(c).read())
165 h2(#sharing_link). Create a collection sharing link
167 {% codeblock as python %}
170 download="https://your.download.server"
171 collection_uuid="qr1hi-4zz18-zzzzzzzzzzzzzzz"
172 token = api.api_client_authorizations().create(body={"api_client_authorization":{"scopes": [
173 "GET /arvados/v1/collections/%s" % collection_uuid,
174 "GET /arvados/v1/collections/%s/" % collection_uuid,
175 "GET /arvados/v1/keep_services/accessible"]}}).execute()
176 print("%s/c=%s/t=%s/_/" % (download, collection_uuid, token["api_token"]))
179 h2. Combine two or more collections
181 Note, if two collections have files of the same name, the contents will be concatenated in the resulting manifest.
183 {% codeblock as python %}
185 import arvados.collection
187 project_uuid = "qr1hi-tpzed-zzzzzzzzzzzzzzz"
188 collection_uuids = ["qr1hi-4zz18-aaaaaaaaaaaaaaa", "qr1hi-4zz18-bbbbbbbbbbbbbbb"]
189 combined_manifest = ""
190 for u in collection_uuids:
191 c = api.collections().get(uuid=u).execute()
192 combined_manifest += c["manifest_text"]
193 newcol = arvados.collection.Collection(combined_manifest)
194 newcol.save_new(name="My combined collection", owner_uuid=project_uuid)
197 h2. Upload a file into a new collection
199 {% codeblock as python %}
201 import arvados.collection
203 project_uuid = "qr1hi-j7d0g-zzzzzzzzzzzzzzz"
204 collection_name = "My collection"
205 filename = "file1.txt"
208 c = arvados.collection.Collection()
209 with open(filename, "rb") as reader:
210 with c.open(filename, "wb") as writer:
211 content = reader.read(128*1024)
213 writer.write(content)
214 content = reader.read(128*1024)
215 c.save_new(name=collection_name, owner_uuid=project_uuid)
216 print("Saved %s to %s" % (collection_name, c.manifest_locator()))
219 h2. Download a file from a collection
221 {% codeblock as python %}
223 import arvados.collection
225 collection_uuid = "qr1hi-4zz18-zzzzzzzzzzzzzzz"
226 filename = "file1.txt"
229 c = arvados.collection.CollectionReader(collection_uuid)
230 with c.open(filename, "rb") as reader:
231 with open(filename, "wb") as writer:
232 content = reader.read(128*1024)
234 writer.write(content)
235 content = reader.read(128*1024)
236 print("Finished downloading %s" % filename)
239 h2. Copy files from a collection a new collection
241 {% codeblock as python %}
242 import arvados.collection
244 source_collection = "x1u39-4zz18-krzg64ufvehgitl"
245 target_project = "x1u39-j7d0g-67q94einb8ptznm"
246 target_name = "Files copied from source_collection"
247 files_to_copy = ["folder1/sample1/sample1_R1.fastq",
248 "folder1/sample2/sample2_R1.fastq"]
250 source = arvados.collection.CollectionReader(source_collection)
251 target = arvados.collection.Collection()
253 for f in files_to_copy:
254 target.copy(f, "", source_collection=source)
256 target.save_new(name=target_name, owner_uuid=target_project)
257 print("Created collection %s" % target.manifest_locator())