@@ -1124,7 +1124,7 @@ def extend(self, md_template):
1124
1124
self .validate (self .columns_restrictions )
1125
1125
self .generate_files ()
1126
1126
1127
- def update (self , md_template ):
1127
+ def _update (self , md_template ):
1128
1128
r"""Update values in the template
1129
1129
1130
1130
Parameters
@@ -1143,22 +1143,19 @@ def update(self, md_template):
1143
1143
passed md_template
1144
1144
"""
1145
1145
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 ())
1149
1146
# Retrieving current metadata
1150
1147
current_map = self .to_dataframe ()
1151
1148
1152
1149
# 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 )
1154
1151
if samples_diff :
1155
1152
raise qdb .exceptions .QiitaDBError (
1156
1153
'The new template differs from what is stored '
1157
1154
'in database by these samples names: %s'
1158
1155
% ', ' .join (samples_diff ))
1159
1156
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 (
1162
1159
current_map .columns )
1163
1160
raise qdb .exceptions .QiitaDBError (
1164
1161
'Some of the columns in your template are not present in '
@@ -1168,15 +1165,16 @@ def update(self, md_template):
1168
1165
1169
1166
# In order to speed up some computation, let's compare only the
1170
1167
# 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 ]
1174
1172
1175
1173
# Get the values that we need to change
1176
1174
# 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
1180
1178
# ne_stacked holds a MultiIndexed DataFrame in which the first
1181
1179
# level of indexing is the sample_name and the second one is the
1182
1180
# columns. We only have 1 column, which holds if that
@@ -1195,8 +1193,8 @@ def update(self, md_template):
1195
1193
changed .index .names = ['sample_name' , 'column' ]
1196
1194
# the combination of np.where and boolean indexing produces
1197
1195
# 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 )]
1200
1198
1201
1199
# to_update is a MultiIndexed DataFrame, in which the index 0 is
1202
1200
# the samples and the index 1 is the columns, we define these
@@ -1235,12 +1233,57 @@ def update(self, md_template):
1235
1233
""" .format (self ._table_name (self ._id ), sql_eq_cols ,
1236
1234
single_value , sql_cols )
1237
1235
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 ]
1239
1238
sample_vals .insert (0 , sample )
1240
1239
qdb .sql_connection .TRN .add (sql , sample_vals )
1241
1240
1242
1241
qdb .sql_connection .TRN .execute ()
1243
1242
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 )
1244
1287
self .validate (self .columns_restrictions )
1245
1288
self .generate_files ()
1246
1289
0 commit comments