<commit_before>package uk.ac.ebi.spot.goci.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;
import uk.ac.ebi.spot.goci.model.EfoColourMap;
import uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism;
import uk.ac.ebi.spot.goci.pussycat.layout.ColourMapper;
import uk.ac.ebi.spot.goci.repository.SingleNucleotidePolymorphismRepository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by dwelter on 03/02/17.
 */

@Controller
public class EnsemblController {

    @Value("${ols.server}")
    private String olsServer;

    @Value("${ols.efo.terms}")
    private String olsEfoTerms;

    @Value("${ols.shortForm}")
    private String olsShortForm;

    private Logger log = LoggerFactory.getLogger(getClass());

    protected Logger getLog() {
        return log;
    }


    private SingleNucleotidePolymorphismRepository singleNucleotidePolymorphismRepository;

    @Autowired
    public EnsemblController(SingleNucleotidePolymorphismRepository singleNucleotidePolymorphismRepository){
        this.singleNucleotidePolymorphismRepository = singleNucleotidePolymorphismRepository;
    }

    @RequestMapping(value = "/api/snpLocation/{range}",
                    method = RequestMethod.GET,
                    produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<PagedResources<SingleNucleotidePolymorphism>> search(@PathVariable String range,
                                                                           @PageableDefault(size = 20, page = 0) Pageable pageable) {

        String chrom = range.split(":")[0];
        String locs = range.split(":")[1];

        int start = Integer.parseInt(locs.split("-")[0]);
        int end = Integer.parseInt(locs.split("-")[1]);

        Page<SingleNucleotidePolymorphism>
                snps =
                singleNucleotidePolymorphismRepository.findByLocationsChromosomeNameAndLocationsChromosomePositionBetween(
                        chrom,
                        start,
                        end,
                        pageable);

        Resource<SingleNucleotidePolymorphism> snpResource = new Resource(snps);

        return new ResponseEntity(snpResource, HttpStatus.OK);
    }

    @RequestMapping(value = "/api/parentMapping/{efoTerm}",
                    method = RequestMethod.GET,
                    produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<EfoColourMap> getColourMapping(@PathVariable String efoTerm) throws IOException {

        Map<String, String> ancestors = getAncestors(efoTerm);

        EfoColourMap colour = getTraitColour(ancestors.get("ancestors"), ancestors.get("iri"), ancestors.get("label"));

        return new ResponseEntity<EfoColourMap>(colour, HttpStatus.OK);
    }

    @RequestMapping(value = "/api/parentMappings",
                    method = RequestMethod.POST,
                    consumes = "application/json",
                    produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<List<EfoColourMap>> getColourMappings(@RequestBody List<String> efoTerms) throws IOException {
        List<EfoColourMap> colours = new ArrayList<>();

        for(String efoTerm : efoTerms) {
            Map<String, String> ancestors = getAncestors(efoTerm);
            colours.add(getTraitColour(ancestors.get("ancestors"), ancestors.get("iri"), ancestors.get("label")));
        }

        return new ResponseEntity<List<EfoColourMap>>(colours, HttpStatus.OK);
    }

    public Map<String, String> getAncestors(String efoTerm)  throws IOException{
        Map<String, String> result = new HashMap<>();
        String uri = olsServer.concat(olsEfoTerms);


        if(efoTerm.contains("http")){
            uri = uri.concat("?").concat(efoTerm);
        }
        else {
           uri = uri.concat(olsShortForm).concat(efoTerm);
        }<commit_msg>Now with cross origin header enabled<commit_after>package uk.ac.ebi.spot.goci.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;
import uk.ac.ebi.spot.goci.model.EfoColourMap;
import uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism;
import uk.ac.ebi.spot.goci.pussycat.layout.ColourMapper;
import uk.ac.ebi.spot.goci.repository.SingleNucleotidePolymorphismRepository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by dwelter on 03/02/17.
 */

@Controller
public class EnsemblController {

    @Value("${ols.server}")
    private String olsServer;

    @Value("${ols.efo.terms}")
    private String olsEfoTerms;

    @Value("${ols.shortForm}")
    private String olsShortForm;

    private Logger log = LoggerFactory.getLogger(getClass());

    protected Logger getLog() {
        return log;
    }


    private SingleNucleotidePolymorphismRepository singleNucleotidePolymorphismRepository;

    @Autowired
    public EnsemblController(SingleNucleotidePolymorphismRepository singleNucleotidePolymorphismRepository){
        this.singleNucleotidePolymorphismRepository = singleNucleotidePolymorphismRepository;
    }

    @CrossOrigin
    @RequestMapping(value = "/api/snpLocation/{range}",
                    method = RequestMethod.GET,
                    produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<PagedResources<SingleNucleotidePolymorphism>> search(@PathVariable String range,
                                                                           @PageableDefault(size = 20, page = 0) Pageable pageable) {

        String chrom = range.split(":")[0];
        String locs = range.split(":")[1];

        int start = Integer.parseInt(locs.split("-")[0]);
        int end = Integer.parseInt(locs.split("-")[1]);

        Page<SingleNucleotidePolymorphism>
                snps =
                singleNucleotidePolymorphismRepository.findByLocationsChromosomeNameAndLocationsChromosomePositionBetween(
                        chrom,
                        start,
                        end,
                        pageable);

        Resource<SingleNucleotidePolymorphism> snpResource = new Resource(snps);

        return new ResponseEntity(snpResource, HttpStatus.OK);
    }

    @CrossOrigin
    @RequestMapping(value = "/api/parentMapping/{efoTerm}",
                    method = RequestMethod.GET,
                    produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<EfoColourMap> getColourMapping(@PathVariable String efoTerm) throws IOException {

        Map<String, String> ancestors = getAncestors(efoTerm);

        EfoColourMap colour = getTraitColour(ancestors.get("ancestors"), ancestors.get("iri"), ancestors.get("label"));

        return new ResponseEntity<EfoColourMap>(colour, HttpStatus.OK);
    }

    @CrossOrigin
    @RequestMapping(value = "/api/parentMappings",
                    method = RequestMethod.POST,
                    consumes = "application/json",
                    produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<List<EfoColourMap>> getColourMappings(@RequestBody List<String> efoTerms) throws IOException {
        List<EfoColourMap> colours = new ArrayList<>();

        for(String efoTerm : efoTerms) {
            Map<String, String> ancestors = getAncestors(efoTerm);
            colours.add(getTraitColour(ancestors.get("ancestors"), ancestors.get("iri"), ancestors.get("label")));
        }

        return new ResponseEntity<List<EfoColourMap>>(colours, HttpStatus.OK);
    }

    public Map<String, String> getAncestors(String efoTerm)  throws IOException{
        Map<String, String> result = new HashMap<>();
        String uri = olsServer.concat(olsEfoTerms);


        if(efoTerm.contains("http")){
            uri = uri.concat("?").concat(efoTerm);
        }
        else {
           uri = uri.concat(olsShortForm).concat(efoTerm);
        }
{'fraction_non_alphanumeric': 0.10292058411682337, 'fraction_numerical': 0.0030006001200240046, 'mean_word_length': 3.354965156794425, 'pattern_counts': {'":': 4, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4235181', 'n_tokens_mistral': 2920, 'n_tokens_neox': 2734, 'n_words': 464}