Skip to content
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

Changes to sample data editing #781

Open
wants to merge 3 commits into
base: testing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 60 additions & 15 deletions wqflask/wqflask/metadata_edits.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@
from gn3.db import fetchone
from gn3.db import insert
from gn3.db import update
from gn3.db.datasets import retrieve_sample_list, retrieve_group_name, retrieve_trait_dataset
from gn3.db.metadata_audit import MetadataAudit
from gn3.db.phenotypes import Phenotype
from gn3.db.phenotypes import Probeset
from gn3.db.phenotypes import Publication
from gn3.db.phenotypes import PublishXRef
from gn3.db.phenotypes import probeset_mapping
from gn3.db.sample_data import delete_sample_data
from gn3.db.sample_data import get_trait_csv_sample_data
from gn3.db.sample_data import get_trait_sample_data, get_trait_csv_sample_data
from gn3.db.sample_data import insert_sample_data
from gn3.db.sample_data import update_sample_data

Expand Down Expand Up @@ -131,8 +132,15 @@ def edit_probeset(conn, name):
def display_phenotype_metadata(dataset_id: str, name: str):
with database_connection() as conn:
_d = edit_phenotype(conn=conn, name=name, dataset_id=dataset_id)

group_name = retrieve_group_name(dataset_id, conn)
sample_list = retrieve_sample_list(group_name)
sample_data = get_trait_sample_data(conn, name, _d.get("publish_xref").phenotype_id)

return render_template(
"edit_phenotype.html",
sample_list = sample_list,
sample_data = sample_data,
publish_xref=_d.get("publish_xref"),
phenotype=_d.get("phenotype"),
publication=_d.get("publication"),
Expand Down Expand Up @@ -171,7 +179,7 @@ def update_phenotype(dataset_id: str, name: str):
or ""
)
phenotype_id = str(data_.get("phenotype-id"))
if not (file_ := request.files.get("file")):
if not (file_ := request.files.get("file")) and data_.get('edited') == "false":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: issue: This seems odd to me. Doing a string comparison the string "false". If there's a way to do directly with a boolean value, it would make this easier to both debug and read.

flash("No sample-data has been uploaded", "warning")
else:
create_dirs_if_not_exists(
Expand All @@ -188,21 +196,35 @@ def update_phenotype(dataset_id: str, name: str):
)
diff_data = {}
with database_connection() as conn:
group_name = retrieve_group_name(dataset_id, conn)
sample_list = retrieve_sample_list(group_name)
headers = ["Strain Name", "Value", "SE", "Count"]
diff_data = remove_insignificant_edits(
diff_data=csv_diff(
base_csv=(
base_csv := get_trait_csv_sample_data(
conn=conn,
trait_name=str(name),
phenotype_id=str(phenotype_id),
)
),
delta_csv=(delta_csv := file_.read().decode()),
tmp_dir=TMPDIR,
),
epsilon=0.001,
base_csv = get_trait_csv_sample_data(
conn=conn,
trait_name=str(name),
phenotype_id=str(phenotype_id),
sample_list=sample_list,
)
if not (file_) and data_.get('edited') == "true":
delta_csv = create_delta_csv(base_csv, data_, sample_list)
diff_data = remove_insignificant_edits(
diff_data=csv_diff(
base_csv=base_csv,
delta_csv=delta_csv,
tmp_dir=TMPDIR,
),
epsilon=0.001,
)
else:
diff_data = remove_insignificant_edits(
diff_data=csv_diff(
base_csv=base_csv,
delta_csv=(delta_csv := file_.read().decode()),
tmp_dir=TMPDIR,
),
epsilon=0.001,
)

invalid_headers = extract_invalid_csv_headers(
allowed_headers=headers, csv_text=delta_csv
)
Expand Down Expand Up @@ -705,3 +727,26 @@ def approve_data(resource_id: str, file_name: str):
"warning",
)
return redirect(url_for("metadata_edit.list_diffs"))

