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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import uk.ac.ebi.spot.goci.model.Association;
import uk.ac.ebi.spot.goci.model.AssociationUploadRow;
import uk.ac.ebi.spot.goci.model.EfoTrait;
import uk.ac.ebi.spot.goci.model.Gene;
import uk.ac.ebi.spot.goci.model.Locus;
import uk.ac.ebi.spot.goci.model.RiskAllele;
import uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism;
import uk.ac.ebi.spot.goci.utils.AssociationCalculationService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * Created by emma on 21/03/2016.
 *
 * @author emma
 *         <p>
 *         Service to create an association from each row of an uploaded spreadsheet, which can then be passed to the
 *         validator.
 */
@Service
public class AssociationRowProcessor {

    private AssociationAttributeService associationAttributeService;

    private AssociationCalculationService associationCalculationService;

    @Autowired
    public AssociationRowProcessor(AssociationAttributeService associationAttributeService,
                                   AssociationCalculationService associationCalculationService) {
        this.associationAttributeService = associationAttributeService;
        this.associationCalculationService = associationCalculationService;
    }

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

    protected Logger getLog() {
        return log;
    }

    public Association createAssociationFromUploadRow(AssociationUploadRow row) {

        Association newAssociation = new Association();

        // Set EFO traits
        if (row.getEfoTrait() != null) {
            String[] uris = row.getEfoTrait().split(",");
            Collection<String> efoUris = new ArrayList<>();

            for (String uri : uris) {
                String trimmedUri = uri.trim();
                efoUris.add(trimmedUri);
            }

            Collection<EfoTrait> efoTraits = associationAttributeService.getEfoTraitsFromRepository(efoUris);
            newAssociation.setEfoTraits(efoTraits);
        }

        /// Set OR
        newAssociation.setOrPerCopyRecip(row.getOrPerCopyRecip());
        newAssociation.setOrPerCopyRecipRange(row.getOrPerCopyRecipRange());

        // Set beta
        newAssociation.setBetaNum(row.getBetaNum());
        newAssociation.setBetaUnit(row.getBetaUnit());
        newAssociation.setBetaDirection(row.getBetaDirection());

        // Calculate OR num if OR recip is present , otherwise set to whatever is in upload
        boolean recipReverse = false;
        if ((row.getOrPerCopyRecip() != null) && (row.getOrPerCopyNum() == null)) {
            getLog().info("Calculating OR from OR recip value");
            newAssociation.setOrPerCopyNum(((100 / row.getOrPerCopyRecip()) / 100));
            recipReverse = true;
        }
        else {
            newAssociation.setOrPerCopyNum(row.getOrPerCopyNum());
        }

        // Calculate range
        // (This logic is retained from Dani's original code)
        if ((row.getOrPerCopyRecipRange() != null) && recipReverse) {
            newAssociation.setRange(associationCalculationService.reverseCI(row.getOrPerCopyRecipRange()));
        }
        else if ((row.getRange() == null) && (row.getStandardError() != null)) {

            if (row.getOrPerCopyNum() != null) {
                newAssociation.setRange(associationCalculationService.setRange(row.getStandardError(),
                                                                               row.getOrPerCopyNum()));
            }
            else {
                if (row.getBetaNum() != null) {
                    newAssociation.setRange(associationCalculationService.setRange(row.getStandardError(),
                                                                                   row.getBetaNum()));
                }
            }
        }
        else {
            newAssociation.setRange(row.getRange());
        }

        // Set values common to all association types
        newAssociation.setRiskFrequency(row.getAssociationRiskFrequency());
        newAssociation.setPvalueMantissa(row.getPvalueMantissa());
        newAssociation.setPvalueExponent(row.getPvalueExponent());
        newAssociation.setPvalueDescription(row.getPvalueDescription());
        newAssociation.setSnpType(row.getSnpType());
        newAssociation.setStandardError(row.getStandardError());
        newAssociation.setDescription(row.getDescription());

        if (row.getMultiSnpHaplotype() != null) {
            if (row.getMultiSnpHaplotype().equalsIgnoreCase("Y")) {
                newAssociation.setMultiSnpHaplotype(true);
            }
        }
        else {
            newAssociation.setMultiSnpHaplotype(false);
        }

        if (row.getSnpInteraction() != null) {
            if (row.getSnpInteraction().equalsIgnoreCase("Y")) {
                newAssociation.setSnpInteraction(true);
            }
        }
        else {
            newAssociation.setSnpInteraction(false);
        }

        String delimiter;
        Collection<Locus> loci = new ArrayList<>();

        if (newAssociation.getSnpInteraction()) {
            delimiter = "x";

            // For SNP interaction studies we need to create a locus per risk allele
            // Handle curator entered risk allele
            Collection<RiskAllele> locusRiskAlleles =
                    createLocusRiskAlleles(row.getStrongestAllele(),
                                           row.getSnp(),
                                           row.getProxy(),
                                           row.getRiskFrequency(),
                                           row.getSnpStatus(),
                                           delimiter);

            // Add genes to relevant loci, split by 'x' delimiter first
            Collection<Locus> lociWithAddedGenes = new ArrayList<>();

            // Deal with genes for each interaction which should be
            // separated by 'x'
            String[] separatedGenes = row.getAuthorReportedGene().split(delimiter);
            int geneIndex = 0;

            for (RiskAllele riskAllele : locusRiskAlleles) {
                Locus locus = new Locus();

                // Set risk alleles, assume one locus per risk allele
                Collection<RiskAllele> currentLocusRiskAlleles = new ArrayList<>();
                currentLocusRiskAlleles.add(riskAllele);
                locus.setStrongestRiskAlleles(currentLocusRiskAlleles);

                // Set gene
                String interactionGene = separatedGenes[geneIndex];
                Collection<Gene> locusGenes = associationAttributeService.createLocusGenes(interactionGene, ",");<commit_msg>Remove collection that is not used<commit_after>package uk.ac.ebi.spot.goci.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import uk.ac.ebi.spot.goci.model.Association;
import uk.ac.ebi.spot.goci.model.AssociationUploadRow;
import uk.ac.ebi.spot.goci.model.EfoTrait;
import uk.ac.ebi.spot.goci.model.Gene;
import uk.ac.ebi.spot.goci.model.Locus;
import uk.ac.ebi.spot.goci.model.RiskAllele;
import uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism;
import uk.ac.ebi.spot.goci.utils.AssociationCalculationService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * Created by emma on 21/03/2016.
 *
 * @author emma
 *         <p>
 *         Service to create an association from each row of an uploaded spreadsheet, which can then be passed to the
 *         validator.
 */
@Service
public class AssociationRowProcessor {

    private AssociationAttributeService associationAttributeService;

    private AssociationCalculationService associationCalculationService;

    @Autowired
    public AssociationRowProcessor(AssociationAttributeService associationAttributeService,
                                   AssociationCalculationService associationCalculationService) {
        this.associationAttributeService = associationAttributeService;
        this.associationCalculationService = associationCalculationService;
    }

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

    protected Logger getLog() {
        return log;
    }

    public Association createAssociationFromUploadRow(AssociationUploadRow row) {

        Association newAssociation = new Association();

        // Set EFO traits
        if (row.getEfoTrait() != null) {
            String[] uris = row.getEfoTrait().split(",");
            Collection<String> efoUris = new ArrayList<>();

            for (String uri : uris) {
                String trimmedUri = uri.trim();
                efoUris.add(trimmedUri);
            }

            Collection<EfoTrait> efoTraits = associationAttributeService.getEfoTraitsFromRepository(efoUris);
            newAssociation.setEfoTraits(efoTraits);
        }

        /// Set OR
        newAssociation.setOrPerCopyRecip(row.getOrPerCopyRecip());
        newAssociation.setOrPerCopyRecipRange(row.getOrPerCopyRecipRange());

        // Set beta
        newAssociation.setBetaNum(row.getBetaNum());
        newAssociation.setBetaUnit(row.getBetaUnit());
        newAssociation.setBetaDirection(row.getBetaDirection());

        // Calculate OR num if OR recip is present , otherwise set to whatever is in upload
        boolean recipReverse = false;
        if ((row.getOrPerCopyRecip() != null) && (row.getOrPerCopyNum() == null)) {
            getLog().info("Calculating OR from OR recip value");
            newAssociation.setOrPerCopyNum(((100 / row.getOrPerCopyRecip()) / 100));
            recipReverse = true;
        }
        else {
            newAssociation.setOrPerCopyNum(row.getOrPerCopyNum());
        }

        // Calculate range
        // (This logic is retained from Dani's original code)
        if ((row.getOrPerCopyRecipRange() != null) && recipReverse) {
            newAssociation.setRange(associationCalculationService.reverseCI(row.getOrPerCopyRecipRange()));
        }
        else if ((row.getRange() == null) && (row.getStandardError() != null)) {

            if (row.getOrPerCopyNum() != null) {
                newAssociation.setRange(associationCalculationService.setRange(row.getStandardError(),
                                                                               row.getOrPerCopyNum()));
            }
            else {
                if (row.getBetaNum() != null) {
                    newAssociation.setRange(associationCalculationService.setRange(row.getStandardError(),
                                                                                   row.getBetaNum()));
                }
            }
        }
        else {
            newAssociation.setRange(row.getRange());
        }

        // Set values common to all association types
        newAssociation.setRiskFrequency(row.getAssociationRiskFrequency());
        newAssociation.setPvalueMantissa(row.getPvalueMantissa());
        newAssociation.setPvalueExponent(row.getPvalueExponent());
        newAssociation.setPvalueDescription(row.getPvalueDescription());
        newAssociation.setSnpType(row.getSnpType());
        newAssociation.setStandardError(row.getStandardError());
        newAssociation.setDescription(row.getDescription());

        if (row.getMultiSnpHaplotype() != null) {
            if (row.getMultiSnpHaplotype().equalsIgnoreCase("Y")) {
                newAssociation.setMultiSnpHaplotype(true);
            }
        }
        else {
            newAssociation.setMultiSnpHaplotype(false);
        }

        if (row.getSnpInteraction() != null) {
            if (row.getSnpInteraction().equalsIgnoreCase("Y")) {
                newAssociation.setSnpInteraction(true);
            }
        }
        else {
            newAssociation.setSnpInteraction(false);
        }

        String delimiter;
        Collection<Locus> loci = new ArrayList<>();

        if (newAssociation.getSnpInteraction()) {
            delimiter = "x";

            // For SNP interaction studies we need to create a locus per risk allele
            // Handle curator entered risk allele
            Collection<RiskAllele> locusRiskAlleles =
                    createLocusRiskAlleles(row.getStrongestAllele(),
                                           row.getSnp(),
                                           row.getProxy(),
                                           row.getRiskFrequency(),
                                           row.getSnpStatus(),
                                           delimiter);

            // Deal with genes for each interaction which should be
            // separated by 'x'
            String[] separatedGenes = row.getAuthorReportedGene().split(delimiter);
            int geneIndex = 0;

            for (RiskAllele riskAllele : locusRiskAlleles) {
                Locus locus = new Locus();

                // Set risk alleles, assume one locus per risk allele
                Collection<RiskAllele> currentLocusRiskAlleles = new ArrayList<>();
                currentLocusRiskAlleles.add(riskAllele);
                locus.setStrongestRiskAlleles(currentLocusRiskAlleles);

                // Set gene
                String interactionGene = separatedGenes[geneIndex];
                Collection<Gene> locusGenes = associationAttributeService.createLocusGenes(interactionGene, ",");
{'fraction_non_alphanumeric': 0.09344908082871316, 'fraction_numerical': 0.0024803034724248615, 'mean_word_length': 2.156573796914575, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1897389', 'n_tokens_mistral': 3750, 'n_tokens_neox': 3436, 'n_words': 657}