From ddb4e7b65e68b5a60d3f9d6c91c853d8d95b56b3 Mon Sep 17 00:00:00 2001
From: Henry Luetcke <hluetcke@ethz.ch>
Date: Wed, 24 Oct 2018 16:31:09 +0200
Subject: [PATCH] connection to openBIS, Masterdata methods

---
 df_to_cell.m           |  6 ------
 df_to_table.m          | 10 ++++++++++
 get_dataset_types.m    | 14 ++++++++++++++
 get_datasets.m         |  2 +-
 get_experiment_types.m |  7 +++----
 get_material_types.m   | 14 ++++++++++++++
 get_sample_types.m     | 14 ++++++++++++++
 get_terms.m            | 15 +++++++++++++++
 login.m                | 23 ++++++++++++++++++++++-
 logout.m               | 13 +++++++++++++
 pybis_example.ipynb    | 20 ++++++++++----------
 11 files changed, 116 insertions(+), 22 deletions(-)
 delete mode 100644 df_to_cell.m
 create mode 100644 df_to_table.m
 create mode 100644 get_dataset_types.m
 create mode 100644 get_material_types.m
 create mode 100644 get_sample_types.m
 create mode 100644 get_terms.m
 create mode 100644 logout.m

diff --git a/df_to_cell.m b/df_to_cell.m
deleted file mode 100644
index c2ce512553e..00000000000
--- a/df_to_cell.m
+++ /dev/null
@@ -1,6 +0,0 @@
-function [matlab_cell] = df_to_cell(df)
-csv_temp = sprintf('%s.csv', tempname);
-df.to_csv(csv_temp);
-matlab_cell = readtable(csv_temp);
-delete(csv_temp);
-end
\ No newline at end of file
diff --git a/df_to_table.m b/df_to_table.m
new file mode 100644
index 00000000000..7135acb2bc9
--- /dev/null
+++ b/df_to_table.m
@@ -0,0 +1,10 @@
+function [matlab_table] = df_to_table(df)
+%df_to_table
+%   Returns a Matlab table for a Python dataframe
+
+csv_temp = sprintf('%s.csv', tempname);
+df.to_csv(csv_temp);
+matlab_table = readtable(csv_temp);
+delete(csv_temp);
+
+end
\ No newline at end of file
diff --git a/get_dataset_types.m b/get_dataset_types.m
new file mode 100644
index 00000000000..e11aaddee27
--- /dev/null
+++ b/get_dataset_types.m
@@ -0,0 +1,14 @@
+function dataset_types = get_dataset_types
+%get_dataset_types
+%   Returns a table of all available dataset types
+
+global obi
+
+% test connection
+test_connection(obi)
+
+dataset_types = obi.get_dataset_types();
+
+dataset_types = df_to_table(dataset_types.df);
+
+end
diff --git a/get_datasets.m b/get_datasets.m
index cc1e71fa94c..67688f26240 100644
--- a/get_datasets.m
+++ b/get_datasets.m
@@ -5,5 +5,5 @@ function [datasets] = get_datasets(ds_type)
 global obi
 
 datasets = obi.get_datasets(pyargs('type',ds_type));
-datasets = df_to_cell(datasets.df);
+datasets = df_to_table(datasets.df);
 end
diff --git a/get_experiment_types.m b/get_experiment_types.m
index befeaeccbe9..a2d70f0286f 100644
--- a/get_experiment_types.m
+++ b/get_experiment_types.m
@@ -1,6 +1,6 @@
 function experiment_types = get_experiment_types
-%UNTITLED2 Summary of this function goes here
-%   Detailed explanation goes here
+%get_experiment_types
+%   Returns a table of all available experiment types
 
 global obi
 
@@ -8,8 +8,7 @@ global obi
 test_connection(obi)
 
 experiment_types = obi.get_experiment_types();
-experiment_types = df_to_cell(experiment_types.df);
 
+experiment_types = df_to_table(experiment_types.df);
 
 end
