JAL-3187 read peptide variant from CSQ:HGVSp if present (VCF/VEP data)
[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     for (SequenceFeature sf : features)
90     {
91       /*
92        * VCF data may already contain the protein consequence
93        */
94       String hgvsp = sf.getValueAsString("CSQ", "HGVSp");
95       if (hgvsp != null)
96       {
97         int colonPos = hgvsp.indexOf(':');
98         if (colonPos >= 0)
99         {
100           String var = hgvsp.substring(colonPos + 1);
101           if (!vars.contains(var))
102           {
103             vars.add(var);
104           }
105           continue;
106         }
107       }
108
109       /*
110        * otherwise, compute codon and peptide variant
111        */
112       // todo avoid duplication of code in AlignmentUtils.buildDnaVariantsMap
113       int cdsPos = sf.getBegin();
114       if (cdsPos != sf.getEnd())
115       {
116         // not handling multi-locus variant features
117         continue;
118       }
119       if (cdsPos != codonPos[0] && cdsPos != codonPos[1]
120               && cdsPos != codonPos[2])
121       {
122         // e.g. feature on intron within spliced codon!
123         continue;
124       }
125
126       String alls = (String) sf.getValue(Gff3Helper.ALLELES);
127       if (alls == null)
128       {
129         continue;
130       }
131       String from3 = StringUtils.toSentenceCase(
132               ResidueProperties.aa2Triplet
133                       .get(String.valueOf(fromResidue)));
134
135       /*
136        * make a peptide variant for each SNP allele 
137        * e.g. C,G,T gives variants G and T for base C
138        */
139       String[] alleles = alls.toUpperCase().split(",");
140       for (String allele : alleles)
141       {
142         allele = allele.trim().toUpperCase();
143         if (allele.length() > 1)
144         {
145           continue; // multi-locus variant
146         }
147         char[] variantCodon = new char[3];
148         variantCodon[0] = baseCodon[0];
149         variantCodon[1] = baseCodon[1];
150         variantCodon[2] = baseCodon[2];
151
152         /*
153          * poke variant base into canonical codon
154          */
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))
160         {
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))
166           {
167             vars.add(var);
168           }
169         }
170       }
171     }
172
173     return vars;
174   }
175 }