<commit_before>/*
 * Copyright 2015 OpenCB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.opencb.biodata.models.feature;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

/**
 * @author <NAME> &lt;<EMAIL>&gt;
 * @author <NAME> &lt;<EMAIL>&gt;
 */
public class Genotype {
    
    private String reference;
    private List<String> alternates;
    private int[] allelesIdx;
    private boolean phased;
    
    private AllelesCode code;
    
    private int count;
    protected static final Pattern genotypePattern = Pattern.compile("/|\\|");

    Genotype() {
    }
    
    public Genotype(String genotype) {
        this(genotype, null, Collections.emptyList());
    }
    
    public Genotype(String genotype, String ref, String alt) {
        this(genotype, ref, Collections.singletonList(alt));
    }

    public Genotype(String genotype, String ref, List<String> alternates) {
        this.reference = ref;
        this.alternates = alternates;
        this.phased = genotype.contains("|");
        this.count = 0;
        parseGenotype(genotype);
    }

    private void parseGenotype(String genotype) {
        String[] alleles = genotypePattern.split(genotype, -1);
        
        this.code = alleles.length > 1 ? AllelesCode.ALLELES_OK : AllelesCode.HAPLOID;
        this.allelesIdx = new int[alleles.length];
        
        for (int i = 0; i < alleles.length; i++) {
            String allele = alleles[i];
            
            if (allele.equals(".") || allele.equals("-1")) {
                this.code = AllelesCode.ALLELES_MISSING;
                this.allelesIdx[i] = -1;
            } else {
                if (StringUtils.isNumeric(allele)) { // Accepts genotypes with form 0/0, 0/1, and so on
                    this.allelesIdx[i] = Integer.parseInt(allele);
                    
                } else { // Accepts genotypes with form A/A, A/T, and so on
                    if (allele.equalsIgnoreCase(reference)) {
                        this.allelesIdx[i] = 0;
                    } else {
                        if (allele.isEmpty()) {
                            throw new IllegalArgumentException("Unable to parse genotype \'" + genotype + "\'. Empty allele: REF=" + reference + ",ALT=" + alternates.stream().collect(Collectors.joining(",")));
                        }
                        int alleleIdx = 1;
                        for (String alternate : alternates) {
                            if (allele.equalsIgnoreCase(alternate)) {
                                this.allelesIdx[i] = alleleIdx;
                                break;
                            }
                            alleleIdx++;
                        }
                        if (alleleIdx > alternates.size()) {
                            throw new IllegalArgumentException("Unable to parse genotype \'" + genotype + "'. Unknown allele \"" + allele + "\". REF=" + reference + ",ALT=" + alternates.stream().collect(Collectors.joining(",")));
                        }
                    }
                }
                
                if (allelesIdx[i] > 1) {
                    this.code = AllelesCode.MULTIPLE_ALTERNATES;
                }
            }
        }
    }

    public String getReference() {
        return reference;
    }

    void setReference(String reference) {
        this.reference = reference;
    }

    public String getAlternate() {
        return alternates == null || alternates.isEmpty() ? null : alternates.get(0);
    }

    void setAlternate(String alternate) {
        this.alternates = Collections.singletonList(alternate);
    }

    public List<String> getAlternates() {
        return alternates;
    }

    public Genotype setAlternates(List<String> alternates) {
        this.alternates = alternates;
        return this;
    }

    public int getAllele(int i) {
        return allelesIdx[i];
    }
    
    public int[] getAllelesIdx() {
        return allelesIdx;
    }
    
    public int[] getNormalizedAllelesIdx() {
        int[] sortedAlleles = Arrays.copyOf(allelesIdx, allelesIdx.length);
        Arrays.sort(sortedAlleles);
        return sortedAlleles;
    }
    
    void setAllelesIdx(int[] allelesIdx) {
        this.allelesIdx = allelesIdx;
    }

    public boolean isAlleleRef(int i) {
        return allelesIdx[i] == 0;
    }
    
    public boolean isPhased() {
        return phased;
    }
    
    void setPhased(boolean phased) {
        this.phased = phased;
    }

    public AllelesCode getCode() {
        return code;
    }

    void setCode(AllelesCode code) {
        this.code = code;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }<commit_msg>model: add isAllelesRef call to genotype - in case for all reference<commit_after>/*
 * Copyright 2015 OpenCB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.opencb.biodata.models.feature;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

/**
 * @author <NAME> &lt;<EMAIL>&gt;
 * @author <NAME> &lt;<EMAIL>&gt;
 */
public class Genotype {
    
