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