ae0243e9ce2de227f76c812b580a5f4f8e3aa944
[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.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         static 
57         {
58                 // TODO load these from a resource file?
59                 canonicalSourceNameLookup.put("uniprotkb/swiss-prot", DBRefSource.UNIPROT);
60                 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
61
62                 // Ensembl values for dbname in xref REST service:
63                 canonicalSourceNameLookup.put("uniprot/sptrembl", DBRefSource.UNIPROT);
64                 canonicalSourceNameLookup.put("uniprot/swissprot", DBRefSource.UNIPROT);
65
66                 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
67                 canonicalSourceNameLookup.put("ensembl", DBRefSource.ENSEMBL);
68                 // Ensembl Gn and Tr are for Ensembl genomic and transcript IDs as served
69                 // from ENA.
70                 canonicalSourceNameLookup.put("ensembl-tr", DBRefSource.ENSEMBL);
71                 canonicalSourceNameLookup.put("ensembl-gn", DBRefSource.ENSEMBL);
72
73             // guarantee we always have lowercase entries for canonical string lookups
74             for (String k : canonicalSourceNameLookup.keySet())
75             {
76               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   public static List<DBRefEntry> selectRefs(List<DBRefEntry> dbrefs, String[] sources) 
91   {
92     if (dbrefs == null || sources == null) 
93         {
94           return dbrefs;
95         }
96
97         // BH TODO (what?)
98         HashSet<String> srcs = new HashSet<String>();
99         for (String src : sources) 
100         {
101           srcs.add(src.toUpperCase());
102         }
103
104         int nrefs = dbrefs.size();
105         List<DBRefEntry> res = new ArrayList<DBRefEntry>();
106         for (int ib = 0; ib < nrefs; ib++) 
107         {
108           DBRefEntry dbr = dbrefs.get(ib);
109           String source = getCanonicalName(dbr.getSource());
110           if (srcs.contains(source.toUpperCase())) 
111           {
112             res.add(dbr);
113           }
114         }
115         if (res.size() > 0) 
116         {
117                 // List<DBRefEntry> reply = new DBRefEntry[res.size()];
118                 return res;// .toArray(reply);
119         }
120         return null;
121   }
122
123   private static boolean selectRefsBS(List<DBRefEntry> dbrefs, int sourceKeys, BitSet bsSelect) 
124   {
125         if (dbrefs == null || sourceKeys == 0) 
126         {
127           return false;
128         }
129         for (int i = 0, n = dbrefs.size(); i < n; i++) 
130         {
131           DBRefEntry dbr = dbrefs.get(i);
132           if ((dbr.getSourceKey() & sourceKeys) != 0) 
133           {
134             bsSelect.clear(i);
135           }
136         }
137         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   static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
152           DbRefComp comparator)
153   {
154     List<DBRefEntry> rfs = new ArrayList<>();
155     if (refs == null || entry == null)
156     {
157       return rfs;
158     }
159     for (int i = 0; i < refs.length; i++)
160     {
161       if (comparator.matches(entry, refs[i]))
162       {
163         rfs.add(refs[i]);
164       }
165     }
166     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         public static String getCanonicalName(String source) 
179         {
180           if (source == null) 
181           {
182                 return null;
183           }
184           String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
185           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         public static List<DBRefEntry> searchRefs(List<DBRefEntry> ref, DBRefEntry entry, int mode) {
203                 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         public static List<DBRefEntry> searchRefs(List<DBRefEntry> refs, String accId) {
219                 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
220                 if (refs == null || accId == null) {
221                         return rfs;
222                 }
223                 for (int i = 0, n = refs.size(); i < n; i++) {
224                         DBRefEntry e = refs.get(i);
225                         if (accId.equals(e.getAccessionId())) {
226                                 rfs.add(e);
227                         }
228                 }
229                 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         static List<DBRefEntry> searchRefs(List<DBRefEntry> refs, DBRefEntry entry, DbRefComp comparator, int mode) {
245                 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
246                 if (refs == null || entry == null) {
247                         return rfs;
248                 }
249                 for (int i = 0, n = refs.size(); i < n; i++) {
250                         DBRefEntry e = refs.get(i);
251                         if (comparator.matches(entry, e, SEARCH_MODE_FULL)) {
252                                 rfs.add(e);
253                         }
254                 }
255                 return rfs;
256         }
257
258         interface DbRefComp {
259                 default public boolean matches(DBRefEntry refa, DBRefEntry refb) {
260                         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                 @Override
272                 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
273                         if ((mode & DB_SOURCE) != 0 && 
274                                         (refa.getSource() == null || DBRefUtils.getCanonicalName(refb.getSource())
275                                         .equals(DBRefUtils.getCanonicalName(refa.getSource())))) {
276                                 if ((mode & DB_VERSION) != 0 && 
277                                                 (refa.getVersion() == null || refb.getVersion().equals(refa.getVersion()))) {
278                                         if ((mode & DB_ID) != 0 && 
279                                                         (refa.getAccessionId() == null || refb.getAccessionId().equals(refa.getAccessionId()))) {
280                                                 if ((mode & DB_MAP) != 0 && 
281                                                                 (refa.getMap() == null || (refb.getMap() != null && refb.getMap().equals(refa.getMap())))) {
282                                                         return true;
283                                                 }
284                                         }
285                                 }
286                         }
287                         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                 @Override
298                 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
299                         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                                 return true;
304                         }
305                         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   public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
325           String version, String acn)
326   {
327     DBRefEntry ref = null;
328     if (dbname != null)
329     {
330       String locsrc = DBRefUtils.getCanonicalName(dbname);
331       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         Regex r = new com.stevesoft.pat.Regex(
338                 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
339         if (r.search(acn.trim()))
340         {
341           String pdbid = r.stringMatched(1);
342           String chaincode = r.stringMatched(2);
343           if (chaincode == null)
344           {
345             chaincode = " ";
346           }
347           // String mapstart = r.stringMatched(3);
348           // String mapend = r.stringMatched(4);
349           if (chaincode.equals(" "))
350           {
351             chaincode = "_";
352           }
353           // construct pdb ref.
354           ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
355           PDBEntry pdbr = new PDBEntry();
356           pdbr.setId(pdbid);
357           pdbr.setType(PDBEntry.Type.PDB);
358           pdbr.setChainCode(chaincode);
359           seq.addPDBId(pdbr);
360         }
361         else
362         {
363           System.err.println("Malformed PDB DR line:" + acn);
364         }
365       }
366       else
367       {
368         // default:
369         ref = new DBRefEntry(locsrc, version, acn.trim());
370       }
371     }
372     if (ref != null)
373     {
374       seq.addDBRef(ref);
375     }
376     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                 @Override
386                 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
387                         if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
388                                         .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
389                                 // We dont care about version
390                                 if (refa.getAccessionId() != null && refb.getAccessionId() != null
391                                                 // FIXME should be && not || here?
392                                                 || refb.getAccessionId().equals(refa.getAccessionId())) {
393                                         if ((refa.getMap() == null || refb.getMap() == null) || (refa.getMap() != null
394                                                         && refb.getMap() != null && refb.getMap().equals(refa.getMap()))) {
395                                                 return true;
396                                         }
397                                 }
398                         }
399                         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                 @Override
411                 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
412                         if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
413                                         .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
414                                 // We dont care about version
415                                 if (refa.getAccessionId() != null && refb.getAccessionId() != null
416                                                 || refb.getAccessionId().equals(refa.getAccessionId())) {
417                                         if ((refa.getMap() == null && refb.getMap() == null)
418                                                         || (refa.getMap() != null && refb.getMap() != null)) {
419                                                 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                                                         return true;
423                                                 }
424                                         }
425                                 }
426                         }
427                         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                 @Override
439                 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
440                         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                                 if (refa.getAccessionId() != null && refb.getAccessionId() != null
447                                                 || refb.getAccessionId().equals(refa.getAccessionId())) {
448                                         if (refa.getMap() == null && refb.getMap() == null) {
449                                                 return true;
450                                         }
451                                         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                                                 return true;
456                                         }
457                                 }
458                         }
459                         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                 @Override
471                 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) 
472                 {
473                         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                                 if (refa.getAccessionId() == null || refa.getAccessionId().equals(refb.getAccessionId())) 
478                                 {
479                                         if (refa.getMap() == null || refb.getMap() == null) 
480                                         {
481                                                 return true;
482                                         }
483                                         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                                                 return true;
489                                         }
490                                 }
491                         }
492                         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   public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
505           String source)
506   {
507     List<DBRefEntry> matches = new ArrayList<>();
508     if (dbRefs != null && source != null)
509     {
510       for (DBRefEntry dbref : dbRefs)
511       {
512         if (source.equalsIgnoreCase(dbref.getSource()))
513         {
514           matches.add(dbref);
515         }
516       }
517     }
518     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         public static boolean nullOrEqual(Object o1, Object o2) {
529                 if (o1 == null || o2 == null) {
530                         return true;
531                 }
532                 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         public static boolean nullOrEqualSource(String o1, String o2) {
545                 if (o1 == null || o2 == null) {
546                         return true;
547                 }
548                 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         public static List<DBRefEntry> selectDbRefs(boolean selectDna, List<DBRefEntry> refs) {
560                 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         public static List<DBRefEntry> searchRefsForSource(List<DBRefEntry> dbRefs, String source) {
575                 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
576                 if (dbRefs != null && source != null) {
577                         for (DBRefEntry dbref : dbRefs) {
578                                 if (source.equalsIgnoreCase(dbref.getSource())) {
579                                         matches.add(dbref);
580                                 }
581                         }
582                 }
583                 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         public static void ensurePrimaries(SequenceI sequence, List<DBRefEntry> pr) {
615                 if (pr.size() == 0) {
616                         // nothing to do
617                         return;
618                 }
619                 int sstart = sequence.getStart();
620                 int send = sequence.getEnd();
621                 boolean isProtein = sequence.isProtein();
622                 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                 List<DBRefEntry> dbrefs = sequence.getDBRefs();
635                 bsSelect.set(0, dbrefs.size());
636
637                 if (!selectRefsBS(dbrefs, isProtein ? DBRefSource.PROTEIN_MASK : DBRefSource.DNA_CODING_MASK, bsSelect))
638                         return;
639
640 //      selfs.addAll(selfArray);
641 //    }
642
643                 // filter non-primary refs
644                 for (int ip = pr.size(); --ip >= 0;) {
645                         DBRefEntry p = pr.get(ip);
646                         for (int i = bsSelect.nextSetBit(0); i >= 0; i = bsSelect.nextSetBit(i + 1)) {
647                                 if (dbrefs.get(i) == p)
648                                         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                 for (int ip = pr.size(), keys = 0; --ip >= 0 && keys != DBRefSource.PRIMARY_MASK;) {
658                         DBRefEntry p = pr.get(ip);
659                         if (isProtein) {
660                                 switch (getCanonicalName(p.getSource())) {
661                                 case DBRefSource.UNIPROT:
662                                         keys |= DBRefSource.UNIPROT_MASK;
663                                         break;
664                                 case DBRefSource.ENSEMBL:
665                                         keys |= DBRefSource.ENSEMBL_MASK;
666                                         break;
667                                 }
668                         } else {
669                                 // TODO: promote transcript refs ??
670                         }
671                         if (keys == 0 || !selectRefsBS(dbrefs, keys, bsSelect))
672                                 return;
673 //      if (candidates != null)
674                         {
675                                 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                                         DBRefEntry cand = dbrefs.get(ic);// candidates.get(ic);
679                                         if (cand.hasMap()) {
680                                                 Mapping map = cand.getMap();
681                                                 SequenceI cto = map.getTo();
682                                                 if (cto != null && cto != sequence) {
683                                                         // can't promote refs with mappings to other sequences
684                                                         continue;
685                                                 }
686                                                 MapList mlist = map.getMap();
687                                                 if (mlist.getFromLowest() != sstart && mlist.getFromHighest() != send) {
688                                                         // can't promote refs with mappings from a region of this sequence
689                                                         // - eg CDS
690                                                         continue;
691                                                 }
692                                         }
693                                         // and promote - not that version must be non-null here, 
694                                         // as p must have passed isPrimaryCandidate()
695                                         cand.setVersion(p.getVersion() + " (promoted)");
696                                         bsSelect.clear(ic);
697                                         // selfs.remove(cand);
698 //          toPromote.add(cand);
699                                         if (!cand.isPrimaryCandidate()) {
700                                                 System.out.println("Warning: Couldn't promote dbref " + cand.toString() + " for sequence "
701                                                                 + sequence.toString());
702                                         }
703                                 }
704                         }
705                 }
706         }
707
708 }