5555QIITA_COLUMN_NAME = 'qiita_sample_column_names'
5656
5757
58+ def _helper_get_categories (table ):
59+ """This is a helper function to avoid duplication of code"""
60+ with qdb .sql_connection .TRN :
61+ sql = """SELECT sample_values->>'columns'
62+ FROM qiita.{0}
63+ WHERE sample_id = '{1}'""" .format (table , QIITA_COLUMN_NAME )
64+ qdb .sql_connection .TRN .add (sql )
65+ results = qdb .sql_connection .TRN .execute_fetchflatten ()
66+ if results :
67+ results = sorted (loads (results [0 ]))
68+ return results
69+
70+
5871class BaseSample (qdb .base .QiitaObject ):
5972 r"""Sample object that accesses the db to get the information of a sample
6073 belonging to a PrepTemplate or a SampleTemplate.
@@ -184,17 +197,7 @@ def _get_categories(self):
184197 set of str
185198 The set of all available metadata categories
186199 """
187- with qdb .sql_connection .TRN :
188- sql = """SELECT sample_values->>'columns'
189- FROM qiita.{0}
190- WHERE sample_id = '{1}'""" .format (
191- self ._dynamic_table , QIITA_COLUMN_NAME )
192- qdb .sql_connection .TRN .add (sql )
193- results = qdb .sql_connection .TRN .execute_fetchflatten ()
194- if results :
195- results = loads (results [0 ])
196-
197- return set (results )
200+ return set (_helper_get_categories (self ._dynamic_table ))
198201
199202 def _to_dict (self ):
200203 r"""Returns the categories and their values in a dictionary
@@ -729,7 +732,7 @@ def delete_column(self, column_name):
729732 If the column_name is selected as a specimen_id_column in the
730733 study.
731734 """
732- if column_name not in self .categories () :
735+ if column_name not in self .categories :
733736 raise qdb .exceptions .QiitaDBColumnError (
734737 "'%s' not in info file %d" % (column_name , self ._id ))
735738 if not self .can_be_updated (columns = {column_name }):
@@ -754,7 +757,7 @@ def delete_column(self, column_name):
754757 qdb .sql_connection .TRN .add (sql , [column_name , QIITA_COLUMN_NAME ])
755758
756759 # deleting from QIITA_COLUMN_NAME
757- columns = self .categories ()
760+ columns = self .categories
758761 columns .remove (column_name )
759762 values = '{"columns": %s}' % dumps (columns )
760763 sql = """UPDATE {0}
@@ -834,7 +837,7 @@ def _common_extend_steps(self, md_template):
834837
835838 # Check if we are adding new columns
836839 headers = md_template .keys ().tolist ()
837- new_cols = set (headers ).difference (self .categories () )
840+ new_cols = set (headers ).difference (self .categories )
838841
839842 if not new_cols and not new_samples :
840843 return None , None
@@ -855,7 +858,7 @@ def _common_extend_steps(self, md_template):
855858 # code). Sorting the new columns to enforce an order
856859 new_cols = sorted (new_cols )
857860
858- cols = self .categories ()
861+ cols = self .categories
859862 cols .extend (new_cols )
860863
861864 values = dumps ({"columns" : cols })
@@ -1167,7 +1170,7 @@ def _common_to_dataframe_steps(self):
11671170 """
11681171 with qdb .sql_connection .TRN :
11691172 # Retrieve all the information from the database
1170- cols = self .categories ()
1173+ cols = self .categories
11711174 sql = """SELECT sample_id, sample_values
11721175 FROM qiita.{0}
11731176 WHERE sample_id != '{1}'""" .format (
@@ -1237,6 +1240,7 @@ def get_filepaths(self):
12371240 self ._filepath_table , self ._id_column , self .id ,
12381241 sort = 'descending' )]
12391242
1243+ @property
12401244 def categories (self ):
12411245 """Identifies the metadata columns present in an info file
12421246
@@ -1245,19 +1249,7 @@ def categories(self):
12451249 cols : list
12461250 The category fields
12471251 """
1248- with qdb .sql_connection .TRN :
1249- sql = """SELECT sample_values->>'columns'
1250- FROM qiita.{0}
1251- WHERE sample_id = '{1}'""" .format (
1252- self ._table_name (self ._id ), QIITA_COLUMN_NAME )
1253-
1254- qdb .sql_connection .TRN .add (sql )
1255-
1256- results = qdb .sql_connection .TRN .execute_fetchflatten ()
1257- if results :
1258- results = sorted (loads (results [0 ]))
1259-
1260- return results
1252+ return _helper_get_categories (self ._table_name (self ._id ))
12611253
12621254 def extend (self , md_template ):
12631255 """Adds the given template to the current one
@@ -1269,7 +1261,7 @@ def extend(self, md_template):
12691261 """
12701262 with qdb .sql_connection .TRN :
12711263 md_template = self ._clean_validate_template (
1272- md_template , self .study_id , current_columns = self .categories () )
1264+ md_template , self .study_id , current_columns = self .categories )
12731265 new_samples , new_columns = self ._common_extend_steps (md_template )
12741266 if new_samples or new_columns :
12751267 self .validate (self .columns_restrictions )
@@ -1396,7 +1388,7 @@ def _update(self, md_template):
13961388 self ._table_name (self ._id ))
13971389 qdb .sql_connection .TRN .add (sql , [dumps (values ), sid ])
13981390
1399- nc = list (set (new_columns ).union (set (self .categories () )))
1391+ nc = list (set (new_columns ).union (set (self .categories )))
14001392 table_name = self ._table_name (self .id )
14011393 values = dumps ({"columns" : nc })
14021394 sql = """UPDATE qiita.{0}
@@ -1430,7 +1422,7 @@ def update(self, md_template):
14301422 with qdb .sql_connection .TRN :
14311423 # Clean and validate the metadata template given
14321424 new_map = self ._clean_validate_template (
1433- md_template , self .study_id , current_columns = self .categories () )
1425+ md_template , self .study_id , current_columns = self .categories )
14341426 samples , columns = self ._update (new_map )
14351427 self .validate (self .columns_restrictions )
14361428 self .generate_files (samples , columns )
@@ -1450,7 +1442,7 @@ def extend_and_update(self, md_template):
14501442 """
14511443 with qdb .sql_connection .TRN :
14521444 md_template = self ._clean_validate_template (
1453- md_template , self .study_id , current_columns = self .categories () )
1445+ md_template , self .study_id , current_columns = self .categories )
14541446 new_samples , new_columns = self ._common_extend_steps (md_template )
14551447 samples , columns = self ._update (md_template )
14561448 if samples is None :
@@ -1516,7 +1508,7 @@ def get_category(self, category):
15161508 If category is not part of the template
15171509 """
15181510 with qdb .sql_connection .TRN :
1519- if category not in self .categories () :
1511+ if category not in self .categories :
15201512 raise qdb .exceptions .QiitaDBColumnError (category )
15211513 sql = """SELECT sample_id,
15221514 COALESCE(sample_values->>'{0}', 'None') AS {0}
@@ -1542,7 +1534,7 @@ def check_restrictions(self, restrictions):
15421534 cols = {col for restriction in restrictions
15431535 for col in restriction .columns }
15441536
1545- return cols .difference (self .categories () )
1537+ return cols .difference (self .categories )
15461538
15471539 def _get_accession_numbers (self , column ):
15481540 """Return the accession numbers stored in `column`
@@ -1639,7 +1631,7 @@ def validate(self, restriction_dict):
16391631 If the values aren't castable
16401632 """
16411633 warning_msg = []
1642- columns = self .categories ()
1634+ columns = self .categories
16431635 wrong_msg = 'Sample "%s", column "%s", wrong value "%s"'
16441636 for label , restriction in restriction_dict .items ():
16451637 missing = set (restriction .columns ).difference (columns )
@@ -1796,7 +1788,7 @@ def validate_restrictions(self):
17961788 success = True
17971789 message = []
17981790 restrictions = self .restrictions
1799- categories = self .categories ()
1791+ categories = self .categories
18001792
18011793 difference = sorted (set (restrictions .keys ()) - set (categories ))
18021794 if difference :
0 commit comments