Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.util

File DBRefUtils.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
33% of files have more coverage

Code metrics

130
178
22
2
708
406
154
0.87
8.09
11
7

Classes

Class Line # Actions
DBRefUtils 41 177 153
0.59756159.8%
DBRefUtils.DbRefComp 258 1 1
0.00%
 

Contributing tests

This file is covered by 164 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.util;
22   
23    import java.util.ArrayList;
24    import java.util.BitSet;
25    import java.util.HashMap;
26    import java.util.HashSet;
27    import java.util.List;
28    import java.util.Map;
29   
30    import com.stevesoft.pat.Regex;
31   
32    import jalview.datamodel.DBRefEntry;
33    import jalview.datamodel.DBRefSource;
34    import jalview.datamodel.Mapping;
35    import jalview.datamodel.PDBEntry;
36    import jalview.datamodel.SequenceI;
37   
38    /**
39    * Utilities for handling DBRef objects and their collections.
40    */
 
41    public class DBRefUtils
42    {
43    /*
44    * lookup from lower-case form of a name to its canonical (standardised) form
45    */
46    private static Map<String, String> canonicalSourceNameLookup = new HashMap<>();
47   
48    public final static int DB_SOURCE = 1;
49    public final static int DB_VERSION = 2;
50    public final static int DB_ID = 4;
51    public final static int DB_MAP = 8;
52   
53    public final static int SEARCH_MODE_NO_MAP_NO_VERSION = DB_SOURCE | DB_ID;
54    public final static int SEARCH_MODE_FULL = DB_SOURCE | DB_VERSION | DB_ID | DB_MAP;
55   
 
56  18 toggle static
57    {
58    // TODO load these from a resource file?
59  18 canonicalSourceNameLookup.put("uniprotkb/swiss-prot", DBRefSource.UNIPROT);
60  18 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
61   
62    // Ensembl values for dbname in xref REST service:
63  18 canonicalSourceNameLookup.put("uniprot/sptrembl", DBRefSource.UNIPROT);
64  18 canonicalSourceNameLookup.put("uniprot/swissprot", DBRefSource.UNIPROT);
65   
66  18 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
67  18 canonicalSourceNameLookup.put("ensembl", DBRefSource.ENSEMBL);
68    // Ensembl Gn and Tr are for Ensembl genomic and transcript IDs as served
69    // from ENA.
70  18 canonicalSourceNameLookup.put("ensembl-tr", DBRefSource.ENSEMBL);
71  18 canonicalSourceNameLookup.put("ensembl-gn", DBRefSource.ENSEMBL);
72   
73    // guarantee we always have lowercase entries for canonical string lookups
74  18 for (String k : canonicalSourceNameLookup.keySet())
75    {
76  144 canonicalSourceNameLookup.put(k.toLowerCase(),
77    canonicalSourceNameLookup.get(k));
78    }
79    }
80   
81    /**
82    * Returns those DBRefEntry objects whose source identifier (once converted to
83    * Jalview's canonical form) is in the list of sources to search for. Returns
84    * null if no matches found.
85    *
86    * @param dbrefs DBRefEntry objects to search
87    * @param sources array of sources to select
88    * @return
89    */
 
90  9961 toggle public static List<DBRefEntry> selectRefs(List<DBRefEntry> dbrefs, String[] sources)
91    {
92  9961 if (dbrefs == null || sources == null)
93    {
94  5921 return dbrefs;
95    }
96   
97    // BH TODO (what?)
98  4040 HashSet<String> srcs = new HashSet<String>();
99  4040 for (String src : sources)
100    {
101  17890 srcs.add(src.toUpperCase());
102    }
103   
104  4040 int nrefs = dbrefs.size();
105  4040 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
106  29686 for (int ib = 0; ib < nrefs; ib++)
107    {
108  25646 DBRefEntry dbr = dbrefs.get(ib);
109  25646 String source = getCanonicalName(dbr.getSource());
110  25646 if (srcs.contains(source.toUpperCase()))
111    {
112  4138 res.add(dbr);
113    }
114    }
115  4040 if (res.size() > 0)
116    {
117    // List<DBRefEntry> reply = new DBRefEntry[res.size()];
118  2320 return res;// .toArray(reply);
119    }
120  1720 return null;
121    }
122   
 
123  31 toggle private static boolean selectRefsBS(List<DBRefEntry> dbrefs, int sourceKeys, BitSet bsSelect)
124    {
125  31 if (dbrefs == null || sourceKeys == 0)
126    {
127  0 return false;
128    }
129  635 for (int i = 0, n = dbrefs.size(); i < n; i++)
130    {
131  604 DBRefEntry dbr = dbrefs.get(i);
132  604 if ((dbr.getSourceKey() & sourceKeys) != 0)
133    {
134  175 bsSelect.clear(i);
135    }
136    }
137  31 return !bsSelect.isEmpty();
138    }
139   
140    /**
141    * Returns a (possibly empty) list of those references that match the given
142    * entry, according to the given comparator.
143    *
144    * @param refs
145    * an array of database references to search
146    * @param entry
147    * an entry to compare against
148    * @param comparator
149    * @return
150    */
 
151  0 toggle static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
152    DbRefComp comparator)
153    {
154  0 List<DBRefEntry> rfs = new ArrayList<>();
155  0 if (refs == null || entry == null)
156    {
157  0 return rfs;
158    }
159  0 for (int i = 0; i < refs.length; i++)
160    {
161  0 if (comparator.matches(entry, refs[i]))
162    {
163  0 rfs.add(refs[i]);
164    }
165    }
166  0 return rfs;
167    }
168   
169    /**
170    * look up source in an internal list of database reference sources and return
171    * the canonical jalview name for the source, or the original string if it has
172    * no canonical form.
173    *
174    * @param source
175    * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*) or
176    * original source
177    */
 
