From: miles Date: Tue, 17 Jul 2012 18:11:08 +0000 (+0000) Subject: Species now unique and alphabetized, geneMaps properly constructed, no errors. X-Git-Url: http://gitweb.planteome.org/?a=commitdiff_plain;h=48ec9fc29e3a424221b6dad21954e45570865456;p=old-jaiswallab-svn%2F.git Species now unique and alphabetized, geneMaps properly constructed, no errors. svn path=/; revision=362 --- diff --git a/Personnel/miles/2/.classpath b/Personnel/miles/2/.classpath new file mode 100644 index 0000000..4e8113d --- /dev/null +++ b/Personnel/miles/2/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/Personnel/miles/2/.gitignore b/Personnel/miles/2/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/Personnel/miles/2/.project b/Personnel/miles/2/.project new file mode 100644 index 0000000..9142c72 --- /dev/null +++ b/Personnel/miles/2/.project @@ -0,0 +1,17 @@ + + + Heatmap + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Personnel/miles/2/.settings/org.eclipse.jdt.core.prefs b/Personnel/miles/2/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..03168ac --- /dev/null +++ b/Personnel/miles/2/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Wed Jul 11 10:08:14 PDT 2012 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/Personnel/miles/2/Lib/mysql-connector-java-5.0.8-bin.jar b/Personnel/miles/2/Lib/mysql-connector-java-5.0.8-bin.jar new file mode 100644 index 0000000..0170c3e Binary files /dev/null and b/Personnel/miles/2/Lib/mysql-connector-java-5.0.8-bin.jar differ diff --git a/Personnel/miles/2/src/Main.java b/Personnel/miles/2/src/Main.java new file mode 100644 index 0000000..dd15395 --- /dev/null +++ b/Personnel/miles/2/src/Main.java @@ -0,0 +1,204 @@ +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; + } + +} diff --git a/Personnel/miles/2/src/Species.java b/Personnel/miles/2/src/Species.java new file mode 100644 index 0000000..dd55da6 --- /dev/null +++ b/Personnel/miles/2/src/Species.java @@ -0,0 +1,31 @@ +import java.util.ArrayList; + + +public class Species { + public String name; + public int[][] geneMap = {}; + private int clusterTotal = 0; + + public Species() { + /** + ArrayList tmp = new ArrayList(); + tmp.add(-1); + System.out.println(tmp.get(0)); + geneMap.add(tmp); + * + */ + } + + public void setName(String newName) { + name = newName; + } + + // adds a cluster, if the cluster already exists in this species' gene map, then it adds a hit to the count + public void addCluster(int clusterID, int hits) { + geneMap = new int[geneMap.length + 1][2]; + geneMap[clusterTotal][0] = clusterID; + geneMap[clusterTotal][1] = hits; + clusterTotal ++; + } + +}