1 /* AlignMatch.java
2  *
3  * created: Wed Jul 14 1999
4  *
5  * This file is part of Artemis
6  *
7  * Copyright(C) 1999,2000  Genome Research Limited
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or(at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/AlignMatch.java,v 1.7 2005-12-13 10:33:29 tjc Exp $
24  */
25 
26 package uk.ac.sanger.artemis;
27 
28 import uk.ac.sanger.artemis.io.Range;
29 import uk.ac.sanger.artemis.util.OutOfRangeException;
30 
31 /**
32  *  Each object of this class represents a single match from an alignment.
33  *
34  *  @author Kim Rutherford
35  *  @version $Id: AlignMatch.java,v 1.7 2005-12-13 10:33:29 tjc Exp $
36  **/
37 
38 public class AlignMatch
39 {
40   /** The range of the match in the subject sequence. */
41   private Range subject_sequence_range = null;
42 
43   /** The range of the match in the query sequence. */
44   private Range query_sequence_range = null;
45 
46   /** The score that was passed to the constructor. */
47   private int score = -1;
48 
49   /**
50    *  The percent identity that was passed to the constructor.
51    **/
52   private int percent_id = -1;
53 
54   /**
55    *  true if and only if the query hits the reverse complement of the subject
56    **/
57   private boolean rev_match;
58 
59   private int match_length;
60 
61   /**
62    *  Create a new AlignMatch object.
63    *  @param rev_match true if and only if the query hits the reverse
64    *    complement of the subject.
65    *  @param score the score for this match, which should be -1 if and only if
66    *    this match has no score.  The parameter must be >= -1.
67    **/
AlignMatch(final Range subject_sequence_range, final Range query_sequence_range, final boolean rev_match, final int score, final int percent_id)68   public AlignMatch(final Range subject_sequence_range,
69                     final Range query_sequence_range,
70                     final boolean rev_match,
71                     final int score,
72                     final int percent_id)
73   {
74     this.subject_sequence_range = subject_sequence_range;
75     this.query_sequence_range   = query_sequence_range;
76     this.rev_match              = rev_match;
77     this.score                  = score;
78     this.percent_id             = percent_id;
79 
80     match_length = Math.abs(getSubjectSequenceStart() -
81                             getSubjectSequenceEnd());
82   }
83 
copy(AlignMatch m)84   public static AlignMatch copy(AlignMatch m)
85   {
86     return new AlignMatch(m.subject_sequence_range,
87                           m.query_sequence_range,
88                           m.rev_match,
89                           m.score,
90                           m.percent_id);
91   }
92 
getLength()93   public int getLength()
94   {
95     return match_length;
96   }
97 
98   /**
99    *  Return the start(base) of the match in the subject sequence.
100    **/
getSubjectSequenceStart()101   public int getSubjectSequenceStart()
102   {
103     return subject_sequence_range.getStart();
104   }
105 
106   /**
107    *  Return the end(base) of the match in the subject sequence.
108    **/
getSubjectSequenceEnd()109   public int getSubjectSequenceEnd()
110   {
111     return subject_sequence_range.getEnd();
112   }
113 
114   /**
115    *  Return the start(base) of the match in the query sequence.
116    **/
getQuerySequenceStart()117   public int getQuerySequenceStart()
118   {
119     if(rev_match)
120       return query_sequence_range.getEnd();
121 
122     return query_sequence_range.getStart();
123   }
124 
125   /**
126    *  Return the end(base) of the match in the query sequence.
127    **/
getQuerySequenceEnd()128   public int getQuerySequenceEnd()
129   {
130     if(rev_match)
131       return query_sequence_range.getStart();
132 
133     return query_sequence_range.getEnd();
134   }
135 
136   /**
137    *  Return the Range of the match in the subject sequence.
138    **/
getSubjectSequenceRange()139   public Range getSubjectSequenceRange()
140   {
141     return subject_sequence_range;
142   }
143 
144   /**
145    *  Return the Range of the match in the query sequence.
146    **/
getQuerySequenceRange()147   public Range getQuerySequenceRange()
148   {
149     return query_sequence_range;
150   }
151 
152   /**
153    * Set the range for either the query or the subject match. This
154    * is used when flipping contigs round.
155    * @param start
156    * @param end
157    * @param subject
158    */
setRange(final int start, final int end, boolean subject, boolean flip)159   public void setRange(final int start, final int end,
160                        boolean subject, boolean flip)
161   {
162     try
163     {
164       if(subject)
165       {
166 	if(start < end)
167 	  this.subject_sequence_range = new Range(start, end);
168 	else
169           this.subject_sequence_range = new Range(end, start);
170       }
171       else
172       {
173         if(start < end)
174          this.query_sequence_range = new Range(start, end);
175         else
176           this.query_sequence_range = new Range(end, start);
177       }
178     }
179     catch(OutOfRangeException ex)
180     {
181       ex.printStackTrace();
182     }
183 
184     if(flip)
185       this.rev_match = !this.rev_match;
186   }
187 
188   /**
189    *  Returns true if and only if the query hits the reverse complement of the
190    *  subject.
191    **/
isRevMatch()192   public boolean isRevMatch()
193   {
194     return rev_match;
195   }
196 
197   /**
198    *  Return the score of this match.
199    **/
getScore()200   public int getScore()
201   {
202     return score;
203   }
204 
205   /**
206    *  Return the percent identity of this match.
207    **/
getPercentID()208   public int getPercentID()
209   {
210     return percent_id;
211   }
212 
213   /**
214    *  Return true if and only if this is a self match(ie query start ==
215    *  subject start && query end == subject end)
216    **/
isSelfMatch()217   public boolean isSelfMatch()
218   {
219     if(getQuerySequenceStart() == getSubjectSequenceStart() &&
220        getQuerySequenceEnd() == getSubjectSequenceEnd())
221       return true;
222     else
223       return false;
224   }
225 }
226