178  316254 toggle public static String getCanonicalName(String source)
179    {
180  316254 if (source == null)
181    {
182  1 return null;
183    }
184  316253 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
185  316253 return canonical == null ? source : canonical;
186    }
187   
188    /**
189    * Returns a (possibly empty) list of those references that match the given
190    * entry. Currently uses a comparator which matches if
191    * <ul>
192    * <li>database sources are the same</li>
193    * <li>accession ids are the same</li>
194    * <li>both have no mapping, or the mappings are the same</li>
195    * </ul>
196    *
197    * @param ref Set of references to search
198    * @param entry pattern to match
199    * @param mode SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
200    * @return
201    */
 
202  5839 toggle public static List<DBRefEntry> searchRefs(List<DBRefEntry> ref, DBRefEntry entry, int mode) {
203  5839 return searchRefs(ref, entry, matchDbAndIdAndEitherMapOrEquivalentMapList, mode);
204    }
205   
206    /**
207    * Returns a list of those references that match the given accession id
208    * <ul>
209    * <li>database sources are the same</li>
210    * <li>accession ids are the same</li>
211    * <li>both have no mapping, or the mappings are the same</li>
212    * </ul>
213    *
214    * @param refs Set of references to search
215    * @param accId accession id to match
216    * @return
217    */
 
218  4 toggle public static List<DBRefEntry> searchRefs(List<DBRefEntry> refs, String accId) {
219  4 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
220  4 if (refs == null || accId == null) {
221  0 return rfs;
222    }
223  12 for (int i = 0, n = refs.size(); i < n; i++) {
224  8 DBRefEntry e = refs.get(i);
225  8 if (accId.equals(e.getAccessionId())) {
226  6 rfs.add(e);
227    }
228    }
229  4 return rfs;
230    // return searchRefs(refs, new DBRefEntry("", "", accId), matchId, SEARCH_MODE_FULL);
231    }
232   
233    /**
234    * Returns a (possibly empty) list of those references that match the given
235    * entry, according to the given comparator.
236    *
237    * @param refs an array of database references to search
238    * @param entry an entry to compare against
239    * @param comparator
240    * @param mode SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION
241    * optional
242    * @return
243    */
 
