<commit_before>package uk.ac.ebi.quickgo.ontology.traversal;

import uk.ac.ebi.quickgo.ontology.model.OntologyRelationType;
import uk.ac.ebi.quickgo.ontology.model.OntologyRelationship;
import uk.ac.ebi.quickgo.ontology.model.graph.AncestorEdge;
import uk.ac.ebi.quickgo.ontology.model.graph.AncestorGraph;
import uk.ac.ebi.quickgo.ontology.model.graph.AncestorGraphRequest;

import java.util.*;
import org.junit.Before;
import org.junit.Test;

import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static uk.ac.ebi.quickgo.ontology.model.OntologyRelationType.IS_A;

/**
 * @author <NAME>
 * Date: 21/06/2017
 * Time: 08:53
 * Created with IntelliJ IDEA.
 */
public class SubGraphCalculatorTest {

    private final String pyrophosphataseActivity = "GO:0016462";
    private final String cyclaseActivity = "GO:0009975";
    private final String catalyticActivity = "GO:0003824";
    private final String molecularFunction = "GO:0003674";
    private final OntologyRelationship py_IA_cy =
            new OntologyRelationship(pyrophosphataseActivity, cyclaseActivity, IS_A);
    private final OntologyRelationship cy_IA_ca = new OntologyRelationship(cyclaseActivity, catalyticActivity, IS_A);
    private final OntologyRelationship ca_IA_mf = new OntologyRelationship(catalyticActivity, molecularFunction, IS_A);
    private OntologyGraphTraversal ontologyGraphMock;

    private OntologyRelationType[] targetRelations;
    private AncestorGraphRequest request;
    private AncestorGraph<String> ancestorGraph;

    @Before
    public void setup() {
        Queue<String> targetVertices = new LinkedList<>(singletonList(pyrophosphataseActivity));
        Set<String> stopVertices = new HashSet<>(singletonList(molecularFunction));
        targetRelations = new OntologyRelationType[]{OntologyRelationType.IS_A};
        request = new AncestorGraphRequest(targetVertices, stopVertices, targetRelations);
        ancestorGraph = new AncestorGraph<>(new HashSet<>(), new HashSet<>());

        ontologyGraphMock = mock(OntologyGraphTraversal.class);
        when(ontologyGraphMock.parents(pyrophosphataseActivity, targetRelations))
                .thenReturn(new HashSet<>(singletonList(py_IA_cy)));
        when(ontologyGraphMock.parents(cyclaseActivity, targetRelations))
                .thenReturn(new HashSet<>(singletonList(cy_IA_ca)));
        when(ontologyGraphMock.parents(catalyticActivity, targetRelations))
                .thenReturn(new HashSet<>(singletonList(ca_IA_mf)));
    }

    @Test
    public void createSimpleSubGraph() {
        SubGraphCalculator.calculateGraph(request, ancestorGraph, ontologyGraphMock).compute();

        assertThat(ancestorGraph.vertices, hasSize(4));
        assertThat(ancestorGraph.vertices, containsInAnyOrder(pyrophosphataseActivity, cyclaseActivity,
                                                              catalyticActivity, molecularFunction));
        assertThat(ancestorGraph.edges, hasSize(3));
        assertThat(ancestorGraph.edges, containsInAnyOrder(toAE(py_IA_cy), toAE(cy_IA_ca), toAE(ca_IA_mf)));
        ancestorGraph.edges.stream().forEach(e -> assertThat(e.predicate, equalTo("is_a")));
    }

    @Test
    public void noParentsExistForVertex() {
        when(ontologyGraphMock.parents(catalyticActivity, targetRelations)).thenThrow(new IllegalArgumentException());

        SubGraphCalculator.calculateGraph(request, ancestorGraph, ontologyGraphMock).compute();

        assertThat(ancestorGraph.vertices, hasSize(3));
        assertThat(ancestorGraph.vertices, containsInAnyOrder(pyrophosphataseActivity, cyclaseActivity,
                                                              catalyticActivity));
        assertThat(ancestorGraph.edges, hasSize(2));
        assertThat(ancestorGraph.edges, containsInAnyOrder(toAE(py_IA_cy), toAE(cy_IA_ca)));
    }

    @Test(expected = IllegalArgumentException.class)
    public void exceptionThrownIfAncestorGraphRequestArgumentIsNull() {
        SubGraphCalculator.calculateGraph(null, ancestorGraph, ontologyGraphMock);
    }

    @Test(expected = IllegalArgumentException.class)
    public void exceptionThrownIfAncestorGraphArgumentIsNull() {
        SubGraphCalculator.calculateGraph(request, null, ontologyGraphMock);
    }

    @Test(expected = IllegalArgumentException.class)
    public void exceptionThrownIfOntologyGraphTraversalArgumentIsNull() {
        SubGraphCalculator.calculateGraph(request, ancestorGraph, null);
    }

    private AncestorEdge toAE(OntologyRelationship or) {
        return new AncestorEdge(or.child, or.relationship.getLongName(), or.parent);
    }
}<commit_msg>Make main method name clearer.<commit_after>package uk.ac.ebi.quickgo.ontology.traversal;

