Merge branch 'documentation/JAL-3407_2.11.1_release' into releases/Release_2_11_1_Branch
[jalview.git] / src / jalview / io / vcf / VCFLoader.java
1 package jalview.io.vcf;
2
3 import jalview.analysis.Dna;
4 import jalview.api.AlignViewControllerGuiI;
5 import jalview.bin.Cache;
6 import jalview.datamodel.DBRefEntry;
7 import jalview.datamodel.GeneLociI;
8 import jalview.datamodel.Mapping;
9 import jalview.datamodel.SequenceFeature;
10 import jalview.datamodel.SequenceI;
11 import jalview.datamodel.features.FeatureAttributeType;
12 import jalview.datamodel.features.FeatureSource;
13 import jalview.datamodel.features.FeatureSources;
14 import jalview.ext.ensembl.EnsemblMap;
15 import jalview.ext.htsjdk.HtsContigDb;
16 import jalview.ext.htsjdk.VCFReader;
17 import jalview.io.gff.Gff3Helper;
18 import jalview.io.gff.SequenceOntologyI;
19 import jalview.util.MapList;
20 import jalview.util.MappingUtils;
21 import jalview.util.MessageManager;
22 import jalview.util.StringUtils;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.Set;
34 import java.util.regex.Pattern;
35 import java.util.regex.PatternSyntaxException;
36
37 import htsjdk.samtools.SAMException;
38 import htsjdk.samtools.SAMSequenceDictionary;
39 import htsjdk.samtools.SAMSequenceRecord;
40 import htsjdk.samtools.util.CloseableIterator;
41 import htsjdk.tribble.TribbleException;
42 import htsjdk.variant.variantcontext.Allele;
43 import htsjdk.variant.variantcontext.VariantContext;
44 import htsjdk.variant.vcf.VCFConstants;
45 import htsjdk.variant.vcf.VCFHeader;
46 import htsjdk.variant.vcf.VCFHeaderLine;
47 import htsjdk.variant.vcf.VCFHeaderLineCount;
48 import htsjdk.variant.vcf.VCFHeaderLineType;
49 import htsjdk.variant.vcf.VCFInfoHeaderLine;
50
51 /**
52  * A class to read VCF data (using the htsjdk) and add variants as sequence
53  * features on dna and any related protein product sequences
54  * 
55  * @author gmcarstairs
56  */
57 public class VCFLoader
58 {
59   private static final String VCF_ENCODABLE = ":;=%,";
60
61   /*
62    * Jalview feature attributes for VCF fixed column data
63    */
64   private static final String VCF_POS = "POS";
65
66   private static final String VCF_ID = "ID";
67
68   private static final String VCF_QUAL = "QUAL";
69
70   private static final String VCF_FILTER = "FILTER";
71
72   private static final String NO_VALUE = VCFConstants.MISSING_VALUE_v4; // '.'
73
74   private static final String DEFAULT_SPECIES = "homo_sapiens";
75
76   /**
77    * A class to model the mapping from sequence to VCF coordinates. Cases include
78    * <ul>
79    * <li>a direct 1:1 mapping where the sequence is one of the VCF contigs</li>
80    * <li>a mapping of sequence to chromosomal coordinates, where sequence and VCF
81    * use the same reference assembly</li>
82    * <li>a modified mapping of sequence to chromosomal coordinates, where sequence
83    * and VCF use different reference assembles</li>
84    * </ul>
85    */
86   class VCFMap
87   {
88     final String chromosome;
89
90     final MapList map;
91
92     VCFMap(String chr, MapList m)
93     {
94       chromosome = chr;
95       map = m;
96     }
97
98     @Override
99     public String toString()
100     {
101       return chromosome + ":" + map.toString();
102     }
103   }
104
105   /*
106    * Lookup keys, and default values, for Preference entries that describe
107    * patterns for VCF and VEP fields to capture
108    */
109   private static final String VEP_FIELDS_PREF = "VEP_FIELDS";
110
111   private static final String VCF_FIELDS_PREF = "VCF_FIELDS";
112
113   private static final String DEFAULT_VCF_FIELDS = ".*";
114
115   private static final String DEFAULT_VEP_FIELDS = ".*";// "Allele,Consequence,IMPACT,SWISSPROT,SIFT,PolyPhen,CLIN_SIG";
116
117   /*
118    * Lookup keys, and default values, for Preference entries that give
119    * mappings from tokens in the 'reference' header to species or assembly
120    */
121   private static final String VCF_ASSEMBLY = "VCF_ASSEMBLY";
122
123   private static final String DEFAULT_VCF_ASSEMBLY = "assembly19=GRCh37,hs37=GRCh37,grch37=GRCh37,grch38=GRCh38";
124
125   private static final String VCF_SPECIES = "VCF_SPECIES"; // default is human
126
127   private static final String DEFAULT_REFERENCE = "grch37"; // fallback default is human GRCh37
128
129   /*
130    * keys to fields of VEP CSQ consequence data
131    * see https://www.ensembl.org/info/docs/tools/vep/vep_formats.html
132    */
133   private static final String CSQ_CONSEQUENCE_KEY = "Consequence";
134   private static final String CSQ_ALLELE_KEY = "Allele";
135   private static final String CSQ_ALLELE_NUM_KEY = "ALLELE_NUM"; // 0 (ref), 1...
136   private static final String CSQ_FEATURE_KEY = "Feature"; // Ensembl stable id
137
138   /*
139    * default VCF INFO key for VEP consequence data
140    * NB this can be overridden running VEP with --vcf_info_field
141    * - we don't handle this case (require identifier to be CSQ)
142    */
143   private static final String CSQ_FIELD = "CSQ";
144
145   /*
146    * separator for fields in consequence data is '|'
147    */
148   private static final String PIPE_REGEX = "\\|";
149
150   /*
151    * delimiter that separates multiple consequence data blocks
152    */
153   private static final String COMMA = ",";
154
155   /*
156    * the feature group assigned to a VCF variant in Jalview
157    */
158   private static final String FEATURE_GROUP_VCF = "VCF";
159
160   /*
161    * internal delimiter used to build keys for assemblyMappings
162    * 
163    */
164   private static final String EXCL = "!";
165
166   /*
167    * the VCF file we are processing
168    */
169   protected String vcfFilePath;
170
171   /*
172    * mappings between VCF and sequence reference assembly regions, as 
173    * key = "species!chromosome!fromAssembly!toAssembly
174    * value = Map{fromRange, toRange}
175    */
176   private Map<String, Map<int[], int[]>> assemblyMappings;
177
178   private VCFReader reader;
179
180   /*
181    * holds details of the VCF header lines (metadata)
182    */
183   private VCFHeader header;
184
185   /*
186    * species (as a valid Ensembl term) the VCF is for 
187    */
188   private String vcfSpecies;
189
190   /*
191    * genome assembly version (as a valid Ensembl identifier) the VCF is for 
192    */
193   private String vcfAssembly;
194
195   /*
196    * a Dictionary of contigs (if present) referenced in the VCF file
197    */
198   private SAMSequenceDictionary dictionary;
199
200   /*
201    * the position (0...) of field in each block of
202    * CSQ (consequence) data (if declared in the VCF INFO header for CSQ)
203    * see http://www.ensembl.org/info/docs/tools/vep/vep_formats.html
204    */
205   private int csqConsequenceFieldIndex = -1;
206   private int csqAlleleFieldIndex = -1;
207   private int csqAlleleNumberFieldIndex = -1;
208   private int csqFeatureFieldIndex = -1;
209
210   // todo the same fields for SnpEff ANN data if wanted
211   // see http://snpeff.sourceforge.net/SnpEff_manual.html#input
212
213   /*
214    * a unique identifier under which to save metadata about feature
215    * attributes (selected INFO field data)
216    */
217   private String sourceId;
218
219   /*
220    * The INFO IDs of data that is both present in the VCF file, and
221    * also matched by any filters for data of interest
222    */
223   List<String> vcfFieldsOfInterest;
224
225   /*
226    * The field offsets and identifiers for VEP (CSQ) data that is both present
227    * in the VCF file, and also matched by any filters for data of interest
228    * for example 0 -> Allele, 1 -> Consequence, ..., 36 -> SIFT, ...
229    */
230   Map<Integer, String> vepFieldsOfInterest;
231
232   /*
233    * key:value for which rejected data has been seen
234    * (the error is logged only once for each combination)
235    */
236   private Set<String> badData;
237
238   /**
239    * Constructor given a VCF file
240    * 
241    * @param alignment
242    */
243   public VCFLoader(String vcfFile)
244   {
245     try
246     {
247       initialise(vcfFile);
248     } catch (IOException e)
249     {
250       System.err.println("Error opening VCF file: " + e.getMessage());
251     }
252
253     // map of species!chromosome!fromAssembly!toAssembly to {fromRange, toRange}
254     assemblyMappings = new HashMap<>();
255   }
256
257   /**
258    * Starts a new thread to query and load VCF variant data on to the given
259    * sequences
260    * <p>
261    * This method is not thread safe - concurrent threads should use separate
262    * instances of this class.
263    * 
264    * @param seqs
265    * @param gui
266    */
267   public void loadVCF(SequenceI[] seqs, final AlignViewControllerGuiI gui)
268   {
269     if (gui != null)
270     {
271       gui.setStatus(MessageManager.getString("label.searching_vcf"));
272     }
273
274     new Thread()
275     {
276       @Override
277       public void run()
278       {
279         VCFLoader.this.doLoad(seqs, gui);
280       }
281     }.start();
282   }
283
284   /**
285    * Reads the specified contig sequence and adds its VCF variants to it
286    * 
287    * @param contig
288    *          the id of a single sequence (contig) to load
289    * @return
290    */
291   public SequenceI loadVCFContig(String contig)
292   {
293     VCFHeaderLine headerLine = header.getOtherHeaderLine(VCFHeader.REFERENCE_KEY);
294     if (headerLine == null)
295     {
296       Cache.log.error("VCF reference header not found");
297       return null;
298     }
299     String ref = headerLine.getValue();
300     if (ref.startsWith("file://"))
301     {
302       ref = ref.substring(7);
303     }
304     setSpeciesAndAssembly(ref);
305
306     SequenceI seq = null;
307     File dbFile = new File(ref);
308
309     if (dbFile.exists())
310     {
311       HtsContigDb db = new HtsContigDb("", dbFile);
312       seq = db.getSequenceProxy(contig);
313       loadSequenceVCF(seq);
314       db.close();
315     }
316     else
317     {
318       Cache.log.error("VCF reference not found: " + ref);
319     }
320
321     return seq;
322   }
323
324   /**
325    * Loads VCF on to one or more sequences
326    * 
327    * @param seqs
328    * @param gui
329    *          optional callback handler for messages
330    */
331   protected void doLoad(SequenceI[] seqs, AlignViewControllerGuiI gui)
332   {
333     try
334     {
335       VCFHeaderLine ref = header
336               .getOtherHeaderLine(VCFHeader.REFERENCE_KEY);
337       String reference = ref == null ? null : ref.getValue();
338
339       setSpeciesAndAssembly(reference);
340
341       int varCount = 0;
342       int seqCount = 0;
343
344       /*
345        * query for VCF overlapping each sequence in turn
346        */
347       for (SequenceI seq : seqs)
348       {
349         int added = loadSequenceVCF(seq);
350         if (added > 0)
351         {
352           seqCount++;
353           varCount += added;
354           transferAddedFeatures(seq);
355         }
356       }
357       if (gui != null)
358       {
359         String msg = MessageManager.formatMessage("label.added_vcf",
360                 varCount, seqCount);
361         gui.setStatus(msg);
362         if (gui.getFeatureSettingsUI() != null)
363         {
364           gui.getFeatureSettingsUI().discoverAllFeatureData();
365         }
366       }
367     } catch (Throwable e)
368     {
369       System.err.println("Error processing VCF: " + e.getMessage());
370       e.printStackTrace();
371       if (gui != null)
372       {
373         gui.setStatus("Error occurred - see console for details");
374       }
375     } finally
376     {
377       if (reader != null)
378       {
379         try
380         {
381           reader.close();
382         } catch (IOException e)
383         {
384           // ignore
385         }
386       }
387       header = null;
388       dictionary = null;
389     }
390   }
391
392   /**
393    * Attempts to determine and save the species and genome assembly version to
394    * which the VCF data applies. This may be done by parsing the {@code reference}
395    * header line, configured in a property file, or (potentially) confirmed
396    * interactively by the user.
397    * <p>
398    * The saved values should be identifiers valid for Ensembl's REST service
399    * {@code map} endpoint, so they can be used (if necessary) to retrieve the
400    * mapping between VCF coordinates and sequence coordinates.
401    * 
402    * @param reference
403    * @see https://rest.ensembl.org/documentation/info/assembly_map
404    * @see https://rest.ensembl.org/info/assembly/human?content-type=text/xml
405    * @see https://rest.ensembl.org/info/species?content-type=text/xml
406    */
407   protected void setSpeciesAndAssembly(String reference)
408   {
409     if (reference == null)
410     {
411       Cache.log.error("No VCF ##reference found, defaulting to "
412               + DEFAULT_REFERENCE + ":" + DEFAULT_SPECIES);
413       reference = DEFAULT_REFERENCE; // default to GRCh37 if not specified
414     }
415     reference = reference.toLowerCase();
416
417     /*
418      * for a non-human species, or other assembly identifier,
419      * specify as a Jalview property file entry e.g.
420      * VCF_ASSEMBLY = hs37=GRCh37,assembly19=GRCh37
421      * VCF_SPECIES = c_elegans=celegans
422      * to map a token in the reference header to a value
423      */
424     String prop = Cache.getDefault(VCF_ASSEMBLY, DEFAULT_VCF_ASSEMBLY);
425     for (String token : prop.split(","))
426     {
427       String[] tokens = token.split("=");
428       if (tokens.length == 2)
429       {
430         if (reference.contains(tokens[0].trim().toLowerCase()))
431         {
432           vcfAssembly = tokens[1].trim();
433           break;
434         }
435       }
436     }
437
438     vcfSpecies = DEFAULT_SPECIES;
439     prop = Cache.getProperty(VCF_SPECIES);
440     if (prop != null)
441     {
442       for (String token : prop.split(","))
443       {
444         String[] tokens = token.split("=");
445         if (tokens.length == 2)
446         {
447           if (reference.contains(tokens[0].trim().toLowerCase()))
448           {
449             vcfSpecies = tokens[1].trim();
450             break;
451           }
452         }
453       }
454     }
455   }
456
457   /**
458    * Opens the VCF file and parses header data
459    * 
460    * @param filePath
461    * @throws IOException
462    */
463   private void initialise(String filePath) throws IOException
464   {
465     vcfFilePath = filePath;
466
467     reader = new VCFReader(filePath);
468
469     header = reader.getFileHeader();
470
471     try
472     {
473       dictionary = header.getSequenceDictionary();
474     } catch (SAMException e)
475     {
476       // ignore - thrown if any contig line lacks length info
477     }
478
479     sourceId = filePath;
480
481     saveMetadata(sourceId);
482
483     /*
484      * get offset of CSQ ALLELE_NUM and Feature if declared
485      */
486     parseCsqHeader();
487   }
488
489   /**
490    * Reads metadata (such as INFO field descriptions and datatypes) and saves
491    * them for future reference
492    * 
493    * @param theSourceId
494    */
495   void saveMetadata(String theSourceId)
496   {
497     List<Pattern> vcfFieldPatterns = getFieldMatchers(VCF_FIELDS_PREF,
498             DEFAULT_VCF_FIELDS);
499     vcfFieldsOfInterest = new ArrayList<>();
500
501     FeatureSource metadata = new FeatureSource(theSourceId);
502
503     for (VCFInfoHeaderLine info : header.getInfoHeaderLines())
504     {
505       String attributeId = info.getID();
506       String desc = info.getDescription();
507       VCFHeaderLineType type = info.getType();
508       FeatureAttributeType attType = null;
509       switch (type)
510       {
511       case Character:
512         attType = FeatureAttributeType.Character;
513         break;
514       case Flag:
515         attType = FeatureAttributeType.Flag;
516         break;
517       case Float:
518         attType = FeatureAttributeType.Float;
519         break;
520       case Integer:
521         attType = FeatureAttributeType.Integer;
522         break;
523       case String:
524         attType = FeatureAttributeType.String;
525         break;
526       }
527       metadata.setAttributeName(attributeId, desc);
528       metadata.setAttributeType(attributeId, attType);
529
530       if (isFieldWanted(attributeId, vcfFieldPatterns))
531       {
532         vcfFieldsOfInterest.add(attributeId);
533       }
534     }
535
536     FeatureSources.getInstance().addSource(theSourceId, metadata);
537   }
538
539   /**
540    * Answers true if the field id is matched by any of the filter patterns, else
541    * false. Matching is against regular expression patterns, and is not
542    * case-sensitive.
543    * 
544    * @param id
545    * @param filters
546    * @return
547    */
548   private boolean isFieldWanted(String id, List<Pattern> filters)
549   {
550     for (Pattern p : filters)
551     {
552       if (p.matcher(id.toUpperCase()).matches())
553       {
554         return true;
555       }
556     }
557     return false;
558   }
559
560   /**
561    * Records 'wanted' fields defined in the CSQ INFO header (if there is one).
562    * Also records the position of selected fields (Allele, ALLELE_NUM, Feature)
563    * required for processing.
564    * <p>
565    * CSQ fields are declared in the CSQ INFO Description e.g.
566    * <p>
567    * Description="Consequence ...from ... VEP. Format: Allele|Consequence|...
568    */
569   protected void parseCsqHeader()
570   {
571     List<Pattern> vepFieldFilters = getFieldMatchers(VEP_FIELDS_PREF,
572             DEFAULT_VEP_FIELDS);
573     vepFieldsOfInterest = new HashMap<>();
574
575     VCFInfoHeaderLine csqInfo = header.getInfoHeaderLine(CSQ_FIELD);
576     if (csqInfo == null)
577     {
578       return;
579     }
580
581     /*
582      * parse out the pipe-separated list of CSQ fields; we assume here that
583      * these form the last part of the description, and contain no spaces
584      */
585     String desc = csqInfo.getDescription();
586     int spacePos = desc.lastIndexOf(" ");
587     desc = desc.substring(spacePos + 1);
588
589     if (desc != null)
590     {
591       String[] format = desc.split(PIPE_REGEX);
592       int index = 0;
593       for (String field : format)
594       {
595         if (CSQ_CONSEQUENCE_KEY.equals(field))
596         {
597           csqConsequenceFieldIndex = index;
598         }
599         if (CSQ_ALLELE_NUM_KEY.equals(field))
600         {
601           csqAlleleNumberFieldIndex = index;
602         }
603         if (CSQ_ALLELE_KEY.equals(field))
604         {
605           csqAlleleFieldIndex = index;
606         }
607         if (CSQ_FEATURE_KEY.equals(field))
608         {
609           csqFeatureFieldIndex = index;
610         }
611
612         if (isFieldWanted(field, vepFieldFilters))
613         {
614           vepFieldsOfInterest.put(index, field);
615         }
616
617         index++;
618       }
619     }
620   }
621
622   /**
623    * Reads the Preference value for the given key, with default specified if no
624    * preference set. The value is interpreted as a comma-separated list of
625    * regular expressions, and converted into a list of compiled patterns ready
626    * for matching. Patterns are forced to upper-case for non-case-sensitive
627    * matching.
628    * <p>
629    * This supports user-defined filters for fields of interest to capture while
630    * processing data. For example, VCF_FIELDS = AF,AC* would mean that VCF INFO
631    * fields with an ID of AF, or starting with AC, would be matched.
632    * 
633    * @param key
634    * @param def
635    * @return
636    */
637   private List<Pattern> getFieldMatchers(String key, String def)
638   {
639     String pref = Cache.getDefault(key, def);
640     List<Pattern> patterns = new ArrayList<>();
641     String[] tokens = pref.split(",");
642     for (String token : tokens)
643     {
644       try
645       {
646       patterns.add(Pattern.compile(token.toUpperCase()));
647       } catch (PatternSyntaxException e)
648       {
649         System.err.println("Invalid pattern ignored: " + token);
650       }
651     }
652     return patterns;
653   }
654
655   /**
656    * Transfers VCF features to sequences to which this sequence has a mapping.
657    * If the mapping is 3:1, computes peptide variants from nucleotide variants.
658    * 
659    * @param seq
660    */
661   protected void transferAddedFeatures(SequenceI seq)
662   {
663     DBRefEntry[] dbrefs = seq.getDBRefs();
664     if (dbrefs == null)
665     {
666       return;
667     }
668     for (DBRefEntry dbref : dbrefs)
669     {
670       Mapping mapping = dbref.getMap();
671       if (mapping == null || mapping.getTo() == null)
672       {
673         continue;
674       }
675
676       SequenceI mapTo = mapping.getTo();
677       MapList map = mapping.getMap();
678       if (map.getFromRatio() == 3)
679       {
680         /*
681          * dna-to-peptide product mapping
682          */
683         // JAL-3187 render on the fly instead
684         // AlignmentUtils.computeProteinFeatures(seq, mapTo, map);
685       }
686       else
687       {
688         /*
689          * nucleotide-to-nucleotide mapping e.g. transcript to CDS
690          */
691         List<SequenceFeature> features = seq.getFeatures()
692                 .getPositionalFeatures(SequenceOntologyI.SEQUENCE_VARIANT);
693         for (SequenceFeature sf : features)
694         {
695           if (FEATURE_GROUP_VCF.equals(sf.getFeatureGroup()))
696           {
697             transferFeature(sf, mapTo, map);
698           }
699         }
700       }
701     }
702   }
703
704   /**
705    * Tries to add overlapping variants read from a VCF file to the given sequence,
706    * and returns the number of variant features added
707    * 
708    * @param seq
709    * @return
710    */
711   protected int loadSequenceVCF(SequenceI seq)
712   {
713     VCFMap vcfMap = getVcfMap(seq);
714     if (vcfMap == null)
715     {
716       return 0;
717     }
718
719     /*
720      * work with the dataset sequence here
721      */
722     SequenceI dss = seq.getDatasetSequence();
723     if (dss == null)
724     {
725       dss = seq;
726     }
727     return addVcfVariants(dss, vcfMap);
728   }
729
730   /**
731    * Answers a map from sequence coordinates to VCF chromosome ranges
732    * 
733    * @param seq
734    * @return
735    */
736   private VCFMap getVcfMap(SequenceI seq)
737   {
738     /*
739      * simplest case: sequence has id and length matching a VCF contig
740      */
741     VCFMap vcfMap = null;
742     if (dictionary != null)
743     {
744       vcfMap = getContigMap(seq);
745     }
746     if (vcfMap != null)
747     {
748       return vcfMap;
749     }
750
751     /*
752      * otherwise, map to VCF from chromosomal coordinates 
753      * of the sequence (if known)
754      */
755     GeneLociI seqCoords = seq.getGeneLoci();
756     if (seqCoords == null)
757     {
758       Cache.log.warn(String.format(
759               "Can't query VCF for %s as chromosome coordinates not known",
760               seq.getName()));
761       return null;
762     }
763
764     String species = seqCoords.getSpeciesId();
765     String chromosome = seqCoords.getChromosomeId();
766     String seqRef = seqCoords.getAssemblyId();
767     MapList map = seqCoords.getMapping();
768
769     // note this requires the configured species to match that
770     // returned with the Ensembl sequence; todo: support aliases?
771     if (!vcfSpecies.equalsIgnoreCase(species))
772     {
773       Cache.log.warn("No VCF loaded to " + seq.getName()
774               + " as species not matched");
775       return null;
776     }
777
778     if (seqRef.equalsIgnoreCase(vcfAssembly))
779     {
780       return new VCFMap(chromosome, map);
781     }
782
783     /*
784      * VCF data has a different reference assembly to the sequence:
785      * query Ensembl to map chromosomal coordinates from sequence to VCF
786      */
787     List<int[]> toVcfRanges = new ArrayList<>();
788     List<int[]> fromSequenceRanges = new ArrayList<>();
789
790     for (int[] range : map.getToRanges())
791     {
792       int[] fromRange = map.locateInFrom(range[0], range[1]);
793       if (fromRange == null)
794       {
795         // corrupted map?!?
796         continue;
797       }
798
799       int[] newRange = mapReferenceRange(range, chromosome, "human", seqRef,
800               vcfAssembly);
801       if (newRange == null)
802       {
803         Cache.log.error(
804                 String.format("Failed to map %s:%s:%s:%d:%d to %s", species,
805                         chromosome, seqRef, range[0], range[1],
806                         vcfAssembly));
807         continue;
808       }
809       else
810       {
811         toVcfRanges.add(newRange);
812         fromSequenceRanges.add(fromRange);
813       }
814     }
815
816     return new VCFMap(chromosome,
817             new MapList(fromSequenceRanges, toVcfRanges, 1, 1));
818   }
819
820   /**
821    * If the sequence id matches a contig declared in the VCF file, and the
822    * sequence length matches the contig length, then returns a 1:1 map of the
823    * sequence to the contig, else returns null
824    * 
825    * @param seq
826    * @return
827    */
828   private VCFMap getContigMap(SequenceI seq)
829   {
830     String id = seq.getName();
831     SAMSequenceRecord contig = dictionary.getSequence(id);
832     if (contig != null)
833     {
834       int len = seq.getLength();
835       if (len == contig.getSequenceLength())
836       {
837         MapList map = new MapList(new int[] { 1, len },
838                 new int[]
839                 { 1, len }, 1, 1);
840         return new VCFMap(id, map);
841       }
842     }
843     return null;
844   }
845
846   /**
847    * Queries the VCF reader for any variants that overlap the mapped chromosome
848    * ranges of the sequence, and adds as variant features. Returns the number of
849    * overlapping variants found.
850    * 
851    * @param seq
852    * @param map
853    *          mapping from sequence to VCF coordinates
854    * @return
855    */
856   protected int addVcfVariants(SequenceI seq, VCFMap map)
857   {
858     boolean forwardStrand = map.map.isToForwardStrand();
859
860     /*
861      * query the VCF for overlaps of each contiguous chromosomal region
862      */
863     int count = 0;
864
865     for (int[] range : map.map.getToRanges())
866     {
867       int vcfStart = Math.min(range[0], range[1]);
868       int vcfEnd = Math.max(range[0], range[1]);
869       try
870       {
871         CloseableIterator<VariantContext> variants = reader
872                 .query(map.chromosome, vcfStart, vcfEnd);
873         while (variants.hasNext())
874         {
875           VariantContext variant = variants.next();
876
877           int[] featureRange = map.map.locateInFrom(variant.getStart(),
878                   variant.getEnd());
879
880           if (featureRange != null)
881           {
882             int featureStart = Math.min(featureRange[0], featureRange[1]);
883             int featureEnd = Math.max(featureRange[0], featureRange[1]);
884             count += addAlleleFeatures(seq, variant, featureStart,
885                     featureEnd, forwardStrand);
886           }
887         }
888         variants.close();
889       } catch (TribbleException e)
890       {
891         /*
892          * RuntimeException throwable by htsjdk
893          */
894         String msg = String.format("Error reading VCF for %s:%d-%d: %s ",
895                 map.chromosome, vcfStart, vcfEnd);
896         Cache.log.error(msg);
897       }
898     }
899
900     return count;
901   }
902
903   /**
904    * A convenience method to get an attribute value for an alternate allele
905    * 
906    * @param variant
907    * @param attributeName
908    * @param alleleIndex
909    * @return
910    */
911   protected String getAttributeValue(VariantContext variant,
912           String attributeName, int alleleIndex)
913   {
914     Object att = variant.getAttribute(attributeName);
915
916     if (att instanceof String)
917     {
918       return (String) att;
919     }
920     else if (att instanceof ArrayList)
921     {
922       return ((List<String>) att).get(alleleIndex);
923     }
924
925     return null;
926   }
927
928   /**
929    * Adds one variant feature for each allele in the VCF variant record, and
930    * returns the number of features added.
931    * 
932    * @param seq
933    * @param variant
934    * @param featureStart
935    * @param featureEnd
936    * @param forwardStrand
937    * @return
938    */
939   protected int addAlleleFeatures(SequenceI seq, VariantContext variant,
940           int featureStart, int featureEnd, boolean forwardStrand)
941   {
942     int added = 0;
943
944     /*
945      * Javadoc says getAlternateAlleles() imposes no order on the list returned
946      * so we proceed defensively to get them in strict order
947      */
948     int altAlleleCount = variant.getAlternateAlleles().size();
949     for (int i = 0; i < altAlleleCount; i++)
950     {
951       added += addAlleleFeature(seq, variant, i, featureStart, featureEnd,
952               forwardStrand);
953     }
954     return added;
955   }
956
957   /**
958    * Inspects one allele and attempts to add a variant feature for it to the
959    * sequence. The additional data associated with this allele is extracted to
960    * store in the feature's key-value map. Answers the number of features added (0
961    * or 1).
962    * 
963    * @param seq
964    * @param variant
965    * @param altAlleleIndex
966    *          (0, 1..)
967    * @param featureStart
968    * @param featureEnd
969    * @param forwardStrand
970    * @return
971    */
972   protected int addAlleleFeature(SequenceI seq, VariantContext variant,
973           int altAlleleIndex, int featureStart, int featureEnd,
974           boolean forwardStrand)
975   {
976     String reference = variant.getReference().getBaseString();
977     Allele alt = variant.getAlternateAllele(altAlleleIndex);
978     String allele = alt.getBaseString();
979
980     /*
981      * insertion after a genomic base, if on reverse strand, has to be 
982      * converted to insertion of complement after the preceding position 
983      */
984     int referenceLength = reference.length();
985     if (!forwardStrand && allele.length() > referenceLength
986             && allele.startsWith(reference))
987     {
988       featureStart -= referenceLength;
989       featureEnd = featureStart;
990       char insertAfter = seq.getCharAt(featureStart - seq.getStart());
991       reference = Dna.reverseComplement(String.valueOf(insertAfter));
992       allele = allele.substring(referenceLength) + reference;
993     }
994
995     /*
996      * build the ref,alt allele description e.g. "G,A", using the base
997      * complement if the sequence is on the reverse strand
998      */
999     StringBuilder sb = new StringBuilder();
1000     sb.append(forwardStrand ? reference : Dna.reverseComplement(reference));
1001     sb.append(COMMA);
1002     sb.append(forwardStrand ? allele : Dna.reverseComplement(allele));
1003     String alleles = sb.toString(); // e.g. G,A
1004
1005     /*
1006      * pick out the consequence data (if any) that is for the current allele
1007      * and feature (transcript) that matches the current sequence
1008      */
1009     String consequence = getConsequenceForAlleleAndFeature(variant, CSQ_FIELD,
1010             altAlleleIndex, csqAlleleFieldIndex,
1011             csqAlleleNumberFieldIndex, seq.getName().toLowerCase(),
1012             csqFeatureFieldIndex);
1013
1014     /*
1015      * pick out the ontology term for the consequence type
1016      */
1017     String type = SequenceOntologyI.SEQUENCE_VARIANT;
1018     if (consequence != null)
1019     {
1020       type = getOntologyTerm(consequence);
1021     }
1022
1023     SequenceFeature sf = new SequenceFeature(type, alleles, featureStart,
1024             featureEnd, FEATURE_GROUP_VCF);
1025     sf.setSource(sourceId);
1026
1027     /*
1028      * save the derived alleles as a named attribute; this will be
1029      * needed when Jalview computes derived peptide variants
1030      */
1031     addFeatureAttribute(sf, Gff3Helper.ALLELES, alleles);
1032
1033     /*
1034      * add selected VCF fixed column data as feature attributes
1035      */
1036     addFeatureAttribute(sf, VCF_POS, String.valueOf(variant.getStart()));
1037     addFeatureAttribute(sf, VCF_ID, variant.getID());
1038     addFeatureAttribute(sf, VCF_QUAL,
1039             String.valueOf(variant.getPhredScaledQual()));
1040     addFeatureAttribute(sf, VCF_FILTER, getFilter(variant));
1041
1042     addAlleleProperties(variant, sf, altAlleleIndex, consequence);
1043
1044     seq.addSequenceFeature(sf);
1045
1046     return 1;
1047   }
1048
1049   /**
1050    * Answers the VCF FILTER value for the variant - or an approximation to it.
1051    * This field is either PASS, or a semi-colon separated list of filters not
1052    * passed. htsjdk saves filters as a HashSet, so the order when reassembled into
1053    * a list may be different.
1054    * 
1055    * @param variant
1056    * @return
1057    */
1058   String getFilter(VariantContext variant)
1059   {
1060     Set<String> filters = variant.getFilters();
1061     if (filters.isEmpty())
1062     {
1063       return NO_VALUE;
1064     }
1065     Iterator<String> iterator = filters.iterator();
1066     String first = iterator.next();
1067     if (filters.size() == 1)
1068     {
1069       return first;
1070     }
1071
1072     StringBuilder sb = new StringBuilder(first);
1073     while (iterator.hasNext())
1074     {
1075       sb.append(";").append(iterator.next());
1076     }
1077
1078     return sb.toString();
1079   }
1080
1081   /**
1082    * Adds one feature attribute unless the value is null, empty or '.'
1083    * 
1084    * @param sf
1085    * @param key
1086    * @param value
1087    */
1088   void addFeatureAttribute(SequenceFeature sf, String key, String value)
1089   {
1090     if (value != null && !value.isEmpty() && !NO_VALUE.equals(value))
1091     {
1092       sf.setValue(key, value);
1093     }
1094   }
1095
1096   /**
1097    * Determines the Sequence Ontology term to use for the variant feature type in
1098    * Jalview. The default is 'sequence_variant', but a more specific term is used
1099    * if:
1100    * <ul>
1101    * <li>VEP (or SnpEff) Consequence annotation is included in the VCF</li>
1102    * <li>sequence id can be matched to VEP Feature (or SnpEff Feature_ID)</li>
1103    * </ul>
1104    * 
1105    * @param consequence
1106    * @return
1107    * @see http://www.sequenceontology.org/browser/current_svn/term/SO:0001060
1108    */
1109   String getOntologyTerm(String consequence)
1110   {
1111     String type = SequenceOntologyI.SEQUENCE_VARIANT;
1112
1113     /*
1114      * could we associate Consequence data with this allele and feature (transcript)?
1115      * if so, prefer the consequence term from that data
1116      */
1117     if (csqAlleleFieldIndex == -1) // && snpEffAlleleFieldIndex == -1
1118     {
1119       /*
1120        * no Consequence data so we can't refine the ontology term
1121        */
1122       return type;
1123     }
1124
1125     if (consequence != null)
1126     {
1127       String[] csqFields = consequence.split(PIPE_REGEX);
1128       if (csqFields.length > csqConsequenceFieldIndex)
1129       {
1130         type = csqFields[csqConsequenceFieldIndex];
1131       }
1132     }
1133     else
1134     {
1135       // todo the same for SnpEff consequence data matching if wanted
1136     }
1137
1138     /*
1139      * if of the form (e.g.) missense_variant&splice_region_variant,
1140      * just take the first ('most severe') consequence
1141      */
1142     if (type != null)
1143     {
1144       int pos = type.indexOf('&');
1145       if (pos > 0)
1146       {
1147         type = type.substring(0, pos);
1148       }
1149     }
1150     return type;
1151   }
1152
1153   /**
1154    * Returns matched consequence data if it can be found, else null.
1155    * <ul>
1156    * <li>inspects the VCF data for key 'vcfInfoId'</li>
1157    * <li>splits this on comma (to distinct consequences)</li>
1158    * <li>returns the first consequence (if any) where</li>
1159    * <ul>
1160    * <li>the allele matches the altAlleleIndex'th allele of variant</li>
1161    * <li>the feature matches the sequence name (e.g. transcript id)</li>
1162    * </ul>
1163    * </ul>
1164    * If matched, the consequence is returned (as pipe-delimited fields).
1165    * 
1166    * @param variant
1167    * @param vcfInfoId
1168    * @param altAlleleIndex
1169    * @param alleleFieldIndex
1170    * @param alleleNumberFieldIndex
1171    * @param seqName
1172    * @param featureFieldIndex
1173    * @return
1174    */
1175   private String getConsequenceForAlleleAndFeature(VariantContext variant,
1176           String vcfInfoId, int altAlleleIndex, int alleleFieldIndex,
1177           int alleleNumberFieldIndex,
1178           String seqName, int featureFieldIndex)
1179   {
1180     if (alleleFieldIndex == -1 || featureFieldIndex == -1)
1181     {
1182       return null;
1183     }
1184     Object value = variant.getAttribute(vcfInfoId);
1185
1186     if (value == null || !(value instanceof List<?>))
1187     {
1188       return null;
1189     }
1190
1191     /*
1192      * inspect each consequence in turn (comma-separated blocks
1193      * extracted by htsjdk)
1194      */
1195     List<String> consequences = (List<String>) value;
1196
1197     for (String consequence : consequences)
1198     {
1199       String[] csqFields = consequence.split(PIPE_REGEX);
1200       if (csqFields.length > featureFieldIndex)
1201       {
1202         String featureIdentifier = csqFields[featureFieldIndex];
1203         if (featureIdentifier.length() > 4
1204                 && seqName.indexOf(featureIdentifier.toLowerCase()) > -1)
1205         {
1206           /*
1207            * feature (transcript) matched - now check for allele match
1208            */
1209           if (matchAllele(variant, altAlleleIndex, csqFields,
1210                   alleleFieldIndex, alleleNumberFieldIndex))
1211           {
1212             return consequence;
1213           }
1214         }
1215       }
1216     }
1217     return null;
1218   }
1219
1220   private boolean matchAllele(VariantContext variant, int altAlleleIndex,
1221           String[] csqFields, int alleleFieldIndex,
1222           int alleleNumberFieldIndex)
1223   {
1224     /*
1225      * if ALLELE_NUM is present, it must match altAlleleIndex
1226      * NB first alternate allele is 1 for ALLELE_NUM, 0 for altAlleleIndex
1227      */
1228     if (alleleNumberFieldIndex > -1)
1229     {
1230       if (csqFields.length <= alleleNumberFieldIndex)
1231       {
1232         return false;
1233       }
1234       String alleleNum = csqFields[alleleNumberFieldIndex];
1235       return String.valueOf(altAlleleIndex + 1).equals(alleleNum);
1236     }
1237
1238     /*
1239      * else consequence allele must match variant allele
1240      */
1241     if (alleleFieldIndex > -1 && csqFields.length > alleleFieldIndex)
1242     {
1243       String csqAllele = csqFields[alleleFieldIndex];
1244       String vcfAllele = variant.getAlternateAllele(altAlleleIndex)
1245               .getBaseString();
1246       return csqAllele.equals(vcfAllele);
1247     }
1248     return false;
1249   }
1250
1251   /**
1252    * Add any allele-specific VCF key-value data to the sequence feature
1253    * 
1254    * @param variant
1255    * @param sf
1256    * @param altAlelleIndex
1257    *          (0, 1..)
1258    * @param consequence
1259    *          if not null, the consequence specific to this sequence (transcript
1260    *          feature) and allele
1261    */
1262   protected void addAlleleProperties(VariantContext variant,
1263           SequenceFeature sf, final int altAlelleIndex, String consequence)
1264   {
1265     Map<String, Object> atts = variant.getAttributes();
1266
1267     for (Entry<String, Object> att : atts.entrySet())
1268     {
1269       String key = att.getKey();
1270
1271       /*
1272        * extract Consequence data (if present) that we are able to
1273        * associated with the allele for this variant feature
1274        */
1275       if (CSQ_FIELD.equals(key))
1276       {
1277         addConsequences(variant, sf, consequence);
1278         continue;
1279       }
1280
1281       /*
1282        * filter out fields we don't want to capture
1283        */
1284       if (!vcfFieldsOfInterest.contains(key))
1285       {
1286         continue;
1287       }
1288
1289       /*
1290        * we extract values for other data which are allele-specific; 
1291        * these may be per alternate allele (INFO[key].Number = 'A') 
1292        * or per allele including reference (INFO[key].Number = 'R') 
1293        */
1294       VCFInfoHeaderLine infoHeader = header.getInfoHeaderLine(key);
1295       if (infoHeader == null)
1296       {
1297         /*
1298          * can't be sure what data belongs to this allele, so
1299          * play safe and don't take any
1300          */
1301         continue;
1302       }
1303
1304       VCFHeaderLineCount number = infoHeader.getCountType();
1305       int index = altAlelleIndex;
1306       if (number == VCFHeaderLineCount.R)
1307       {
1308         /*
1309          * one value per allele including reference, so bump index
1310          * e.g. the 3rd value is for the  2nd alternate allele
1311          */
1312         index++;
1313       }
1314       else if (number != VCFHeaderLineCount.A)
1315       {
1316         /*
1317          * don't save other values as not allele-related
1318          */
1319         continue;
1320       }
1321
1322       /*
1323        * take the index'th value
1324        */
1325       String value = getAttributeValue(variant, key, index);
1326       if (value != null && isValid(variant, key, value))
1327       {
1328         /*
1329          * decode colon, semicolon, equals sign, percent sign, comma (only)
1330          * as required by the VCF specification (para 1.2)
1331          */
1332         value = StringUtils.urlDecode(value, VCF_ENCODABLE);
1333         addFeatureAttribute(sf, key, value);
1334       }
1335     }
1336   }
1337
1338   /**
1339    * Answers true for '.', null, or an empty value, or if the INFO type is String.
1340    * If the INFO type is Integer or Float, answers false if the value is not in
1341    * valid format.
1342    * 
1343    * @param variant
1344    * @param infoId
1345    * @param value
1346    * @return
1347    */
1348   protected boolean isValid(VariantContext variant, String infoId,
1349           String value)
1350   {
1351     if (value == null || value.isEmpty() || NO_VALUE.equals(value))
1352     {
1353       return true;
1354     }
1355     VCFInfoHeaderLine infoHeader = header.getInfoHeaderLine(infoId);
1356     if (infoHeader == null)
1357     {
1358       Cache.log.error("Field " + infoId + " has no INFO header");
1359       return false;
1360     }
1361     VCFHeaderLineType infoType = infoHeader.getType();
1362     try
1363     {
1364       if (infoType == VCFHeaderLineType.Integer)
1365       {
1366         Integer.parseInt(value);
1367       }
1368       else if (infoType == VCFHeaderLineType.Float)
1369       {
1370         Float.parseFloat(value);
1371       }
1372     } catch (NumberFormatException e)
1373     {
1374       logInvalidValue(variant, infoId, value);
1375       return false;
1376     }
1377     return true;
1378   }
1379
1380   /**
1381    * Logs an error message for malformed data; duplicate messages (same id and
1382    * value) are not logged
1383    * 
1384    * @param variant
1385    * @param infoId
1386    * @param value
1387    */
1388   private void logInvalidValue(VariantContext variant, String infoId,
1389           String value)
1390   {
1391     if (badData == null)
1392     {
1393       badData = new HashSet<>();
1394     }
1395     String token = infoId + ":" + value;
1396     if (!badData.contains(token))
1397     {
1398       badData.add(token);
1399       Cache.log.error(String.format("Invalid VCF data at %s:%d %s=%s",
1400               variant.getContig(), variant.getStart(), infoId, value));
1401     }
1402   }
1403
1404   /**
1405    * Inspects CSQ data blocks (consequences) and adds attributes on the sequence
1406    * feature.
1407    * <p>
1408    * If <code>myConsequence</code> is not null, then this is the specific
1409    * consequence data (pipe-delimited fields) that is for the current allele and
1410    * transcript (sequence) being processed)
1411    * 
1412    * @param variant
1413    * @param sf
1414    * @param myConsequence
1415    */
1416   protected void addConsequences(VariantContext variant, SequenceFeature sf,
1417           String myConsequence)
1418   {
1419     Object value = variant.getAttribute(CSQ_FIELD);
1420
1421     if (value == null || !(value instanceof List<?>))
1422     {
1423       return;
1424     }
1425
1426     List<String> consequences = (List<String>) value;
1427
1428     /*
1429      * inspect CSQ consequences; restrict to the consequence
1430      * associated with the current transcript (Feature)
1431      */
1432     Map<String, String> csqValues = new HashMap<>();
1433
1434     for (String consequence : consequences)
1435     {
1436       if (myConsequence == null || myConsequence.equals(consequence))
1437       {
1438         String[] csqFields = consequence.split(PIPE_REGEX);
1439
1440         /*
1441          * inspect individual fields of this consequence, copying non-null
1442          * values which are 'fields of interest'
1443          */
1444         int i = 0;
1445         for (String field : csqFields)
1446         {
1447           if (field != null && field.length() > 0)
1448           {
1449             String id = vepFieldsOfInterest.get(i);
1450             if (id != null)
1451             {
1452               /*
1453                * VCF spec requires encoding of special characters e.g. '='
1454                * so decode them here before storing
1455                */
1456               field = StringUtils.urlDecode(field, VCF_ENCODABLE);
1457               csqValues.put(id, field);
1458             }
1459           }
1460           i++;
1461         }
1462       }
1463     }
1464
1465     if (!csqValues.isEmpty())
1466     {
1467       sf.setValue(CSQ_FIELD, csqValues);
1468     }
1469   }
1470
1471   /**
1472    * A convenience method to complement a dna base and return the string value
1473    * of its complement
1474    * 
1475    * @param reference
1476    * @return
1477    */
1478   protected String complement(byte[] reference)
1479   {
1480     return String.valueOf(Dna.getComplement((char) reference[0]));
1481   }
1482
1483   /**
1484    * Determines the location of the query range (chromosome positions) in a
1485    * different reference assembly.
1486    * <p>
1487    * If the range is just a subregion of one for which we already have a mapping
1488    * (for example, an exon sub-region of a gene), then the mapping is just
1489    * computed arithmetically.
1490    * <p>
1491    * Otherwise, calls the Ensembl REST service that maps from one assembly
1492    * reference's coordinates to another's
1493    * 
1494    * @param queryRange
1495    *          start-end chromosomal range in 'fromRef' coordinates
1496    * @param chromosome
1497    * @param species
1498    * @param fromRef
1499    *          assembly reference for the query coordinates
1500    * @param toRef
1501    *          assembly reference we wish to translate to
1502    * @return the start-end range in 'toRef' coordinates
1503    */
1504   protected int[] mapReferenceRange(int[] queryRange, String chromosome,
1505           String species, String fromRef, String toRef)
1506   {
1507     /*
1508      * first try shorcut of computing the mapping as a subregion of one
1509      * we already have (e.g. for an exon, if we have the gene mapping)
1510      */
1511     int[] mappedRange = findSubsumedRangeMapping(queryRange, chromosome,
1512             species, fromRef, toRef);
1513     if (mappedRange != null)
1514     {
1515       return mappedRange;
1516     }
1517
1518     /*
1519      * call (e.g.) http://rest.ensembl.org/map/human/GRCh38/17:45051610..45109016:1/GRCh37
1520      */
1521     EnsemblMap mapper = new EnsemblMap();
1522     int[] mapping = mapper.getAssemblyMapping(species, chromosome, fromRef,
1523             toRef, queryRange);
1524
1525     if (mapping == null)
1526     {
1527       // mapping service failure
1528       return null;
1529     }
1530
1531     /*
1532      * save mapping for possible future re-use
1533      */
1534     String key = makeRangesKey(chromosome, species, fromRef, toRef);
1535     if (!assemblyMappings.containsKey(key))
1536     {
1537       assemblyMappings.put(key, new HashMap<int[], int[]>());
1538     }
1539
1540     assemblyMappings.get(key).put(queryRange, mapping);
1541
1542     return mapping;
1543   }
1544
1545   /**
1546    * If we already have a 1:1 contiguous mapping which subsumes the given query
1547    * range, this method just calculates and returns the subset of that mapping,
1548    * else it returns null. In practical terms, if a gene has a contiguous
1549    * mapping between (for example) GRCh37 and GRCh38, then we assume that its
1550    * subsidiary exons occupy unchanged relative positions, and just compute
1551    * these as offsets, rather than do another lookup of the mapping.
1552    * <p>
1553    * If in future these assumptions prove invalid (e.g. for bacterial dna?!),
1554    * simply remove this method or let it always return null.
1555    * <p>
1556    * Warning: many rapid calls to the /map service map result in a 429 overload
1557    * error response
1558    * 
1559    * @param queryRange
1560    * @param chromosome
1561    * @param species
1562    * @param fromRef
1563    * @param toRef
1564    * @return
1565    */
1566   protected int[] findSubsumedRangeMapping(int[] queryRange, String chromosome,
1567           String species, String fromRef, String toRef)
1568   {
1569     String key = makeRangesKey(chromosome, species, fromRef, toRef);
1570     if (assemblyMappings.containsKey(key))
1571     {
1572       Map<int[], int[]> mappedRanges = assemblyMappings.get(key);
1573       for (Entry<int[], int[]> mappedRange : mappedRanges.entrySet())
1574       {
1575         int[] fromRange = mappedRange.getKey();
1576         int[] toRange = mappedRange.getValue();
1577         if (fromRange[1] - fromRange[0] == toRange[1] - toRange[0])
1578         {
1579           /*
1580            * mapping is 1:1 in length, so we trust it to have no discontinuities
1581            */
1582           if (MappingUtils.rangeContains(fromRange, queryRange))
1583           {
1584             /*
1585              * fromRange subsumes our query range
1586              */
1587             int offset = queryRange[0] - fromRange[0];
1588             int mappedRangeFrom = toRange[0] + offset;
1589             int mappedRangeTo = mappedRangeFrom + (queryRange[1] - queryRange[0]);
1590             return new int[] { mappedRangeFrom, mappedRangeTo };
1591           }
1592         }
1593       }
1594     }
1595     return null;
1596   }
1597
1598   /**
1599    * Transfers the sequence feature to the target sequence, locating its start
1600    * and end range based on the mapping. Features which do not overlap the
1601    * target sequence are ignored.
1602    * 
1603    * @param sf
1604    * @param targetSequence
1605    * @param mapping
1606    *          mapping from the feature's coordinates to the target sequence
1607    */
1608   protected void transferFeature(SequenceFeature sf,
1609           SequenceI targetSequence, MapList mapping)
1610   {
1611     int[] mappedRange = mapping.locateInTo(sf.getBegin(), sf.getEnd());
1612   
1613     if (mappedRange != null)
1614     {
1615       String group = sf.getFeatureGroup();
1616       int newBegin = Math.min(mappedRange[0], mappedRange[1]);
1617       int newEnd = Math.max(mappedRange[0], mappedRange[1]);
1618       SequenceFeature copy = new SequenceFeature(sf, newBegin, newEnd,
1619               group, sf.getScore());
1620       targetSequence.addSequenceFeature(copy);
1621     }
1622   }
1623
1624   /**
1625    * Formats a ranges map lookup key
1626    * 
1627    * @param chromosome
1628    * @param species
1629    * @param fromRef
1630    * @param toRef
1631    * @return
1632    */
1633   protected static String makeRangesKey(String chromosome, String species,
1634           String fromRef, String toRef)
1635   {
1636     return species + EXCL + chromosome + EXCL + fromRef + EXCL
1637             + toRef;
1638   }
1639 }