1 |
package sequence; |
2 |
|
3 |
import java.sql.SQLException; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Iterator; |
6 |
|
7 |
import proteinstructure.PdbCodeNotFoundError; |
8 |
import proteinstructure.PdbLoadError; |
9 |
import proteinstructure.TemplateList; |
10 |
|
11 |
import tools.MySQLConnection; |
12 |
|
13 |
/** |
14 |
* A convenience class for a list of GTGHits |
15 |
* |
16 |
*/ |
17 |
public class GTGHitList implements Iterable<GTGHit> { |
18 |
|
19 |
private ArrayList<GTGHit> hits; |
20 |
|
21 |
private int queryLength; |
22 |
|
23 |
public GTGHitList() { |
24 |
this.hits = new ArrayList<GTGHit>(); |
25 |
} |
26 |
|
27 |
public void add(GTGHit hit) { |
28 |
hits.add(hit); |
29 |
} |
30 |
|
31 |
public int size() { |
32 |
return this.hits.size(); |
33 |
} |
34 |
|
35 |
public void printTabular() { |
36 |
for (GTGHit hit:this.hits) { |
37 |
hit.print(); |
38 |
} |
39 |
} |
40 |
|
41 |
/** |
42 |
* Returns and iterator over this GTG hits list |
43 |
* @return |
44 |
*/ |
45 |
public Iterator<GTGHit> iterator() { |
46 |
return this.hits.iterator(); |
47 |
} |
48 |
|
49 |
/** |
50 |
* Reassigns the serials of subjectStart and subjectEnd positions based on the |
51 |
* pdbase full SEQRES sequence, i.e. converts GTG serials to our internal residue |
52 |
* serials (cif serials) |
53 |
* @param conn |
54 |
* @param pdbaseDb |
55 |
* @throws SQLException |
56 |
* @throws PdbCodeNotFoundError |
57 |
* @throws PdbLoadError |
58 |
* @see GTGHit.reassignSubjectSerials |
59 |
*/ |
60 |
public void reassignSubjectSerials(MySQLConnection conn, String pdbaseDb) throws SQLException, PdbCodeNotFoundError, PdbLoadError { |
61 |
for (GTGHit hit: this) { |
62 |
hit.reassignSubjectSerials(conn, pdbaseDb); |
63 |
} |
64 |
} |
65 |
|
66 |
/** |
67 |
* Prints a tabular overview of the hits in this list. |
68 |
* Currently, if the query length is set, (i.e. > 0) a simple ascii-art |
69 |
* overview of the matches is printed for each hit. |
70 |
*/ |
71 |
public void print() { |
72 |
printSome(this.size()); |
73 |
} |
74 |
|
75 |
/** |
76 |
* Prints a tabular overview of the first numHits hits in this list. |
77 |
* Currently, if the query length is set, (i.e. > 0) a simple ascii-art |
78 |
* overview of the matches is printed for each hit. |
79 |
* @param numHits |
80 |
*/ |
81 |
public void printSome(int numHits) { |
82 |
|
83 |
int outputLength = 80; // length of graphical output in screen columns |
84 |
|
85 |
if(queryLength > 0) { |
86 |
double scaleFactor = 1.0 * outputLength / queryLength; |
87 |
GTGHit.printHeaderWithOverview(queryLength, scaleFactor); |
88 |
for (int i = 0; i < Math.min(hits.size(), numHits); i++) { |
89 |
GTGHit hit = hits.get(i); |
90 |
hit.printWithOverview(scaleFactor); |
91 |
} |
92 |
} else { |
93 |
// print without graphical overview |
94 |
for (int i = 0; i < Math.min(hits.size(), numHits); i++) { |
95 |
GTGHit hit = hits.get(i); |
96 |
hit.print(); |
97 |
} |
98 |
} |
99 |
} |
100 |
|
101 |
/** |
102 |
* Set the query length needed by the print() and printSome() methods. |
103 |
* @param l |
104 |
*/ |
105 |
public void setQueryLength(int l) { |
106 |
this.queryLength = l; |
107 |
} |
108 |
|
109 |
/** |
110 |
* Applies an total score cutoff trimming out of this list all hits with totalScore |
111 |
* lower than given cutoff |
112 |
* @param totalScoreCutoff |
113 |
*/ |
114 |
public void applyCutoff(int totalScoreCutoff) { |
115 |
Iterator<GTGHit> it = this.iterator(); |
116 |
while (it.hasNext()) { |
117 |
if (it.next().totalScore<=totalScoreCutoff) { |
118 |
it.remove(); |
119 |
} |
120 |
} |
121 |
} |
122 |
|
123 |
/** |
124 |
* Filters out from this GTGHitList PDB hits released after the given date |
125 |
* (in format yyyymmdd, e.g. 20060425) |
126 |
* @param conn |
127 |
* @param pdbaseDb |
128 |
* @param date date in format yyyymmdd |
129 |
* @throws SQLException |
130 |
*/ |
131 |
public void filterByMaxReleaseDate(MySQLConnection conn, String pdbaseDb, String date) throws SQLException { |
132 |
String[] filteredIds = TemplateList.filterIdsByMaxReleaseDate(conn, pdbaseDb, this.getTemplateIds(), date); |
133 |
for (String filteredId:filteredIds) { |
134 |
this.hits.remove(this.getHit(filteredId)); |
135 |
} |
136 |
} |
137 |
|
138 |
/** |
139 |
* Filters out hits that rank below given rank. |
140 |
* @param rank |
141 |
*/ |
142 |
public void filterByMaxRank(int rank) { |
143 |
Iterator<GTGHit> it = this.iterator(); |
144 |
int i=1; |
145 |
while (it.hasNext()) { |
146 |
it.next(); |
147 |
if (i>rank) it.remove(); |
148 |
i++; |
149 |
} |
150 |
} |
151 |
|
152 |
/** |
153 |
* Returns the hit with the best totalScore or null if this hit list is empty. |
154 |
* @return |
155 |
*/ |
156 |
public GTGHit getBestHit() { |
157 |
if(this.size() == 0) return null; |
158 |
GTGHit bestHit = this.hits.get(0); |
159 |
for(GTGHit hit:hits) { |
160 |
if(hit.totalScore > bestHit.totalScore) { |
161 |
bestHit = hit; |
162 |
} |
163 |
} |
164 |
return bestHit; |
165 |
} |
166 |
|
167 |
/** |
168 |
* Gets a GTGHit given its subjectId or null if GTGHit not present in this list |
169 |
* @param templateId |
170 |
* @return |
171 |
*/ |
172 |
public GTGHit getHit(String templateId) { |
173 |
for (GTGHit hit:this) { |
174 |
if (hit.getTemplateId().equals(templateId)) return hit; |
175 |
} |
176 |
return null; |
177 |
} |
178 |
|
179 |
/** |
180 |
* Returns a list of template ids (subject ids) as concatenated |
181 |
* pdbCodes+pdbChainCodes e.g. 1abcA |
182 |
* @return |
183 |
*/ |
184 |
public String[] getTemplateIds() { |
185 |
String[] ids = new String[this.size()]; |
186 |
for (int i=0;i<this.size();i++) { |
187 |
ids[i] = this.hits.get(i).getTemplateId(); |
188 |
} |
189 |
return ids; |
190 |
} |
191 |
|
192 |
} |