Skip to content

Commit

Permalink
Merge pull request #168 from xnought/desc
Browse files Browse the repository at this point in the history
feat: add description to proteins
  • Loading branch information
ansengarvin authored Feb 14, 2024
2 parents 86f4a4d + 6b59895 commit 2603896
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 27 deletions.
1 change: 1 addition & 0 deletions backend/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CREATE TABLE species (
CREATE TABLE proteins (
id serial PRIMARY KEY,
name text NOT NULL UNIQUE, -- user specified name of the protein (TODO: consider having a string limit)
description text,
length integer, -- length of amino acid sequence
mass numeric, -- mass in amu/daltons
content bytea, -- stored markdown for the protein article (TODO: consider having a limit to how big this can be)
Expand Down
34 changes: 30 additions & 4 deletions backend/src/api/protein.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ def get_protein_entry(protein_name: str):
"""
with Database() as db:
try:
query = """SELECT proteins.name, proteins.length, proteins.mass, proteins.content, proteins.refs, species.name as species_name FROM proteins
query = """SELECT proteins.name,
proteins.description,
proteins.length,
proteins.mass,
proteins.content,
proteins.refs,
species.name
FROM proteins
JOIN species ON species.id = proteins.species_id
WHERE proteins.name = %s;"""
entry_sql = db.execute_return(query, [protein_name])
Expand All @@ -125,7 +132,15 @@ def get_protein_entry(protein_name: str):
if entry_sql is not None and len(entry_sql) != 0:
# return the only entry
only_returned_entry = entry_sql[0]
name, length, mass, content, refs, species_name = only_returned_entry
(
name,
description,
length,
mass,
content,
refs,
species_name,
) = only_returned_entry

# if byte arrays are present, decode them into a string
if content is not None:
Expand All @@ -135,6 +150,7 @@ def get_protein_entry(protein_name: str):

return ProteinEntry(
name=name,
description=description,
length=length,
mass=mass,
content=content,
Expand Down Expand Up @@ -200,12 +216,13 @@ def upload_protein_entry(body: UploadBody):

try:
# add the protein itself
query = """INSERT INTO proteins (name, length, mass, content, refs, species_id)
VALUES (%s, %s, %s, %s, %s, (SELECT id FROM species WHERE name = %s));"""
query = """INSERT INTO proteins (name, description, length, mass, content, refs, species_id)
VALUES (%s, %s, %s, %s, %s, %s, (SELECT id FROM species WHERE name = %s));"""
db.execute(
query,
[
pdb.name,
body.description,
pdb.num_amino_acids,
pdb.mass_daltons,
str_to_bytea(body.content),
Expand Down Expand Up @@ -267,5 +284,14 @@ def edit_protein_entry(body: EditBody):
],
)

if body.new_description is not None:
db.execute(
"""UPDATE proteins SET description = %s WHERE name = %s""",
[
body.new_description,
body.old_name if not name_changed else body.new_name,
],
)

except Exception:
return UploadError.WRITE_ERROR
15 changes: 10 additions & 5 deletions backend/src/api/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ def search_proteins(body: SearchProteinsBody):
filter_clauses = gen_sql_filters(
body.species_filter, body.length_filter, body.mass_filter
)
entries_query = """SELECT proteins.name, proteins.length, proteins.mass, species.name as species_name FROM proteins
JOIN species ON species.id = proteins.species_id
WHERE proteins.name ILIKE %s"""

entries_query = """SELECT proteins.name,
proteins.description,
proteins.length,
proteins.mass,
species.name
FROM proteins
JOIN species ON species.id = proteins.species_id
WHERE proteins.name ILIKE %s"""
log.warn(filter_clauses)
entries_result = db.execute_return(
sanitize_query(entries_query + filter_clauses),
Expand All @@ -91,8 +95,9 @@ def search_proteins(body: SearchProteinsBody):
length=length,
mass=mass,
species_name=species_name,
description=description,
)
for name, length, mass, species_name in entries_result
for name, description, length, mass, species_name in entries_result
],
total_found=len(entries_result),
)
Expand Down
3 changes: 3 additions & 0 deletions backend/src/api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ProteinEntry(CamelModel):
species_name: str
content: str | None = None
refs: str | None = None
description: str | None = None


class AllEntries(CamelModel):
Expand All @@ -38,6 +39,7 @@ class AllEntries(CamelModel):

class UploadBody(CamelModel):
name: str
description: str
species_name: str
content: str # markdown content from user
refs: str # references used in content (bibtex form)
Expand All @@ -62,6 +64,7 @@ class EditBody(CamelModel):
new_species_name: str
new_content: str | None = None
new_refs: str | None = None
new_description: str | None = None


class LoginBody(CamelModel):
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/lib/ListProteins.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import dummy from "../images/dummy.png";
export let allEntries: ProteinEntry[] | null = null;
const dummyDesc =
"scriptionDescriptionDescription DescriptionDescription Description Description";
</script>

<div class="prot-grid">
Expand All @@ -17,7 +15,7 @@
<div
class="prot-container"
on:click={() => navigate(`/protein/${entry.name}`)}
title={`Name:${entry.name}\nDescription:${dummyDesc}`}
title={`Name:${entry.name}\nDescription:${entry.description}`}
>
<div class="prot-thumb mr-2">
<img class="prot-thumb" src={dummy} alt="dummy" />
Expand All @@ -27,7 +25,9 @@
{entry.name}
</div>
<div class="prot-desc">
{dummyDesc}
{#if entry.description}
{entry.description}
{/if}
</div>
<div>
<div><b>Organism</b>: {entry.speciesName}</div>
Expand Down Expand Up @@ -87,6 +87,7 @@
.prot-grid {
display: flex;
gap: 20px;
align-content: flex-start;
flex-wrap: wrap;
padding: 10px;
margin-left: 10px;
Expand Down
1 change: 1 addition & 0 deletions frontend/src/lib/openapi/models/EditBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export type EditBody = {
newSpeciesName: string;
newContent?: (string | null);
newRefs?: (string | null);
newDescription?: (string | null);
};

1 change: 1 addition & 0 deletions frontend/src/lib/openapi/models/ProteinEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export type ProteinEntry = {
speciesName: string;
content?: (string | null);
refs?: (string | null);
description?: (string | null);
};

1 change: 1 addition & 0 deletions frontend/src/lib/openapi/models/UploadBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/* eslint-disable */
export type UploadBody = {
name: string;
description: string;
speciesName: string;
content: string;
refs: string;
Expand Down
38 changes: 32 additions & 6 deletions frontend/src/routes/Edit.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
import { navigate } from "svelte-routing";
import { onMount } from "svelte";
import ArticleEditor from "../lib/ArticleEditor.svelte";
import {user} from "../lib/stores/user"
import { user } from "../lib/stores/user";
// key difference, here we get the information, then populate it in the upload form that can be edited
// and reuploaded/edited
export let urlId: string;
// store original too so we can see if the user changed/edited the content
let name: string;
let ogName: string;
let description: string;
let ogDescription: string;
let ogContent: string;
let content: string;
let ogRefs: string;
Expand All @@ -28,8 +31,10 @@
// when this component mounts, request protein wikipedia entry from backend
onMount(async () => {
if (!$user.loggedIn) {
alert("You are not logged in. You are being redirected to home. TODO: Make this better.")
navigate("/")
alert(
"You are not logged in. You are being redirected to home. TODO: Make this better."
);
navigate("/");
}
// Request the protein from backend given ID
Expand All @@ -40,17 +45,21 @@
if (entry == null) {
error = true;
} else {
// keep track of db value and the value we change (og denotes original / db)
name = entry.name;
ogName = name;
content = entry.content ?? "";
ogContent = content; // log original content
ogContent = content;
refs = entry.refs ?? "";
ogRefs = refs; // log original refs
ogRefs = refs;
species = entry.speciesName;
ogSpecies = species;
description = entry.description ?? "";
ogDescription = description;
}
allSpecies = await Backend.searchSpecies();
Expand All @@ -59,7 +68,8 @@
name !== ogName ||
content !== ogContent ||
refs !== ogRefs ||
species !== ogSpecies;
species !== ogSpecies ||
ogDescription !== description;
</script>

<svelte:head>
Expand Down Expand Up @@ -90,6 +100,18 @@
{/if}
</div>

<div>
<Label for="protein-desc" class="block mb-2"
>Protein Description</Label
>
<Input
bind:value={description}
style="width: 600px"
id="protein-desc"
placeholder="Description"
/>
</div>

<div class="flex gap-5 mb-2">
<div>
<Label for="species-select" class="mb-2"
Expand Down Expand Up @@ -128,6 +150,10 @@
? content
: undefined,
newRefs: refs !== ogRefs ? refs : undefined,
newDescription:
description !== ogDescription
? description
: undefined,
});
if (err) {
uploadError = err;
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/routes/Protein.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@
</h1>

<div id="description">
DescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescription
Description Description Description Description
{#if entry.description}
{entry.description}
{/if}
</div>

<EntryCard title="Computed Insights">
Expand Down
24 changes: 20 additions & 4 deletions frontend/src/routes/Upload.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@
import ArticleEditor from "../lib/ArticleEditor.svelte";
import { onMount } from "svelte";
import Cookies from "js-cookie";
import {user} from "../lib/stores/user"
import { user } from "../lib/stores/user";
let species: string[] | null;
let selectedSpecies: string = "unknown";
const authToken = Cookies.get("auth")
const authToken = Cookies.get("auth");
onMount(async () => {
if (!$user.loggedIn) {
alert("You are not logged in. You are being redirected to home. TODO: Make this better.")
navigate("/")
alert(
"You are not logged in. You are being redirected to home. TODO: Make this better."
);
navigate("/");
}
species = await Backend.searchSpecies();
});
let name: string = "";
let description: string = "";
let content: string = "";
let files: FileList | undefined; // bind:files on the Fileupload
let uploadError: UploadError | undefined;
Expand Down Expand Up @@ -62,6 +65,18 @@
{/if}
</div>

<div>
<Label for="protein-desc" class="block mb-2"
>Protein Description</Label
>
<Input
bind:value={description}
style="width: 600px"
id="protein-desc"
placeholder="Description"
/>
</div>

<div class="flex gap-5 mb-2">
<div>
<Label for="species-select" class="mb-2">Select a Species</Label
Expand Down Expand Up @@ -95,6 +110,7 @@
try {
const err = await Backend.uploadProteinEntry({
name,
description,
pdbFileStr,
content,
refs,
Expand Down
10 changes: 8 additions & 2 deletions galaxy/upload_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def remove_box():
os.system(f"rm -rf {DIR}")


def upload_protein_file(path, name, species_name, content="", refs=""):
def upload_protein_file(path, name, species_name, content="", refs="", desc=""):
with open(path, "r") as f:
pdb_file_str = f.read()

Expand All @@ -24,6 +24,7 @@ def upload_protein_file(path, name, species_name, content="", refs=""):
"content": content,
"refs": refs,
"pdb_file_str": pdb_file_str,
"description": desc,
}
out = requests.post("http://localhost:8000/protein/upload", json=payload)
return out
Expand All @@ -43,7 +44,12 @@ def upload_all():
name = fn.split(".")[0].replace("_", " ")
species_name = available_species[fn[:2]]
upload_protein_file(
full_path, name, species_name, content=CONTENT, refs=REFS
full_path,
name,
species_name,
content=CONTENT,
refs=REFS,
desc="from the venom lab at osu",
)
print("uploaded", full_path, name, species_name)
remove_box()
Expand Down

0 comments on commit 2603896

Please sign in to comment.