--- /dev/null
+import java.sql.*;
+import java.util.Properties;
+import java.awt.GradientPaint;
+
+//mysql> select super_id, species, count(species) from super_clust where super_id IN (1,2) group by species, super_id order by species;
+
+
+
+
+/**
+ * @author miles
+ *
+ */
+public class Main {
+ String[] species;
+
+ public static void main(String[] args) {
+ String[] geneIDs = new String[] {"mgf009407m"};
+ getHeatChartData(geneIDs);
+ }
+
+ // creates a connection, distributes work and information to the various helper functions
+ // gathers the final list of species and their geneMaps in one place
+ public static Species[] getHeatChartData(String[] geneIDs) { // TODO make error messages more helpful
+ int[] clusterIDs;
+
+ Connection connection = openConnection();
+ Statement statement = null;
+
+ connection = selectDatabase(statement, connection);
+
+ clusterIDs = getAllClusters(geneIDs, connection);
+
+ Species[] species = findAllSpecies(clusterIDs, connection);
+ // TODO find a way to sort the species
+ //species.sort;
+
+ for(int i = 0; i < species.length; i ++) {
+ System.out.println(species[i].name + " " + species[i].geneMap[0][1]);
+ }
+
+ return species;
+ }
+
+ // prints out the heatMap
+ public void displayHeatChart(Species[] species) {
+
+ }
+
+ // generates the logic to gather the list of all species that fall into the given cluster ID's
+ // generates a list of unique species objects and their geneMaps
+ private static Species[] findAllSpecies(int[] clusterIDs, Connection connection) {
+ Species[] rawSpecies;
+ int speciesCount = 0;
+ String currentSpecies = null;
+
+ String inStatement = getInStatement(clusterIDs, connection);
+
+ PreparedStatement findSpecies = null;
+ ResultSet rs = null;
+
+ try {
+ findSpecies = connection.prepareStatement(
+ "select super_id, species, count(species) from super_clust where super_id "
+ + inStatement +
+ "group by species, super_id order by species");
+ } catch (SQLException e) {
+ System.out.println("Statement failed to prepare");
+ }
+
+
+ try {
+ rs = findSpecies.executeQuery();
+ }
+ catch (SQLException e) {
+ System.out.println("Could not execute Query, possible error in statement");
+ }
+
+ try {
+ rs.last();
+ speciesCount = rs.getRow();
+ rs.beforeFirst();
+ rs.next();
+ currentSpecies = rs.getString(2);
+ }
+ catch (SQLException e) {
+ System.out.println("Error in returned data - check database");
+ }
+
+ rawSpecies = new Species[speciesCount]; // initialize it to the correct size
+ speciesCount = 0; // reusing the species count variable for other purposes
+
+ try {
+ rawSpecies[speciesCount] = new Species();
+ rawSpecies[speciesCount].setName(currentSpecies);
+ rawSpecies[speciesCount].addCluster(rs.getInt(1), rs.getInt(3));
+ while(rs.next()) {
+ String name = rs.getString(2);
+ if (name == currentSpecies) {
+ rawSpecies[speciesCount].addCluster(rs.getInt(1), rs.getInt(3));
+ } else {
+ currentSpecies = name;
+ speciesCount ++;
+ rawSpecies[speciesCount] = new Species();
+ rawSpecies[speciesCount].setName(currentSpecies);
+ rawSpecies[speciesCount].addCluster(rs.getInt(1), rs.getInt(3));
+ }
+ }
+ }
+ catch (SQLException e) {
+ System.out.println("Unexpected Error");
+ }
+
+ return rawSpecies;
+
+ }
+
+ private static String getInStatement(int[] clusterIDs, Connection connection) {
+ String inStatement = "IN (";
+ for (int i = 0; i < clusterIDs.length; i ++) {
+ inStatement = inStatement + clusterIDs[i];
+ if(i+1 < clusterIDs.length) {
+ inStatement = inStatement + ",";
+ } else {
+ inStatement = inStatement + ")";
+ }
+ }
+ return inStatement;
+ }
+
+ // finds ALL clusters that the given geneID's fall into
+ private static int[] getAllClusters(String[] geneIDs, Connection connection) {
+ ResultSet results = null;
+ int[] clusterIDs = {-1};
+
+ for (int i = 0; i < geneIDs.length; i ++) {
+ // finds the cluster that the gene is in
+ PreparedStatement findCluster;
+ try {
+ findCluster = connection.prepareStatement("SELECT DISTINCT super_id FROM super_clust WHERE gene = '" + geneIDs[i] + "'");
+ } catch (SQLException e1) {
+ System.out.println("Unexpected Error");
+ findCluster = null;
+ }
+ try {
+ results = findCluster.executeQuery();
+ } catch (SQLException e) {
+ System.out.println("Unable to execute Query");
+ }
+ try {
+ results.next();
+ clusterIDs[i] = (results.getInt(1));
+ System.out.println(clusterIDs[i]);
+ } catch (SQLException e) {
+ System.out.println("Gene not found");
+ }
+ }
+ return clusterIDs;
+ }
+
+ // selects the database to use
+ private static Connection selectDatabase(Statement statement, Connection connection) {
+ PreparedStatement useDatabase;
+ try {
+ useDatabase = connection.prepareStatement("USE inparanoid_data");
+ } catch (SQLException e2) {
+ System.out.println("Unexpected Error");
+ useDatabase = null;
+ }
+ try {
+ useDatabase.execute();
+ } catch (SQLException e) {
+ System.out.println("Unexpected Error");
+ }
+ return connection;
+ }
+
+ private static Connection openConnection() {
+ Properties properties = new Properties();
+ properties.setProperty("user", "inparanoid-read-user");
+ properties.setProperty("password", "inparanoid-read-user_pw");
+ properties.setProperty("autoReconnect", "true");
+ properties.setProperty("useOldUTF8Behavior", "true");
+ properties.setProperty("zeroDateTimeBehavior", "convertToNull");
+ properties.setProperty("dontTrackOpenResources", "true");
+ String url = "jdbc:mysql://floret.cgrb.oregonstate.edu:3306/inparanoid_data";
+
+ try {
+ Class.forName("com.mysql.jdbc.Driver").newInstance(); //Or any other driver
+ }
+ catch(Exception x){
+ System.out.println( "Unable to load the driver class!" );
+ }
+ try {
+ Connection c = DriverManager.getConnection(url, properties);
+ return c;
+ }
+ catch(SQLException x) {
+ System.out.println("Couldn’t get connection!");
+ }
+ return null;
+ }
+
+}