import uk.ac.ebi.quickgo.ontology.model.OntologyRelationType;
import uk.ac.ebi.quickgo.ontology.model.OntologyRelationship;
import uk.ac.ebi.quickgo.ontology.model.graph.AncestorEdge;
import uk.ac.ebi.quickgo.ontology.model.graph.AncestorGraph;
import uk.ac.ebi.quickgo.ontology.model.graph.AncestorGraphRequest;

import java.util.*;
import org.junit.Before;
import org.junit.Test;

import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static uk.ac.ebi.quickgo.ontology.model.OntologyRelationType.IS_A;

/**
 * @author <NAME>
 * Date: 21/06/2017
 * Time: 08:53
 * Created with IntelliJ IDEA.
 */
public class SubGraphCalculatorTest {

    private final String pyrophosphataseActivity = "GO:0016462";
    private final String cyclaseActivity = "GO:0009975";
    private final String catalyticActivity = "GO:0003824";
    private final String molecularFunction = "GO:0003674";
    private final OntologyRelationship py_IA_cy =
            new OntologyRelationship(pyrophosphataseActivity, cyclaseActivity, IS_A);
    private final OntologyRelationship cy_IA_ca = new OntologyRelationship(cyclaseActivity, catalyticActivity, IS_A);
    private final OntologyRelationship ca_IA_mf = new OntologyRelationship(catalyticActivity, molecularFunction, IS_A);
    private OntologyGraphTraversal ontologyGraphMock;

    private OntologyRelationType[] targetRelations;
    private AncestorGraphRequest request;
    private AncestorGraph<String> ancestorGraph;

    @Before
    public void setup() {
        Queue<String> targetVertices = new LinkedList<>(singletonList(pyrophosphataseActivity));
        Set<String> stopVertices = new HashSet<>(singletonList(molecularFunction));
        targetRelations = new OntologyRelationType[]{OntologyRelationType.IS_A};
        request = new AncestorGraphRequest(targetVertices, stopVertices, targetRelations);
        ancestorGraph = new AncestorGraph<>(new HashSet<>(), new HashSet<>());

        ontologyGraphMock = mock(OntologyGraphTraversal.class);
        when(ontologyGraphMock.parents(pyrophosphataseActivity, targetRelations))
                .thenReturn(new HashSet<>(singletonList(py_IA_cy)));
        when(ontologyGraphMock.parents(cyclaseActivity, targetRelations))
                .thenReturn(new HashSet<>(singletonList(cy_IA_ca)));
        when(ontologyGraphMock.parents(catalyticActivity, targetRelations))
                .thenReturn(new HashSet<>(singletonList(ca_IA_mf)));
    }

    @Test
    public void createSimpleSubGraph() {
        SubGraphCalculator.populateAncestorGraphForRequest(request, ontologyGraphMock, ancestorGraph).compute();

        assertThat(ancestorGraph.vertices, hasSize(4));
        assertThat(ancestorGraph.vertices, containsInAnyOrder(pyrophosphataseActivity, cyclaseActivity,
                                                              catalyticActivity, molecularFunction));
        assertThat(ancestorGraph.edges, hasSize(3));
        assertThat(ancestorGraph.edges, containsInAnyOrder(toAE(py_IA_cy), toAE(cy_IA_ca), toAE(ca_IA_mf)));
        ancestorGraph.edges.stream().forEach(e -> assertThat(e.predicate, equalTo("is_a")));
    }

    @Test
    public void noParentsExistForVertex() {
        when(ontologyGraphMock.parents(catalyticActivity, targetRelations)).thenThrow(new IllegalArgumentException());

        SubGraphCalculator.populateAncestorGraphForRequest(request, ontologyGraphMock, ancestorGraph).compute();

        assertThat(ancestorGraph.vertices, hasSize(3));
        assertThat(ancestorGraph.vertices, containsInAnyOrder(pyrophosphataseActivity, cyclaseActivity,
                                                              catalyticActivity));
        assertThat(ancestorGraph.edges, hasSize(2));
        assertThat(ancestorGraph.edges, containsInAnyOrder(toAE(py_IA_cy), toAE(cy_IA_ca)));
    }

    @Test(expected = IllegalArgumentException.class)
    public void exceptionThrownIfAncestorGraphRequestArgumentIsNull() {
        SubGraphCalculator.populateAncestorGraphForRequest(null, ontologyGraphMock, ancestorGraph);
    }

    @Test(expected = IllegalArgumentException.class)
    public void exceptionThrownIfAncestorGraphArgumentIsNull() {
        SubGraphCalculator.populateAncestorGraphForRequest(request, ontologyGraphMock, null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void exceptionThrownIfOntologyGraphTraversalArgumentIsNull() {
        SubGraphCalculator.populateAncestorGraphForRequest(request, null, ancestorGraph);
    }

    private AncestorEdge toAE(OntologyRelationship or) {
        return new AncestorEdge(or.child, or.relationship.getLongName(), or.parent);
    }
}
{'fraction_non_alphanumeric': 0.09242424242424242, 'fraction_numerical': 0.008888888888888889, 'mean_word_length': 4.42223439211391, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3755937', 'n_tokens_mistral': 2962, 'n_tokens_neox': 2801, 'n_words': 460}