<commit_before>package org.phenoscape.obd.loader;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.obd.model.CompositionalDescription;
import org.obd.model.Graph;
import org.obd.model.LinkStatement;
import org.obd.model.LiteralStatement;
import org.obd.model.Node;
import org.obd.model.Statement;
import org.obd.model.CompositionalDescription.Predicate;
import org.obo.datamodel.OBOClass;
import org.phenoscape.model.Character;
import org.phenoscape.model.DataSet;
import org.phenoscape.model.Phenotype;
import org.phenoscape.model.Specimen;
import org.phenoscape.model.State;
import org.phenoscape.model.Taxon;
import org.purl.obo.vocab.RelationVocabulary;

/**
 * Bridges between phenoscape objects and a model expressed using OBO
 * primitives.
 * 
 * OBD Instances and the relations between them generally reflect java instances
 * of the corresponding phenoscape model. We model cells, states and
 * matrixes/datasets using instances of CDAO owl classes. We go beyond the
 * phenoscape model and CDAO in positing an annotation link between the
 * phenotype and the taxon. The annotation is linked to the cell.
 * 
 * TODO: reverse mapping TODO: finalize relations and classes; both from OBO_REL
 * and CDAO TODO: make this subclass a generic bridge framework
 * 
 * @author cjm
 * 
 */
public class OBDModelBridge {

    protected Graph graph;

    private static final RelationVocabulary relationVocabulary = new RelationVocabulary();
    private Map<Character, String> characterIdMap;
    private Map<State, String> stateIdMap;
    private Map<Taxon, String> taxonIdMap;
    private Map<Taxon, String> taxonIDToOTUID;
    private Map<Phenotype, String> phenotypeIdMap;
    protected Set<LinkStatement> phenotypes;

    public Graph getGraph() {
        return graph;
    }

    public void setGraph(Graph graph) {
        this.graph = graph;
    }

