001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.ws.hmmer;
022
023import java.io.Serializable;
024import java.util.SortedSet;
025
026/** The results of a Hmmer search for a single sequence
027 *
028 * @author Andreas Prlic
029 * @since 3.0.3
030 */
031public class HmmerResult implements Comparable<HmmerResult>, Serializable{
032
033        /**
034         *
035         */
036        private static final long serialVersionUID = -6016026193090737943L;
037
038        String desc ;
039        Float score;
040        Float evalue;
041        Double pvalue;
042        String acc;
043        Integer dcl;
044        String name;
045        Integer ndom;
046        Integer nreported;
047
048        SortedSet<HmmerDomain>domains;
049
050        public SortedSet<HmmerDomain> getDomains() {
051                return domains;
052        }
053        public void setDomains(SortedSet<HmmerDomain> domains) {
054                this.domains = domains;
055        }
056        public String getDesc() {
057                return desc;
058        }
059        public void setDesc(String desc) {
060                this.desc = desc;
061        }
062        public Float getScore() {
063                return score;
064        }
065        public void setScore(Float score) {
066                this.score = score;
067        }
068        public Float getEvalue() {
069                return evalue;
070        }
071        public void setEvalue(Float evalue) {
072                this.evalue = evalue;
073        }
074        public Double getPvalue() {
075                return pvalue;
076        }
077        public void setPvalue(Double pvalue) {
078                this.pvalue = pvalue;
079        }
080        public String getAcc() {
081                return acc;
082        }
083        public void setAcc(String acc) {
084                this.acc = acc;
085        }
086        public Integer getDcl() {
087                return dcl;
088        }
089        public void setDcl(Integer dcl) {
090                this.dcl = dcl;
091        }
092        public String getName() {
093                return name;
094        }
095        public void setName(String name) {
096                this.name = name;
097        }
098        public Integer getNdom() {
099                return ndom;
100        }
101        public void setNdom(Integer ndom) {
102                this.ndom = ndom;
103        }
104        public Integer getNreported() {
105                return nreported;
106        }
107        public void setNreported(Integer nreported) {
108                this.nreported = nreported;
109        }
110        @Override
111        public String toString() {
112                return "HmmerResult [acc=" + acc + ", desc=" + desc + ", score=" + score + ", evalue="
113                                + evalue + ", pvalue=" + pvalue + ", dcl="
114                                + dcl + ", name=" + name + ", ndom=" + ndom + ", nreported="
115                                + nreported + ", domains=" + domains + "]";
116        }
117
118
119        @Override
120        public int compareTo(HmmerResult o) {
121                //      sort  by the start position of the first domain
122
123                if ( emptyDomains(this) && emptyDomains(o)){
124                        return 0;
125                }
126
127                if ( ! emptyDomains(this) && emptyDomains(o))
128                        return -1;
129
130                if ( emptyDomains(this) && (! emptyDomains(o)))
131                        return 1;
132
133                // ok when we are here, both domains are not empty
134
135                HmmerDomain me = this.getDomains().first();
136                HmmerDomain other = o.getDomains().first();
137
138                //System.out.println(" domains: " + me.getHmmAcc() + " " + other.getHmmAcc()+ " " + me.getSqFrom().compareTo(other.getSqFrom()));
139
140                return(me.getSqFrom().compareTo(other.getSqFrom()));
141        }
142        private boolean emptyDomains(HmmerResult o) {
143                if ( o.getDomains() == null || o.getDomains().size() == 0)
144                        return true;
145                return false;
146        }
147
148
149        /** Get the overlap between two HmmerResult objects
150         *
151         * @param other
152         * @return 0 if no overlap, otherwise the length of the overlap
153         */
154        public int getOverlapLength(HmmerResult other){
155
156                int overlap = 0;
157                for ( HmmerDomain d1 : getDomains()){
158                        for (HmmerDomain d2 : other.getDomains()){
159                                overlap += getOverlap(d1, d2);
160                        }
161                }
162                return overlap;
163
164        }
165
166        private int getOverlap(HmmerDomain one, HmmerDomain other){
167                int xs = one.getSqFrom();
168                int ys = one.getSqTo();
169                int as = other.getSqFrom();
170                int bs = other.getSqTo();
171
172                int overlap = 0;
173                //1:
174
175                if ((( xs< as)  && ( as<ys)) || ((xs < bs) && ( bs <= ys)) || (as<xs && ys<bs)) {
176
177                        //2:
178
179                        if ( xs < as) {
180                                if ( ys < bs)
181                                        overlap = ys-as;
182                                else
183                                        overlap = bs-as;
184                        } else {
185                                if  ( ys < bs)
186                                        overlap = ys -xs;
187                                else
188                                        overlap = bs - xs;
189
190                        }
191
192                }
193
194                return overlap;
195        }
196
197}