From 9dc59b8eb2d1b11bd36b281ae5ba72e9ee04677a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?BIOPZ-B=C3=B6rsch=20Anastasiya?=
 <anastasiya.boersch@unibas.ch>
Date: Fri, 24 Jan 2020 15:32:36 +0100
Subject: [PATCH] - add Python script `scripts/labkey_api.py` to retrieve
 LabKey tables - add usage information in file `scripts/labkey_api.md`

---
 scripts/labkey_api.md | 49 +++++++++++++++++++++++++++++++++++++++++++
 scripts/labkey_api.py | 27 ++++++++++++++++++++++++
 2 files changed, 76 insertions(+)
 create mode 100644 scripts/labkey_api.md
 create mode 100644 scripts/labkey_api.py

diff --git a/scripts/labkey_api.md b/scripts/labkey_api.md
new file mode 100644
index 0000000..5846e03
--- /dev/null
+++ b/scripts/labkey_api.md
@@ -0,0 +1,49 @@
+> **NOTE**: Include this info in the main doc in the root directory once 
+> available; add information on how to get credentials for the LabKey server 
+> (i.e., how to obatain a password for the `.netrc` file)
+
+In order to connect to the LabKey through API, you will first need to create a
+file `.netrc` in your home directory:
+
+```bash
+touch ${HOME}/.netrc
+```
+
+Add the following lines to the file:
+
+```console
+machine <remote-instance-of-labkey-server>  
+login <user-email>
+password <user-password>  
+```
+
+To secure the file, set permissions in a way that only you can see the content
+of the file: 
+
+```bash
+chmod 400 .netrc
+```
+
+Install the `labkey` and `pandas` packages, ideally from a virtual environment
+(e.g., `virtualenv` or `conda`):
+
+```bash
+pip install labkey pandas
+```
+
+Run the LabKey API client script:
+
+```bash
+python labkey_api.py project_name labkey_table_nane  
+```
+
+Example:
+
+```bash
+python labkey_api.py TEST_ABOERSCH RNA_Seq_data_template  
+```
+
+Right now the script prints a representation of a `pandas` data frame
+containing the requested LabKey table the the screen. For further processing
+the current script could be included in another script, or it could be modified
+to write out the data in a desired file format (e.g., TSV).
diff --git a/scripts/labkey_api.py b/scripts/labkey_api.py
new file mode 100644
index 0000000..759acb4
--- /dev/null
+++ b/scripts/labkey_api.py
@@ -0,0 +1,27 @@
+# This script targets the client api version 0.4.0 and later
+
+#
+#  Check the page: https://github.com/LabKey/labkey-api-python/blob/master/samples/query_examples.py
+#  for example about filtering in queries.
+#  A starting point to investigate further is here:
+#  https://www.labkey.org/download/clientapi_docs/javascript-api/symbols/LABKEY.Query.Filter.html
+
+import labkey
+import pandas as pd
+import sys
+
+# for convenience, load QueryFilter explicitly (avoids long lines in filter definitions)
+from labkey.query import QueryFilter
+
+if __name__ == "__main__":
+  # These are values of variables for which the script works
+  # project_name = "TEST_ABOERSCH"
+  # query_name = "RNA_Seq_data_template"
+  project_name = sys.argv[1]
+  query_name = sys.argv[2]
+  server_context = labkey.utils.create_server_context('labkey.scicore.unibas.ch', '/Zavolan Group/'+project_name, 'labkey', use_ssl=True)
+  schema_name = "lists"
+  results = labkey.query.select_rows(server_context,schema_name,query_name)
+  table_of_data = pd.DataFrame(results["rows"])
+  print(table_of_data)
+
-- 
GitLab