X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/2f66d4cc05e9442a9bb69969744d0750a02a1ed4..f7bf9d69603db2d500563648460e2a96524de266:/doc/sdk/python/cookbook.html.textile.liquid diff --git a/doc/sdk/python/cookbook.html.textile.liquid b/doc/sdk/python/cookbook.html.textile.liquid index bd7f64b33d..f3186ebbb6 100644 --- a/doc/sdk/python/cookbook.html.textile.liquid +++ b/doc/sdk/python/cookbook.html.textile.liquid @@ -47,7 +47,7 @@ h2. Get input of a CWL workflow {% codeblock as python %} import arvados api = arvados.api() -container_request_uuid="qr1hi-xvhdp-zzzzzzzzzzzzzzz" +container_request_uuid="zzzzz-xvhdp-zzzzzzzzzzzzzzz" container_request = api.container_requests().get(uuid=container_request_uuid).execute() print(container_request["mounts"]["/var/lib/cwl/cwl.input.json"]) {% endcodeblock %} @@ -58,7 +58,7 @@ h2. Get output of a CWL workflow import arvados import arvados.collection api = arvados.api() -container_request_uuid="qr1hi-xvhdp-zzzzzzzzzzzzzzz" +container_request_uuid="zzzzz-xvhdp-zzzzzzzzzzzzzzz" container_request = api.container_requests().get(uuid=container_request_uuid).execute() collection = arvados.collection.CollectionReader(container_request["output_uuid"]) print(collection.open("cwl.output.json").read()) @@ -81,7 +81,7 @@ def get_cr_state(cr_uuid): return 'On hold' else: return 'Queued' - elif c['state'] == 'Complete' and c['exit_code'] != 0 + elif c['state'] == 'Complete' and c['exit_code'] != 0: return 'Failed' elif c['state'] == 'Running': if c['runtime_status'].get('error', None): @@ -89,7 +89,7 @@ def get_cr_state(cr_uuid): elif c['runtime_status'].get('warning', None): return 'Warning' return c['state'] -container_request_uuid = 'qr1hi-xvhdp-zzzzzzzzzzzzzzz' +container_request_uuid = 'zzzzz-xvhdp-zzzzzzzzzzzzzzz' print(get_cr_state(container_request_uuid)) {% endcodeblock %} @@ -98,7 +98,7 @@ h2. List input of child requests {% codeblock as python %} import arvados api = arvados.api() -parent_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz" +parent_request_uuid = "zzzzz-xvhdp-zzzzzzzzzzzzzzz" namefilter = "bwa%" # the "like" filter uses SQL pattern match syntax container_request = api.container_requests().get(uuid=parent_request_uuid).execute() parent_container_uuid = container_request["container_uuid"] @@ -117,7 +117,7 @@ h2. List output of child requests {% codeblock as python %} import arvados api = arvados.api() -parent_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz" +parent_request_uuid = "zzzzz-xvhdp-zzzzzzzzzzzzzzz" namefilter = "bwa%" # the "like" filter uses SQL pattern match syntax container_request = api.container_requests().get(uuid=parent_request_uuid).execute() parent_container_uuid = container_request["container_uuid"] @@ -136,7 +136,7 @@ h2. List failed child requests {% codeblock as python %} import arvados api = arvados.api() -parent_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz" +parent_request_uuid = "zzzzz-xvhdp-zzzzzzzzzzzzzzz" container_request = api.container_requests().get(uuid=parent_request_uuid).execute() parent_container_uuid = container_request["container_uuid"] child_requests = api.container_requests().list(filters=[ @@ -144,7 +144,7 @@ child_requests = api.container_requests().list(filters=[ child_containers = {c["container_uuid"]: c for c in child_requests["items"]} cancelled_child_containers = api.containers().list(filters=[ ["exit_code", "!=", "0"], - ["uuid", "in", child_containers.keys()]], limit=1000).execute() + ["uuid", "in", list(child_containers.keys())]], limit=1000).execute() for c in cancelled_child_containers["items"]: print("%s (%s)" % (child_containers[c["uuid"]]["name"], child_containers[c["uuid"]]["uuid"])) {% endcodeblock %} @@ -155,11 +155,12 @@ h2. Get log of a child request import arvados import arvados.collection api = arvados.api() -container_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz" +container_request_uuid = "zzzzz-xvhdp-zzzzzzzzzzzzzzz" container_request = api.container_requests().get(uuid=container_request_uuid).execute() collection = arvados.collection.CollectionReader(container_request["log_uuid"]) for c in collection: - print(collection.open(c).read()) + if isinstance(collection.find(c), arvados.arvfile.ArvadosFile): + print(collection.open(c).read()) {% endcodeblock %} h2(#sharing_link). Create a collection sharing link @@ -168,7 +169,7 @@ h2(#sharing_link). Create a collection sharing link import arvados api = arvados.api() download="https://your.download.server" -collection_uuid="qr1hi-4zz18-zzzzzzzzzzzzzzz" +collection_uuid="zzzzz-4zz18-zzzzzzzzzzzzzzz" token = api.api_client_authorizations().create(body={"api_client_authorization":{"scopes": [ "GET /arvados/v1/collections/%s" % collection_uuid, "GET /arvados/v1/collections/%s/" % collection_uuid, @@ -184,8 +185,8 @@ Note, if two collections have files of the same name, the contents will be conca import arvados import arvados.collection api = arvados.api() -project_uuid = "qr1hi-tpzed-zzzzzzzzzzzzzzz" -collection_uuids = ["qr1hi-4zz18-aaaaaaaaaaaaaaa", "qr1hi-4zz18-bbbbbbbbbbbbbbb"] +project_uuid = "zzzzz-tpzed-zzzzzzzzzzzzzzz" +collection_uuids = ["zzzzz-4zz18-aaaaaaaaaaaaaaa", "zzzzz-4zz18-bbbbbbbbbbbbbbb"] combined_manifest = "" for u in collection_uuids: c = api.collections().get(uuid=u).execute() @@ -200,7 +201,7 @@ h2. Upload a file into a new collection import arvados import arvados.collection -project_uuid = "qr1hi-j7d0g-zzzzzzzzzzzzzzz" +project_uuid = "zzzzz-j7d0g-zzzzzzzzzzzzzzz" collection_name = "My collection" filename = "file1.txt" @@ -222,7 +223,7 @@ h2. Download a file from a collection import arvados import arvados.collection -collection_uuid = "qr1hi-4zz18-zzzzzzzzzzzzzzz" +collection_uuid = "zzzzz-4zz18-zzzzzzzzzzzzzzz" filename = "file1.txt" api = arvados.api() @@ -236,13 +237,13 @@ with c.open(filename, "rb") as reader: print("Finished downloading %s" % filename) {% endcodeblock %} -h2. Copy files from a collection a new collection +h2. Copy files from a collection to a new collection {% codeblock as python %} import arvados.collection -source_collection = "x1u39-4zz18-krzg64ufvehgitl" -target_project = "x1u39-j7d0g-67q94einb8ptznm" +source_collection = "zzzzz-4zz18-zzzzzzzzzzzzzzz" +target_project = "zzzzz-j7d0g-zzzzzzzzzzzzzzz" target_name = "Files copied from source_collection" files_to_copy = ["folder1/sample1/sample1_R1.fastq", "folder1/sample2/sample2_R1.fastq"] @@ -256,3 +257,78 @@ for f in files_to_copy: target.save_new(name=target_name, owner_uuid=target_project) print("Created collection %s" % target.manifest_locator()) {% endcodeblock %} + +h2. Copy files from a collection to another collection + +{% codeblock as python %} +import arvados.collection + +source_collection = "zzzzz-4zz18-zzzzzzzzzzzzzzz" +target_collection = "zzzzz-4zz18-aaaaaaaaaaaaaaa" +files_to_copy = ["folder1/sample1/sample1_R1.fastq", + "folder1/sample2/sample2_R1.fastq"] + +source = arvados.collection.CollectionReader(source_collection) +target = arvados.collection.Collection(target_collection) + +for f in files_to_copy: + target.copy(f, "", source_collection=source) + +target.save() +{% endcodeblock %} + +h2. Delete a file from an existing collection + +{% codeblock as python %} +import arvados + +c = arvados.collection.Collection("zzzzz-4zz18-zzzzzzzzzzzzzzz") +c.remove("file2.txt") +c.save() +{% endcodeblock %} + +h2. Listing records with paging + +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). + +{% codeblock as python %} +import arvados.util + +api = arvados.api() +for c in arvados.util.keyset_list_all(api.collections().list, filters=[["name", "like", "%sample123%"]]): + print("got collection " + c["uuid"]) +{% endcodeblock %} + +h2. Querying the vocabulary definition + +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: + +{% codeblock as python %} +from arvados import api, vocabulary +voc = vocabulary.load_vocabulary(api('v1')) + +[k.identifier for k in set(voc.key_aliases.values())] +# Example output: ['IDTAGCOLORS', 'IDTAGFRUITS', 'IDTAGCOMMENT', 'IDTAGIMPORTANCES', 'IDTAGCATEGORIES', 'IDTAGSIZES', 'IDTAGANIMALS'] +voc['IDTAGSIZES'].preferred_label +# Example output: 'Size' +[v.preferred_label for v in set(voc['size'].value_aliases.values())] +# Example output: ['S', 'M', 'L', 'XL', 'XS'] +voc['size']['s'].aliases +# Example output: ['S', 'small'] +voc['size']['Small'].identifier +# Example output: 'IDVALSIZES2' +{% endcodeblock %} + +h2. Translating between vocabulary identifiers and labels + +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: + +{% codeblock as python %} +from arvados import api, vocabulary +voc = vocabulary.load_vocabulary(api('v1')) + +voc.convert_to_labels({'IDTAGIMPORTANCES': 'IDVALIMPORTANCES1'}) +# Example output: {'Importance': 'Critical'} +voc.convert_to_identifiers({'creature': 'elephant'}) +# Example output: {'IDTAGANIMALS': 'IDVALANIMALS3'} +{% endcodeblock %} \ No newline at end of file