<commit_before>package uk.ac.ebi.spot.rdf.builder;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.ebi.spot.atlas.rdf.Organism;
import uk.ac.ebi.spot.rdf.exception.UnknownOrganismTypeException;
import uk.ac.ebi.spot.rdf.utils.HashingIdGenerator;
import uk.ac.ebi.spot.rdf.utils.PropertyReader;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.HashSet;

/**
 * @author <NAME>
 * @date 31/07/2014
 * Samples, Phenotypes and Ontologies Team, EMBL-EBI
 */
public class PropertiesBasedURIProvider implements URIProvider {

    private PropertyReader propertyReader;

    protected final Logger log = LoggerFactory.getLogger(getClass());

    public String buildVersionNumber = "UNKNOWN";

    private static String HASH = "#";
    private static String DASH = "-";
    private static String SLASH = "/";

    private static String ASSAY = "assay";
    private static String SAMPLE = "sample";
    private static String CHAR = "characteristic";
    private static String EXPR = "expression";
    private static String EXPERIMENT = "experiment";

    public PropertyReader getPropertyReader() {
        return propertyReader;
    }

    public void setPropertyReader(PropertyReader propertyReader) {
        this.propertyReader = propertyReader;
    }

    public PropertiesBasedURIProvider(PropertyReader propertyReader) {
        this.propertyReader = propertyReader;
        try {
            if (getPropertyReader().getProp().contains("gxa.rdf.version")) {
                if (!getPropertyReader().getProp().get("gxa.rdf.version").equals("")) {
                    this.buildVersionNumber = getPropertyReader().getProp().get("gxa.rdf.version").toString();
                    log.info("setting gxa version number to " + buildVersionNumber);
                }
            }
        }
        catch (Exception e) {
            log.warn("Can't read version number from properties file");
        }
    }

    public String read (String property) {
        return  getPropertyReader().getProp().getProperty(property);
    }

    public URI getExperimentUri(String accession) {
        String path = MessageFormat.format(read("gxa.accesion.uri"), accession);
        return URI.create(path.toString());
    }

    public URI getAssayUri(String experimentAccession, String assayAccession) {
        String path = MessageFormat.format(
                read("gxa.assay.uri"),
                experimentAccession,
                HashingIdGenerator.generateHashEncodedID(assayAccession.toLowerCase()));
        return URI.create(path.toString());
    }

    public URI getSampleUri(String experimentAccession, String type, String value) {
        String path = MessageFormat.format(
                read("gxa.sample.uri"),
                experimentAccession,
                HashingIdGenerator.generateHashEncodedID(type.toLowerCase(), value.toLowerCase()));

        return URI.create(path.toString());
    }

