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.HashSet;
13 * A data bean to hold a list of mapped sequence features (e.g. CDS features
14 * mapped from protein), and the mapping between the sequences. It also provides
15 * a method to derive peptide variants from codon variants.
19 public class MappedFeatures
21 private static final String HGV_SP = "HGVSp";
23 private static final String CSQ = "CSQ";
26 * the mapping from one sequence to another
28 public final Mapping mapping;
31 * the sequence mapped from
33 public final SequenceI fromSeq;
36 * features on the sequence mapped to that overlap the mapped positions
38 public final List<SequenceFeature> features;
41 * the residue position in the sequence mapped to
43 private final int toPosition;
46 * the residue at toPosition
48 private final char toResidue;
51 * if the mapping is 3:1 or 1:3 (peptide to CDS), this holds the
52 * mapped positions i.e. codon base positions in CDS; to
53 * support calculation of peptide variants from alleles
55 private final int[] codonPos;
57 private final char[] baseCodon;
64 * the sequence mapped from (e.g. CDS)
66 * the residue position in the sequence mapped to
68 * the residue character at position pos
70 * list of mapped features found in the 'from' sequence at
71 * the mapped position(s)
73 public MappedFeatures(Mapping theMapping, SequenceI from, int pos,
74 char res, List<SequenceFeature> theFeatures)
80 features = theFeatures;
83 * determine codon positions and canonical codon
84 * for a peptide-to-CDS mapping
86 int[] codonIntervals = mapping.getMap().locateInFrom(toPosition, toPosition);
87 int[] codonPositions = codonIntervals == null ? null
88 : MappingUtils.flattenRanges(codonIntervals);
89 if (codonPositions != null && codonPositions.length == 3)
91 codonPos = codonPositions;
92 baseCodon = new char[3];
93 int cdsStart = fromSeq.getStart();
94 baseCodon[0] = fromSeq.getCharAt(codonPos[0] - cdsStart);
95 baseCodon[1] = fromSeq.getCharAt(codonPos[1] - cdsStart);
96 baseCodon[2] = fromSeq.getCharAt(codonPos[2] - cdsStart);
106 * Computes and returns comma-delimited HGVS notation peptide variants derived
107 * from codon allele variants. If no variants are found, answers an empty
111 * a sequence feature (which must be one of those held in this
115 public String findProteinVariants(SequenceFeature sf)
117 if (!features.contains(sf) || baseCodon == null)
123 * VCF data may already contain the protein consequence
125 String hgvsp = sf.getValueAsString(CSQ, HGV_SP);
128 int colonPos = hgvsp.lastIndexOf(':');
131 String var = hgvsp.substring(colonPos + 1);
132 if (var.contains("p.")) // sanity check
140 * otherwise, compute codon and peptide variant
142 int cdsPos = sf.getBegin();
143 if (cdsPos != sf.getEnd())
145 // not handling multi-locus variant features
148 if (cdsPos != codonPos[0] && cdsPos != codonPos[1]
149 && cdsPos != codonPos[2])
151 // e.g. feature on intron within spliced codon!
155 String alls = (String) sf.getValue(Gff3Helper.ALLELES);
161 String from3 = StringUtils.toSentenceCase(
162 ResidueProperties.aa2Triplet.get(String.valueOf(toResidue)));
165 * make a peptide variant for each SNP allele
166 * e.g. C,G,T gives variants G and T for base C
168 Set<String> variantPeptides = new HashSet<>();
169 String[] alleles = alls.toUpperCase().split(",");
170 StringBuilder vars = new StringBuilder();
172 for (String allele : alleles)
174 allele = allele.trim().toUpperCase();
175 if (allele.length() > 1 || "-".equals(allele))
177 continue; // multi-locus variant
179 char[] variantCodon = new char[3];
180 variantCodon[0] = baseCodon[0];
181 variantCodon[1] = baseCodon[1];
182 variantCodon[2] = baseCodon[2];
185 * poke variant base into canonical codon;
186 * ignore first 'allele' (canonical base)
188 final int i = cdsPos == codonPos[0] ? 0
189 : (cdsPos == codonPos[1] ? 1 : 2);
190 variantCodon[i] = allele.toUpperCase().charAt(0);
191 if (variantCodon[i] == baseCodon[i])
195 String codon = new String(variantCodon);
196 String peptide = ResidueProperties.codonTranslate(codon);
197 boolean synonymous = toResidue == peptide.charAt(0);
198 StringBuilder var = new StringBuilder();
202 * synonymous variant notation e.g. c.1062C>A(p.=)
204 var.append("c.").append(String.valueOf(cdsPos))
205 .append(String.valueOf(baseCodon[i])).append(">")
206 .append(String.valueOf(variantCodon[i]))
212 * missense variant notation e.g. p.Arg355Met
214 String to3 = ResidueProperties.STOP.equals(peptide) ? "Ter"
215 : StringUtils.toSentenceCase(
216 ResidueProperties.aa2Triplet.get(peptide));
217 var.append("p.").append(from3).append(String.valueOf(toPosition))
220 if (!variantPeptides.contains(peptide)) // duplicate consequence
222 variantPeptides.add(peptide);
223 if (vars.length() > 0)
231 return vars.toString();