JAL-1705 regular expression updates, tests, other refactoring
[jalview.git] / src / jalview / ext / ensembl / EnsemblProtein.java
1 package jalview.ext.ensembl;
2
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.SequenceFeature;
5
6 import java.util.Arrays;
7 import java.util.List;
8
9 import com.stevesoft.pat.Regex;
10
11 public class EnsemblProtein extends EnsemblSeqProxy
12 {
13   /*
14    * accepts ENSP with 11 digits
15    * or ENSMUSP or similar for other species
16    * or CCDSnnnnn.nn with at least 3 digits
17    */
18   private static final Regex ACCESSION_REGEX = new Regex(
19           "(ENS([A-Z]{3}|)P[0-9]{11}$)" + "|" + "(CCDS[0-9.]{3,}$)");
20
21   private static final List<String> CROSSREFS = Arrays.asList(new String[] {
22       "PDB", "Uniprot/SPTREMBL", "Uniprot/SWISSPROT" });
23
24   public EnsemblProtein()
25   {
26     super();
27   }
28
29   @Override
30   public String getDbName()
31   {
32     return "ENSEMBL (Protein)";
33   }
34
35   @Override
36   protected EnsemblSeqType getSourceEnsemblType()
37   {
38     return EnsemblSeqType.PROTEIN;
39   }
40
41   /**
42    * Returns false, as this fetcher does not retrieve DNA sequences.
43    */
44   @Override
45   public boolean isDnaCoding()
46   {
47     return false;
48   }
49
50   /**
51    * Test query is to the protein translation of transcript ENST00000288602
52    */
53   @Override
54   public String getTestQuery()
55   {
56     return "ENSP00000288602";
57   }
58
59   /**
60    * Overrides base class method to do nothing - genomic features are not
61    * applicable to the protein product sequence
62    */
63   @Override
64   protected void addFeaturesAndProduct(String accId, AlignmentI alignment)
65   {
66   }
67
68   @Override
69   protected EnsemblFeatureType[] getFeaturesToFetch()
70   {
71     // not applicable - can't fetch genomic features for a protein sequence
72     return null;
73   }
74
75   @Override
76   protected boolean identifiesSequence(SequenceFeature sf, String accId)
77   {
78     // not applicable - protein sequence is not a 'subset' of genomic sequence
79     return false;
80   }
81
82   @Override
83   protected List<String> getCrossReferenceDatabases()
84   {
85     return CROSSREFS;
86   }
87
88   @Override
89   public Regex getAccessionValidator()
90   {
91     return ACCESSION_REGEX;
92   }
93
94   /**
95    * Returns an accession id for a query, including conversion of ENST* to
96    * ENSP*. This supports querying for the protein sequence for a transcript
97    * (ENST identifier) and returning the ENSP identifier.
98    */
99   @Override
100   public String getAccessionIdFromQuery(String query)
101   {
102     String accId = super.getAccessionIdFromQuery(query);
103
104     /*
105      * ensure last character before (11) digits is P
106      * ENST00000288602 -> ENSP00000288602
107      * ENSMUST00000288602 -> ENSMUSP00000288602
108      */
109     if (accId != null && accId.length() >= 12)
110     {
111       char[] chars = accId.toCharArray();
112       chars[chars.length - 12] = 'P';
113       accId = new String(chars);
114     }
115     return accId;
116   }
117
118 }