-
Notifications
You must be signed in to change notification settings - Fork 79
Sample generate files improvements #2749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
charles-cowart
merged 7 commits into
qiita-spots:dev
from
antgonza:sample-generate_files-improvements
Dec 12, 2018
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8d8f674
sample info file generate_files is now "smart"
antgonza 9401943
upstream dev
antgonza 26dca2e
fix test
antgonza b0dc603
Merge branch 'dev' of https://github.com/biocore/qiita into sample-ge…
antgonza a93ca98
addressing @charles-cowart comments
antgonza 9e5c964
fix error
antgonza 1005f85
fix error
antgonza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
sample info file generate_files is now "smart"
- Loading branch information
commit 8d8f67427eb067e7342d314a73cafa4cfb0cd70e
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -696,7 +696,7 @@ def _common_delete_sample_steps(self, sample_names): | |
| qdb.sql_connection.TRN.add(sql2, [sn, self.id]) | ||
| qdb.sql_connection.TRN.execute() | ||
|
|
||
| self.generate_files() | ||
| self.generate_files(samples=sample_names) | ||
|
|
||
| def delete_column(self, column_name): | ||
| """Delete `column_name` from info file | ||
|
|
@@ -803,6 +803,13 @@ def _common_extend_steps(self, md_template): | |
| ---------- | ||
| md_template : DataFrame | ||
| The metadata template file contents indexed by sample ids | ||
|
|
||
| Returns | ||
| ------- | ||
| list of str | ||
| The new samples being added | ||
| list of str | ||
| The new columns being added | ||
| """ | ||
| with qdb.sql_connection.TRN: | ||
| # Check if we are adding new samples | ||
|
|
@@ -887,6 +894,8 @@ def _common_extend_steps(self, md_template): | |
| # Execute all the steps | ||
| qdb.sql_connection.TRN.execute() | ||
|
|
||
| return new_samples, new_cols | ||
|
|
||
| @classmethod | ||
| def exists(cls, obj_id): | ||
| r"""Checks if already exists a MetadataTemplate for the provided object | ||
|
|
@@ -1233,9 +1242,9 @@ def extend(self, md_template): | |
| with qdb.sql_connection.TRN: | ||
| md_template = self._clean_validate_template( | ||
| md_template, self.study_id, current_columns=self.categories()) | ||
| self._common_extend_steps(md_template) | ||
| new_samples, new_columns = self._common_extend_steps(md_template) | ||
| self.validate(self.columns_restrictions) | ||
| self.generate_files() | ||
| self.generate_files(new_samples, new_columns) | ||
|
|
||
| def _update(self, md_template): | ||
| r"""Update values in the template | ||
|
|
@@ -1245,6 +1254,13 @@ def _update(self, md_template): | |
| md_template : DataFrame | ||
| The metadata template file contents indexed by samples ids | ||
|
|
||
| Returns | ||
| ------- | ||
| set of str | ||
| The samples that were updated | ||
| set of str | ||
| The columns that were updated | ||
|
|
||
| Raises | ||
| ------ | ||
| QiitaDBError | ||
|
|
@@ -1301,7 +1317,7 @@ def _update(self, md_template): | |
| "There are no differences between the data stored in the " | ||
| "DB and the new data provided", | ||
| qdb.exceptions.QiitaDBWarning) | ||
| return | ||
| return set([]), set([]) | ||
|
|
||
| changed.index.names = ['sample_name', 'column'] | ||
| # the combination of np.where and boolean indexing produces | ||
|
|
@@ -1312,7 +1328,9 @@ def _update(self, md_template): | |
| to_update = pd.DataFrame({'to': changed_to}, index=changed.index) | ||
|
|
||
| new_columns = [] | ||
| samples_updated = [] | ||
| for sid, df in to_update.groupby(level=0): | ||
| samples_updated.append(sid) | ||
| values = {k[1]: v for k, v in df.to_dict()['to'].iteritems()} | ||
| new_columns.extend(values.keys()) | ||
| sql = """UPDATE qiita.{0} | ||
|
|
@@ -1321,9 +1339,9 @@ def _update(self, md_template): | |
| self._table_name(self._id)) | ||
| qdb.sql_connection.TRN.add(sql, [dumps(values), sid]) | ||
|
|
||
| new_columns = list(set(new_columns).union(set(self.categories()))) | ||
| nc = list(set(new_columns).union(set(self.categories()))) | ||
| table_name = self._table_name(self.id) | ||
| values = '{"columns": %s}' % dumps(new_columns) | ||
| values = '{"columns": %s}' % dumps(nc) | ||
| sql = """UPDATE qiita.{0} | ||
| SET sample_values = %s | ||
| WHERE sample_id = '{1}'""".format( | ||
|
|
@@ -1332,6 +1350,8 @@ def _update(self, md_template): | |
|
|
||
| qdb.sql_connection.TRN.execute() | ||
|
|
||
| return set(samples_updated), set(new_columns) | ||
|
|
||
| def update(self, md_template): | ||
| r"""Update values in the template | ||
|
|
||
|
|
@@ -1354,9 +1374,9 @@ def update(self, md_template): | |
| # Clean and validate the metadata template given | ||
| new_map = self._clean_validate_template( | ||
| md_template, self.study_id, current_columns=self.categories()) | ||
| self._update(new_map) | ||
| samples, columns = self._update(new_map) | ||
| self.validate(self.columns_restrictions) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feels like it'd be more readable to test for not None or similar here, instead of having to go down into validate(), but I defer. |
||
| self.generate_files() | ||
| self.generate_files(samples, columns) | ||
|
|
||
| def extend_and_update(self, md_template): | ||
| """Performs the update and extend operations at once | ||
|
|
@@ -1374,10 +1394,13 @@ def extend_and_update(self, md_template): | |
| with qdb.sql_connection.TRN: | ||
| md_template = self._clean_validate_template( | ||
| md_template, self.study_id, current_columns=self.categories()) | ||
| self._common_extend_steps(md_template) | ||
| self._update(md_template) | ||
| new_samples, new_columns = self._common_extend_steps(md_template) | ||
| samples, columns = self._update(md_template) | ||
| samples.update(new_samples) | ||
| columns.update(new_columns) | ||
|
|
||
| self.validate(self.columns_restrictions) | ||
| self.generate_files() | ||
| self.generate_files(samples, columns) | ||
|
|
||
| def update_category(self, category, samples_and_values): | ||
| """Update an existing column | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it better to return two empty sets vs None, or (None, None), throw an Exception, etc?