Merge develop to Release_2_8_3_Branch
[jalview.git] / src / jalview / datamodel / xdb / embl / EmblEntry.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel.xdb.embl;
22
23 import jalview.datamodel.DBRefEntry;
24 import jalview.datamodel.DBRefSource;
25 import jalview.datamodel.FeatureProperties;
26 import jalview.datamodel.Mapping;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
30
31 import java.util.Enumeration;
32 import java.util.Hashtable;
33 import java.util.Iterator;
34 import java.util.Vector;
35
36 public class EmblEntry
37 {
38   String accession;
39
40   String version;
41
42   String taxDivision;
43
44   String desc;
45
46   String rCreated;
47
48   String rLastUpdated;
49
50   String lastUpdated;
51
52   Vector keywords;
53
54   Vector refs;
55
56   Vector dbRefs;
57
58   Vector features;
59
60   EmblSequence sequence;
61
62   /**
63    * @return the accession
64    */
65   public String getAccession()
66   {
67     return accession;
68   }
69
70   /**
71    * @param accession
72    *          the accession to set
73    */
74   public void setAccession(String accession)
75   {
76     this.accession = accession;
77   }
78
79   /**
80    * @return the dbRefs
81    */
82   public Vector getDbRefs()
83   {
84     return dbRefs;
85   }
86
87   /**
88    * @param dbRefs
89    *          the dbRefs to set
90    */
91   public void setDbRefs(Vector dbRefs)
92   {
93     this.dbRefs = dbRefs;
94   }
95
96   /**
97    * @return the desc
98    */
99   public String getDesc()
100   {
101     return desc;
102   }
103
104   /**
105    * @param desc
106    *          the desc to set
107    */
108   public void setDesc(String desc)
109   {
110     this.desc = desc;
111   }
112
113   /**
114    * @return the features
115    */
116   public Vector getFeatures()
117   {
118     return features;
119   }
120
121   /**
122    * @param features
123    *          the features to set
124    */
125   public void setFeatures(Vector features)
126   {
127     this.features = features;
128   }
129
130   /**
131    * @return the keywords
132    */
133   public Vector getKeywords()
134   {
135     return keywords;
136   }
137
138   /**
139    * @param keywords
140    *          the keywords to set
141    */
142   public void setKeywords(Vector keywords)
143   {
144     this.keywords = keywords;
145   }
146
147   /**
148    * @return the lastUpdated
149    */
150   public String getLastUpdated()
151   {
152     return lastUpdated;
153   }
154
155   /**
156    * @param lastUpdated
157    *          the lastUpdated to set
158    */
159   public void setLastUpdated(String lastUpdated)
160   {
161     this.lastUpdated = lastUpdated;
162   }
163
164   /**
165    * @return the refs
166    */
167   public Vector getRefs()
168   {
169     return refs;
170   }
171
172   /**
173    * @param refs
174    *          the refs to set
175    */
176   public void setRefs(Vector refs)
177   {
178     this.refs = refs;
179   }
180
181   /**
182    * @return the releaseCreated
183    */
184   public String getRCreated()
185   {
186     return rCreated;
187   }
188
189   /**
190    * @param releaseCreated
191    *          the releaseCreated to set
192    */
193   public void setRcreated(String releaseCreated)
194   {
195     this.rCreated = releaseCreated;
196   }
197
198   /**
199    * @return the releaseLastUpdated
200    */
201   public String getRLastUpdated()
202   {
203     return rLastUpdated;
204   }
205
206   /**
207    * @param releaseLastUpdated
208    *          the releaseLastUpdated to set
209    */
210   public void setRLastUpdated(String releaseLastUpdated)
211   {
212     this.rLastUpdated = releaseLastUpdated;
213   }
214
215   /**
216    * @return the sequence
217    */
218   public EmblSequence getSequence()
219   {
220     return sequence;
221   }
222
223   /**
224    * @param sequence
225    *          the sequence to set
226    */
227   public void setSequence(EmblSequence sequence)
228   {
229     this.sequence = sequence;
230   }
231
232   /**
233    * @return the taxDivision
234    */
235   public String getTaxDivision()
236   {
237     return taxDivision;
238   }
239
240   /**
241    * @param taxDivision
242    *          the taxDivision to set
243    */
244   public void setTaxDivision(String taxDivision)
245   {
246     this.taxDivision = taxDivision;
247   }
248
249   /**
250    * @return the version
251    */
252   public String getVersion()
253   {
254     return version;
255   }
256
257   /**
258    * @param version
259    *          the version to set
260    */
261   public void setVersion(String version)
262   {
263     this.version = version;
264   }
265
266   /*
267    * EMBL Feature support is limited. The text below is included for the benefit
268    * of any developer working on improving EMBL feature import in Jalview.
269    * Extract from EMBL feature specification see
270    * http://www.embl-ebi.ac.uk/embl/Documentation
271    * /FT_definitions/feature_table.html 3.5 Location 3.5.1 Purpose
272    * 
273    * The location indicates the region of the presented sequence which
274    * corresponds to a feature.
275    * 
276    * 3.5.2 Format and conventions The location contains at least one sequence
277    * location descriptor and may contain one or more operators with one or more
278    * sequence location descriptors. Base numbers refer to the numbering in the
279    * entry. This numbering designates the first base (5' end) of the presented
280    * sequence as base 1. Base locations beyond the range of the presented
281    * sequence may not be used in location descriptors, the only exception being
282    * location in a remote entry (see 3.5.2.1, e).
283    * 
284    * Location operators and descriptors are discussed in more detail below.
285    * 
286    * 3.5.2.1 Location descriptors
287    * 
288    * The location descriptor can be one of the following: (a) a single base
289    * number (b) a site between two indicated adjoining bases (c) a single base
290    * chosen from within a specified range of bases (not allowed for new entries)
291    * (d) the base numbers delimiting a sequence span (e) a remote entry
292    * identifier followed by a local location descriptor (i.e., a-d)
293    * 
294    * A site between two adjoining nucleotides, such as endonucleolytic cleavage
295    * site, is indicated by listing the two points separated by a carat (^). The
296    * permitted formats for this descriptor are n^n+1 (for example 55^56), or,
297    * for circular molecules, n^1, where "n" is the full length of the molecule,
298    * ie 1000^1 for circular molecule with length 1000.
299    * 
300    * A single base chosen from a range of bases is indicated by the first base
301    * number and the last base number of the range separated by a single period
302    * (e.g., '12.21' indicates a single base taken from between the indicated
303    * points). From October 2006 the usage of this descriptor is restricted : it
304    * is illegal to use "a single base from a range" (c) either on its own or in
305    * combination with the "sequence span" (d) descriptor for newly created
306    * entries. The existing entries where such descriptors exist are going to be
307    * retrofitted.
308    * 
309    * Sequence spans are indicated by the starting base number and the ending
310    * base number separated by two periods (e.g., '34..456'). The '<' and '>'
311    * symbols may be used with the starting and ending base numbers to indicate
312    * that an end point is beyond the specified base number. The starting and
313    * ending base positions can be represented as distinct base numbers
314    * ('34..456') or a site between two indicated adjoining bases.
315    * 
316    * A location in a remote entry (not the entry to which the feature table
317    * belongs) can be specified by giving the accession-number and sequence
318    * version of the remote entry, followed by a colon ":", followed by a
319    * location descriptor which applies to that entry's sequence (i.e.
320    * J12345.1:1..15, see also examples below)
321    * 
322    * 3.5.2.2 Operators
323    * 
324    * The location operator is a prefix that specifies what must be done to the
325    * indicated sequence to find or construct the location corresponding to the
326    * feature. A list of operators is given below with their definitions and most
327    * common format.
328    * 
329    * complement(location) Find the complement of the presented sequence in the
330    * span specified by " location" (i.e., read the complement of the presented
331    * strand in its 5'-to-3' direction)
332    * 
333    * join(location,location, ... location) The indicated elements should be
334    * joined (placed end-to-end) to form one contiguous sequence
335    * 
336    * order(location,location, ... location) The elements can be found in the
337    * specified order (5' to 3' direction), but nothing is implied about the
338    * reasonableness about joining them
339    * 
340    * Note : location operator "complement" can be used in combination with
341    * either " join" or "order" within the same location; combinations of "join"
342    * and "order" within the same location (nested operators) are illegal.
343    * 
344    * 
345    * 
346    * 3.5.3 Location examples
347    * 
348    * The following is a list of common location descriptors with their meanings:
349    * 
350    * Location Description
351    * 
352    * 467 Points to a single base in the presented sequence
353    * 
354    * 340..565 Points to a continuous range of bases bounded by and including the
355    * starting and ending bases
356    * 
357    * <345..500 Indicates that the exact lower boundary point of a feature is
358    * unknown. The location begins at some base previous to the first base
359    * specified (which need not be contained in the presented sequence) and
360    * continues to and includes the ending base
361    * 
362    * <1..888 The feature starts before the first sequenced base and continues to
363    * and includes base 888
364    * 
365    * 1..>888 The feature starts at the first sequenced base and continues beyond
366    * base 888
367    * 
368    * 102.110 Indicates that the exact location is unknown but that it is one of
369    * the bases between bases 102 and 110, inclusive
370    * 
371    * 123^124 Points to a site between bases 123 and 124
372    * 
373    * join(12..78,134..202) Regions 12 to 78 and 134 to 202 should be joined to
374    * form one contiguous sequence
375    * 
376    * 
377    * complement(34..126) Start at the base complementary to 126 and finish at
378    * the base complementary to base 34 (the feature is on the strand
379    * complementary to the presented strand)
380    * 
381    * 
382    * complement(join(2691..4571,4918..5163)) Joins regions 2691 to 4571 and 4918
383    * to 5163, then complements the joined segments (the feature is on the strand
384    * complementary to the presented strand)
385    * 
386    * join(complement(4918..5163),complement(2691..4571)) Complements regions
387    * 4918 to 5163 and 2691 to 4571, then joins the complemented segments (the
388    * feature is on the strand complementary to the presented strand)
389    * 
390    * J00194.1:100..202 Points to bases 100 to 202, inclusive, in the entry (in
391    * this database) with primary accession number 'J00194'
392    * 
393    * join(1..100,J00194.1:100..202) Joins region 1..100 of the existing entry
394    * with the region 100..202 of remote entry J00194
395    */
396   /**
397    * Recover annotated sequences from EMBL file
398    * 
399    * @param noNa
400    *          don't return nucleic acid sequences
401    * @param sourceDb
402    *          TODO
403    * @param noProtein
404    *          don't return any translated protein sequences marked in features
405    * @return dataset sequences with DBRefs and features - DNA always comes first
406    */
407   public jalview.datamodel.SequenceI[] getSequences(boolean noNa,
408           boolean noPeptide, String sourceDb)
409   { // TODO: ensure emblEntry.getSequences behaves correctly for returning all
410     // cases of noNa and noPeptide
411     Vector seqs = new Vector();
412     Sequence dna = null;
413     if (!noNa)
414     {
415       // In theory we still need to create this if noNa is set to avoid a null
416       // pointer exception
417       dna = new Sequence(sourceDb + "|" + accession, sequence.getSequence());
418       dna.setDescription(desc);
419       DBRefEntry retrievedref = new DBRefEntry(sourceDb, version, accession);
420       dna.addDBRef(retrievedref);
421       // add map to indicate the sequence is a valid coordinate frame for the
422       // dbref
423       retrievedref.setMap(new Mapping(null, new int[]
424       { 1, dna.getLength() }, new int[]
425       { 1, dna.getLength() }, 1, 1));
426       // TODO: transform EMBL Database refs to canonical form
427       if (dbRefs != null)
428       {
429         for (Iterator i = dbRefs.iterator(); i.hasNext(); dna
430                 .addDBRef((DBRefEntry) i.next()))
431         {
432           ;
433         }
434       }
435     }
436     try
437     {
438       for (Iterator i = features.iterator(); i.hasNext();)
439       {
440         EmblFeature feature = (EmblFeature) i.next();
441         if (!noNa)
442         {
443           if (feature.dbRefs != null && feature.dbRefs.size() > 0)
444           {
445             for (Iterator dbr = feature.dbRefs.iterator(); dbr.hasNext(); dna
446                     .addDBRef((DBRefEntry) dbr.next()))
447             {
448               ;
449             }
450           }
451         }
452         if (FeatureProperties.isCodingFeature(sourceDb, feature.getName()))
453         {
454           parseCodingFeature(feature, sourceDb, seqs, dna, noPeptide);
455         }
456         else
457         {
458           // General feature type.
459           if (!noNa)
460           {
461             if (feature.dbRefs != null && feature.dbRefs.size() > 0)
462             {
463               for (Iterator dbr = feature.dbRefs.iterator(); dbr.hasNext(); dna
464                       .addDBRef((DBRefEntry) dbr.next()))
465               {
466                 ;
467               }
468             }
469           }
470         }
471       }
472     } catch (Exception e)
473     {
474       System.err.println("EMBL Record Features parsing error!");
475       System.err
476               .println("Please report the following to help@jalview.org :");
477       System.err.println("EMBL Record " + accession);
478       System.err.println("Resulted in exception: " + e.getMessage());
479       e.printStackTrace(System.err);
480     }
481     if (!noNa && dna != null)
482     {
483       seqs.add(dna);
484     }
485     SequenceI[] sqs = new SequenceI[seqs.size()];
486     for (int i = 0, j = seqs.size(); i < j; i++)
487     {
488       sqs[i] = (SequenceI) seqs.elementAt(i);
489       seqs.set(i, null);
490     }
491     return sqs;
492   }
493
494   /**
495    * attempt to extract coding region and product from a feature and properly
496    * decorate it with annotations.
497    * 
498    * @param feature
499    *          coding feature
500    * @param sourceDb
501    *          source database for the EMBLXML
502    * @param seqs
503    *          place where sequences go
504    * @param dna
505    *          parent dna sequence for this record
506    * @param noPeptide
507    *          flag for generation of Peptide sequence objects
508    */
509   private void parseCodingFeature(EmblFeature feature, String sourceDb,
510           Vector seqs, Sequence dna, boolean noPeptide)
511   {
512     boolean isEmblCdna = sourceDb.equals(DBRefSource.EMBLCDS);
513     // extract coding region(s)
514     jalview.datamodel.Mapping map = null;
515     int[] exon = null;
516     if (feature.locations != null && feature.locations.size() > 0)
517     {
518       for (Enumeration locs = feature.locations.elements(); locs
519               .hasMoreElements();)
520       {
521         EmblFeatureLocations loc = (EmblFeatureLocations) locs
522                 .nextElement();
523         int[] se = loc.getElementRanges(accession);
524         if (exon == null)
525         {
526           exon = se;
527         }
528         else
529         {
530           int[] t = new int[exon.length + se.length];
531           System.arraycopy(exon, 0, t, 0, exon.length);
532           System.arraycopy(se, 0, t, exon.length, se.length);
533           exon = t;
534         }
535       }
536     }
537     String prseq = null;
538     String prname = new String();
539     String prid = null;
540     Hashtable vals = new Hashtable();
541     int prstart = 1;
542     // get qualifiers
543     if (feature.getQualifiers() != null
544             && feature.getQualifiers().size() > 0)
545     {
546       for (Iterator quals = feature.getQualifiers().iterator(); quals
547               .hasNext();)
548       {
549         Qualifier q = (Qualifier) quals.next();
550         if (q.getName().equals("translation"))
551         {
552           StringBuffer prsq = new StringBuffer(q.getValues()[0]);
553           int p = prsq.indexOf(" ");
554           while (p > -1)
555           {
556             prsq.deleteCharAt(p);
557             p = prsq.indexOf(" ", p);
558           }
559           prseq = prsq.toString();
560           prsq = null;
561
562         }
563         else if (q.getName().equals("protein_id"))
564         {
565           prid = q.getValues()[0];
566         }
567         else if (q.getName().equals("codon_start"))
568         {
569           prstart = Integer.parseInt(q.getValues()[0]);
570         }
571         else if (q.getName().equals("product"))
572         {
573           prname = q.getValues()[0];
574         }
575         else
576         {
577           // throw anything else into the additional properties hash
578           String[] s = q.getValues();
579           StringBuffer sb = new StringBuffer();
580           if (s != null)
581           {
582             for (int i = 0; i < s.length; i++)
583             {
584               sb.append(s[i]);
585               sb.append("\n");
586             }
587           }
588           vals.put(q.getName(), sb.toString());
589         }
590       }
591     }
592     Sequence product = null;
593     DBRefEntry protEMBLCDS = null;
594     exon = adjustForPrStart(prstart, exon);
595     boolean noProteinDbref=true;
596     
597     if (prseq != null && prname != null && prid != null)
598     {
599       // extract proteins.
600       product = new Sequence(prid, prseq, 1, prseq.length());
601       product.setDescription(((prname.length() == 0) ? "Protein Product from "
602               + sourceDb
603               : prname));
604       if (!noPeptide)
605       {
606         // Protein is also added to vector of sequences returned
607         seqs.add(product);
608       }
609       // we have everything - create the mapping and perhaps the protein
610       // sequence
611       if (exon == null || exon.length == 0)
612       {
613         System.err
614                 .println("Implementation Notice: EMBLCDS records not properly supported yet - Making up the CDNA region of this sequence... may be incorrect ("
615                         + sourceDb + ":" + getAccession() + ")");
616         if (prseq.length() * 3 == (1 - prstart + dna.getSequence().length))
617         {
618           System.err
619                   .println("Not allowing for additional stop codon at end of cDNA fragment... !");
620           // this might occur for CDS sequences where no features are
621           // marked.
622           exon = new int[]
623           { dna.getStart() + (prstart - 1), dna.getEnd() };
624           map = new jalview.datamodel.Mapping(product, exon, new int[]
625           { 1, prseq.length() }, 3, 1);
626         }
627         if ((prseq.length() + 1) * 3 == (1 - prstart + dna.getSequence().length))
628         {
629           System.err
630                   .println("Allowing for additional stop codon at end of cDNA fragment... will probably cause an error in VAMSAs!");
631           exon = new int[]
632           { dna.getStart() + (prstart - 1), dna.getEnd() - 3 };
633           map = new jalview.datamodel.Mapping(product, exon, new int[]
634           { 1, prseq.length() }, 3, 1);
635         }
636       }
637       else
638       {
639         // Trim the exon mapping if necessary - the given product may only be a
640         // fragment of a larger protein. (EMBL:AY043181 is an example)
641
642         if (isEmblCdna)
643         {
644           // TODO: Add a DbRef back to the parent EMBL sequence with the exon
645           // map
646           // if given a dataset reference, search dataset for parent EMBL
647           // sequence if it exists and set its map
648           // make a new feature annotating the coding contig
649         }
650         else
651         {
652           // final product length trunctation check
653
654           map = new jalview.datamodel.Mapping(product,
655                   adjustForProteinLength(prseq.length(), exon), new int[]
656                   { 1, prseq.length() }, 3, 1);
657           // reconstruct the EMBLCDS entry
658           // TODO: this is only necessary when there codon annotation is
659           // complete (I think JBPNote)
660           DBRefEntry pcdnaref = new DBRefEntry();
661           pcdnaref.setAccessionId(prid);
662           pcdnaref.setSource(DBRefSource.EMBLCDS);
663           pcdnaref.setVersion(getVersion()); // same as parent EMBL version.
664           jalview.util.MapList mp = new jalview.util.MapList(new int[]
665           { 1, prseq.length() }, new int[]
666           { 1 + (prstart - 1), (prstart - 1) + 3 * prseq.length() }, 1, 3);
667           // { 1 + (prstart - 1) * 3,
668           // 1 + (prstart - 1) * 3 + prseq.length() * 3 - 1 }, new int[]
669           // { 1prstart, prstart + prseq.length() - 1 }, 3, 1);
670           pcdnaref.setMap(new Mapping(mp));
671           if (product != null)
672           {
673             product.addDBRef(pcdnaref);
674             protEMBLCDS = new DBRefEntry(pcdnaref);
675             protEMBLCDS.setSource(DBRefSource.EMBLCDSProduct);
676             product.addDBRef(protEMBLCDS);
677             
678           }     
679           
680         }
681       }
682       // add cds feature to dna seq - this may include the stop codon
683       for (int xint = 0; exon != null && xint < exon.length; xint += 2)
684       {
685         SequenceFeature sf = new SequenceFeature();
686         sf.setBegin(exon[xint]);
687         sf.setEnd(exon[xint + 1]);
688         sf.setType(feature.getName());
689         sf.setFeatureGroup(sourceDb);
690         sf.setDescription("Exon " + (1 + xint / 2)
691                 + " for protein '" + prname + "' EMBLCDS:" + prid);
692         sf.setValue(FeatureProperties.EXONPOS, new Integer(1 + xint));
693         sf.setValue(FeatureProperties.EXONPRODUCT, prname);
694         if (vals != null && vals.size() > 0)
695         {
696           Enumeration kv = vals.keys();
697           while (kv.hasMoreElements())
698           {
699             Object key = kv.nextElement();
700             if (key != null)
701             {
702               sf.setValue(key.toString(), vals.get(key));
703             }
704           }
705         }
706         dna.addSequenceFeature(sf);
707       }
708     }
709     // add dbRefs to sequence
710     if (feature.dbRefs != null && feature.dbRefs.size() > 0)
711     {
712       for (Iterator dbr = feature.dbRefs.iterator(); dbr.hasNext();)
713       {
714         DBRefEntry ref = (DBRefEntry) dbr.next();
715         ref.setSource(jalview.util.DBRefUtils.getCanonicalName(ref
716                 .getSource()));
717         // Hard code the kind of protein product accessions that EMBL cite
718         if (ref.getSource().equals(jalview.datamodel.DBRefSource.UNIPROT))
719         {
720           ref.setMap(map);
721           if (map != null && map.getTo() != null)
722           {
723             map.getTo().addDBRef(
724                     new DBRefEntry(ref.getSource(), ref.getVersion(), ref
725                             .getAccessionId())); // don't copy map over.
726             if (map.getTo().getName().indexOf(prid) == 0)
727             {
728               map.getTo().setName(
729                       jalview.datamodel.DBRefSource.UNIPROT + "|"
730                               + ref.getAccessionId());
731             }
732           }
733           noProteinDbref = false;
734         }
735         if (product != null)
736         {
737           DBRefEntry pref = new DBRefEntry(ref.getSource(),
738                   ref.getVersion(), ref.getAccessionId());
739           pref.setMap(null); // reference is direct
740           product.addDBRef(pref);
741           // Add converse mapping reference
742           if (map != null)
743           {
744             Mapping pmap = new Mapping(dna, map.getMap().getInverse());
745             pref = new DBRefEntry(sourceDb, getVersion(),
746                     this.getAccession());
747             pref.setMap(pmap);
748             if (map.getTo() != null)
749             {
750               map.getTo().addDBRef(pref);
751             }
752           }
753         }
754         dna.addDBRef(ref);
755       }
756       if (noProteinDbref && product != null)
757       {
758         // add protein coding reference to dna sequence so xref matches
759         if (protEMBLCDS == null)
760         {
761           protEMBLCDS = new DBRefEntry();
762           protEMBLCDS.setAccessionId(prid);
763           protEMBLCDS.setSource(DBRefSource.EMBLCDSProduct);
764           protEMBLCDS.setVersion(getVersion());
765           protEMBLCDS
766                   .setMap(new Mapping(product, map.getMap().getInverse()));
767         }
768         product.addDBRef(protEMBLCDS);
769           
770         // Add converse mapping reference
771         if (map != null)
772         {
773           Mapping pmap = new Mapping(product, protEMBLCDS.getMap().getMap()
774                   .getInverse());
775           DBRefEntry ncMap = new DBRefEntry(protEMBLCDS);
776           ncMap.setMap(pmap);
777           if (map.getTo() != null)
778           {
779             dna.addDBRef(ncMap);
780           }
781         }
782       }
783     }
784   }
785
786   private int[] adjustForPrStart(int prstart, int[] exon)
787   {
788
789     int origxon[], sxpos = -1;
790     int sxstart, sxstop; // unnecessary variables used for debugging
791     // first adjust range for codon start attribute
792     if (prstart > 1)
793     {
794       origxon = new int[exon.length];
795       System.arraycopy(exon, 0, origxon, 0, exon.length);
796       int cdspos = 0;
797       for (int x = 0; x < exon.length && sxpos == -1; x += 2)
798       {
799         cdspos += exon[x + 1] - exon[x] + 1;
800         if (prstart <= cdspos)
801         {
802           sxpos = x;
803           sxstart = exon[x];
804           sxstop = exon[x + 1];
805           // and adjust start boundary of first exon.
806           exon[x] = exon[x + 1] - cdspos + prstart;
807           break;
808         }
809       }
810
811       if (sxpos > 0)
812       {
813         int[] nxon = new int[exon.length - sxpos];
814         System.arraycopy(exon, sxpos, nxon, 0, exon.length - sxpos);
815         exon = nxon;
816       }
817     }
818     return exon;
819   }
820
821   /**
822    * truncate the last exon interval to the prlength'th codon
823    * 
824    * @param prlength
825    * @param exon
826    * @return new exon
827    */
828   private int[] adjustForProteinLength(int prlength, int[] exon)
829   {
830
831     int origxon[], sxpos = -1, endxon = 0, cdslength = prlength * 3;
832     int sxstart, sxstop; // unnecessary variables used for debugging
833     // first adjust range for codon start attribute
834     if (prlength >= 1 && exon != null)
835     {
836       origxon = new int[exon.length];
837       System.arraycopy(exon, 0, origxon, 0, exon.length);
838       int cdspos = 0;
839       for (int x = 0; x < exon.length && sxpos == -1; x += 2)
840       {
841         cdspos += exon[x + 1] - exon[x] + 1;
842         if (cdslength <= cdspos)
843         {
844           // advanced beyond last codon.
845           sxpos = x;
846           sxstart = exon[x];
847           sxstop = exon[x + 1];
848           if (cdslength != cdspos)
849           {
850             System.err
851                     .println("Truncating final exon interval on region by "
852                             + (cdspos - cdslength));
853           }
854           // locate the new end boundary of final exon as endxon
855           endxon = exon[x + 1] - cdspos + cdslength;
856           break;
857         }
858       }
859
860       if (sxpos != -1)
861       {
862         // and trim the exon interval set if necessary
863         int[] nxon = new int[sxpos + 2];
864         System.arraycopy(exon, 0, nxon, 0, sxpos + 2);
865         nxon[sxpos + 1] = endxon; // update the end boundary for the new exon
866                                   // set
867         exon = nxon;
868       }
869     }
870     return exon;
871   }
872 }