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