1 package jalview.datamodel;
3 import java.util.HashSet;
7 import jalview.io.gff.Gff3Helper;
8 import jalview.schemes.ResidueProperties;
9 import jalview.util.MapList;
10 import jalview.util.MappingUtils;
11 import jalview.util.StringUtils;
14 * A data bean to hold a list of mapped sequence features (e.g. CDS features
15 * mapped from protein), and the mapping between the sequences. It also provides
16 * a method to derive peptide variants from codon variants.
20 public class MappedFeatures
23 * VEP CSQ:HGVSp (if present) is a short-cut to the protein variant consequence
25 private static final String HGV_SP = "HGVSp";
27 private static final String CSQ = "CSQ";
30 * the sequence the mapped features are on
32 private final SequenceI featureSequence;
35 * the mapping between sequences;
36 * NB this could be in either sense (from or to featureSequence)
38 private final Mapping mapping;
41 * features on featureSequence that overlap the mapped positions
43 public final List<SequenceFeature> features;
46 * the residue position in the sequence mapped to
48 private final int toPosition;
51 * the residue at toPosition
53 private final char toResidue;
56 * if the mapping is 3:1 or 1:3 (peptide to CDS), this holds the
57 * mapped positions i.e. codon base positions in CDS; to
58 * support calculation of peptide variants from alleles
60 private final int[] codonPos;
62 private final char[] baseCodon;
68 * sequence mapping (which may be either to, or from, the sequence
69 * holding the linked features)
71 * the sequence hosting the virtual features
73 * the residue position in the sequence mapped to
75 * the residue character at position pos
77 * list of mapped features found in the 'featureSeq' sequence at the
80 public MappedFeatures(Mapping theMapping, SequenceI featureSeq, int pos,
81 char res, List<SequenceFeature> theFeatures)
84 featureSequence = featureSeq;
87 features = theFeatures;
90 * determine codon positions and canonical codon
91 * for a peptide-to-CDS mapping
93 int[] codonIntervals = mapping.getMap().locateInFrom(toPosition, toPosition);
94 int[] codonPositions = codonIntervals == null ? null
95 : MappingUtils.flattenRanges(codonIntervals);
96 if (codonPositions != null && codonPositions.length == 3)
98 codonPos = codonPositions;
99 baseCodon = new char[3];
100 int cdsStart = featureSequence.getStart();
101 baseCodon[0] = Character
102 .toUpperCase(featureSequence.getCharAt(codonPos[0] - cdsStart));
103 baseCodon[1] = Character
104 .toUpperCase(featureSequence.getCharAt(codonPos[1] - cdsStart));
105 baseCodon[2] = Character
106 .toUpperCase(featureSequence.getCharAt(codonPos[2] - cdsStart));
116 * Computes and returns comma-delimited HGVS notation peptide variants derived
117 * from codon allele variants. If no variants are found, answers an empty
118 * string. The peptide variant is either simply read from the "CSQ:HGVSp"
119 * attribute if present, else computed based on the "alleles" attribute if
120 * present. If neither attribute is found, no variant (empty string) is
124 * a sequence feature (which must be one of those held in this
128 public String findProteinVariants(SequenceFeature sf)
130 if (!features.contains(sf) || baseCodon == null)
136 * VCF data may already contain the protein consequence
138 String hgvsp = sf.getValueAsString(CSQ, HGV_SP);
141 int colonPos = hgvsp.lastIndexOf(':');
144 String var = hgvsp.substring(colonPos + 1);
145 if (var.contains("p.")) // sanity check
153 * otherwise, compute codon and peptide variant
155 int cdsPos = sf.getBegin();
156 if (cdsPos != sf.getEnd())
158 // not handling multi-locus variant features
161 if (cdsPos != codonPos[0] && cdsPos != codonPos[1]
162 && cdsPos != codonPos[2])
164 // e.g. feature on intron within spliced codon!
168 String alls = (String) sf.getValue(Gff3Helper.ALLELES);
174 String from3 = StringUtils.toSentenceCase(
175 ResidueProperties.aa2Triplet.get(String.valueOf(toResidue)));
178 * make a peptide variant for each SNP allele
179 * e.g. C,G,T gives variants G and T for base C
181 Set<String> variantPeptides = new HashSet<>();
182 String[] alleles = alls.toUpperCase().split(",");
183 StringBuilder vars = new StringBuilder();
185 for (String allele : alleles)
187 allele = allele.trim().toUpperCase();
188 if (allele.length() > 1 || "-".equals(allele))
190 continue; // multi-locus variant
192 char[] variantCodon = new char[3];
193 variantCodon[0] = baseCodon[0];
194 variantCodon[1] = baseCodon[1];
195 variantCodon[2] = baseCodon[2];
198 * poke variant base into canonical codon;
199 * ignore first 'allele' (canonical base)
201 final int i = cdsPos == codonPos[0] ? 0
202 : (cdsPos == codonPos[1] ? 1 : 2);
203 variantCodon[i] = allele.toUpperCase().charAt(0);
204 if (variantCodon[i] == baseCodon[i])
208 String codon = new String(variantCodon);
209 String peptide = ResidueProperties.codonTranslate(codon);
210 boolean synonymous = toResidue == peptide.charAt(0);
211 StringBuilder var = new StringBuilder();
215 * synonymous variant notation e.g. c.1062C>A(p.=)
217 var.append("c.").append(String.valueOf(cdsPos))
218 .append(String.valueOf(baseCodon[i])).append(">")
219 .append(String.valueOf(variantCodon[i]))
225 * missense variant notation e.g. p.Arg355Met
227 String to3 = ResidueProperties.STOP.equals(peptide) ? "Ter"
228 : StringUtils.toSentenceCase(
229 ResidueProperties.aa2Triplet.get(peptide));
230 var.append("p.").append(from3).append(String.valueOf(toPosition))
233 if (!variantPeptides.contains(peptide)) // duplicate consequence
235 variantPeptides.add(peptide);
236 if (vars.length() > 0)
244 return vars.toString();
248 * Answers the name of the linked sequence holding any mapped features
252 public String getLinkedSequenceName()
254 return featureSequence == null ? null : featureSequence.getName();
258 * Answers the mapped ranges (as one or more [start, end] positions) which
259 * correspond to the given [begin, end] range of the linked sequence.
262 * Example: MappedFeatures with CDS features mapped to peptide
263 * CDS/200-220 gtc aac TGa acGt att AAC tta
264 * mapped to PEP/6-7 WN by mapping [206, 207, 210, 210, 215, 217] to [6, 7]
265 * getMappedPositions(206, 206) should return [6, 6]
266 * getMappedPositions(200, 214) should return [6, 6]
267 * getMappedPositions(210, 215) should return [6, 7]
274 public int[] getMappedPositions(int begin, int end)
276 MapList map = mapping.getMap();
277 return mapping.to == featureSequence ? map.locateInFrom(begin, end)
278 : map.locateInTo(begin, end);
282 * Answers true if the linked features are on coding sequence, false if on
287 public boolean isFromCds()
289 if (mapping.getMap().getFromRatio() == 3)
291 return mapping.to != featureSequence;
293 return mapping.to == featureSequence;