def replace_uri(g, initial_uri, replacement_uri):
    """
    Replaces a given URI for all subjects or objects (not predicates) in a given graph

    :param g: the graph to replace URIs in
    :param initial_uri: the URI to replace
    :param replacement_uri: the replacement URI
    :return: the altered graph g
    """
    # replace all subjects
    u = '''
        DELETE {
            ?s ?p ?o .
        }
        INSERT {
            <%(replacement_uri)s> ?p ?o .
        }
        WHERE {
            ?s ?p ?o .
            FILTER (STR(?s) = "%(initial_uri)s")
            # Nick: this really seems to need to be a FILTER, not a subgraph match i.e. <> ?p ?o . Don't know why.
        }
    ''' % {'replacement_uri': replacement_uri, 'initial_uri': initial_uri}
    g.update(u)

    # replace all objects
    u = '''
        DELETE {
            ?s ?p ?o .
        }
        INSERT {
            ?s ?p <%(replacement_uri)s> .
        }
        WHERE {
            ?s ?p ?o .
            FILTER (STR(?o) = "%(initial_uri)s")
        }
    ''' % {'replacement_uri': replacement_uri, 'initial_uri': initial_uri}
    g.update(u)

    # there are no predicates to place (no placeholder relations)
    return g