prototype DBRef and Feature Type semantics (for annotating coding regions and disting...
[jalview.git] / src / jalview / datamodel / xdb / embl / EmblEntry.java
1 package jalview.datamodel.xdb.embl;
2
3 import jalview.datamodel.DBRefEntry;
4 import jalview.datamodel.DBRefSource;
5 import jalview.datamodel.FeatureProperties;
6 import jalview.datamodel.Sequence;
7 import jalview.datamodel.SequenceFeature;
8 import jalview.datamodel.SequenceI;
9
10 import java.util.Enumeration;
11 import java.util.Hashtable;
12 import java.util.Iterator;
13 import java.util.Vector;
14
15 public class EmblEntry
16 {
17   String accession;
18
19   String version;
20
21   String taxDivision;
22
23   String desc;
24
25   String rCreated;
26
27   String rLastUpdated;
28
29   String lastUpdated;
30
31   Vector keywords;
32
33   Vector refs;
34
35   Vector dbRefs;
36
37   Vector features;
38
39   EmblSequence sequence;
40
41   /**
42    * @return the accession
43    */
44   public String getAccession()
45   {
46     return accession;
47   }
48
49   /**
50    * @param accession
51    *          the accession to set
52    */
53   public void setAccession(String accession)
54   {
55     this.accession = accession;
56   }
57
58   /**
59    * @return the dbRefs
60    */
61   public Vector getDbRefs()
62   {
63     return dbRefs;
64   }
65
66   /**
67    * @param dbRefs
68    *          the dbRefs to set
69    */
70   public void setDbRefs(Vector dbRefs)
71   {
72     this.dbRefs = dbRefs;
73   }
74
75   /**
76    * @return the desc
77    */
78   public String getDesc()
79   {
80     return desc;
81   }
82
83   /**
84    * @param desc
85    *          the desc to set
86    */
87   public void setDesc(String desc)
88   {
89     this.desc = desc;
90   }
91
92   /**
93    * @return the features
94    */
95   public Vector getFeatures()
96   {
97     return features;
98   }
99
100   /**
101    * @param features
102    *          the features to set
103    */
104   public void setFeatures(Vector features)
105   {
106     this.features = features;
107   }
108
109   /**
110    * @return the keywords
111    */
112   public Vector getKeywords()
113   {
114     return keywords;
115   }
116
117   /**
118    * @param keywords
119    *          the keywords to set
120    */
121   public void setKeywords(Vector keywords)
122   {
123     this.keywords = keywords;
124   }
125
126   /**
127    * @return the lastUpdated
128    */
129   public String getLastUpdated()
130   {
131     return lastUpdated;
132   }
133
134   /**
135    * @param lastUpdated
136    *          the lastUpdated to set
137    */
138   public void setLastUpdated(String lastUpdated)
139   {
140     this.lastUpdated = lastUpdated;
141   }
142
143   /**
144    * @return the refs
145    */
146   public Vector getRefs()
147   {
148     return refs;
149   }
150
151   /**
152    * @param refs
153    *          the refs to set
154    */
155   public void setRefs(Vector refs)
156   {
157     this.refs = refs;
158   }
159
160   /**
161    * @return the releaseCreated
162    */
163   public String getRCreated()
164   {
165     return rCreated;
166   }
167
168   /**
169    * @param releaseCreated
170    *          the releaseCreated to set
171    */
172   public void setRcreated(String releaseCreated)
173   {
174     this.rCreated = releaseCreated;
175   }
176
177   /**
178    * @return the releaseLastUpdated
179    */
180   public String getRLastUpdated()
181   {
182     return rLastUpdated;
183   }
184
185   /**
186    * @param releaseLastUpdated
187    *          the releaseLastUpdated to set
188    */
189   public void setRLastUpdated(String releaseLastUpdated)
190   {
191     this.rLastUpdated = releaseLastUpdated;
192   }
193
194   /**
195    * @return the sequence
196    */
197   public EmblSequence getSequence()
198   {
199     return sequence;
200   }
201
202   /**
203    * @param sequence
204    *          the sequence to set
205    */
206   public void setSequence(EmblSequence sequence)
207   {
208     this.sequence = sequence;
209   }
210
211   /**
212    * @return the taxDivision
213    */
214   public String getTaxDivision()
215   {
216     return taxDivision;
217   }
218
219   /**
220    * @param taxDivision
221    *          the taxDivision to set
222    */
223   public void setTaxDivision(String taxDivision)
224   {
225     this.taxDivision = taxDivision;
226   }
227
228   /**
229    * @return the version
230    */
231   public String getVersion()
232   {
233     return version;
234   }
235
236   /**
237    * @param version
238    *          the version to set
239    */
240   public void setVersion(String version)
241   {
242     this.version = version;
243   }
244
245   /*
246    * EMBL Feature support is limited. The text below is included for the benefit
247    * of any developer working on improving EMBL feature import in Jalview.
248    * Extract from EMBL feature specification see
249    * http://www.embl-ebi.ac.uk/embl/Documentation/FT_definitions/feature_table.html
250    * 3.5 Location 3.5.1 Purpose
251    * 
252    * The location indicates the region of the presented sequence which
253    * corresponds to a feature.
254    * 
255    * 3.5.2 Format and conventions The location contains at least one sequence
256    * location descriptor and may contain one or more operators with one or more
257    * sequence location descriptors. Base numbers refer to the numbering in the
258    * entry. This numbering designates the first base (5' end) of the presented
259    * sequence as base 1. Base locations beyond the range of the presented
260    * sequence may not be used in location descriptors, the only exception being
261    * location in a remote entry (see 3.5.2.1, e).
262    * 
263    * Location operators and descriptors are discussed in more detail below.
264    * 
265    * 3.5.2.1 Location descriptors
266    * 
267    * The location descriptor can be one of the following: (a) a single base
268    * number (b) a site between two indicated adjoining bases (c) a single base
269    * chosen from within a specified range of bases (not allowed for new entries)
270    * (d) the base numbers delimiting a sequence span (e) a remote entry
271    * identifier followed by a local location descriptor (i.e., a-d)
272    * 
273    * A site between two adjoining nucleotides, such as endonucleolytic cleavage
274    * site, is indicated by listing the two points separated by a carat (^). The
275    * permitted formats for this descriptor are n^n+1 (for example 55^56), or,
276    * for circular molecules, n^1, where "n" is the full length of the molecule,
277    * ie 1000^1 for circular molecule with length 1000.
278    * 
279    * A single base chosen from a range of bases is indicated by the first base
280    * number and the last base number of the range separated by a single period
281    * (e.g., '12.21' indicates a single base taken from between the indicated
282    * points). From October 2006 the usage of this descriptor is restricted : it
283    * is illegal to use "a single base from a range" (c) either on its own or in
284    * combination with the "sequence span" (d) descriptor for newly created
285    * entries. The existing entries where such descriptors exist are going to be
286    * retrofitted.
287    * 
288    * Sequence spans are indicated by the starting base number and the ending
289    * base number separated by two periods (e.g., '34..456'). The '<' and '>'
290    * symbols may be used with the starting and ending base numbers to indicate
291    * that an end point is beyond the specified base number. The starting and
292    * ending base positions can be represented as distinct base numbers
293    * ('34..456') or a site between two indicated adjoining bases.
294    * 
295    * A location in a remote entry (not the entry to which the feature table
296    * belongs) can be specified by giving the accession-number and sequence
297    * version of the remote entry, followed by a colon ":", followed by a
298    * location descriptor which applies to that entry's sequence (i.e.
299    * J12345.1:1..15, see also examples below)
300    * 
301    * 3.5.2.2 Operators
302    * 
303    * The location operator is a prefix that specifies what must be done to the
304    * indicated sequence to find or construct the location corresponding to the
305    * feature. A list of operators is given below with their definitions and most
306    * common format.
307    * 
308    * complement(location) Find the complement of the presented sequence in the
309    * span specified by " location" (i.e., read the complement of the presented
310    * strand in its 5'-to-3' direction)
311    * 
312    * join(location,location, ... location) The indicated elements should be
313    * joined (placed end-to-end) to form one contiguous sequence
314    * 
315    * order(location,location, ... location) The elements can be found in the
316    * specified order (5' to 3' direction), but nothing is implied about the
317    * reasonableness about joining them
318    * 
319    * Note : location operator "complement" can be used in combination with
320    * either " join" or "order" within the same location; combinations of "join"
321    * and "order" within the same location (nested operators) are illegal.
322    * 
323    * 
324    * 
325    * 3.5.3 Location examples
326    * 
327    * The following is a list of common location descriptors with their meanings:
328    * 
329    * Location Description
330    * 
331    * 467 Points to a single base in the presented sequence
332    * 
333    * 340..565 Points to a continuous range of bases bounded by and including the
334    * starting and ending bases
335    * 
336    * <345..500 Indicates that the exact lower boundary point of a feature is
337    * unknown. The location begins at some base previous to the first base
338    * specified (which need not be contained in the presented sequence) and
339    * continues to and includes the ending base
340    * 
341    * <1..888 The feature starts before the first sequenced base and continues to
342    * and includes base 888
343    * 
344    * 1..>888 The feature starts at the first sequenced base and continues beyond
345    * base 888
346    * 
347    * 102.110 Indicates that the exact location is unknown but that it is one of
348    * the bases between bases 102 and 110, inclusive
349    * 
350    * 123^124 Points to a site between bases 123 and 124
351    * 
352    * join(12..78,134..202) Regions 12 to 78 and 134 to 202 should be joined to
353    * form one contiguous sequence
354    * 
355    * 
356    * complement(34..126) Start at the base complementary to 126 and finish at
357    * the base complementary to base 34 (the feature is on the strand
358    * complementary to the presented strand)
359    * 
360    * 
361    * complement(join(2691..4571,4918..5163)) Joins regions 2691 to 4571 and 4918
362    * to 5163, then complements the joined segments (the feature is on the strand
363    * complementary to the presented strand)
364    * 
365    * join(complement(4918..5163),complement(2691..4571)) Complements regions
366    * 4918 to 5163 and 2691 to 4571, then joins the complemented segments (the
367    * feature is on the strand complementary to the presented strand)
368    * 
369    * J00194.1:100..202 Points to bases 100 to 202, inclusive, in the entry (in
370    * this database) with primary accession number 'J00194'
371    * 
372    * join(1..100,J00194.1:100..202) Joins region 1..100 of the existing entry
373    * with the region 100..202 of remote entry J00194
374    * 
375    */
376   /**
377    * Recover annotated sequences from EMBL file
378    * 
379    * @param noNa
380    *          don't return nucleic acid sequences
381    * @param sourceDb
382    *          TODO
383    * @param noProtein
384    *          don't return any translated protein sequences marked in features
385    * @return dataset sequences with DBRefs and features - DNA always comes first
386    */
387   public jalview.datamodel.SequenceI[] getSequences(boolean noNa,
388           boolean noPeptide, String sourceDb)
389   {
390     Vector seqs = new Vector();
391     Sequence dna = null;
392     if (!noNa)
393     {
394       dna = new Sequence(sourceDb + "|" + accession, sequence.getSequence());
395       dna.setDescription(desc);
396       dna.addDBRef(new DBRefEntry(sourceDb, version, accession));
397       // TODO: add mapping for parentAccession attribute
398       // TODO: transform EMBL Database refs to canonical form
399       if (dbRefs != null)
400         for (Iterator i = dbRefs.iterator(); i.hasNext(); dna
401                 .addDBRef((DBRefEntry) i.next()))
402           ;
403     }
404     try
405     {
406       for (Iterator i = features.iterator(); i.hasNext();)
407       {
408         boolean nextFeature=false;
409         EmblFeature feature = (EmblFeature) i.next();
410         if (!noNa)
411         {
412           if (feature.dbRefs != null && feature.dbRefs.size() > 0)
413           {
414             for (Iterator dbr = feature.dbRefs.iterator(); dbr.hasNext(); dna
415                     .addDBRef((DBRefEntry) dbr.next()))
416               ;
417           }
418         }
419         if (FeatureProperties.isCodingFeature(DBRefSource.EMBL, "CDS"))
420         {
421           // extract coding region(s)
422           jalview.datamodel.Mapping map = null;
423           int[] exon = null;
424           if (feature.locations != null && feature.locations.size() > 0)
425           {
426             for (Enumeration locs = feature.locations.elements(); locs
427                     .hasMoreElements();)
428             {
429               EmblFeatureLocations loc = (EmblFeatureLocations) locs
430                       .nextElement();
431               int[] se = loc.getElementRanges(accession);
432               if (exon == null)
433               {
434                 exon = se;
435               }
436               else
437               {
438                 int[] t = new int[exon.length + se.length];
439                 System.arraycopy(exon, 0, t, 0, exon.length);
440                 System.arraycopy(se, 0, t, exon.length, se.length);
441                 exon = t;
442               }
443             }
444           }
445           String prseq = null;
446           String prname = new String();
447           String prid = null;
448           Hashtable vals = new Hashtable();
449           int prstart = 1;
450           // get qualifiers
451           if (feature.getQualifiers() != null
452                   && feature.getQualifiers().size() > 0)
453           {
454             for (Iterator quals = feature.getQualifiers().iterator(); quals
455                     .hasNext();)
456             {
457               Qualifier q = (Qualifier) quals.next();
458               if (q.getName().equals("translation"))
459               {
460                 prseq = q.getValues()[0];
461               }
462               else if (q.getName().equals("protein_id"))
463               {
464                 prid = q.getValues()[0];
465               }
466               else if (q.getName().equals("codon_start"))
467               {
468                 prstart = Integer.parseInt(q.getValues()[0]);
469               }
470               else if (q.getName().equals("product"))
471               {
472                 prname = q.getValues()[0];
473               }
474               else
475               {
476                 // throw anything else into the additional properties hash
477                 vals.put(q.getName(), q.getValues().toString());
478               }
479             }
480           }
481           Sequence product = null;
482           if (prseq != null && prname != null && prid != null)
483           {
484             // extract proteins.
485             if (!noPeptide)
486             {
487               product = new Sequence(sourceDb + "|" + "EMBLCDS|" + prid
488                       + "|" + prname, prseq, prstart, prstart
489                       + prseq.length() - 1);
490               product.setDescription("Protein Product from " + sourceDb);
491               seqs.add(product);
492             }
493             // we have everything - create the mapping and perhaps the protein
494             // sequence
495             map = new jalview.datamodel.Mapping(product, exon, new int[]
496             { prstart, prstart + prseq.length() - 1 }, 3, 1);
497             // add cds feature to dna seq - this may include the stop codon
498             for (int xint = 0; xint < exon.length; xint += 2)
499             {
500               SequenceFeature sf = new SequenceFeature();
501               sf.setBegin(exon[xint]);
502               sf.setEnd(exon[xint + 1]);
503               sf.setType(feature.getName());
504               sf.setFeatureGroup(jalview.datamodel.DBRefSource.EMBL);
505               sf.setDescription("Exon " + (1 + xint) + " for protein '"
506                       + prname + "' EMBLCDS:" + prid);
507               sf.setValue(FeatureProperties.EXONPOS, new Integer(1+xint));
508               sf.setValue(FeatureProperties.EXONPRODUCT, prname);
509               if (vals != null && vals.size() > 0)
510               {
511                 Enumeration kv = vals.elements();
512                 while (kv.hasMoreElements())
513                 {
514                   Object key = kv.nextElement();
515                   if (key != null)
516                     sf.setValue(key.toString(), vals.get(key));
517                 }
518               }
519               dna.addSequenceFeature(sf);
520             }
521           }
522           // add dbRefs to sequence
523           if (feature.dbRefs != null && feature.dbRefs.size() > 0)
524           {
525             for (Iterator dbr = feature.dbRefs.iterator(); dbr.hasNext();)
526             {
527               DBRefEntry ref = (DBRefEntry) dbr.next();
528               ref.setSource(jalview.util.DBRefUtils.getCanonicalName(ref
529                       .getSource()));
530               // Hard code the kind of protein product accessions that EMBL cite
531               if (ref.getSource().equals(
532                       jalview.datamodel.DBRefSource.UNIPROT))
533               {
534                 ref.setMap(map);
535               }
536               if (product != null)
537               {
538                 DBRefEntry pref = new DBRefEntry(ref.getSource(), ref
539                         .getVersion(), ref.getAccessionId());
540                 pref.setMap(null); // reference is direct
541               }
542               dna.addDBRef(ref);
543             }
544           }
545
546         }
547         else
548         {
549           // General feature type.
550           if (!noNa)
551           {
552             if (feature.dbRefs != null && feature.dbRefs.size() > 0)
553             {
554               for (Iterator dbr = feature.dbRefs.iterator(); dbr.hasNext(); dna
555                       .addDBRef((DBRefEntry) dbr.next()))
556                 ;
557             }
558           }
559         }
560       }
561     } catch (Exception e)
562     {
563       System.err.println("EMBL Record Features parsing error!");
564       System.err.println("Please report the following to help@jalview.org :");
565       System.err.println("EMBL Record "+accession);
566       System.err.println("Resulted in exception: "+e.getMessage());
567       e.printStackTrace(System.err);
568     }
569     if (!noNa && dna!=null)
570     {
571       seqs.add(dna);
572     }
573     SequenceI[] sqs = new SequenceI[seqs.size()];
574     for (int i = 0, j = seqs.size(); i < j; i++)
575     {
576       sqs[i] = (SequenceI) seqs.elementAt(i);
577       seqs.set(i, null);
578     }
579     return sqs;
580   }
581 }