244  5839 toggle static List<DBRefEntry> searchRefs(List<DBRefEntry> refs, DBRefEntry entry, DbRefComp comparator, int mode) {
245  5839 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
246  5839 if (refs == null || entry == null) {
247  0 return rfs;
248    }
249  137788 for (int i = 0, n = refs.size(); i < n; i++) {
250  131949 DBRefEntry e = refs.get(i);
251  131949 if (comparator.matches(entry, e, SEARCH_MODE_FULL)) {
252  1451 rfs.add(e);
253    }
254    }
255  5839 return rfs;
256    }
257   
 
258    interface DbRefComp {
 
259  0 toggle default public boolean matches(DBRefEntry refa, DBRefEntry refb) {
260  0 return matches(refa, refb, SEARCH_MODE_FULL);
261    };
262   
263    public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode);
264    }
265   
266    /**
267    * match on all non-null fields in refa
268    */
269    // TODO unused - remove? would be broken by equating "" with null
270    public static DbRefComp matchNonNullonA = new DbRefComp() {
 
271  0 toggle @Override
272    public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
273  0 if ((mode & DB_SOURCE) != 0 &&
274    (refa.getSource() == null || DBRefUtils.getCanonicalName(refb.getSource())
275    .equals(DBRefUtils.getCanonicalName(refa.getSource())))) {
276  0 if ((mode & DB_VERSION) != 0 &&
277    (refa.getVersion() == null || refb.getVersion().equals(refa.getVersion()))) {
278  0 if ((mode & DB_ID) != 0 &&
279    (refa.getAccessionId() == null || refb.getAccessionId().equals(refa.getAccessionId()))) {
280  0 if ((mode & DB_MAP) != 0 &&
281    (refa.getMap() == null || (refb.getMap() != null && refb.getMap().equals(refa.getMap())))) {
282  0 return true;
283    }
284    }
285    }
286    }
287  0 return false;
288    }
289    };
290   
291    /**
292    * either field is null or field matches for all of source, version, accession
293    * id and map.
294    */
295    // TODO unused - remove?
296    public static DbRefComp matchEitherNonNull = new DbRefComp() {
 
297  0 toggle @Override
298    public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
299  0 if (nullOrEqualSource(refa.getSource(), refb.getSource())
300    && nullOrEqual(refa.getVersion(), refb.getVersion())
301    && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
302    && nullOrEqual(refa.getMap(), refb.getMap())) {
303  0 return true;
304    }
305  0 return false;
306    }
307   
308    };
309   
310    /**
311    * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
312    * database is PDB.
313    * <p>
314    * Used by file parsers to generate DBRefs from annotation within file (eg
315    * Stockholm)
316    *
317    * @param dbname
318    * @param version
319    * @param acn
320    * @param seq
321    * where to annotate with reference
322    * @return parsed version of entry that was added to seq (if any)
323    */
 
