File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -87,6 +87,39 @@ def _lock_non_sandbox(self):
87
87
raise qdb .exceptions .QiitaDBStatusError (
88
88
"Illegal operation on non-sandbox study!" )
89
89
90
+ @classmethod
91
+ def from_title (cls , title ):
92
+ """Instantiate Study from title
93
+
94
+ Parameters
95
+ ----------
96
+ title : str
97
+ Tht title to search for
98
+
99
+ Returns
100
+ -------
101
+ Study
102
+ The study with the given title
103
+
104
+ Raises
105
+ ------
106
+ QiitaDBUnknownIDError
107
+ If the title doesn't exist
108
+ """
109
+ with qdb .sql_connection .TRN :
110
+ sql = """SELECT study_id
111
+ FROM qiita.{}
112
+ WHERE study_title = %s""" .format (cls ._table )
113
+
114
+ qdb .sql_connection .TRN .add (sql , [title ])
115
+ sid = qdb .sql_connection .TRN .execute_fetchflatten ()
116
+
117
+ if not sid :
118
+ raise qdb .exceptions .QiitaDBUnknownIDError (
119
+ cls ._table , f'"{ title } " does not exist' )
120
+
121
+ return qdb .study .Study (sid [0 ])
122
+
90
123
@classmethod
91
124
def iter (cls ):
92
125
"""Iterate over all studies in the database
Original file line number Diff line number Diff line change @@ -207,6 +207,14 @@ def _change_processed_data_status(self, new_status):
207
207
qdb .sql_connection .perform_as_transaction (
208
208
"UPDATE qiita.artifact SET visibility_id = %s" , (id_status ,))
209
209
210
+ def test_from_title (self ):
211
+ study = qdb .study .Study .from_title (
212
+ 'Identification of the Microbiomes for Cannabis Soils' )
213
+ self .assertEqual (study , qdb .study .Study (1 ))
214
+
215
+ with self .assertRaises (qdb .exceptions .QiitaDBUnknownIDError ):
216
+ qdb .study .Study .from_title ('Study title' )
217
+
210
218
def test_get_info (self ):
211
219
# Test get all info for single study
212
220
qiita_config .portal = 'QIITA'
You can’t perform that action at this time.
0 commit comments