1c156cdd9301a9f4b2da38902beee2a9539bec16
[jalview.git] / src / jalview / analysis / SequenceIdMatcher.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 */
19
20 package jalview.analysis;
21
22 import java.util.Vector;
23 import java.util.Hashtable;
24 import jalview.datamodel.SequenceI;
25
26 /**
27  * <p>Title: </p>
28  * SequenceIdMatcher
29  * <p>Description: </p>
30  * Routine which does approximate Sequence Id resolution by name using string containment rather than equivalence
31  * <p>Copyright: Copyright (c) 2004</p>
32  *
33  * <p>Company: Dundee University</p>
34  *
35  * @author not attributable
36  * @version 1.0
37  */
38 public class SequenceIdMatcher
39 {
40
41   private class SeqIdName
42   {
43     String id;
44
45     SeqIdName(String s)
46     {
47       id = new String(s);
48     }
49
50     public int hashCode()
51     {
52       return (id.substring(0, 4).hashCode());
53     }
54
55     public boolean equals(Object s)
56     {
57       if (s instanceof SeqIdName)
58       {
59         return this.equals( (SeqIdName) s);
60       }
61       else
62       {
63         if (s instanceof String)
64         {
65           return this.equals( (String) s);
66         }
67       }
68       return false;
69     }
70
71     public boolean equals(SeqIdName s)
72     {
73       if (id.startsWith(s.id) || s.id.startsWith(id))
74       {
75         return true;
76       }
77       return false;
78     }
79
80     public boolean equals(String s)
81     {
82       if (id.startsWith(s) || s.startsWith(id))
83       {
84         return true;
85       }
86       return false;
87     }
88   }
89
90   private Hashtable names;
91
92   public SequenceIdMatcher(SequenceI[] seqs)
93   {
94     names = new Hashtable();
95     for (int i = 0; i < seqs.length; i++)
96     {
97       names.put(new SeqIdName(seqs[i].getName()), seqs[i]);
98     }
99   }
100
101   SequenceI findIdMatch(SequenceI seq)
102   {
103     SeqIdName nam = new SeqIdName(seq.getName());
104     if (names.containsKey(nam))
105     {
106       return (SequenceI) names.get(nam);
107     }
108     return null;
109   }
110
111   SequenceI findIdMatch(String seqnam)
112   {
113     SeqIdName nam = new SeqIdName(seqnam);
114     if (names.containsKey(nam))
115     {
116       return (SequenceI) names.get(nam);
117     }
118     return null;
119   }
120
121   /**
122    * findIdMatch
123    *
124    * Return pointers to sequences (or sequence object containers)
125    * which have same Id as a given set of different sequence objects
126    *
127    * @param seqs SequenceI[]
128    * @return SequenceI[]
129    */
130
131   SequenceI[] findIdMatch(SequenceI[] seqs)
132   {
133     SequenceI[] namedseqs = new SequenceI[seqs.length];
134
135     int i = 0;
136     SeqIdName nam;
137     if (seqs.length > 0)
138     {
139       do
140       {
141         nam = new SeqIdName(seqs[i].getName());
142         if (names.containsKey(nam))
143         {
144           namedseqs[i] = (SequenceI) names.get(nam);
145         }
146         else
147         {
148           namedseqs[i] = null;
149         }
150       }
151       while (i++ < seqs.length);
152     }
153     return namedseqs;
154   }
155
156 }