/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package de.charite.compbio.exomiser.core.factories;

import jannovar.exception.JannovarException;
import jannovar.io.SerializationManager;
import jannovar.reference.Chromosome;
import jannovar.reference.TranscriptModel;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Handles de-serialising of known genes files produced from UCSC or Ensemble data.
 * 
 * @author Jules Jacobsen <jules.jacobsen@sanger.ac.uk>
 */
public class ChromosomeMapFactory {
    
    public static final Logger logger = LoggerFactory.getLogger(ChromosomeMapFactory.class);
    
    /**
     * Jannovar makes a serialized file that represents a
     * HashMap<String, TranscriptModel> containing each and every
     * {@link jannovar.reference.TranscriptModel TranscriptModel} object. This
     * method both deserializes this file and also adds each TranscriptModel to
     * the corresponding IntervalTree of the
     * {@link jannovar.reference.Chromosome Chromosome} object. When we are
     * done, the {@link exomizer.Exomizer#chromosomeMap} contains Chromosome
     * objects for chromosomes 1-22,X,Y, and M, each of which contains the
     * TranscriptModel objects for each of the genes located on those
     * chromosomes.
     */
    public static Map<Byte,Chromosome> deserializeKnownGeneData(Path serealizedKnownGenePath) {
        logger.info("DESERIALISING KNOWN GENES FILE: {}", serealizedKnownGenePath);
        ArrayList<TranscriptModel> kgList = null;
        SerializationManager manager = new SerializationManager();
        try {
            kgList = manager.deserializeKnownGeneList(serealizedKnownGenePath.toString());
        } catch (JannovarException je) {
            String message = String.format("Unable to deserialize the known gene definition file: %s", serealizedKnownGenePath);
            logger.error(message);
            throw new RuntimeException(message, je);
        }
        logger.info("DONE DESERIALISING KNOWN GENES");
        return Chromosome.constructChromosomeMapWithIntervalTree(kgList);
    }
 
}

@@ -183,18 +183,16 @@
 .%0D%0A */%0D%0A
-%0D%0A
 package 
@@ -632,16 +632,20 @@
 Ensemble
+%0D%0A *
  data.%0D%0A
@@ -646,17 +646,16 @@
 ata.%0D%0A *
- 
 %0D%0A * @au
@@ -747,20 +747,16 @@
 tory %7B%0D%0A
-    
 %0D%0A    pu
@@ -847,16 +847,97 @@
 );%0D%0A    
+private static final SerializationManager manager = new SerializationManager();%0D%0A
 %0D%0A    /*
@@ -996,24 +996,16 @@
 esents a
-%0D%0A     *
  HashMap
@@ -1012,16 +1012,24 @@
 %3CString,
+%0D%0A     *
  Transcr
@@ -1631,16 +1631,17 @@
 ap%3CByte,
+ 
 Chromoso
@@ -1826,90 +1826,363 @@
 el%3E 
-kgList = null;%0D%0A        SerializationManager manager = new SerializationManager();
+transcriptModels = makeTranscriptModelsFromFile(serealizedKnownGenePath);%0D%0A        logger.info(%22DONE DESERIALISING KNOWN GENES%22);%0D%0A        return Chromosome.constructChromosomeMapWithIntervalTree(transcriptModels);%0D%0A    %7D%0D%0A%0D%0A    private static ArrayList%3CTranscriptModel%3E makeTranscriptModelsFromFile(Path serealizedKnownGenePath) throws RuntimeException %7B
 %0D%0A  
@@ -2210,16 +2210,14 @@
     
-kgList =
+return
  man
@@ -2561,147 +2561,15 @@
  %7D%0D%0A
-        logger.info(%22DONE DESERIALISING KNOWN GENES%22);%0D%0A        return Chromosome.constructChromosomeMapWithIntervalTree(kgList);%0D%0A
     %7D%0D%0A
- 
 %0D%0A%7D%0D

Test refactor of ChromosomeMapFactory.