    private String reference;
    private List<String> alternates;
    private int[] allelesIdx;
    private boolean phased;
    
    private AllelesCode code;
    
    private int count;
    protected static final Pattern genotypePattern = Pattern.compile("/|\\|");

    Genotype() {
    }
    
    public Genotype(String genotype) {
        this(genotype, null, Collections.emptyList());
    }
    
    public Genotype(String genotype, String ref, String alt) {
        this(genotype, ref, Collections.singletonList(alt));
    }

    public Genotype(String genotype, String ref, List<String> alternates) {
        this.reference = ref;
        this.alternates = alternates;
        this.phased = genotype.contains("|");
        this.count = 0;
        parseGenotype(genotype);
    }

    private void parseGenotype(String genotype) {
        String[] alleles = genotypePattern.split(genotype, -1);
        
        this.code = alleles.length > 1 ? AllelesCode.ALLELES_OK : AllelesCode.HAPLOID;
        this.allelesIdx = new int[alleles.length];
        
        for (int i = 0; i < alleles.length; i++) {
            String allele = alleles[i];
            
            if (allele.equals(".") || allele.equals("-1")) {
                this.code = AllelesCode.ALLELES_MISSING;
                this.allelesIdx[i] = -1;
            } else {
                if (StringUtils.isNumeric(allele)) { // Accepts genotypes with form 0/0, 0/1, and so on
                    this.allelesIdx[i] = Integer.parseInt(allele);
                    
                } else { // Accepts genotypes with form A/A, A/T, and so on
                    if (allele.equalsIgnoreCase(reference)) {
                        this.allelesIdx[i] = 0;
                    } else {
                        if (allele.isEmpty()) {
                            throw new IllegalArgumentException("Unable to parse genotype \'" + genotype + "\'. Empty allele: REF=" + reference + ",ALT=" + alternates.stream().collect(Collectors.joining(",")));
                        }
                        int alleleIdx = 1;
                        for (String alternate : alternates) {
                            if (allele.equalsIgnoreCase(alternate)) {
                                this.allelesIdx[i] = alleleIdx;
                                break;
                            }
                            alleleIdx++;
                        }
                        if (alleleIdx > alternates.size()) {
                            throw new IllegalArgumentException("Unable to parse genotype \'" + genotype + "'. Unknown allele \"" + allele + "\". REF=" + reference + ",ALT=" + alternates.stream().collect(Collectors.joining(",")));
                        }
                    }
                }
                
                if (allelesIdx[i] > 1) {
                    this.code = AllelesCode.MULTIPLE_ALTERNATES;
                }
            }
        }
    }

    public String getReference() {
        return reference;
    }

    void setReference(String reference) {
        this.reference = reference;
    }

    public String getAlternate() {
        return alternates == null || alternates.isEmpty() ? null : alternates.get(0);
    }

    void setAlternate(String alternate) {
        this.alternates = Collections.singletonList(alternate);
    }

    public List<String> getAlternates() {
        return alternates;
    }

    public Genotype setAlternates(List<String> alternates) {
        this.alternates = alternates;
        return this;
    }

    public int getAllele(int i) {
        return allelesIdx[i];
    }
    
    public int[] getAllelesIdx() {
        return allelesIdx;
    }
    
    public int[] getNormalizedAllelesIdx() {
        int[] sortedAlleles = Arrays.copyOf(allelesIdx, allelesIdx.length);
        Arrays.sort(sortedAlleles);
        return sortedAlleles;
    }
    
    void setAllelesIdx(int[] allelesIdx) {
        this.allelesIdx = allelesIdx;
    }

    public boolean isAlleleRef(int i) {
        return allelesIdx[i] == 0;
    }

    public boolean isAllelesRefs(){
        int len = allelesIdx.length;
        for(int i = 0; i < len; ++i){
            if(! isAlleleRef(i))
                return false;
        }
        return true;
    }

    public boolean isPhased() {
        return phased;
    }

    void setPhased(boolean phased) {
        this.phased = phased;
    }

    public AllelesCode getCode() {
        return code;
    }

    void setCode(AllelesCode code) {
        this.code = code;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
{'fraction_non_alphanumeric': 0.10408254822790489, 'fraction_numerical': 0.0043965903992821895, 'mean_word_length': 1.8477261113949923, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7394229', 'n_tokens_mistral': 3219, 'n_tokens_neox': 3028, 'n_words': 782}