001    /**
002     * Copyright (C) 2010 The Roslin Institute <contact andy.law@roslin.ed.ac.uk>
003     *
004     * This file is part of the Ensembl Java API demonstration project developed by the
005     * Bioinformatics Group at The Roslin Institute, The Royal (Dick) School of
006     * Veterinary Studies, University of Edinburgh.
007     *
008     * This is free software: you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (version 3) as published by
010     * the Free Software Foundation.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * in this software distribution. If not, see <http://www.gnu.org/licenses/gpl-3.0.html/>.
019     */
020    
021    package uk.ac.roslin.ensembl.model;
022    
023    import java.io.Serializable;
024    import java.util.Comparator;
025    import java.util.TreeSet;
026    
027    /**
028     * Used to be abstract with signature
029     * public abstract class MappingSet<CLAZZ extends Mapping> extends TreeSet<CLAZZ> implements Serializable {
030     * @author tpaterso
031     */
032    public class MappingSet extends TreeSet<Mapping> implements Serializable {
033    
034        //this is tragically bad design - should have two subclasses, one Mapping on Source,
035        //one Mapping on Target ( and potentially other subclasses with other comparators )
036    
037        public MappingSet() {
038            super(Mapping.mappingOnSourceComparator);
039            //super(new MappingOnSourceComparator());
040        }
041    
042        public MappingSet(Comparator differentComparator) {
043            super(differentComparator);
044        }
045    
046        public Coordinate getExtent() {
047            if (this.isEmpty()) {
048                return null;
049            }
050    
051    
052            //if (this.comparator() instanceof MappingOnSourceComparator) {
053            if (this.comparator().equals(Mapping.mappingOnSourceComparator)) {
054    
055                Coordinate out = new Coordinate(null, null, 1);
056    
057                if (this.first().getSourceCoordinates()!=null) {
058                    out.setStart(this.first().getSourceCoordinates().getStart());
059                }
060                Integer end = 0;
061                for (Mapping m : this) {
062                    if (m.getSourceCoordinates() != null
063                            && m.getSourceCoordinates().getEnd() > end ) {
064                        end =  m.getSourceCoordinates().getEnd();
065                    }
066                }
067                if (end != 0) {
068                    out.setEnd(end);
069                }
070    
071                return out;
072    
073            //} else if (this.comparator() instanceof MappingOnTargetComparator) {
074            } else if (this.comparator().equals(Mapping.mappingOnTargetComparator)) {
075                Coordinate out = new Coordinate(null, null, 1);
076    
077                if (this.first().getTargetCoordinates()!=null) {
078                    out.setStart(this.first().getTargetCoordinates().getStart());
079                }
080    
081                Integer end = 0;
082                for (Mapping m : this) {
083                    if (m.getTargetCoordinates() != null
084                            && m.getTargetCoordinates().getEnd() > end ) {
085                        end =  m.getTargetCoordinates().getEnd();
086                    }
087                }
088                if (end != 0) {
089                    out.setEnd(end);
090                }
091    
092                return out;
093    
094            }
095    
096            return null;
097        }
098        
099    }