Skip to content

Nanopublications templates

NanopubClaim

Bases: Nanopub

Quickly claim a statement.

Constructs statement triples around the provided text following the Hypotheses and Claims Ontology (http://purl.org/petapico/o/hycl).

Parameters:

Name Type Description Default
conf NanopubConf

config for the nanopub

required
claim str

the text of the statement, example: ‘All cats are grey’

required
Source code in nanopub/templates/nanopub_claim.py
class NanopubClaim(Nanopub):
    """Quickly claim a statement.

    Constructs statement triples around the provided text following the Hypotheses and Claims
    Ontology (http://purl.org/petapico/o/hycl).

    Args:
        conf: config for the nanopub
        claim (str): the text of the statement, example: 'All cats are grey'
    """

    def __init__(
        self,
        claim: str,
        conf: NanopubConf,
    ) -> None:
        conf = deepcopy(conf)
        conf.add_prov_generated_time = True
        conf.add_pubinfo_generated_time = True
        conf.attribute_publication_to_profile = True
        super().__init__(
            conf=conf,
        )

        if not self.profile:
            raise ProfileError("No profile provided, cannot generate a Nanopub Claim")

        this_statement = self._metadata.namespace.claim
        # this_statement = BNode("mystatement")
        self.assertion.add((this_statement, RDF.type, HYCL.Statement))
        self.assertion.add((this_statement, RDFS.label, Literal(claim)))

        orcid_id_uri = URIRef(self.profile.orcid_id)
        self.provenance.add((orcid_id_uri, HYCL.claims, this_statement))

NanopubRetract

Bases: Nanopub

Retract a nanopublication.

Publish a retraction nanpublication that declares retraction of the nanopublication that corresponds to the ‘uri’ argument.

Parameters:

Name Type Description Default
conf NanopubConf

config for the nanopub

required
uri str

The uri pointing to the to-be-retracted nanopublication

required
force bool

Toggle using force to retract, this will even retract the nanopublication if it is signed with a different public key than the one in the user profile.

False
Source code in nanopub/templates/nanopub_retract.py
class NanopubRetract(Nanopub):
    """Retract a nanopublication.

    Publish a retraction nanpublication that declares retraction of the nanopublication that
    corresponds to the 'uri' argument.

    Args:
        conf: config for the nanopub
        uri (str): The uri pointing to the to-be-retracted nanopublication
        force (bool): Toggle using force to retract, this will even retract the
            nanopublication if it is signed with a different public key than the one
            in the user profile.
    """

    def __init__(
        self,
        conf: NanopubConf,
        uri: str,
        force: bool = False,
    ) -> None:
        conf = deepcopy(conf)
        conf.add_prov_generated_time = True
        conf.add_pubinfo_generated_time = True
        conf.attribute_publication_to_profile = True
        conf.attribute_assertion_to_profile = True
        super().__init__(
            conf=conf,
        )
        if not self.profile:
            raise ProfileError("No profile provided, cannot generate a Nanopub to retract another nanopub")

        if not force:
            self._check_public_keys_match(uri)
        orcid_id = self.profile.orcid_id
        self.assertion.add(
            (URIRef(orcid_id), NPX.retracts, URIRef(uri))
        )


    def _check_public_keys_match(self, uri):
        """Check for matching public keys of a nanopublication with the profile.

        Raises:
            AssertionError: When the nanopublication is signed with a public key that does not
                match the public key in the profile
        """
        np = Nanopub(
            uri,
            conf=NanopubConf(
                use_test_server=self._conf.use_test_server,
                use_server=self._conf.use_server,
            )
        )
        if np.metadata.public_key is None:
            raise MalformedNanopubError(f"Public key not found in the nanopub {np.source_uri}")
        if self._conf.profile.public_key is None:
            raise ValueError(f"Public key not found for profile {self._conf.profile.orcid_id}")
        if np.metadata.public_key != self._conf.profile.public_key is None:
            raise AssertionError(
                "The public key in your profile does not match the public key"
                "that the publication that you want to retract is signed with."
            )

NanopubIndex

Bases: Nanopub

Publish a list of nanopub URIs in a Nanopub Index

Parameters:

Name Type Description Default
conf NanopubConf

config for the nanopub

required
np_list Union[List[str], List[Nanopub]]

List of nanopub URIs

required
title str

Title of the Nanopub Index

required
description str

Description of the Nanopub Index

required
creation_time str

Creation time of the Nanopub Index, in format YYYY-MM-DDThh-mm-ss

required
creators List[str]

List of the ORCID of the creators of the Nanopub Index

required
see_also str

A URL to a page with further information on the Nanopub Index

None
Source code in nanopub/templates/nanopub_index.py
class NanopubIndex(Nanopub):
    """Publish a list of nanopub URIs in a Nanopub Index

    Args:
        conf: config for the nanopub
        np_list: List of nanopub URIs
        title: Title of the Nanopub Index
        description: Description of the Nanopub Index
        creation_time: Creation time of the Nanopub Index, in format YYYY-MM-DDThh-mm-ss
        creators: List of the ORCID of the creators of the Nanopub Index
        see_also: A URL to a page with further information on the Nanopub Index
    """

    def __init__(
        self,
        conf: NanopubConf,
        np_list: Union[List[str], List[Nanopub]],
        title: str,
        description: str,
        creation_time: str,
        creators: List[str],
        see_also: str = None,
        top_level: bool = False,
    ) -> None:
        conf = deepcopy(conf)
        conf.add_prov_generated_time = False
        conf.add_pubinfo_generated_time = True
        conf.attribute_publication_to_profile = True
        super().__init__(
            conf=conf,
        )

        for np in np_list:
            if isinstance(np, Nanopub):
                np_uri = np.source_uri
            else:
                np_uri = np
            if top_level:
                self.assertion.add((DUMMY_URI, NPX.appendsIndex, URIRef(np_uri)))
            else:
                self.assertion.add((DUMMY_URI, NPX.includesElement, URIRef(np_uri)))

        self.pubinfo.add((DUMMY_URI, RDF.type, NPX.NanopubIndex))
        self.pubinfo.add((DUMMY_URI, DC.title, Literal(title)))
        self.pubinfo.add((DUMMY_URI, DC.description, Literal(description)))
        if see_also:
            self.pubinfo.add((DUMMY_URI, RDFS.seeAlso, URIRef(see_also)))
        for creator in creators:
            self.pubinfo.add((DUMMY_URI, PAV.createdBy, URIRef(creator)))
        # TODO: use current time if not provided
        # datetime.datetime.now().astimezone().replace(microsecond=0).isoformat() ?
        self.pubinfo.add(
            (
                DUMMY_URI,
                DCTERMS.created,
                Literal(creation_time, datatype=XSD.dateTime, normalize=False),
            )
        )

        self.provenance.add((DUMMY_NAMESPACE.assertion, RDF.type, NPX.IndexAssertion))

create_nanopub_index(conf, np_list, title, description, creation_time, creators, see_also=None)

Create a Nanopub index.

Publish a list of nanopub URIs in a Nanopub Index

Parameters:

Name Type Description Default
np_list Union[List[str], List[Nanopub]]

List of nanopub URIs

required
title str

Title of the Nanopub Index

required
description str

Description of the Nanopub Index

required
creation_time str

Creation time of the Nanopub Index, in format YYYY-MM-DDThh-mm-ss

required
creators List[str]

List of the ORCID of the creators of the Nanopub Index

required
see_also str

A URL to a page with further information on the Nanopub Index

None
Source code in nanopub/templates/nanopub_index.py
def create_nanopub_index(
    conf: NanopubConf,
    np_list: Union[List[str], List[Nanopub]],
    title: str,
    description: str,
    creation_time: str,
    creators: List[str],
    see_also: str = None,
) -> List[Nanopub]:
    """Create a Nanopub index.

    Publish a list of nanopub URIs in a Nanopub Index

    Args:
        np_list: List of nanopub URIs
        title: Title of the Nanopub Index
        description: Description of the Nanopub Index
        creation_time: Creation time of the Nanopub Index, in format YYYY-MM-DDThh-mm-ss
        creators: List of the ORCID of the creators of the Nanopub Index
        see_also: A URL to a page with further information on the Nanopub Index
    """
    pub_list: List[Nanopub] = []
    for i in range(0, len(np_list), MAX_NP_PER_INDEX):
        np_chunk = np_list[i:i + MAX_NP_PER_INDEX]
        pub = NanopubIndex(
            conf,
            np_chunk,
            title,
            description,
            creation_time,
            creators,
            see_also,
            top_level=False
        )
        pub.sign()
        log.info(f"Signed Nanopub Index: {pub.source_uri}")
        pub_list.append(pub)

    if len(pub_list) > 1:
        toplevel_pub = NanopubIndex(
            conf,
            pub_list,
            title,
            description,
            creation_time,
            creators,
            see_also,
            top_level=True
        )
        toplevel_pub.sign()
        log.info(f"Signed top level Nanopub Index: {toplevel_pub.source_uri}")
        pub_list.append(toplevel_pub)

    return pub_list

NanopubIntroduction

Bases: Nanopub

Publish a Nanopub introduction to introduce a key pair for an ORCID

Parameters:

Name Type Description Default
conf NanopubConf

config for the nanopub

required
host Optional[str]

the service where the keypair are hosted

None
Source code in nanopub/templates/nanopub_introduction.py
class NanopubIntroduction(Nanopub):
    """Publish a Nanopub introduction to introduce a key pair for an ORCID

    Args:
        conf: config for the nanopub
        host: the service where the keypair are hosted
    """

    def __init__(
        self,
        conf: NanopubConf,
        host: Optional[str] = None,
    ) -> None:
        conf = deepcopy(conf)
        conf.add_prov_generated_time = False
        conf.add_pubinfo_generated_time = True
        conf.attribute_publication_to_profile = True
        conf.attribute_assertion_to_profile = True
        super().__init__(
            conf=conf,
        )
        if not self.profile:
            raise ProfileError("No profile provided, cannot generate a Nanopub Introduction")

        key_declaration = self._metadata.namespace.keyDeclaration
        orcid_node = URIRef(self.conf.profile.orcid_id)

        self.assertion.add((key_declaration, NPX.declaredBy, orcid_node))
        self.assertion.add((key_declaration, NPX.hasAlgorithm, Literal("RSA")))
        self.assertion.add((key_declaration, NPX.hasPublicKey, Literal(self.conf.profile.public_key)))
        self.assertion.add((orcid_node, FOAF.name, Literal(self.conf.profile.name)))
        if host:
            self.assertion.add((key_declaration, NPX.hasKeyLocation, URIRef(host)))