324  1539 toggle public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
325    String version, String acn)
326    {
327  1539 DBRefEntry ref = null;
328  1539 if (dbname != null)
329    {
330  1539 String locsrc = DBRefUtils.getCanonicalName(dbname);
331  1539 if (locsrc.equals(DBRefSource.PDB))
332    {
333    /*
334    * Check for PFAM style stockhom PDB accession id citation e.g.
335    * "1WRI A; 7-80;"
336    */
337  27 Regex r = new com.stevesoft.pat.Regex(
338    "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
339  27 if (r.search(acn.trim()))
340    {
341  27 String pdbid = r.stringMatched(1);
342  27 String chaincode = r.stringMatched(2);
343  27 if (chaincode == null)
344    {
345  0 chaincode = " ";
346    }
347    // String mapstart = r.stringMatched(3);
348    // String mapend = r.stringMatched(4);
349  27 if (chaincode.equals(" "))
350    {
351  0 chaincode = "_";
352    }
353    // construct pdb ref.
354  27 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
355  27 PDBEntry pdbr = new PDBEntry();
356  27 pdbr.setId(pdbid);
357  27 pdbr.setType(PDBEntry.Type.PDB);
358  27 pdbr.setChainCode(chaincode);
359  27 seq.addPDBId(pdbr);
360    }
361    else
362    {
363  0 System.err.println("Malformed PDB DR line:" + acn);
364    }
365    }
366    else
367    {
368    // default:
369  1512 ref = new DBRefEntry(locsrc, version, acn.trim());
370    }
371    }
372  1539 if (ref != null)
373    {
374  1539 seq.addDBRef(ref);
375    }
376  1539 return ref;
377    }
378   
379    /**
380    * accession ID and DB must be identical. Version is ignored. Map is either not
381    * defined or is a match (or is compatible?)
382    */
383    // TODO unused - remove?
384    public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp() {
 
385  0 toggle @Override
386    public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
387  0 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
388    .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
389    // We dont care about version
390  0 if (refa.getAccessionId() != null && refb.getAccessionId() != null
391    // FIXME should be && not || here?
392    || refb.getAccessionId().equals(refa.getAccessionId())) {
393  0 if ((refa.getMap() == null || refb.getMap() == null) || (refa.getMap() != null
394    && refb.getMap() != null && refb.getMap().equals(refa.getMap()))) {
395  0 return true;
396    }
397    }
398    }
399  0 return false;
400    }
401    };
402   
403    /**
404    * accession ID and DB must be identical. Version is ignored. No map on either
405    * or map but no maplist on either or maplist of map on a is the complement of
406    * maplist of map on b.
407    */
408    // TODO unused - remove?
409    public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp() {
 
410  0 toggle @Override
411    public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
412  0 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
413    .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
414    // We dont care about version
415  0 if (refa.getAccessionId() != null && refb.getAccessionId() != null
416    || refb.getAccessionId().equals(refa.getAccessionId())) {
417  0 if ((refa.getMap() == null && refb.getMap() == null)
418    || (refa.getMap() != null && refb.getMap() != null)) {
419  0 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
420    || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
421    && refb.getMap().getMap().getInverse().equals(refa.getMap().getMap()))) {
422  0 return true;
423    }
424    }
425    }
426    }
427  0 return false;
428    }
429    };
430   
431    /**
432    * accession ID and DB must be identical. Version is ignored. No map on both or
433    * or map but no maplist on either or maplist of map on a is equivalent to the
434    * maplist of map on b.
435    */
436    // TODO unused - remove?
437    public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp() {
 
438  0 toggle @Override
439    public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
440  0 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
441    .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
442    // We dont care about version
443    // if ((refa.getVersion()==null || refb.getVersion()==null)
444    // || refb.getVersion().equals(refa.getVersion()))
445    // {
446  0 if (refa.getAccessionId() != null && refb.getAccessionId() != null
447    || refb.getAccessionId().equals(refa.getAccessionId())) {
448  0 if (refa.getMap() == null && refb.getMap() == null) {
449  0 return true;
450    }
451  0 if (refa.getMap() != null && refb.getMap() != null
452    && ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
453    || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
454    && refb.getMap().getMap().equals(refa.getMap().getMap())))) {
455  0 return true;
456    }
457    }
458    }
459  0 return false;
460    }
461    };
462   
463    /**
464    * accession ID and DB must be identical, or null on a. Version is ignored. No
465    * map on either or map but no maplist on either or maplist of map on a is
466    * equivalent to the maplist of map on b.
467    */
468    public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
469    {
 
470  131949 toggle @Override
471    public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
472    {
473  131949 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
474    .equals(DBRefUtils.getCanonicalName(refa.getSource())))
475    {
476    // We dont care about version
477  16257 if (refa.getAccessionId() == null || refa.getAccessionId().equals(refb.getAccessionId()))
478    {
479  1496 if (refa.getMap() == null || refb.getMap() == null)
480    {
481  1450 return true;
482    }
483  46 if ((refa.getMap() != null && refb.getMap() != null)
484    && (refb.getMap().getMap() == null && refa.getMap().getMap() == null)
485    || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
486    && (refb.getMap().getMap().equals(refa.getMap().getMap()))))
487    {
488  1 return true;
489    }
490    }
491    }
492  130498 return false;
493    }
494    };
495   
496    /**
497    * Returns the (possibly empty) list of those supplied dbrefs which have the
498    * specified source database, with a case-insensitive match of source name
499    *
500    * @param dbRefs
501    * @param source
502    * @return
503    */
 
