@@ -1124,7 +1124,7 @@ def extend(self, md_template):
11241124 self .validate (self .columns_restrictions )
11251125 self .generate_files ()
11261126
1127- def update (self , md_template ):
1127+ def _update (self , md_template ):
11281128 r"""Update values in the template
11291129
11301130 Parameters
@@ -1143,22 +1143,19 @@ def update(self, md_template):
11431143 passed md_template
11441144 """
11451145 with qdb .sql_connection .TRN :
1146- # Clean and validate the metadata template given
1147- new_map = self ._clean_validate_template (
1148- md_template , self .study_id , current_columns = self .categories ())
11491146 # Retrieving current metadata
11501147 current_map = self .to_dataframe ()
11511148
11521149 # simple validations of sample ids and column names
1153- samples_diff = set (new_map .index ).difference (current_map .index )
1150+ samples_diff = set (md_template .index ).difference (current_map .index )
11541151 if samples_diff :
11551152 raise qdb .exceptions .QiitaDBError (
11561153 'The new template differs from what is stored '
11571154 'in database by these samples names: %s'
11581155 % ', ' .join (samples_diff ))
11591156
1160- if not set (current_map .columns ).issuperset (new_map .columns ):
1161- columns_diff = set (new_map .columns ).difference (
1157+ if not set (current_map .columns ).issuperset (md_template .columns ):
1158+ columns_diff = set (md_template .columns ).difference (
11621159 current_map .columns )
11631160 raise qdb .exceptions .QiitaDBError (
11641161 'Some of the columns in your template are not present in '
@@ -1168,15 +1165,16 @@ def update(self, md_template):
11681165
11691166 # In order to speed up some computation, let's compare only the
11701167 # common columns and rows. current_map.columns and
1171- # current_map.index are supersets of new_map.columns and
1172- # new_map.index, respectivelly, so this will not fail
1173- current_map = current_map [new_map .columns ].loc [new_map .index ]
1168+ # current_map.index are supersets of md_template.columns and
1169+ # md_template.index, respectivelly, so this will not fail
1170+ current_map = current_map [
1171+ md_template .columns ].loc [md_template .index ]
11741172
11751173 # Get the values that we need to change
11761174 # diff_map is a DataFrame that hold boolean values. If a cell is
1177- # True, means that the new_map is different from the current_map
1178- # while False means that the cell has the same value
1179- diff_map = current_map != new_map
1175+ # True, means that the md_template is different from the
1176+ # current_map while False means that the cell has the same value
1177+ diff_map = current_map != md_template
11801178 # ne_stacked holds a MultiIndexed DataFrame in which the first
11811179 # level of indexing is the sample_name and the second one is the
11821180 # columns. We only have 1 column, which holds if that
@@ -1195,8 +1193,8 @@ def update(self, md_template):
11951193 changed .index .names = ['sample_name' , 'column' ]
11961194 # the combination of np.where and boolean indexing produces
11971195 # a numpy array with only the values that actually changed
1198- # between the current_map and new_map
1199- changed_to = new_map .values [np .where (diff_map )]
1196+ # between the current_map and md_template
1197+ changed_to = md_template .values [np .where (diff_map )]
12001198
12011199 # to_update is a MultiIndexed DataFrame, in which the index 0 is
12021200 # the samples and the index 1 is the columns, we define these
@@ -1235,12 +1233,57 @@ def update(self, md_template):
12351233 """ .format (self ._table_name (self ._id ), sql_eq_cols ,
12361234 single_value , sql_cols )
12371235 for sample in samples_to_update :
1238- sample_vals = [new_map [col ][sample ] for col in cols_to_update ]
1236+ sample_vals = [md_template [col ][sample ]
1237+ for col in cols_to_update ]
12391238 sample_vals .insert (0 , sample )
12401239 qdb .sql_connection .TRN .add (sql , sample_vals )
12411240
12421241 qdb .sql_connection .TRN .execute ()
12431242
1243+ def update (self , md_template ):
1244+ r"""Update values in the template
1245+
1246+ Parameters
1247+ ----------
1248+ md_template : DataFrame
1249+ The metadata template file contents indexed by samples ids
1250+
1251+ Raises
1252+ ------
1253+ QiitaDBError
1254+ If md_template and db do not have the same sample ids
1255+ If md_template and db do not have the same column headers
1256+ If self.can_be_updated is not True
1257+ QiitaDBWarning
1258+ If there are no differences between the contents of the DB and the
1259+ passed md_template
1260+ """
1261+ with qdb .sql_connection .TRN :
1262+ # Clean and validate the metadata template given
1263+ new_map = self ._clean_validate_template (
1264+ md_template , self .study_id , current_columns = self .categories ())
1265+ self ._update (new_map )
1266+ self .validate (self .columns_restrictions )
1267+ self .generate_files ()
1268+
1269+ def extend_and_update (self , md_template ):
1270+ """Performs the update and extend operations at once
1271+
1272+ Parameters
1273+ ----------
1274+ md_template : DataFrame
1275+ The metadata template contents indexed by sample ids
1276+
1277+ See Also
1278+ --------
1279+ update
1280+ extend
1281+ """
1282+ with qdb .sql_connection .TRN :
1283+ md_template = self ._clean_validate_template (
1284+ md_template , self .study_id , current_columns = self .categories ())
1285+ self ._common_extend_steps (md_template )
1286+ self ._update (md_template )
12441287 self .validate (self .columns_restrictions )
12451288 self .generate_files ()
12461289
0 commit comments