def create_delta_csv(base_csv, form_data, sample_list):
base_csv_lines = base_csv.split("\n")
delta_csv_lines = [base_csv_lines[0]]

for line in base_csv_lines[1:]:
Copy link
Contributor

@jgarte jgarte May 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @zsloan @BonfaceKilz, maybe there's a way to code this that doesn't have a high cyclomatic complexity?

Otherwise, #781 LGTM

Feel free to ignore if you think this is a nitpick...

sample = {}
sample['name'], sample['value'], sample['error'], sample['n_cases'] = line.split(",")
for key in form_data:
if sample['name'] in key:
new_line_items = [sample['name']]
for field in ["value", "error", "n_cases"]:
if form_data.get(field + ":" + sample['name']):
if form_data.get(field + ":" + sample['name']).isnumeric():
new_line_items.append(form_data.get(field + ":" + sample['name']))
continue
new_line_items.append(sample[field])
delta_csv_lines.append(",".join(new_line_items))
break
else:
delta_csv_lines.append(line)

return "\n".join(delta_csv_lines)
12 changes: 9 additions & 3 deletions wqflask/wqflask/templates/edit_phenotype.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ <h1>Trait Metadata and Data Editing Form: {{ name }}</h1>
<input name="inbred-set-id" class="changed" type="hidden" value="{{ publish_xref.inbred_set_id }}"/>
<input name="phenotype-id" class="changed" type="hidden" value="{{ publish_xref.phenotype_id }}"/>
<input name="comments" class="changed" type="hidden" value="{{ publish_xref.comments }}"/>
<input name="edited" class="changed" type="hidden" value="false"/>
</div>
</div>
<div>
Expand Down Expand Up @@ -208,10 +209,10 @@ <h2>Edit Metadata</h2>
<td><input type="checkbox"></td>
<td>{{ loop.index }}</td>
<td>{{ sample }}</td>
<td><input type="text" name="value:{{ sample }}" value="{% if sample_data.get(sample) %}{{ '%0.3f' | format(sample_data.get(sample).value | float) }}{% else %}x{% endif %}" size=4 maxlength=4></input></td>
<td><input type="text" name="value:{{ sample }}" class="table_input" value="{% if sample_data.get(sample) %}{{ '%0.3f' | format(sample_data.get(sample).value | float) }}{% else %}x{% endif %}" size=4 maxlength=4></input></td>
<td>±</td>
<td><input type="text" name="error:{{ sample }}" step='0.001' value="{% if sample_data.get(sample).error %}{{ '%0.3f' | format(sample_data.get(sample).error | float) }}{% else %}x{% endif %}" size=4 maxlength=4></input></td>
<td><input type="text" name="n_cases:{{ sample }}" value="{% if sample_data.get(sample).n_cases %}{{ sample_data.get(sample).n_cases }}{% else %}x{% endif %}" size=3 maxlength=3></input></td>
<td><input type="text" name="error:{{ sample }}" class="table_input" value="{% if sample_data.get(sample).error %}{{ '%0.3f' | format(sample_data.get(sample).error | float) }}{% else %}x{% endif %}" size=4 maxlength=4></input></td>
<td><input type="text" name="n_cases:{{ sample }}" class="table_input" value="{% if sample_data.get(sample).n_cases %}{{ sample_data.get(sample).n_cases }}{% else %}x{% endif %}" size=3 maxlength=3></input></td>
</tr>
{% endfor %}
</tbody>
Expand All @@ -234,5 +235,10 @@ <h2>Edit Metadata</h2>
$("input[type=submit]").click(function(){
$(":input:not(.changed)").attr("disabled", "disabled");
});

// This is an awkward way to detect changes to table data, so it doesn't process it otherwise
$(".table_input").change(function(){
$("input[name=edited]").val("true");
});
</script>
{% endblock %}