-
diff --git a/get_material_types.m b/get_material_types.m
new file mode 100644
index 00000000000..ab92d06d5bc
--- /dev/null
+++ b/get_material_types.m
@@ -0,0 +1,14 @@
+function material_types = get_material_types
+%get_material_types
+%   Returns a table of all available material types
+
+global obi
+
+% test connection
+test_connection(obi)
+
+material_types = obi.get_material_types();
+
+material_types = df_to_table(material_types.df);
+
+end
diff --git a/get_sample_types.m b/get_sample_types.m
new file mode 100644
index 00000000000..65f5435dfc9
--- /dev/null
+++ b/get_sample_types.m
@@ -0,0 +1,14 @@
+function sample_types = get_sample_types
+%get_sample_types
+%   Returns a table of all available sample types
+
+global obi
+
+% test connection
+test_connection(obi)
+
+sample_types = obi.get_sample_types();
+
+sample_types = df_to_table(sample_types.df);
+
+end
diff --git a/get_terms.m b/get_terms.m
new file mode 100644
index 00000000000..3114cc7dec6
--- /dev/null
+++ b/get_terms.m
@@ -0,0 +1,15 @@
+function terms = get_terms
+%get_sample_types
+%Returns information about existing vocabulary terms. 
+%If a vocabulary code is provided, it only returns the terms of that vocabulary
+
+global obi
+
+% test connection
+test_connection(obi)
+
+terms = obi.get_terms();
+
+terms = df_to_table(terms.df);
+
+end
diff --git a/login.m b/login.m
index f49d23f2419..441a746f49e 100644
--- a/login.m
+++ b/login.m
@@ -1,11 +1,32 @@
-function login(url, user, pw)
+function login(varargin)
 %UNTITLED Summary of this function goes here
 %   Detailed explanation goes here
 
 global obi
 
+if nargin
+    url = varargin{1};
+    user = varargin{2};
+    pw = varargin{3};
+else
+    [url, user, pw] = user_url_pw_inputdlg;
+end
+
 obi = py.pybis.Openbis(url, pyargs('verify_certificates', 0));
 obi.login(user, pw, pyargs('save_token', 1));
 
 end
 
+function [url, user, pw] = user_url_pw_inputdlg
+
+prompt = {'openBIS URL:', 'openBIS user:'};
+title = 'openBIS connection details';
+definput = {'https://XYZ.ethz.ch/openbis:8443', ''};
+answer = inputdlg(prompt, title, 1, definput);
+
+url = answer{1};
+user = answer{2};
+
+pw = passwordEntryDialog('CheckPasswordLength',0);
+
+end
\ No newline at end of file
diff --git a/logout.m b/logout.m
new file mode 100644
index 00000000000..9942dc2aed4
--- /dev/null
+++ b/logout.m
@@ -0,0 +1,13 @@
+function logout
+%LOGOUT
+%   Log out of openBIS. After logout, the session token is no longer valid.
+
+global obi
+
+% test connection
+test_connection(obi)
+
+obi.logout;
+
+end
+
diff --git a/pybis_example.ipynb b/pybis_example.ipynb
index 4008e1d7517..ad001d3d1a8 100644
--- a/pybis_example.ipynb
+++ b/pybis_example.ipynb
@@ -30,7 +30,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 3,
+   "execution_count": 5,
    "metadata": {},
    "outputs": [
     {
@@ -48,7 +48,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": 6,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -58,16 +58,16 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 7,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "'hluetcke-181024130359554x90E8C39FF84921ECC3D5436AE2ECFD05'"
+       "'hluetcke-181024154540290xCA25DBE8ED8C9FB138503F6C3F0C8D69'"
       ]
      },
-     "execution_count": 5,
+     "execution_count": 7,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -78,7 +78,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": 8,
    "metadata": {},
    "outputs": [
     {
@@ -87,7 +87,7 @@
        "True"
       ]
      },
-     "execution_count": 6,
+     "execution_count": 8,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -98,7 +98,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": 9,
    "metadata": {},
    "outputs": [
     {
@@ -143,7 +143,7 @@
        "0  DSS1  https://limb.ethz.ch:443/datastore_server  https://limb.ethz.ch:443"
       ]
      },
-     "execution_count": 7,
+     "execution_count": 9,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -441,7 +441,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.6.6"
+   "version": "3.6.5"
   }
  },
  "nbformat": 4,
-- 
GitLab