504  0 toggle public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
505    String source)
506    {
507  0 List<DBRefEntry> matches = new ArrayList<>();
508  0 if (dbRefs != null && source != null)
509    {
510  0 for (DBRefEntry dbref : dbRefs)
511    {
512  0 if (source.equalsIgnoreCase(dbref.getSource()))
513    {
514  0 matches.add(dbref);
515    }
516    }
517    }
518  0 return matches;
519    }
520   
521    /**
522    * Returns true if either object is null, or they are equal
523    *
524    * @param o1
525    * @param o2
526    * @return
527    */
 
528  0 toggle public static boolean nullOrEqual(Object o1, Object o2) {
529  0 if (o1 == null || o2 == null) {
530  0 return true;
531    }
532  0 return o1.equals(o2);
533    }
534   
535    /**
536    * canonicalise source string before comparing. null is always wildcard
537    *
538    * @param o1 - null or source string to compare
539    * @param o2 - null or source string to compare
540    * @return true if either o1 or o2 are null, or o1 equals o2 under
541    * DBRefUtils.getCanonicalName
542    * (o1).equals(DBRefUtils.getCanonicalName(o2))
543    */
 
544  0 toggle public static boolean nullOrEqualSource(String o1, String o2) {
545  0 if (o1 == null || o2 == null) {
546  0 return true;
547    }
548  0 return DBRefUtils.getCanonicalName(o1).equals(DBRefUtils.getCanonicalName(o2));
549    }
550   
551    /**
552    * Selects just the DNA or protein references from a set of references
553    *
554    * @param selectDna if true, select references to 'standard' DNA databases, else
555    * to 'standard' peptide databases
556    * @param refs a set of references to select from
557    * @return
558    */
 
559  9856 toggle public static List<DBRefEntry> selectDbRefs(boolean selectDna, List<DBRefEntry> refs) {
560  9856 return selectRefs(refs, selectDna ? DBRefSource.DNACODINGDBS : DBRefSource.PROTEINDBS);
561    // could attempt to find other cross
562    // refs here - ie PDB xrefs
563    // (not dna, not protein seq)
564    }
565   
566    /**
567    * Returns the (possibly empty) list of those supplied dbrefs which have the
568    * specified source database, with a case-insensitive match of source name
569    *
570    * @param dbRefs
571    * @param source
572    * @return
573    */
 
574  47 toggle public static List<DBRefEntry> searchRefsForSource(List<DBRefEntry> dbRefs, String source) {
575  47 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
576  47 if (dbRefs != null && source != null) {
577  45 for (DBRefEntry dbref : dbRefs) {
578  134 if (source.equalsIgnoreCase(dbref.getSource())) {
579  46 matches.add(dbref);
580    }
581    }
582    }
583  47 return matches;
584    }
585   
586    /**
587    * promote direct database references to primary for nucleotide or protein
588    * sequences if they have an appropriate primary ref
589    * <table>
590    * <tr>
591    * <th>Seq Type</th>
592    * <th>Primary DB</th>
593    * <th>Direct which will be promoted</th>
594    * </tr>
595    * <tr align=center>
596    * <td>peptides</td>
597    * <td>Ensembl</td>
598    * <td>Uniprot</td>
599    * </tr>
600    * <tr align=center>
601    * <td>peptides</td>
602    * <td>Ensembl</td>
603    * <td>Uniprot</td>
604    * </tr>
605    * <tr align=center>
606    * <td>dna</td>
607    * <td>Ensembl</td>
608    * <td>ENA</td>
609    * </tr>
610    * </table>
611    *
612    * @param sequence
613    */
 
