/*
 Copyright (C) 2003 EBI, GRL
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
 
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.ensembl.healthcheck.testcase.generic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.ensembl.healthcheck.DatabaseRegistryEntry;
import org.ensembl.healthcheck.ReportManager;
import org.ensembl.healthcheck.Species;
import org.ensembl.healthcheck.Team;
import org.ensembl.healthcheck.testcase.Priority;
import org.ensembl.healthcheck.testcase.SingleDatabaseTestCase;

/**
 * Check for certain combinations of logic name and transcript name.
 */

public class TranscriptNames extends SingleDatabaseTestCase {

	/**
	 * Constructor
	 */
	public TranscriptNames() {

		addToGroup("post_genebuild");
		addToGroup("release");
		addToGroup("core_xrefs");
		addToGroup("pre-compara-handover");
		addToGroup("post-compara-handover");
		
		setDescription("Check for certain combinations of logic name and transcript name.");
		setPriority(Priority.AMBER);
		setEffect("Transcript names do not match the logic names.");
		setTeamResponsible(Team.CORE);
	}

	/**
	 * Run the test.
	 * 
	 * @param dbre
	 *          The database to use.
	 * @return true if the test passed.
	 * 
	 */
	public boolean run(DatabaseRegistryEntry dbre) {

		boolean result = true;

		// only valid in human, mouse and zebrafish
		Species s = dbre.getSpecies();
		if (!(s.equals(Species.HOMO_SAPIENS) || s.equals(Species.MUS_MUSCULUS) || s.equals(Species.DANIO_RERIO))) {
			return true;
		}

		// Hash of logic_names to the patterns that transcript names should match
		Map<String, String> logicNameRegexp = new HashMap<String, String>();
		logicNameRegexp.put("ensembl", "-2[0-9][0-9]$");
		logicNameRegexp.put("ensembl_havana_transcript", "-0[0-9][0-9]$");
		logicNameRegexp.put("havana", "-0[0-9][0-9]$");
		//logicNameRegexp.put("ncrna", "-2[0-9][0-9]$");
		logicNameRegexp.put("ensembl_ig_gene", "-2[0-9][0-9]$");
		logicNameRegexp.put("havana_ig_gene", "-0[0-9][0-9]$");
		logicNameRegexp.put("ncrna_pseudogene", "-2[0-9][0-9]$");
		logicNameRegexp.put("mt_genbank_import", "-2[0-9][0-9]$");
		logicNameRegexp.put("lrg_import", "-2[0-9][0-9]$");
		logicNameRegexp.put("ensembl_lincrna", "-2[0-9][0-9]$");

		Connection con = dbre.getConnection();

		try {

			PreparedStatement stmt = con
					.prepareStatement("SELECT COUNT(*) FROM analysis a, transcript t, xref x WHERE a.analysis_id=t.analysis_id AND t.display_xref_id=x.xref_id AND a.logic_name=? AND x.display_label NOT REGEXP ?");

			for (String logicName : logicNameRegexp.keySet()) {

				String regexp = logicNameRegexp.get(logicName);
				stmt.setString(1, logicName);
				stmt.setString(2, regexp);

				ResultSet rs = stmt.executeQuery();

				rs.first();
				int rows = rs.getInt(1);

				if (rows > 0) {

					result = false;
					ReportManager.problem(this, con, String.format("%d transcripts with logic name %s have names which don't match the required pattern (%s)", rows, logicName, regexp));

				} else {

					ReportManager.correct(this, con, String.format("All transcripts with logic name %s have correct names", logicName));

				}

				rs.close();

			}

		} catch (SQLException se) {
			se.printStackTrace();
		}

		return result;

	}

} // TranscriptNames

@@ -1254,16 +1254,62 @@
 stCase;%0A
+import org.ensembl.healthcheck.util.DBUtils;%0A%0A
 %0A/**%0A * 
@@ -2165,114 +2165,75 @@
 ();%0A
-%09%09if (!(s.equals(Species.HOMO_SAPIENS) %7C%7C s.equals(Species.MUS_MUSCULUS) %7C%7C s.equals(Species.DANIO_RERIO))
+                boolean is_merged = testMerged(s);%0A%09%09if (!is_merged
 ) %7B%0A
@@ -3980,16 +3980,401 @@
 t;%0A%0A%09%7D%0A%0A
+%0A  private boolean testMerged(Species s) %7B%0A    boolean result = false;%0A    int taxon = s.getTaxonID();%0A    int rows = DBUtils.getRowCount(getProductionDatabase().getConnection(), %22SELECT count(*) FROM species s, attrib_type at WHERE at.attrib_type_id = s.attrib_type_id AND code = 'merged' AND taxon = %22 + taxon);%0A    if (rows %3E 0) %7B%0A      result = true;%0A    %7D%0A    return result;%0A  %7D%0A%0A
 %7D // Tra

test is run for merged species check if species is merged rather than have a hard-coded list of species