1 package jalview.datamodel;
3 import jalview.io.gff.Gff3Helper;
4 import jalview.schemes.ResidueProperties;
5 import jalview.util.MappingUtils;
6 import jalview.util.StringUtils;
8 import java.util.ArrayList;
12 * A data bean to hold a list of mapped sequence features (e.g. CDS features
13 * mapped from protein), and the mapping between the sequences
17 public class MappedFeatures
20 * the mapping from CDS to peptide
22 public final Mapping mapping;
25 * the CDS sequence mapped to
27 public final SequenceI fromSeq;
30 * the residue position in the peptide sequence
32 public final int fromPosition;
35 * the peptide residue at the position
37 public final char fromResidue;
40 * features on CDS that overlap the codon positions
42 public final List<SequenceFeature> features;
52 public MappedFeatures(Mapping theMapping, SequenceI from, int pos,
54 List<SequenceFeature> theFeatures)
60 features = theFeatures;
64 * Computes and returns a (possibly empty) list of HGVS notation peptide
65 * variants derived from codon allele variants
69 public List<String> findProteinVariants()
71 List<String> vars = new ArrayList<>();
74 * determine canonical codon
76 int[] codonPos = MappingUtils.flattenRanges(
77 mapping.getMap().locateInFrom(fromPosition, fromPosition));
78 if (codonPos.length != 3)
83 final char[] baseCodon = new char[3];
84 int cdsStart = fromSeq.getStart();
85 baseCodon[0] = fromSeq.getCharAt(codonPos[0] - cdsStart);
86 baseCodon[1] = fromSeq.getCharAt(codonPos[1] - cdsStart);
87 baseCodon[2] = fromSeq.getCharAt(codonPos[2] - cdsStart);
89 for (SequenceFeature sf : features)
92 * VCF data may already contain the protein consequence
94 String hgvsp = sf.getValueAsString("CSQ", "HGVSp");
97 int colonPos = hgvsp.indexOf(':');
100 String var = hgvsp.substring(colonPos + 1);
101 if (!vars.contains(var))
110 * otherwise, compute codon and peptide variant
112 // todo avoid duplication of code in AlignmentUtils.buildDnaVariantsMap
113 int cdsPos = sf.getBegin();
114 if (cdsPos != sf.getEnd())
116 // not handling multi-locus variant features
119 if (cdsPos != codonPos[0] && cdsPos != codonPos[1]
120 && cdsPos != codonPos[2])
122 // e.g. feature on intron within spliced codon!
126 String alls = (String) sf.getValue(Gff3Helper.ALLELES);
131 String from3 = StringUtils.toSentenceCase(
132 ResidueProperties.aa2Triplet
133 .get(String.valueOf(fromResidue)));
136 * make a peptide variant for each SNP allele
137 * e.g. C,G,T gives variants G and T for base C
139 String[] alleles = alls.toUpperCase().split(",");
140 for (String allele : alleles)
142 allele = allele.trim().toUpperCase();
143 if (allele.length() > 1)
145 continue; // multi-locus variant
147 char[] variantCodon = new char[3];
148 variantCodon[0] = baseCodon[0];
149 variantCodon[1] = baseCodon[1];
150 variantCodon[2] = baseCodon[2];
153 * poke variant base into canonical codon
155 int i = cdsPos == codonPos[0] ? 0 : (cdsPos == codonPos[1] ? 1 : 2);
156 variantCodon[i] = allele.toUpperCase().charAt(0);
157 String codon = new String(variantCodon);
158 String peptide = ResidueProperties.codonTranslate(codon);
159 if (fromResidue != peptide.charAt(0))
161 String to3 = ResidueProperties.STOP.equals(peptide) ? "STOP"
162 : StringUtils.toSentenceCase(
163 ResidueProperties.aa2Triplet.get(peptide));
164 String var = "p." + from3 + fromPosition + to3;
165 if (!vars.contains(var))