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