8 Copyright (C) The Arvados Authors. All rights reserved.
10 SPDX-License-Identifier: CC-BY-SA-3.0
13 # "Cancel a container request":#cancel-a-container-request
14 # "Cancel all container requests":#cancel-all-container-requests
15 # "List completed container requests":#list-completed-container-requests
16 # "Get input of a CWL workflow":#get-input-of-a-cwl-workflow
17 # "Get output of a CWL workflow":#get-output-of-a-cwl-workflow
18 # "Get state of a CWL workflow":#get-state-of-a-cwl-workflow
19 # "List input of child requests":#list-input-of-child-requests
20 # "List output of child requests":#list-output-of-child-requests
21 # "List failed child requests":#list-failed-child-requests
22 # "Get log of a child request":#get-log-of-a-child-request
23 # "Create a collection sharing link":#sharing-link
24 # "Combine two or more collections":#combine-two-or-more-collections
25 # "Upload a file into a new collection":#upload-a-file-into-a-new-collection
26 # "Download a file from a collection":#download-a-file-from-a-collection
27 # "Copy files from a collection to a new collection":#copy-files-from-a-collection-to-a-new-collection
28 # "Copy files from a collection to another collection":#copy-files-from-a-collection-to-another-collection
29 # "Delete a file from an existing collection":#delete-a-file-from-an-existing-collection
30 # "Listing records with paging":#listing-records-with-paging
31 # "Querying the vocabulary definition":#querying-the-vocabulary-definition
32 # "Translating between vocabulary identifiers and labels":#translating-between-vocabulary-identifiers-and-labels
33 # "Create a Project":#create-a-project
35 h2(#cancel-a-container-request). Cancel a container request
37 {% codeblock as python %}
39 arvados.api().container_requests().update(uuid=container_request_uuid, body={"priority": 0}).execute()
42 h2(#cancel-all-container-requests). Cancel all container requests
44 {% codeblock as python %}
47 result = api.container_requests().list(filters=[["state", "=", "Committed"], ["priority", ">", "0"]]).execute()["items"]
48 for container_request in result:
49 api.container_requests().update(uuid=container_request["uuid"], body={"priority": 0}).execute()
52 h2(#list-completed-container-requests). List completed container requests
54 {% codeblock as python %}
57 result = api.container_requests().list(filters=[["name", "like", name], ["state", "=", "Final"]]).execute()["items"]
58 container_uuids = [cr["container_uuid"] for cr in result]
59 containers = api.containers().list(filters=[["uuid", "in", container_uuids]]).execute()["items"]
60 container_dict = {c["uuid"]: c for c in containers}
62 for container_request in result:
63 container = container_dict[container_request["container_uuid"]]
64 print("%s, %s, %s" % (container_request["uuid"], container_request["name"], "Success" if container["exit_code"] == 0 else "Failed"))
67 h2(#get-input-of-a-cwl-workflow). Get input of a CWL workflow
69 {% codeblock as python %}
72 container_request_uuid="zzzzz-xvhdp-zzzzzzzzzzzzzzz"
73 container_request = api.container_requests().get(uuid=container_request_uuid).execute()
74 print(container_request["mounts"]["/var/lib/cwl/cwl.input.json"])
77 h2(#get-output-of-a-cwl-workflow). Get output of a CWL workflow
79 {% codeblock as python %}
81 import arvados.collection
83 container_request_uuid="zzzzz-xvhdp-zzzzzzzzzzzzzzz"
84 container_request = api.container_requests().get(uuid=container_request_uuid).execute()
85 collection = arvados.collection.CollectionReader(container_request["output_uuid"])
86 print(collection.open("cwl.output.json").read())
89 h2(#get-state-of-a-cwl-workflow). Get state of a CWL workflow
91 {% codeblock as python %}
93 def get_cr_state(cr_uuid):
95 cr = api.container_requests().get(uuid=cr_uuid).execute()
96 if cr['container_uuid'] is None:
98 c = api.containers().get(uuid=cr['container_uuid']).execute()
99 if cr['state'] == 'Final' and c['state'] != 'Complete':
101 elif c['state'] in ['Locked', 'Queued']:
102 if c['priority'] == 0:
106 elif c['state'] == 'Complete' and c['exit_code'] != 0:
108 elif c['state'] == 'Running':
109 if c['runtime_status'].get('error', None):
111 elif c['runtime_status'].get('warning', None):
114 container_request_uuid = 'zzzzz-xvhdp-zzzzzzzzzzzzzzz'
115 print(get_cr_state(container_request_uuid))
118 h2(#list-input-of-child-requests). List input of child requests
120 {% codeblock as python %}
123 parent_request_uuid = "zzzzz-xvhdp-zzzzzzzzzzzzzzz"
124 namefilter = "bwa%" # the "like" filter uses SQL pattern match syntax
125 container_request = api.container_requests().get(uuid=parent_request_uuid).execute()
126 parent_container_uuid = container_request["container_uuid"]
127 child_requests = api.container_requests().list(filters=[
128 ["requesting_container_uuid", "=", parent_container_uuid],
129 ["name", "like", namefilter]]).execute()
130 for c in child_requests["items"]:
131 print("%s" % c["name"])
132 for m in c["mounts"].values():
133 if "portable_data_hash" in m:
134 print(" %s" % m["portable_data_hash"])
137 h2(#list-output-of-child-requests). List output of child requests
139 {% codeblock as python %}
142 parent_request_uuid = "zzzzz-xvhdp-zzzzzzzzzzzzzzz"
143 namefilter = "bwa%" # the "like" filter uses SQL pattern match syntax
144 container_request = api.container_requests().get(uuid=parent_request_uuid).execute()
145 parent_container_uuid = container_request["container_uuid"]
146 child_requests = api.container_requests().list(filters=[
147 ["requesting_container_uuid", "=", parent_container_uuid],
148 ["name", "like", namefilter]]).execute()
149 output_uuids = [c["output_uuid"] for c in child_requests["items"]]
150 collections = api.collections().list(filters=[["uuid", "in", output_uuids]]).execute()
151 uuid_to_pdh = {c["uuid"]: c["portable_data_hash"] for c in collections["items"]}
152 for c in child_requests["items"]:
153 print("%s -> %s" % (c["name"], uuid_to_pdh[c["output_uuid"]]))
156 h2(#list-failed-child-requests). List failed child requests
158 {% codeblock as python %}
161 parent_request_uuid = "zzzzz-xvhdp-zzzzzzzzzzzzzzz"
162 container_request = api.container_requests().get(uuid=parent_request_uuid).execute()
163 parent_container_uuid = container_request["container_uuid"]
164 child_requests = api.container_requests().list(filters=[
165 ["requesting_container_uuid", "=", parent_container_uuid]], limit=1000).execute()
166 child_containers = {c["container_uuid"]: c for c in child_requests["items"]}
167 cancelled_child_containers = api.containers().list(filters=[
168 ["exit_code", "!=", "0"],
169 ["uuid", "in", list(child_containers.keys())]], limit=1000).execute()
170 for c in cancelled_child_containers["items"]:
171 print("%s (%s)" % (child_containers[c["uuid"]]["name"], child_containers[c["uuid"]]["uuid"]))
174 h2(#get-log-of-a-child-request). Get log of a child request
176 {% codeblock as python %}
178 import arvados.collection
180 container_request_uuid = "zzzzz-xvhdp-zzzzzzzzzzzzzzz"
181 container_request = api.container_requests().get(uuid=container_request_uuid).execute()
182 collection = arvados.collection.CollectionReader(container_request["log_uuid"])
184 if isinstance(collection.find(c), arvados.arvfile.ArvadosFile):
185 print(collection.open(c).read())
188 h2(#sharing_link). Create a collection sharing link
190 {% codeblock as python %}
193 download="https://your.download.server"
194 collection_uuid="zzzzz-4zz18-zzzzzzzzzzzzzzz"
195 token = api.api_client_authorizations().create(body={"api_client_authorization":{"scopes": [
196 "GET /arvados/v1/collections/%s" % collection_uuid,
197 "GET /arvados/v1/collections/%s/" % collection_uuid,
198 "GET /arvados/v1/keep_services/accessible"]}}).execute()
199 print("%s/c=%s/t=%s/_/" % (download, collection_uuid, token["api_token"]))
202 h2(#combine-two-or-more-collections). Combine two or more collections
204 Note, if two collections have files of the same name, the contents will be concatenated in the resulting manifest.
206 {% codeblock as python %}
208 import arvados.collection
210 project_uuid = "zzzzz-j7d0g-zzzzzzzzzzzzzzz"
211 collection_uuids = ["zzzzz-4zz18-aaaaaaaaaaaaaaa", "zzzzz-4zz18-bbbbbbbbbbbbbbb"]
212 combined_manifest = ""
213 for u in collection_uuids:
214 c = api.collections().get(uuid=u).execute()
215 combined_manifest += c["manifest_text"]
216 newcol = arvados.collection.Collection(combined_manifest)
217 newcol.save_new(name="My combined collection", owner_uuid=project_uuid)
220 h2(#upload-a-file-into-a-new-collection). Upload a file into a new collection
222 {% codeblock as python %}
224 import arvados.collection
226 project_uuid = "zzzzz-j7d0g-zzzzzzzzzzzzzzz"
227 collection_name = "My collection"
228 filename = "file1.txt"
231 c = arvados.collection.Collection()
232 with open(filename, "rb") as reader:
233 with c.open(filename, "wb") as writer:
234 content = reader.read(128*1024)
236 writer.write(content)
237 content = reader.read(128*1024)
238 c.save_new(name=collection_name, owner_uuid=project_uuid)
239 print("Saved %s to %s" % (collection_name, c.manifest_locator()))
242 h2(#download-a-file-from-a-collection). Download a file from a collection
244 {% codeblock as python %}
246 import arvados.collection
248 collection_uuid = "zzzzz-4zz18-zzzzzzzzzzzzzzz"
249 filename = "file1.txt"
252 c = arvados.collection.CollectionReader(collection_uuid)
253 with c.open(filename, "rb") as reader:
254 with open(filename, "wb") as writer:
255 content = reader.read(128*1024)
257 writer.write(content)
258 content = reader.read(128*1024)
259 print("Finished downloading %s" % filename)
262 h2(#copy-files-from-a-collection-to-a-new-collection). Copy files from a collection to a new collection
264 {% codeblock as python %}
265 import arvados.collection
267 source_collection = "zzzzz-4zz18-zzzzzzzzzzzzzzz"
268 target_project = "zzzzz-j7d0g-zzzzzzzzzzzzzzz"
269 target_name = "Files copied from source_collection"
270 files_to_copy = ["folder1/sample1/sample1_R1.fastq",
271 "folder1/sample2/sample2_R1.fastq"]
273 source = arvados.collection.CollectionReader(source_collection)
274 target = arvados.collection.Collection()
276 for f in files_to_copy:
277 target.copy(f, "", source_collection=source)
279 target.save_new(name=target_name, owner_uuid=target_project)
280 print("Created collection %s" % target.manifest_locator())
283 h2(#copy-files-from-a-collection-to-another-collection). Copy files from a collection to another collection
285 {% codeblock as python %}
286 import arvados.collection
288 source_collection = "zzzzz-4zz18-zzzzzzzzzzzzzzz"
289 target_collection = "zzzzz-4zz18-aaaaaaaaaaaaaaa"
290 files_to_copy = ["folder1/sample1/sample1_R1.fastq",
291 "folder1/sample2/sample2_R1.fastq"]
293 source = arvados.collection.CollectionReader(source_collection)
294 target = arvados.collection.Collection(target_collection)
296 for f in files_to_copy:
297 target.copy(f, "", source_collection=source)
302 h2(#delete-a-file-from-an-existing-collection). Delete a file from an existing collection
304 {% codeblock as python %}
307 c = arvados.collection.Collection("zzzzz-4zz18-zzzzzzzzzzzzzzz")
308 c.remove("file2.txt")
312 h2(#listing-records-with-paging). Listing records with paging
314 Use the @arvados.util.keyset_list_all@ helper method to iterate over all the records matching an optional filter. This method handles paging internally and returns results incrementally using a Python iterator. The first parameter of the method takes a @list@ method of an Arvados resource (@collections@, @container_requests@, etc).
316 {% codeblock as python %}
320 for c in arvados.util.keyset_list_all(api.collections().list, filters=[["name", "like", "%sample123%"]]):
321 print("got collection " + c["uuid"])
324 h2(#querying-the-vocabulary-definition). Querying the vocabulary definition
326 The Python SDK provides facilities to interact with the "active metadata vocabulary":{{ site.baseurl }}/admin/metadata-vocabulary.html in the system. The developer can do key and value lookups in a case-insensitive manner:
328 {% codeblock as python %}
329 from arvados import api, vocabulary
330 voc = vocabulary.load_vocabulary(api('v1'))
332 [k.identifier for k in set(voc.key_aliases.values())]
333 # Example output: ['IDTAGCOLORS', 'IDTAGFRUITS', 'IDTAGCOMMENT', 'IDTAGIMPORTANCES', 'IDTAGCATEGORIES', 'IDTAGSIZES', 'IDTAGANIMALS']
334 voc['IDTAGSIZES'].preferred_label
335 # Example output: 'Size'
336 [v.preferred_label for v in set(voc['size'].value_aliases.values())]
337 # Example output: ['S', 'M', 'L', 'XL', 'XS']
338 voc['size']['s'].aliases
339 # Example output: ['S', 'small']
340 voc['size']['Small'].identifier
341 # Example output: 'IDVALSIZES2'
344 h2(#translating-between-vocabulary-identifiers-and-labels). Translating between vocabulary identifiers and labels
346 Client software might need to present properties to the user in a human-readable form or take input from the user without requiring them to remember identifiers. For these cases, there're a couple of conversion methods that take a dictionary as input like this:
348 {% codeblock as python %}
349 from arvados import api, vocabulary
350 voc = vocabulary.load_vocabulary(api('v1'))
352 voc.convert_to_labels({'IDTAGIMPORTANCES': 'IDVALIMPORTANCES1'})
353 # Example output: {'Importance': 'Critical'}
354 voc.convert_to_identifiers({'creature': 'elephant'})
355 # Example output: {'IDTAGANIMALS': 'IDVALANIMALS3'}
358 h2(#create-a-project). Create a Project
360 {% codeblock as python %}
363 parent_project_uuid = "zzzzz-j7d0g-zzzzzzzzzzzzzzz"
364 project_name = "My project"
366 g = arvados.api().groups().create(body={
368 "group_class": "project",
369 "owner_uuid": parent_project_uuid,
370 "name": project_name,
373 print("New project uuid is", g["uuid"])