|
| 1 | +/* |
| 2 | + * Copyright 2015-2020 OpenCB |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.opencb.biodata.formats.variant; |
| 18 | + |
| 19 | +import org.opencb.biodata.models.variant.avro.AlleleOrigin; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +/** |
| 25 | + * Created by fjlopez on 22/06/15. |
| 26 | + */ |
| 27 | +public class VariantAnnotationUtils { |
| 28 | + |
| 29 | + private static final Map<String, AlleleOrigin> ORIGIN_STRING_TO_ALLELE_ORIGIN = new HashMap<>(); |
| 30 | + private static final Map<Character, Character> COMPLEMENTARY_NT = new HashMap<>(); |
| 31 | + |
| 32 | + static { |
| 33 | + |
| 34 | + /////////////////////////////////////////////////////////////////////// |
| 35 | + ///// ClinVar and Cosmic allele origins to SO terms /////////////// |
| 36 | + /////////////////////////////////////////////////////////////////////// |
| 37 | + ORIGIN_STRING_TO_ALLELE_ORIGIN.put("germline", AlleleOrigin.germline_variant); |
| 38 | + ORIGIN_STRING_TO_ALLELE_ORIGIN.put("maternal", AlleleOrigin.maternal_variant); |
| 39 | + ORIGIN_STRING_TO_ALLELE_ORIGIN.put("de novo", AlleleOrigin.de_novo_variant); |
| 40 | + ORIGIN_STRING_TO_ALLELE_ORIGIN.put("paternal", AlleleOrigin.paternal_variant); |
| 41 | + ORIGIN_STRING_TO_ALLELE_ORIGIN.put("somatic", AlleleOrigin.somatic_variant); |
| 42 | + |
| 43 | + COMPLEMENTARY_NT.put('A', 'T'); |
| 44 | + COMPLEMENTARY_NT.put('a', 't'); |
| 45 | + COMPLEMENTARY_NT.put('C', 'G'); |
| 46 | + COMPLEMENTARY_NT.put('c', 'g'); |
| 47 | + COMPLEMENTARY_NT.put('G', 'C'); |
| 48 | + COMPLEMENTARY_NT.put('g', 'c'); |
| 49 | + COMPLEMENTARY_NT.put('T', 'A'); |
| 50 | + COMPLEMENTARY_NT.put('t', 'a'); |
| 51 | + COMPLEMENTARY_NT.put('N', 'N'); |
| 52 | + COMPLEMENTARY_NT.put('n', 'n'); |
| 53 | + } |
| 54 | + |
| 55 | + public static String reverseComplement(String string) { |
| 56 | + return reverseComplement(string, false); |
| 57 | + } |
| 58 | + |
| 59 | + public static String reverseComplement(String string, boolean failOnUnknownNt) { |
| 60 | + StringBuilder stringBuilder = new StringBuilder(string).reverse(); |
| 61 | + for (int i = 0; i < stringBuilder.length(); i++) { |
| 62 | + char nextNt = stringBuilder.charAt(i); |
| 63 | + // Protection against weird characters, e.g. alternate:"TBS" found in ClinVar |
| 64 | + if (VariantAnnotationUtils.COMPLEMENTARY_NT.containsKey(nextNt)) { |
| 65 | + stringBuilder.setCharAt(i, VariantAnnotationUtils.COMPLEMENTARY_NT.get(nextNt)); |
| 66 | + } else { |
| 67 | + if (failOnUnknownNt) { |
| 68 | + throw new IllegalArgumentException("Unknown nucleotide: '" + nextNt+ "'. " |
| 69 | + + "Unable to reverse-complement sequence '" + string + "'."); |
| 70 | + } else { |
| 71 | + return null; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + return stringBuilder.toString(); |
| 76 | + } |
| 77 | + |
| 78 | + public static AlleleOrigin parseAlleleOrigin(String alleleOrigin) { |
| 79 | + return ORIGIN_STRING_TO_ALLELE_ORIGIN.get(alleleOrigin); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments