JAL-3626 Regex optimization from JAL-3253-applet; also minor
[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   private static Regex PARSE_REGEX;
311
312   private static Regex getParseRegex()
313   {
314     return (PARSE_REGEX == null ? PARSE_REGEX = Platform.newRegex(
315             "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)")
316             : PARSE_REGEX);
317   }
318
319   /**
320    * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
321    * database is PDB.
322    * <p>
323    * Used by file parsers to generate DBRefs from annotation within file (eg
324    * Stockholm)
325    * 
326    * @param dbname
327    * @param version
328    * @param acn
329    * @param seq
330    *          where to annotate with reference
331    * @return parsed version of entry that was added to seq (if any)
332    */
333   public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
334           String version, String acn)
335   {
336     DBRefEntry ref = null;
337     if (dbname != null)
338     {
339       String locsrc = DBRefUtils.getCanonicalName(dbname);
340       if (locsrc.equals(DBRefSource.PDB))
341       {
342         /*
343          * Check for PFAM style stockhom PDB accession id citation e.g.
344          * "1WRI A; 7-80;"
345          */
346         Regex r = getParseRegex();
347         if (r.search(acn.trim()))
348         {
349           String pdbid = r.stringMatched(1);
350           String chaincode = r.stringMatched(2);
351           if (chaincode == null)
352           {
353             chaincode = " ";
354           }
355           // String mapstart = r.stringMatched(3);
356           // String mapend = r.stringMatched(4);
357           if (chaincode.equals(" "))
358           {
359             chaincode = "_";
360           }
361           // construct pdb ref.
362           ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
363           PDBEntry pdbr = new PDBEntry();
364           pdbr.setId(pdbid);
365           pdbr.setType(PDBEntry.Type.PDB);
366           pdbr.setChainCode(chaincode);
367           seq.addPDBId(pdbr);
368         }
369         else
370         {
371           System.err.println("Malformed PDB DR line:" + acn);
372         }
373       }
374       else
375       {
376         // default:
377         ref = new DBRefEntry(locsrc, version, acn.trim());
378       }
379     }
380     if (ref != null)
381     {
382       seq.addDBRef(ref);
383     }
384     return ref;
385   }
386
387         /**
388          * accession ID and DB must be identical. Version is ignored. Map is either not
389          * defined or is a match (or is compatible?)
390          */
391         // TODO unused - remove?
392         public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp() {
393                 @Override
394                 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
395                         if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
396                                         .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
397                                 // We dont care about version
398                                 if (refa.getAccessionId() != null && refb.getAccessionId() != null
399                                                 // FIXME should be && not || here?
400                                                 || refb.getAccessionId().equals(refa.getAccessionId())) {
401                                         if ((refa.getMap() == null || refb.getMap() == null) || (refa.getMap() != null
402                                                         && refb.getMap() != null && refb.getMap().equals(refa.getMap()))) {
403                                                 return true;
404                                         }
405                                 }
406                         }
407                         return false;
408                 }
409         };
410
411         /**
412          * accession ID and DB must be identical. Version is ignored. No map on either
413          * or map but no maplist on either or maplist of map on a is the complement of
414          * maplist of map on b.
415          */
416         // TODO unused - remove?
417         public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp() {
418                 @Override
419                 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
420                         if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
421                                         .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
422                                 // We dont care about version
423                                 if (refa.getAccessionId() != null && refb.getAccessionId() != null
424                                                 || refb.getAccessionId().equals(refa.getAccessionId())) {
425                                         if ((refa.getMap() == null && refb.getMap() == null)
426                                                         || (refa.getMap() != null && refb.getMap() != null)) {
427                                                 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
428                                                                 || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
429                                                                                 && refb.getMap().getMap().getInverse().equals(refa.getMap().getMap()))) {
430                                                         return true;
431                                                 }
432                                         }
433                                 }
434                         }
435                         return false;
436                 }
437         };
438
439         /**
440          * accession ID and DB must be identical. Version is ignored. No map on both or
441          * or map but no maplist on either or maplist of map on a is equivalent to the
442          * maplist of map on b.
443          */
444         // TODO unused - remove?
445         public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp() {
446                 @Override
447                 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
448                         if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
449                                         .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
450                                 // We dont care about version
451                                 // if ((refa.getVersion()==null || refb.getVersion()==null)
452                                 // || refb.getVersion().equals(refa.getVersion()))
453                                 // {
454                                 if (refa.getAccessionId() != null && refb.getAccessionId() != null
455                                                 || refb.getAccessionId().equals(refa.getAccessionId())) {
456                                         if (refa.getMap() == null && refb.getMap() == null) {
457                                                 return true;
458                                         }
459                                         if (refa.getMap() != null && refb.getMap() != null
460                                                         && ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
461                                                                         || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
462                                                                                         && refb.getMap().getMap().equals(refa.getMap().getMap())))) {
463                                                 return true;
464                                         }
465                                 }
466                         }
467                         return false;
468                 }
469         };
470
471         /**
472          * accession ID and DB must be identical, or null on a. Version is ignored. No
473          * map on either or map but no maplist on either or maplist of map on a is
474          * equivalent to the maplist of map on b.
475          */
476         public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp() 
477         {
478                 @Override
479                 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) 
480                 {
481                         if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
482                                         .equals(DBRefUtils.getCanonicalName(refa.getSource()))) 
483                         {
484                                 // We dont care about version
485                                 if (refa.getAccessionId() == null || refa.getAccessionId().equals(refb.getAccessionId())) 
486                                 {
487                                         if (refa.getMap() == null || refb.getMap() == null) 
488                                         {
489                                                 return true;
490                                         }
491                                         if ((refa.getMap() != null && refb.getMap() != null)
492                                                         && (refb.getMap().getMap() == null && refa.getMap().getMap() == null)
493                                                         || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
494                                                                         && (refb.getMap().getMap().equals(refa.getMap().getMap())))) 
495                                         {
496                                                 return true;
497                                         }
498                                 }
499                         }
500                         return false;
501                 }
502         };
503
504         /**
505    * Returns the (possibly empty) list of those supplied dbrefs which have the
506    * specified source database, with a case-insensitive match of source name
507    * 
508    * @param dbRefs
509    * @param source
510    * @return
511    */
512   public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
513           String source)
514   {
515     List<DBRefEntry> matches = new ArrayList<>();
516     if (dbRefs != null && source != null)
517     {
518       for (DBRefEntry dbref : dbRefs)
519       {
520         if (source.equalsIgnoreCase(dbref.getSource()))
521         {
522           matches.add(dbref);
523         }
524       }
525     }
526     return matches;
527   }
528
529         /**
530          * Returns true if either object is null, or they are equal
531          * 
532          * @param o1
533          * @param o2
534          * @return
535          */
536         public static boolean nullOrEqual(Object o1, Object o2) {
537                 if (o1 == null || o2 == null) {
538                         return true;
539                 }
540                 return o1.equals(o2);
541         }
542
543         /**
544          * canonicalise source string before comparing. null is always wildcard
545          * 
546          * @param o1 - null or source string to compare
547          * @param o2 - null or source string to compare
548          * @return true if either o1 or o2 are null, or o1 equals o2 under
549          *         DBRefUtils.getCanonicalName
550          *         (o1).equals(DBRefUtils.getCanonicalName(o2))
551          */
552         public static boolean nullOrEqualSource(String o1, String o2) {
553                 if (o1 == null || o2 == null) {
554                         return true;
555                 }
556                 return DBRefUtils.getCanonicalName(o1).equals(DBRefUtils.getCanonicalName(o2));
557         }
558
559         /**
560          * Selects just the DNA or protein references from a set of references
561          * 
562          * @param selectDna if true, select references to 'standard' DNA databases, else
563          *                  to 'standard' peptide databases
564          * @param refs      a set of references to select from
565          * @return
566          */
567         public static List<DBRefEntry> selectDbRefs(boolean selectDna, List<DBRefEntry> refs) {
568                 return selectRefs(refs, selectDna ? DBRefSource.DNACODINGDBS : DBRefSource.PROTEINDBS);
569                 // could attempt to find other cross
570                 // refs here - ie PDB xrefs
571                 // (not dna, not protein seq)
572         }
573
574         /**
575          * Returns the (possibly empty) list of those supplied dbrefs which have the
576          * specified source database, with a case-insensitive match of source name
577          * 
578          * @param dbRefs
579          * @param source
580          * @return
581          */
582         public static List<DBRefEntry> searchRefsForSource(List<DBRefEntry> dbRefs, String source) {
583                 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
584                 if (dbRefs != null && source != null) {
585                         for (DBRefEntry dbref : dbRefs) {
586                                 if (source.equalsIgnoreCase(dbref.getSource())) {
587                                         matches.add(dbref);
588                                 }
589                         }
590                 }
591                 return matches;
592         }
593
594         /**
595          * promote direct database references to primary for nucleotide or protein
596          * sequences if they have an appropriate primary ref
597          * <table>
598          * <tr>
599          * <th>Seq Type</th>
600          * <th>Primary DB</th>
601          * <th>Direct which will be promoted</th>
602          * </tr>
603          * <tr align=center>
604          * <td>peptides</td>
605          * <td>Ensembl</td>
606          * <td>Uniprot</td>
607          * </tr>
608          * <tr align=center>
609          * <td>peptides</td>
610          * <td>Ensembl</td>
611          * <td>Uniprot</td>
612          * </tr>
613          * <tr align=center>
614          * <td>dna</td>
615          * <td>Ensembl</td>
616          * <td>ENA</td>
617          * </tr>
618          * </table>
619          * 
620          * @param sequence
621          */
622         public static void ensurePrimaries(SequenceI sequence, List<DBRefEntry> pr) {
623                 if (pr.size() == 0) {
624                         // nothing to do
625                         return;
626                 }
627                 int sstart = sequence.getStart();
628                 int send = sequence.getEnd();
629                 boolean isProtein = sequence.isProtein();
630                 BitSet bsSelect = new BitSet();
631
632 //    List<DBRefEntry> selfs = new ArrayList<DBRefEntry>();
633 //    {
634
635 //      List<DBRefEntry> selddfs = selectDbRefs(!isprot, sequence.getDBRefs());
636 //      if (selfs == null || selfs.size() == 0)
637 //      {
638 //        // nothing to do
639 //        return;
640 //      }
641
642                 List<DBRefEntry> dbrefs = sequence.getDBRefs();
643                 bsSelect.set(0, dbrefs.size());
644
645                 if (!selectRefsBS(dbrefs, isProtein ? DBRefSource.PROTEIN_MASK : DBRefSource.DNA_CODING_MASK, bsSelect))
646                         return;
647
648 //      selfs.addAll(selfArray);
649 //    }
650
651                 // filter non-primary refs
652                 for (int ip = pr.size(); --ip >= 0;) {
653                         DBRefEntry p = pr.get(ip);
654                         for (int i = bsSelect.nextSetBit(0); i >= 0; i = bsSelect.nextSetBit(i + 1)) {
655                                 if (dbrefs.get(i) == p)
656                                         bsSelect.clear(i);
657                         }
658 //      while (selfs.contains(p))
659 //      {
660 //        selfs.remove(p);
661 //      }
662                 }
663 //    List<DBRefEntry> toPromote = new ArrayList<DBRefEntry>();
664
665                 for (int ip = pr.size(), keys = 0; --ip >= 0 && keys != DBRefSource.PRIMARY_MASK;) {
666                         DBRefEntry p = pr.get(ip);
667                         if (isProtein) {
668                                 switch (getCanonicalName(p.getSource())) {
669                                 case DBRefSource.UNIPROT:
670                                         keys |= DBRefSource.UNIPROT_MASK;
671                                         break;
672                                 case DBRefSource.ENSEMBL:
673                                         keys |= DBRefSource.ENSEMBL_MASK;
674                                         break;
675                                 }
676                         } else {
677                                 // TODO: promote transcript refs ??
678                         }
679                         if (keys == 0 || !selectRefsBS(dbrefs, keys, bsSelect))
680                                 return;
681 //      if (candidates != null)
682                         {
683                                 for (int ic = bsSelect.nextSetBit(0); ic >= 0; ic = bsSelect.nextSetBit(ic + 1))
684 //        for (int ic = 0, n = candidates.size(); ic < n; ic++)
685                                 {
686                                         DBRefEntry cand = dbrefs.get(ic);// candidates.get(ic);
687                                         if (cand.hasMap()) {
688                                                 Mapping map = cand.getMap();
689                                                 SequenceI cto = map.getTo();
690                                                 if (cto != null && cto != sequence) {
691                                                         // can't promote refs with mappings to other sequences
692                                                         continue;
693                                                 }
694                                                 MapList mlist = map.getMap();
695                                                 if (mlist.getFromLowest() != sstart && mlist.getFromHighest() != send) {
696                                                         // can't promote refs with mappings from a region of this sequence
697                                                         // - eg CDS
698                                                         continue;
699                                                 }
700                                         }
701                                         // and promote - not that version must be non-null here, 
702                                         // as p must have passed isPrimaryCandidate()
703                                         cand.setVersion(p.getVersion() + " (promoted)");
704                                         bsSelect.clear(ic);
705                                         // selfs.remove(cand);
706 //          toPromote.add(cand);
707                                         if (!cand.isPrimaryCandidate()) {
708                                                 System.out.println("Warning: Couldn't promote dbref " + cand.toString() + " for sequence "
709                                                                 + sequence.toString());
710                                         }
711                                 }
712                         }
713                 }
714         }
715
716 }