/**
   * Test loading a session with spaces in the path and special characters in the file names
   *
   * @throws Exception
   */
  @Test
  public void testLoadSessionSpecialChars() throws Exception {
    int expTrackCount = 5;
    String path = TestUtils.DATA_DIR + "sessions/special_chars.xml";
    rewriteRestoreSession(path);

    assertEquals(expTrackCount, IGV.getInstance().getVisibleTrackCount());
  }
  @Test
  public void testLoadDataSourceTrack() throws Exception {
    String path = TestUtils.DATA_DIR + "sessions/tdf_session.xml";
    rewriteRestoreSession(path);

    DataSourceTrack track = (DataSourceTrack) IGV.getInstance().getAllTracks().get(0);
    assertEquals(true, TestUtils.runMethod(track, "getNormalize"));
    assertEquals("NA12878.SLX.egfr.sam.tdf", track.getName());
  }
  @Test
  public void testLoadSessionWithoutLoadedGenome() throws Exception {
    GenomeManager.getInstance().setCurrentGenome(null);
    assertNull(GenomeManager.getInstance().getGenomeId());

    rewriteRestoreSession(EcoliFastaSessionPath);

    assertNotNull(GenomeManager.getInstance().getGenomeId());
  }
  /**
   * Very basic test, just makes sure it loads without throwing an exception
   *
   * @throws Exception
   */
  @Test
  public void testLoadBedtools_Intersect() throws Exception {
    String sessionPath = TestUtils.DATA_DIR + "sessions/GSM_bedtools_intersect.xml";
    String toolPath = "/usr/local/bin/bedtools";
    boolean haveTool = PluginSpecReader.isToolPathValid(toolPath);
    Assume.assumeTrue(haveTool);

    rewriteRestoreSession(sessionPath);
  }
  private void tstLoadVCF(String sessionPath) throws Exception {
    rewriteRestoreSession(sessionPath);

    VariantTrack vTrack = (VariantTrack) IGV.getInstance().getAllTracks().get(0);

    assertEquals(3, vTrack.getAllSamples().size());
    assertEquals(8, vTrack.getSquishedHeight());
    assertEquals(VariantTrack.ColorMode.ALLELE, vTrack.getColorMode());
  }
  @Test
  public void testLoadSequenceTrackOnlyDiffGenome() throws Exception {
    rewriteRestoreSession(EcoliFastaSessionPath);

    assertFalse("Gene track exists but it shouldn't", IGV.getInstance().hasGeneTrack());

    assertTrue("Sequence track doesn't exist but it should", IGV.getInstance().hasSequenceTrack());
    assertNotNull(
        "Sequence track doesn't exist but it should", IGV.getInstance().getSequenceTrack());
  }
  @Test
  public void testLoadOverlaySession() throws Exception {
    String sessionpath = TestUtils.DATA_DIR + "sessions/hind_gistic_overlay.xml";
    rewriteRestoreSession(sessionpath);

    // Load the session, check things went well
    List<DataTrack> dataTracks = IGV.getInstance().getDataTracks();
    assertEquals(3, dataTracks.size());
    assertTrue(dataTracks.get(0) instanceof MergedTracks);
  }
  /**
   * Loads path, returns first alignment track
   *
   * @param sessionPath
   * @return
   */
  private AlignmentTrack getAlignmentTrack(String sessionPath) throws Exception {
    rewriteRestoreSession(sessionPath);

    AlignmentTrack alTrack = null;
    for (Track tk : IGV.getInstance().getAllTracks()) {
      if (tk instanceof AlignmentTrack) {
        alTrack = (AlignmentTrack) tk;
      }
    }

    return alTrack;
  }
  @Test
  public void testLoadCoverageTrackSession() throws Exception {
    String path = TestUtils.DATA_DIR + "sessions/coverage_snpThreshold_NA06984.xml";
    rewriteRestoreSession(path);

    // We should have 1 coverage track
    CoverageTrack track = (CoverageTrack) IGV.getInstance().getAllTracks().get(0);

    assertTrue(track.isShowReference());
    assertEquals(0.5, track.getSnpThreshold(), 1e-5);
    assertTrue(track.getAutoScale());
  }
 @Test
 public void testGWAS() throws Exception {
   String sessionPath = TestUtils.DATA_DIR + "sessions/smallp_session.xml";
   rewriteRestoreSession(sessionPath);
   boolean haveGWAS = false;
   for (Track track : IGV.getInstance().getAllTracks()) {
     if (track instanceof GWASTrack) {
       haveGWAS = true;
       break;
     }
   }
   assertTrue("No GWAS track loaded", haveGWAS);
   assertEquals(3, IGV.getInstance().getVisibleTrackCount());
 }
  /**
   * Test loading a session with a bedtools analysis track
   *
   * @throws Exception
   */
  @Test
  public void testLoadBedtools_Subtract() throws Exception {
    String toolPath = "/usr/local/bin/bedtools";
    boolean haveTool = PluginSpecReader.isToolPathValid(toolPath);
    Assume.assumeTrue(haveTool);

    String sessionPath = TestUtils.DATA_DIR + "sessions/GSM_bedtools_subtract.xml";

    rewriteRestoreSession(sessionPath);

    String trackAname = "GSM1004654_100k.bed";
    String trackBname = "GSM1004654_10k.bed";
    String analId = "BEDTools Remove/Subtract";

    FeatureTrack analTrack = null, trackA = null, trackB = null;
    for (Track track : IGV.getInstance().getAllTracks()) {
      if (track.getId().equals(analId)) {
        analTrack = (FeatureTrack) track;
      } else if (track.getName().equals(trackAname)) {
        trackA = (FeatureTrack) track;
      } else if (track.getName().equals(trackBname)) {
        trackB = (FeatureTrack) track;
      }
    }

    String chr = "chr2";
    int start = 177932002;
    int end = 180561093;

    List<Feature> aFeatures = trackA.getFeatures(chr, start, end);
    List<Feature> bFeatures = trackB.getFeatures(chr, start, end);
    List<Feature> analFeatures = analTrack.getFeatures(chr, start, end);

    // Fairly coarse check, these actually might not exactly be true
    // due to splitting of exons
    int checked = 0;
    for (Feature afeat : analFeatures) {
      if (afeat.getStart() < start || afeat.getStart() > end) continue;
      // This particular feature splits funny, it's not a bug, at least not with IGV
      if (afeat.getStart() == 178625608) continue;

      assertTrue(listContainsFeature(aFeatures, afeat));
      assertFalse(listContainsFeature(bFeatures, afeat));
      checked++;
    }
    assert checked > 0;
  }
  @Test
  public void testLoadSequenceTrackOnlySameGenome() throws Exception {
    String genomePath = TestUtils.DATA_DIR + "fasta/ecoli_out.padded.fasta";
    GenomeManager.getInstance().loadGenome(genomePath, null);
    genome = GenomeManager.getInstance().getCurrentGenome();
    IGV.getInstance().removeTracks(IGV.getInstance().getAllTracks());

    assertFalse("Sequence Track exists but it shouldn't", IGV.getInstance().hasSequenceTrack());

    String sessionPath = TestUtils.DATA_DIR + "sessions/ecoli_out_seqonly.xml";
    rewriteRestoreSession(sessionPath);

    assertFalse("Gene track exists but it shouldn't", IGV.getInstance().hasGeneTrack());

    assertTrue("Sequence track doesn't exist but it should", IGV.getInstance().hasSequenceTrack());
    assertNotNull(
        "Sequence track doesn't exist but it should", IGV.getInstance().getSequenceTrack());
  }
  /** Test creating an analysis track, saving the session, and loading it */
  @Ignore("Not ready yet")
  @Test
  public void testSaveLoadPluginSession() throws Exception {
    String toolPath = "/usr/local/bin/bedtools";
    boolean haveTool = PluginSpecReader.isToolPathValid(toolPath);
    Assume.assumeTrue(haveTool);

    String sessionPath = TestUtils.DATA_DIR + "sessions/GSM_beds.xml";
    rewriteRestoreSession(sessionPath);

    String trackAname = "GSM1004654_100k.bed";
    String trackBname = "GSM1004654_10k.bed";

    // Generate bedtools subtraction track
    PluginSpecReader reader = PluginSpecReader.create(toolPath);
    PluginSpecReader.Tool tool = reader.getTools().get(0);
    PluginSpecReader.Command command =
        AbstractPluginTest.findCommandElementByName(tool, "Subtract");
  }
  @Test
  public void testLoadCombinedDataSourceSession() throws Exception {
    String sessionpath = TestUtils.DATA_DIR + "sessions/subtypes_wdiff.xml";
    rewriteRestoreSession(sessionpath);

    String combPart0 = "TRIBE_p_TCGAaffx_B1_2_GBM_Nsp_GenomeWideSNP_6_E11_155884";
    String combPart1 = "TRIGS_p_TCGAaffxB5_sty_GenomeWideSNP_6_D05_223156";

    DataTrack track0 =
        Iterables.find(IGV.getInstance().getDataTracks(), new ContIdPredicate(combPart0));
    DataTrack track1 =
        Iterables.find(IGV.getInstance().getDataTracks(), new ContIdPredicate(combPart1));

    DataSourceTrack combTrack =
        (DataSourceTrack)
            Iterables.find(
                IGV.getInstance().getDataTracks(),
                new ContIdPredicate(new String[] {combPart0, combPart1}));

    assertTrue(combTrack.getRenderer() instanceof BarChartRenderer);
    assertEquals("Difference", combTrack.getName());

    String chr = "chr1";
    int start = 0;
    int end = 1000;

    LocusScore sumScore0 = track0.getSummaryScores(chr, start, end, 0).get(0);
    LocusScore sumScore1 = track1.getSummaryScores(chr, start, end, 0).get(0);
    LocusScore sumScoreComb = combTrack.getSummaryScores(chr, start, end, 0).get(0);

    assertEquals(sumScore0.getStart(), sumScore1.getStart());
    assertEquals(sumScore0.getStart(), sumScoreComb.getStart());
    assertEquals(sumScore1.getEnd(), sumScoreComb.getEnd());

    assertEquals(sumScore0.getScore() - sumScore1.getScore(), sumScoreComb.getScore(), 1e-10);
  }