/*
 * Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
 * Copyright [2016-2019] EMBL-European Bioinformatics Institute
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.ensembl.healthcheck.testcase.compara;

import java.sql.Connection;

import org.ensembl.healthcheck.DatabaseRegistryEntry;
import org.ensembl.healthcheck.ReportManager;
import org.ensembl.healthcheck.Team;
import org.ensembl.healthcheck.testcase.compara.AbstractComparaTestCase;

/**
 * An EnsEMBL Healthcheck test case that looks for the consistency of the
 * genomic_align_tree table
 */

public class CheckGenomicAlignTreeTable extends AbstractComparaTestCase {

	public CheckGenomicAlignTreeTable() {
		setDescription("Check the consistency of the genomic_align_tree table.");
		setTeamResponsible(Team.COMPARA);
	}

	public boolean run(DatabaseRegistryEntry dbre) {
		Connection con = dbre.getConnection();

		if (!tableHasRows(con, "genomic_align_tree")) {
			ReportManager.correct(this, con, "NO ENTRIES in genomic_align_tree table, so nothing to test IGNORED");
			return true;
		}

		boolean result = true;

		// Check the left_node_id values are set (and assume right_node_ids have also been set)
		// FIXME: this will have to be updated when left_node_id becomes NULLable
		result &= checkCountIsNonZero(con, "genomic_align_tree", "left_node_id != 0");

		/* Looking at distance_to_parent > 1 is true for LOW_COVERAGE but not epo */
		/* Update 2015-30-04: there are nodes with distance_to_parent > 1
		 * in all the EPO alignments, but also for the "11 fish EPO_LOW_COVERAGE"
		 */
		//result &= checkCountIsZero(con, "genomic_align_tree", "distance_to_parent > 1");

		return result;
	}

} // CheckGenomicAlignTreeTable

@@ -1021,16 +1021,61 @@
 estCase;
+%0Aimport org.ensembl.healthcheck.util.DBUtils;
 %0A%0A/**%0A *
@@ -1705,16 +1705,423 @@
  true;%0A%0A
+%09%09String%5B%5D method_link_species_set_ids = DBUtils.getColumnValues(con, %22SELECT method_link_species_set_id FROM method_link_species_set LEFT JOIN method_link USING (method_link_id) WHERE class IN (%5C%22GenomicAlignTree.ancestral_alignment%5C%22, %5C%22GenomicAlignTree.tree_alignment%5C%22)%22);%0A%0A%09%09for (String mlss_id: method_link_species_set_ids) %7B%0A%09%09%09String mlss_id_condition = %22FLOOR(node_id/10000000000) = %22 + mlss_id;%0A%0A%09
 %09%09// Che
@@ -2204,16 +2204,17 @@
  set)%0A%09%09
+%09
 // FIXME
@@ -2281,16 +2281,17 @@
 Lable%0A%09%09
+%09
 result &
@@ -2339,17 +2339,42 @@
 _tree%22, 
-%22
+mlss_id_condition + %22 AND 
 left_nod
@@ -2387,16 +2387,17 @@
 = 0%22);%0A%0A
+%09
 %09%09/* Loo
@@ -2467,16 +2467,17 @@
  epo */%0A
+%09
 %09%09/* Upd
@@ -2538,16 +2538,17 @@
 t %3E 1%0A%09%09
+%09
  * in al
@@ -2615,20 +2615,22 @@
 RAGE%22%0A%09%09
+%09
  */%0A
+%09
 %09%09//resu
@@ -2679,17 +2679,42 @@
 _tree%22, 
-%22
+mlss_id_condition + %22 AND 
 distance
@@ -2730,16 +2730,20 @@
 t %3E 1%22);
+%0A%09%09%7D
 %0A%0A%09%09retu

test each alignment separately, otherwise the test would still pass even if only 1 alignment meets the condition