JAL-3187 hacks to get peptide variant in to Jmol hover tooltip
[jalview.git] / src / jalview / datamodel / MappedFeatures.java
1 package jalview.datamodel;
2
3 import jalview.io.gff.Gff3Helper;
4 import jalview.schemes.ResidueProperties;
5 import jalview.util.MappingUtils;
6 import jalview.util.StringUtils;
7
8 import java.util.ArrayList;
9 import java.util.List;
10
11 /**
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
14  * 
15  * @author gmcarstairs
16  */
17 public class MappedFeatures
18 {
19   /*
20    * the mapping from CDS to peptide
21    */
22   public final Mapping mapping;
23
24   /**
25    * the CDS sequence mapped to
26    */
27   public final SequenceI fromSeq;
28
29   /*
30    * the residue position in the peptide sequence
31    */
32   public final int fromPosition;
33
34   /*
35    * the peptide residue at the position 
36    */
37   public final char fromResidue;
38
39   /*
40    * features on CDS that overlap the codon positions
41    */
42   public final List<SequenceFeature> features;
43
44   /**
45    * Constructor
46    * 
47    * @param theMapping
48    * @param pos
49    * @param res
50    * @param theFeatures
51    */
52   public MappedFeatures(Mapping theMapping, SequenceI from, int pos,
53           char res,
54           List<SequenceFeature> theFeatures)
55   {
56     mapping = theMapping;
57     fromSeq = from;
58     fromPosition = pos;
59     fromResidue = res;
60     features = theFeatures;
61   }
62
63   /**
64    * Computes and returns a (possibly empty) list of HGVS notation peptide
65    * variants derived from codon allele variants
66    * 
67    * @return
68    */
69   public List<String> findProteinVariants()
70   {
71     List<String> vars = new ArrayList<>();
72
73     /*
74      * determine canonical codon
75      */
76     int[] codonPos = MappingUtils.flattenRanges(
77             mapping.getMap().locateInFrom(fromPosition, fromPosition));
78     if (codonPos.length != 3)
79     {
80       // error
81       return vars;
82     }
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);
88
89     // todo avoid duplication of code in AlignmentUtils.buildDnaVariantsMap
90
91     for (SequenceFeature sf : features)
92     {
93       int cdsPos = sf.getBegin();
94       if (cdsPos != sf.getEnd())
95       {
96         // not handling multi-locus variant features
97         continue;
98       }
99       if (cdsPos != codonPos[0] && cdsPos != codonPos[1]
100               && cdsPos != codonPos[2])
101       {
102         // e.g. feature on intron within spliced codon!
103         continue;
104       }
105
106       String alls = (String) sf.getValue(Gff3Helper.ALLELES);
107       if (alls == null)
108       {
109         continue;
110       }
111       String from3 = StringUtils.toSentenceCase(
112               ResidueProperties.aa2Triplet
113                       .get(String.valueOf(fromResidue)));
114
115       /*
116        * make a peptide variant for each SNP allele 
117        * e.g. C,G,T gives variants G and T for base C
118        */
119       String[] alleles = alls.toUpperCase().split(",");
120       for (String allele : alleles)
121       {
122         allele = allele.trim().toUpperCase();
123         if (allele.length() > 1)
124         {
125           continue; // multi-locus variant
126         }
127         char[] variantCodon = new char[3];
128         variantCodon[0] = baseCodon[0];
129         variantCodon[1] = baseCodon[1];
130         variantCodon[2] = baseCodon[2];
131
132         /*
133          * poke variant base into canonical codon
134          */
135         int i = cdsPos == codonPos[0] ? 0 : (cdsPos == codonPos[1] ? 1 : 2);
136         variantCodon[i] = allele.toUpperCase().charAt(0);
137         String codon = new String(variantCodon);
138         String peptide = ResidueProperties.codonTranslate(codon);
139         if (fromResidue != peptide.charAt(0))
140         {
141           String to3 = StringUtils.toSentenceCase(
142                   ResidueProperties.aa2Triplet.get(peptide));
143           String var = "p." + from3 + fromPosition + to3;
144           if (!vars.contains(var))
145           {
146             vars.add(var);
147           }
148         }
149       }
150     }
151
152     return vars;
153   }
154 }