import jalview.util.MappingUtils;
import jalview.util.StringUtils;
-import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
/**
* A data bean to hold a list of mapped sequence features (e.g. CDS features
- * mapped from protein), and the mapping between the sequences
+ * mapped from protein), and the mapping between the sequences. It also provides
+ * a method to derive peptide variants from codon variants.
*
* @author gmcarstairs
*/
public class MappedFeatures
{
+ private static final String HGV_SP = "HGVSp";
+
+ private static final String CSQ = "CSQ";
+
/*
- * the mapping from CDS to peptide
+ * the mapping from one sequence to another
*/
public final Mapping mapping;
/**
- * the CDS sequence mapped to
+ * the sequence mapped from
*/
public final SequenceI fromSeq;
/*
- * the residue position in the peptide sequence
+ * features on the sequence mapped to that overlap the mapped positions
*/
- public final int fromPosition;
+ public final List<SequenceFeature> features;
/*
- * the peptide residue at the position
+ * the residue position in the sequence mapped to
*/
- public final char fromResidue;
+ private final int toPosition;
/*
- * features on CDS that overlap the codon positions
+ * the residue at toPosition
*/
- public final List<SequenceFeature> features;
+ private final char toResidue;
+
+ /*
+ * if the mapping is 3:1 or 1:3 (peptide to CDS), this holds the
+ * mapped positions i.e. codon base positions in CDS; to
+ * support calculation of peptide variants from alleles
+ */
+ private final int[] codonPos;
+
+ private final char[] baseCodon;
/**
* Constructor
*
* @param theMapping
+ * @param from
+ * the sequence mapped from (e.g. CDS)
* @param pos
+ * the residue position in the sequence mapped to
* @param res
+ * the residue character at position pos
* @param theFeatures
+ * list of mapped features found in the 'from' sequence at
+ * the mapped position(s)
*/
public MappedFeatures(Mapping theMapping, SequenceI from, int pos,
- char res,
- List<SequenceFeature> theFeatures)
+ char res, List<SequenceFeature> theFeatures)
{
mapping = theMapping;
fromSeq = from;
- fromPosition = pos;
- fromResidue = res;
+ toPosition = pos;
+ toResidue = res;
features = theFeatures;
+
+ /*
+ * determine codon positions and canonical codon
+ * for a peptide-to-CDS mapping
+ */
+ int[] codonIntervals = mapping.getMap().locateInFrom(toPosition, toPosition);
+ int[] codonPositions = codonIntervals == null ? null
+ : MappingUtils.flattenRanges(codonIntervals);
+ if (codonPositions != null && codonPositions.length == 3)
+ {
+ codonPos = codonPositions;
+ baseCodon = new char[3];
+ int cdsStart = fromSeq.getStart();
+ baseCodon[0] = fromSeq.getCharAt(codonPos[0] - cdsStart);
+ baseCodon[1] = fromSeq.getCharAt(codonPos[1] - cdsStart);
+ baseCodon[2] = fromSeq.getCharAt(codonPos[2] - cdsStart);
+ }
+ else
+ {
+ codonPos = null;
+ baseCodon = null;
+ }
}
/**
- * Computes and returns a (possibly empty) list of HGVS notation peptide
- * variants derived from codon allele variants
+ * Computes and returns comma-delimited HGVS notation peptide variants derived
+ * from codon allele variants. If no variants are found, answers an empty
+ * string.
*
+ * @param sf
+ * a sequence feature (which must be one of those held in this
+ * object)
* @return
*/
- public List<String> findProteinVariants()
+ public String findProteinVariants(SequenceFeature sf)
{
- List<String> vars = new ArrayList<>();
+ if (!features.contains(sf) || baseCodon == null)
+ {
+ return "";
+ }
/*
- * determine canonical codon
+ * VCF data may already contain the protein consequence
*/
- int[] codonPos = MappingUtils.flattenRanges(
- mapping.getMap().locateInFrom(fromPosition, fromPosition));
- if (codonPos.length != 3)
+ String hgvsp = sf.getValueAsString(CSQ, HGV_SP);
+ if (hgvsp != null)
{
- // error
- return vars;
+ int colonPos = hgvsp.lastIndexOf(':');
+ if (colonPos >= 0)
+ {
+ String var = hgvsp.substring(colonPos + 1);
+ if (var.contains("p.")) // sanity check
+ {
+ return var;
+ }
+ }
}
- final char[] baseCodon = new char[3];
- int cdsStart = fromSeq.getStart();
- baseCodon[0] = fromSeq.getCharAt(codonPos[0] - cdsStart);
- baseCodon[1] = fromSeq.getCharAt(codonPos[1] - cdsStart);
- baseCodon[2] = fromSeq.getCharAt(codonPos[2] - cdsStart);
- for (SequenceFeature sf : features)
+ /*
+ * otherwise, compute codon and peptide variant
+ */
+ int cdsPos = sf.getBegin();
+ if (cdsPos != sf.getEnd())
{
- /*
- * VCF data may already contain the protein consequence
- */
- String hgvsp = sf.getValueAsString("CSQ", "HGVSp");
- if (hgvsp != null)
+ // not handling multi-locus variant features
+ return "";
+ }
+ if (cdsPos != codonPos[0] && cdsPos != codonPos[1]
+ && cdsPos != codonPos[2])
+ {
+ // e.g. feature on intron within spliced codon!
+ return "";
+ }
+
+ String alls = (String) sf.getValue(Gff3Helper.ALLELES);
+ if (alls == null)
+ {
+ return "";
+ }
+
+ String from3 = StringUtils.toSentenceCase(
+ ResidueProperties.aa2Triplet.get(String.valueOf(toResidue)));
+
+ /*
+ * make a peptide variant for each SNP allele
+ * e.g. C,G,T gives variants G and T for base C
+ */
+ Set<String> variantPeptides = new HashSet<>();
+ String[] alleles = alls.toUpperCase().split(",");
+ StringBuilder vars = new StringBuilder();
+
+ for (String allele : alleles)
+ {
+ allele = allele.trim().toUpperCase();
+ if (allele.length() > 1 || "-".equals(allele))
{
- int colonPos = hgvsp.indexOf(':');
- if (colonPos >= 0)
- {
- String var = hgvsp.substring(colonPos + 1);
- if (!vars.contains(var))
- {
- vars.add(var);
- }
- continue;
- }
+ continue; // multi-locus variant
}
+ char[] variantCodon = new char[3];
+ variantCodon[0] = baseCodon[0];
+ variantCodon[1] = baseCodon[1];
+ variantCodon[2] = baseCodon[2];
/*
- * otherwise, compute codon and peptide variant
+ * poke variant base into canonical codon;
+ * ignore first 'allele' (canonical base)
*/
- // todo avoid duplication of code in AlignmentUtils.buildDnaVariantsMap
- int cdsPos = sf.getBegin();
- if (cdsPos != sf.getEnd())
+ final int i = cdsPos == codonPos[0] ? 0
+ : (cdsPos == codonPos[1] ? 1 : 2);
+ variantCodon[i] = allele.toUpperCase().charAt(0);
+ if (variantCodon[i] == baseCodon[i])
{
- // not handling multi-locus variant features
continue;
}
- if (cdsPos != codonPos[0] && cdsPos != codonPos[1]
- && cdsPos != codonPos[2])
+ String codon = new String(variantCodon);
+ String peptide = ResidueProperties.codonTranslate(codon);
+ boolean synonymous = toResidue == peptide.charAt(0);
+ StringBuilder var = new StringBuilder();
+ if (synonymous)
{
- // e.g. feature on intron within spliced codon!
- continue;
- }
-
- String alls = (String) sf.getValue(Gff3Helper.ALLELES);
- if (alls == null)
- {
- continue;
+ /*
+ * synonymous variant notation e.g. c.1062C>A(p.=)
+ */
+ var.append("c.").append(String.valueOf(cdsPos))
+ .append(String.valueOf(baseCodon[i])).append(">")
+ .append(String.valueOf(variantCodon[i]))
+ .append("(p.=)");
}
- String from3 = StringUtils.toSentenceCase(
- ResidueProperties.aa2Triplet
- .get(String.valueOf(fromResidue)));
-
- /*
- * make a peptide variant for each SNP allele
- * e.g. C,G,T gives variants G and T for base C
- */
- String[] alleles = alls.toUpperCase().split(",");
- for (String allele : alleles)
+ else
{
- allele = allele.trim().toUpperCase();
- if (allele.length() > 1)
- {
- continue; // multi-locus variant
- }
- char[] variantCodon = new char[3];
- variantCodon[0] = baseCodon[0];
- variantCodon[1] = baseCodon[1];
- variantCodon[2] = baseCodon[2];
-
/*
- * poke variant base into canonical codon
+ * missense variant notation e.g. p.Arg355Met
*/
- int i = cdsPos == codonPos[0] ? 0 : (cdsPos == codonPos[1] ? 1 : 2);
- variantCodon[i] = allele.toUpperCase().charAt(0);
- String codon = new String(variantCodon);
- String peptide = ResidueProperties.codonTranslate(codon);
- if (fromResidue != peptide.charAt(0))
+ String to3 = ResidueProperties.STOP.equals(peptide) ? "Ter"
+ : StringUtils.toSentenceCase(
+ ResidueProperties.aa2Triplet.get(peptide));
+ var.append("p.").append(from3).append(String.valueOf(toPosition))
+ .append(to3);
+ }
+ if (!variantPeptides.contains(peptide)) // duplicate consequence
+ {
+ variantPeptides.add(peptide);
+ if (vars.length() > 0)
{
- String to3 = ResidueProperties.STOP.equals(peptide) ? "STOP"
- : StringUtils.toSentenceCase(
- ResidueProperties.aa2Triplet.get(peptide));
- String var = "p." + from3 + fromPosition + to3;
- if (!vars.contains(var))
- {
- vars.add(var);
- }
+ vars.append(",");
}
+ vars.append(var);
}
}
- return vars;
+ return vars.toString();
}
}