diff --git a/qiita_db/study.py b/qiita_db/study.py
index 03e6ef607..ef21feddc 100644
--- a/qiita_db/study.py
+++ b/qiita_db/study.py
@@ -464,6 +464,42 @@ def delete(cls, id_):
qdb.sql_connection.TRN.execute()
+ @classmethod
+ def get_tags(cls):
+ """Returns the available study tags
+
+ Returns
+ -------
+ list of DictCursor
+ Table-like structure of metadata, one tag per row. Can be
+ accessed as a list of dictionaries, keyed on column name.
+ """
+ with qdb.sql_connection.TRN:
+ sql = """SELECT study_tag_id, study_tag
+ FROM qiita.study_tags"""
+
+ qdb.sql_connection.TRN.add(sql)
+ return qdb.sql_connection.TRN.execute_fetchindex()
+
+ @classmethod
+ def insert_tags(cls, user, tags):
+ """Insert available study tags
+
+ Parameters
+ ----------
+ user : qiita_db.user.User
+ The user adding the tags
+ tags : list of str
+ The list of tags to add
+ """
+ with qdb.sql_connection.TRN:
+ sql = """INSERT INTO qiita.study_tags (email, study_tag)
+ VALUES (%s, %s)"""
+ sql_args = [[user.email, tag] for tag in tags]
+
+ qdb.sql_connection.TRN.add(sql, sql_args, many=True)
+ qdb.sql_connection.TRN.execute()
+
# --- Attributes ---
@property
@@ -921,7 +957,52 @@ def ebi_submission_status(self, value):
ebi_submission_status.__doc__.format(', '.join(_VALID_EBI_STATUS))
- # --- methods ---
+ @property
+ def tags(self):
+ """Returns the tags of the study
+
+ Returns
+ -------
+ list of str
+ The study tags
+ """
+ with qdb.sql_connection.TRN:
+ sql = """SELECT study_tag_id, study_tag
+ FROM qiita.study_tags
+ LEFT JOIN qiita.per_study_tags USING (study_tag_id)
+ WHERE study_id = {0}""".format(self._id)
+ qdb.sql_connection.TRN.add(sql)
+ return qdb.sql_connection.TRN.execute_fetchindex()
+
+ @tags.setter
+ def tags(self, tag_ids):
+ """Sets the tags of the study
+
+ Parameters
+ ----------
+ tag_ids : list of int
+ The tag ids of the study
+ """
+ with qdb.sql_connection.TRN:
+ sql = """DELETE FROM qiita.per_study_tags WHERE study_id = %s"""
+ qdb.sql_connection.TRN.add(sql, [self._id])
+
+ if tag_ids:
+ sql = """INSERT INTO qiita.per_study_tags
+ (study_tag_id, study_id)
+ SELECT %s, %s
+ WHERE
+ NOT EXISTS (
+ SELECT study_tag_id, study_id
+ FROM qiita.per_study_tags
+ WHERE study_tag_id = %s AND study_id = %s
+ )"""
+ sql_args = [[tid, self._id, tid, self._id] for tid in tag_ids]
+ qdb.sql_connection.TRN.add(sql, sql_args, many=True)
+
+ qdb.sql_connection.TRN.execute()
+
+# --- methods ---
def artifacts(self, dtype=None, artifact_type=None):
"""Returns the list of artifacts associated with the study
diff --git a/qiita_db/support_files/patches/50.sql b/qiita_db/support_files/patches/50.sql
new file mode 100644
index 000000000..f732ef7b5
--- /dev/null
+++ b/qiita_db/support_files/patches/50.sql
@@ -0,0 +1,19 @@
+-- Feb 3, 2017
+-- adding study tagging system
+
+CREATE TABLE qiita.study_tags (
+ study_tag_id bigserial NOT NULL,
+ email varchar NOT NULL,
+ study_tag varchar NOT NULL,
+ CONSTRAINT pk_study_tag UNIQUE ( study_tag ),
+ CONSTRAINT pk_study_tag_id PRIMARY KEY ( study_tag_id )
+) ;
+
+CREATE INDEX idx_study_tag_id ON qiita.study_tags ( study_tag_id ) ;
+ALTER TABLE qiita.study_tags ADD CONSTRAINT fk_study_tags FOREIGN KEY ( email ) REFERENCES qiita.qiita_user( email );
+
+CREATE TABLE qiita.per_study_tags (
+ study_tag_id bigint NOT NULL,
+ study_id bigint NOT NULL,
+ CONSTRAINT pk_per_study_tags PRIMARY KEY ( study_tag_id, study_id )
+) ;
diff --git a/qiita_db/support_files/qiita-db.dbs b/qiita_db/support_files/qiita-db.dbs
index 889d80d87..1f7ebe2f5 100644
--- a/qiita_db/support_files/qiita-db.dbs
+++ b/qiita_db/support_files/qiita-db.dbs
@@ -1120,6 +1120,26 @@