    public Graph translate(DataSet dataset) throws IOException {
        String dsId = UUID.randomUUID().toString();
        graph = new Graph();
        phenotypes = new HashSet<LinkStatement>();
        characterIdMap = new HashMap<Character, String>();
        stateIdMap = new HashMap<State, String>();
        taxonIdMap = new HashMap<Taxon, String>();
        taxonIDToOTUID = new HashMap<Taxon, String>(); 
        phenotypeIdMap = new HashMap<Phenotype, String>();
        // Dataset metadata
        this.graph.addNode(OBDUtil.createInstanceNode(dsId, Vocab.DATASET_TYPE_ID));
        final String curators = dataset.getCurators();
        final Node pubNode = OBDUtil.createInstanceNode(dataset.getPublication(), Vocab.PUBLICATION_TYPE_ID);
        this.graph.addNode(pubNode);
        LinkStatement ds2p = new LinkStatement(dsId, Vocab.HAS_PUB_REL_ID, pubNode.getId());
        graph.addStatement(ds2p);

        if(curators != null){
            LiteralStatement ds2curators = new LiteralStatement(dsId, Vocab.HAS_CURATORS_REL_ID, curators);
            graph.addStatement(ds2curators);
        }
        for (Taxon taxon : dataset.getTaxa()) {
            // avoid uploading taxa without names; Cartik1.0
            //TODO add all taxa as OTUs; check to see if valid name is set before creating annotations
            if ((taxon.getValidName() != null) && (taxon.getValidName().getName() != null) && (taxon.getValidName().getName().length() > 0)) {
                Node taxonNode = translate(taxon);
                if (taxonNode.getId() != null) {
                    taxonIdMap.put(taxon, taxonNode.getId());
                    final String otuId = UUID.randomUUID().toString();
                    final Node otuNode = OBDUtil.createInstanceNode(otuId, Vocab.OTU_TYPE_ID);
                    this.taxonIDToOTUID.put(taxon, otuNode.getId());
                    if (!StringUtils.isBlank(taxon.getPublicationName())) {
                        otuNode.setLabel(taxon.getPublicationName());
                    } else {<commit_msg>Removed unnecessary node creation for publications.<commit_after>package org.phenoscape.obd.loader;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.obd.model.CompositionalDescription;
import org.obd.model.Graph;
import org.obd.model.LinkStatement;
import org.obd.model.LiteralStatement;
import org.obd.model.Node;
import org.obd.model.Statement;
import org.obd.model.CompositionalDescription.Predicate;
import org.obo.datamodel.OBOClass;
import org.phenoscape.model.Character;
import org.phenoscape.model.DataSet;
import org.phenoscape.model.Phenotype;
import org.phenoscape.model.Specimen;
import org.phenoscape.model.State;
import org.phenoscape.model.Taxon;
import org.purl.obo.vocab.RelationVocabulary;

/**
 * Bridges between phenoscape objects and a model expressed using OBO
 * primitives.
 * 
 * OBD Instances and the relations between them generally reflect java instances
 * of the corresponding phenoscape model. We model cells, states and
 * matrixes/datasets using instances of CDAO owl classes. We go beyond the
 * phenoscape model and CDAO in positing an annotation link between the
 * phenotype and the taxon. The annotation is linked to the cell.
 * 
 * TODO: reverse mapping TODO: finalize relations and classes; both from OBO_REL
 * and CDAO TODO: make this subclass a generic bridge framework
 * 
 * @author cjm
 * 
 */
public class OBDModelBridge {

    protected Graph graph;

    private static final RelationVocabulary relationVocabulary = new RelationVocabulary();
    private Map<Character, String> characterIdMap;
    private Map<State, String> stateIdMap;
    private Map<Taxon, String> taxonIdMap;
    private Map<Taxon, String> taxonIDToOTUID;
    private Map<Phenotype, String> phenotypeIdMap;
    protected Set<LinkStatement> phenotypes;

    public Graph getGraph() {
        return graph;
    }

    public void setGraph(Graph graph) {
        this.graph = graph;
    }

    public Graph translate(DataSet dataset) throws IOException {
        String dsId = UUID.randomUUID().toString();
        graph = new Graph();
        phenotypes = new HashSet<LinkStatement>();
        characterIdMap = new HashMap<Character, String>();
        stateIdMap = new HashMap<State, String>();
        taxonIdMap = new HashMap<Taxon, String>();
        taxonIDToOTUID = new HashMap<Taxon, String>(); 
        phenotypeIdMap = new HashMap<Phenotype, String>();
        // Dataset metadata
        this.graph.addNode(OBDUtil.createInstanceNode(dsId, Vocab.DATASET_TYPE_ID));
        final String curators = dataset.getCurators();
        LinkStatement ds2p = new LinkStatement(dsId, Vocab.HAS_PUB_REL_ID, dataset.getPublication());
        graph.addStatement(ds2p);

        if (curators != null) {
            LiteralStatement ds2curators = new LiteralStatement(dsId, Vocab.HAS_CURATORS_REL_ID, curators);
            graph.addStatement(ds2curators);
        }
        for (Taxon taxon : dataset.getTaxa()) {
            // avoid uploading taxa without names; Cartik1.0
            //TODO add all taxa as OTUs; check to see if valid name is set before creating annotations
            if ((taxon.getValidName() != null) && (taxon.getValidName().getName() != null) && (taxon.getValidName().getName().length() > 0)) {
                Node taxonNode = translate(taxon);
                if (taxonNode.getId() != null) {
                    taxonIdMap.put(taxon, taxonNode.getId());
                    final String otuId = UUID.randomUUID().toString();
                    final Node otuNode = OBDUtil.createInstanceNode(otuId, Vocab.OTU_TYPE_ID);
                    this.taxonIDToOTUID.put(taxon, otuNode.getId());
                    if (!StringUtils.isBlank(taxon.getPublicationName())) {
                        otuNode.setLabel(taxon.getPublicationName());
                    } else {
{'fraction_non_alphanumeric': 0.09703167872287354, 'fraction_numerical': 0.001746071339486156, 'mean_word_length': 3.240613432046536, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, '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': '5038193', 'n_tokens_mistral': 2397, 'n_tokens_neox': 2313, 'n_words': 606}