614  298 toggle public static void ensurePrimaries(SequenceI sequence, List<DBRefEntry> pr) {
615  298 if (pr.size() == 0) {
616    // nothing to do
617  268 return;
618    }
619  30 int sstart = sequence.getStart();
620  30 int send = sequence.getEnd();
621  30 boolean isProtein = sequence.isProtein();
622  30 BitSet bsSelect = new BitSet();
623   
624    // List<DBRefEntry> selfs = new ArrayList<DBRefEntry>();
625    // {
626   
627    // List<DBRefEntry> selddfs = selectDbRefs(!isprot, sequence.getDBRefs());
628    // if (selfs == null || selfs.size() == 0)
629    // {
630    // // nothing to do
631    // return;
632    // }
633   
634  30 List<DBRefEntry> dbrefs = sequence.getDBRefs();
635  30 bsSelect.set(0, dbrefs.size());
636   
637  30 if (!selectRefsBS(dbrefs, isProtein ? DBRefSource.PROTEIN_MASK : DBRefSource.DNA_CODING_MASK, bsSelect))
638  2 return;
639   
640    // selfs.addAll(selfArray);
641    // }
642   
643    // filter non-primary refs
644  61 for (int ip = pr.size(); --ip >= 0;) {
645  33 DBRefEntry p = pr.get(ip);
646  475 for (int i = bsSelect.nextSetBit(0); i >= 0; i = bsSelect.nextSetBit(i + 1)) {
647  442 if (dbrefs.get(i) == p)
648  3 bsSelect.clear(i);
649    }
650    // while (selfs.contains(p))
651    // {
652    // selfs.remove(p);
653    // }
654    }
655    // List<DBRefEntry> toPromote = new ArrayList<DBRefEntry>();
656   
657  29 for (int ip = pr.size(), keys = 0; --ip >= 0 && keys != DBRefSource.PRIMARY_MASK;) {
658  28 DBRefEntry p = pr.get(ip);
659  28 if (isProtein) {
660  3 switch (getCanonicalName(p.getSource())) {
661  1 case DBRefSource.UNIPROT:
662  1 keys |= DBRefSource.UNIPROT_MASK;
663  1 break;
664  0 case DBRefSource.ENSEMBL:
665  0 keys |= DBRefSource.ENSEMBL_MASK;
666  0 break;
667    }
668    } else {
669    // TODO: promote transcript refs ??
670    }
671  28 if (keys == 0 || !selectRefsBS(dbrefs, keys, bsSelect))
672  27 return;
673    // if (candidates != null)
674    {
675  3 for (int ic = bsSelect.nextSetBit(0); ic >= 0; ic = bsSelect.nextSetBit(ic + 1))
676    // for (int ic = 0, n = candidates.size(); ic < n; ic++)
677    {
678  2 DBRefEntry cand = dbrefs.get(ic);// candidates.get(ic);
679  2 if (cand.hasMap()) {
680  0 Mapping map = cand.getMap();
681  0 SequenceI cto = map.getTo();
682  0 if (cto != null && cto != sequence) {
683    // can't promote refs with mappings to other sequences
684  0 continue;
685    }
686  0 MapList mlist = map.getMap();
687  0 if (mlist.getFromLowest() != sstart && mlist.getFromHighest() != send) {
688    // can't promote refs with mappings from a region of this sequence
689    // - eg CDS
690  0 continue;
691    }
692    }
693    // and promote - not that version must be non-null here,
694    // as p must have passed isPrimaryCandidate()
695  2 cand.setVersion(p.getVersion() + " (promoted)");
696  2 bsSelect.clear(ic);
697    // selfs.remove(cand);
698    // toPromote.add(cand);
699  2 if (!cand.isPrimaryCandidate()) {
700  0 System.out.println("Warning: Couldn't promote dbref " + cand.toString() + " for sequence "
701    + sequence.toString());
702    }
703    }
704    }
705    }
706    }
707   
708    }