    @Override
    public URI getExpressionUri(String experimentAccession, String id) {
        String path = null;
        try {
            path = MessageFormat.format(read("gxa.expression.uri"), experimentAccession, URLEncoder.encode(id, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return URI.create(path.toString());
    }

    @Override
    public URI getFactorUri(String experimentAccession, String type, String value) {
        String path = MessageFormat.format(
                read("gxa.factor.uri"),
                experimentAccession,
                HashingIdGenerator.generateHashEncodedID(type.toLowerCase(), value.toLowerCase()));
        return URI.create(path.toString());
    }

    public URI getExperimentType(String description) {
        return URI.create(read("rdf.terms") + description);
    }

    public URI getIdentifierRelUri() {
        return URI.create(read("gxa.identifier.relation"));
    }

    public URI getDescriptionRelUri() {
        return URI.create(read("gxa.description.relation"));
    }

    public URI getPubmedPredicateUri() {
        return URI.create(read("gxa.pmid.relation"));
    }

    public URI getProbeDesignElementType() {
        return URI.create(read("microarray.probe.type"));
    }

    public URI getPValueRelation() {
        return URI.create(read("gxa.pvalue.relation"));
    }

    public URI getTStatRelation() {
        return URI.create(read("gxa.tstat.relation"));
    }


    public URI getAssayType() {
        return URI.create(read("gxa.assay.type"));
    }


    public URI getOrganismUri(String organismName) throws UnknownOrganismTypeException {
        if (Organism.isValid(organismName)) {
            return URI.create(
                    read("obo.base") +
                            "NCBITaxon_" + Organism.getOrganimsByName(organismName).getId()
            ) ;
        }
        throw new UnknownOrganismTypeException("Unknown organism class:" + organismName);

    }


    public URI getEfoUriFromFragment(String semanticTag) {
        return getEFOIri(semanticTag);
    }

    public URI getAssayToSampleRel() {
        return URI.create(read("gxa.assay2sample.relation"));
    }


    public URI getPropertyTypeAnnotationProperty() {
        return URI.create(read("gxa.propertyType.relation"));
    }


    public URI getPropertyValueAnnotationProperty() {
        return URI.create(read("gxa.propertyValue.relation"));
    }

    public URI getDesignElementUri(String platform, String designElement) {
        String path = MessageFormat.format(read("gxa.probe.uri"), platform, designElement);
        return URI.create(path.toString());
    }


    public URI getDiffValueToProbeElementRel() {
        return URI.create(read("gxa.refersto.relation"));
    }


    public Collection<URI> getBioentityUri(String id, String speciesName) {

        Collection<URI> uris = new HashSet<URI>();

        String path = MessageFormat.format(read("ensembl.identifiers.uri"), "", id);
        String path2 = MessageFormat.format(read("ensembl.resource.uri"), id);
        if (Organism.isValid(speciesName)) {
            String kingdom = Organism.getOrganimsByName(speciesName).getKingdom();

            if (!kingdom.equals("eukaryote")) {
                path = MessageFormat.format(read("ensembl.resource.uri"), "." + kingdom, id);
            }
        }
        else if (id.startsWith("MIMAT"))  {
            path = MessageFormat.format(read("mirBase.mature.resource.uri"),  id);
        }
        else if (id.startsWith("MI"))  {
            path = MessageFormat.format(read("mirBase.resource.uri"),  id);
        }
        uris.add(URI.create(path));
        uris.add(URI.create(path2));

        return uris;
    }


    public URI getExperimentToAssayRel() {
        return URI.create(read("gxa.hasPart.relation"));
    }

    public URI getExperimentToAnalysisRel() {
        return URI.create(read("gxa.hasPart.relation"));
    }

    public URI getPubmedUri(String pubmedId) {
        String path = MessageFormat.format(read("pubmed.resource.uri"), pubmedId);
        return URI.create(path.toString());
    }<commit_msg>fix identifiers uri for ensembl genomes adatasest<commit_after>package uk.ac.ebi.spot.rdf.builder;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.ebi.spot.atlas.rdf.Organism;
import uk.ac.ebi.spot.rdf.exception.UnknownOrganismTypeException;
import uk.ac.ebi.spot.rdf.utils.HashingIdGenerator;
import uk.ac.ebi.spot.rdf.utils.PropertyReader;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.HashSet;

/**
 * @author <NAME>
 * @date 31/07/2014
 * Samples, Phenotypes and Ontologies Team, EMBL-EBI
 */
public class PropertiesBasedURIProvider implements URIProvider {

    private PropertyReader propertyReader;

    protected final Logger log = LoggerFactory.getLogger(getClass());

    public String buildVersionNumber = "UNKNOWN";

    private static String HASH = "#";
    private static String DASH = "-";
    private static String SLASH = "/";

    private static String ASSAY = "assay";
    private static String SAMPLE = "sample";
    private static String CHAR = "characteristic";
    private static String EXPR = "expression";
    private static String EXPERIMENT = "experiment";

    public PropertyReader getPropertyReader() {
        return propertyReader;
    }

    public void setPropertyReader(PropertyReader propertyReader) {
        this.propertyReader = propertyReader;
    }

    public PropertiesBasedURIProvider(PropertyReader propertyReader) {
        this.propertyReader = propertyReader;
        try {
            if (getPropertyReader().getProp().contains("gxa.rdf.version")) {
                if (!getPropertyReader().getProp().get("gxa.rdf.version").equals("")) {
                    this.buildVersionNumber = getPropertyReader().getProp().get("gxa.rdf.version").toString();
                    log.info("setting gxa version number to " + buildVersionNumber);
                }
            }
        }
        catch (Exception e) {
            log.warn("Can't read version number from properties file");
        }
    }

    public String read (String property) {
        return  getPropertyReader().getProp().getProperty(property);
    }

    public URI getExperimentUri(String accession) {
        String path = MessageFormat.format(read("gxa.accesion.uri"), accession);
        return URI.create(path.toString());
    }

    public URI getAssayUri(String experimentAccession, String assayAccession) {
        String path = MessageFormat.format(
                read("gxa.assay.uri"),
                experimentAccession,
                HashingIdGenerator.generateHashEncodedID(assayAccession.toLowerCase()));
        return URI.create(path.toString());
    }

    public URI getSampleUri(String experimentAccession, String type, String value) {
        String path = MessageFormat.format(
                read("gxa.sample.uri"),
                experimentAccession,
                HashingIdGenerator.generateHashEncodedID(type.toLowerCase(), value.toLowerCase()));

        return URI.create(path.toString());
    }

    @Override
    public URI getExpressionUri(String experimentAccession, String id) {
        String path = null;
        try {
            path = MessageFormat.format(read("gxa.expression.uri"), experimentAccession, URLEncoder.encode(id, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return URI.create(path.toString());
    }

    @Override
    public URI getFactorUri(String experimentAccession, String type, String value) {
        String path = MessageFormat.format(
                read("gxa.factor.uri"),
                experimentAccession,
                HashingIdGenerator.generateHashEncodedID(type.toLowerCase(), value.toLowerCase()));
        return URI.create(path.toString());
    }

    public URI getExperimentType(String description) {
        return URI.create(read("rdf.terms") + description);
    }

    public URI getIdentifierRelUri() {
        return URI.create(read("gxa.identifier.relation"));
    }

    public URI getDescriptionRelUri() {
        return URI.create(read("gxa.description.relation"));
    }

    public URI getPubmedPredicateUri() {
        return URI.create(read("gxa.pmid.relation"));
    }

    public URI getProbeDesignElementType() {
        return URI.create(read("microarray.probe.type"));
    }

    public URI getPValueRelation() {
        return URI.create(read("gxa.pvalue.relation"));
    }

    public URI getTStatRelation() {
        return URI.create(read("gxa.tstat.relation"));
    }


    public URI getAssayType() {
        return URI.create(read("gxa.assay.type"));
    }


    public URI getOrganismUri(String organismName) throws UnknownOrganismTypeException {
        if (Organism.isValid(organismName)) {
            return URI.create(
                    read("obo.base") +
                            "NCBITaxon_" + Organism.getOrganimsByName(organismName).getId()
            ) ;
        }
        throw new UnknownOrganismTypeException("Unknown organism class:" + organismName);

    }


    public URI getEfoUriFromFragment(String semanticTag) {
        return getEFOIri(semanticTag);
    }

    public URI getAssayToSampleRel() {
        return URI.create(read("gxa.assay2sample.relation"));
    }


    public URI getPropertyTypeAnnotationProperty() {
        return URI.create(read("gxa.propertyType.relation"));
    }


    public URI getPropertyValueAnnotationProperty() {
        return URI.create(read("gxa.propertyValue.relation"));
    }

    public URI getDesignElementUri(String platform, String designElement) {
        String path = MessageFormat.format(read("gxa.probe.uri"), platform, designElement);
        return URI.create(path.toString());
    }


    public URI getDiffValueToProbeElementRel() {
        return URI.create(read("gxa.refersto.relation"));
    }


    public Collection<URI> getBioentityUri(String id, String speciesName) {

        Collection<URI> uris = new HashSet<URI>();

        String path = MessageFormat.format(read("ensembl.identifiers.uri"), "", id);
        String path2 = MessageFormat.format(read("ensembl.resource.uri"), id);
        if (Organism.isValid(speciesName)) {
            String kingdom = Organism.getOrganimsByName(speciesName).getKingdom();

            if (!kingdom.equals("eukaryote")) {
                path = MessageFormat.format(read("ensembl.identifiers.uri"), "." + kingdom, id);
            }
        }
        else if (id.startsWith("MIMAT"))  {
            path = MessageFormat.format(read("mirBase.mature.resource.uri"),  id);
        }
        else if (id.startsWith("MI"))  {
            path = MessageFormat.format(read("mirBase.resource.uri"),  id);
        }
        uris.add(URI.create(path));
        uris.add(URI.create(path2));

        return uris;
    }


    public URI getExperimentToAssayRel() {
        return URI.create(read("gxa.hasPart.relation"));
    }

    public URI getExperimentToAnalysisRel() {
        return URI.create(read("gxa.hasPart.relation"));
    }

    public URI getPubmedUri(String pubmedId) {
        String path = MessageFormat.format(read("pubmed.resource.uri"), pubmedId);
        return URI.create(path.toString());
    }
{'fraction_non_alphanumeric': 0.11525470462535328, 'fraction_numerical': 0.0019301027090370165, 'mean_word_length': 2.810874704491726, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4559292', 'n_tokens_mistral': 4102, 'n_tokens_neox': 3951, 'n_words': 770}