Skip to content

Commit

Permalink
Merge pull request #463 from xchem/m2ms-1202-achristie
Browse files Browse the repository at this point in the history
ref-url and rationale rules
  • Loading branch information
alanbchristie authored Dec 9, 2023
2 parents 6f107c7 + 47438cb commit 12da9f8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
8 changes: 6 additions & 2 deletions viewer/cset_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
TextScoreValues,
User,
)
from viewer.utils import is_url, word_count


def dataType(a_str: str) -> str:
Expand Down Expand Up @@ -381,12 +382,15 @@ def set_mol(
computed_molecule.name = name
computed_molecule.smiles = smiles
# Extract possible reference URL and Rationale
computed_molecule.ref_url = (
# URLs have to be valid URLs and rationals must contain more than one word
ref_url: Optional[str] = (
mol.GetProp('ref_url') if mol.HasProp('ref_url') else None
)
computed_molecule.rationale = (
computed_molecule.ref_url = ref_url if is_url(ref_url) else None
rationale: Optional[str] = (
mol.GetProp('rationale') if mol.HasProp('rationale') else None
)
computed_molecule.rationale = rationale if word_count(rationale) > 1 else None
# To void the error
# needs to have a value for field "id"
# before this many-to-many relationship can be used.
Expand Down
29 changes: 29 additions & 0 deletions viewer/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
create_squonk_job_request_url,
delete_media_sub_directory,
get_https_host,
is_url,
word_count,
)


Expand Down Expand Up @@ -66,3 +68,30 @@ def test_get_https_host(self):
request_mock.get_host.return_value = "example.com"
result = get_https_host(request_mock)
self.assertEqual(result, "https://example.com")

def test_is_url(self):
self.assertTrue(is_url("https://example.com"))
self.assertTrue(is_url("http://example.com"))
self.assertTrue(is_url("ftp://example.com"))
self.assertTrue(is_url("ftps://example.com"))
self.assertTrue(is_url("sftp://example.com"))
self.assertTrue(is_url("ssh://example.com"))
self.assertTrue(is_url("file://example.com"))
self.assertTrue(is_url("mailto://example.com"))
self.assertTrue(is_url("telnet://example.com"))
self.assertTrue(is_url("gopher://example.com"))
self.assertTrue(is_url("wais://example.com"))
self.assertTrue(is_url("ldap://example.com"))
self.assertTrue(is_url("news://example.com"))
self.assertTrue(is_url("nntp://example.com"))
self.assertTrue(is_url("prospero://example.com"))
# SOme failures...
self.assertFalse(is_url("/data/blob.html"))
self.assertFalse(is_url(532))
self.assertFalse(is_url(None))

def test_word_count(self):
self.assertEquals(2, word_count("Hello world"))
self.assertEquals(1, word_count("Hello"))
self.assertEquals(0, word_count(""))
self.assertEquals(0, word_count(None))
15 changes: 15 additions & 0 deletions viewer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import os
import shutil
from pathlib import Path
from typing import Optional
from urllib.parse import urlparse

from django.conf import settings
from rdkit import Chem
Expand All @@ -16,6 +18,19 @@
SDF_VERSION = 'ver_1.2'


def is_url(url: Optional[str]) -> bool:
try:
result = urlparse(url)
return all([result.scheme, result.netloc])
except (ValueError, AttributeError):
return False


def word_count(text: Optional[str]) -> int:
"""Returns an 'approximate' word count."""
return len(text.split()) if text else 0


def create_squonk_job_request_url(instance_id):
"""Creates the Squonk Instance API url from an instance ID (UUID)."""
return settings.SQUONK2_INSTANCE_API + str(instance_id)
Expand Down

0 comments on commit 12da9f8

Please sign in to comment.