From: miles Date: Mon, 23 Jul 2012 22:16:47 +0000 (+0000) Subject: Working copy with JHeatChart.java. Puts the species axis at the bottom of the graph... X-Git-Url: http://gitweb.planteome.org/?a=commitdiff_plain;h=8c0af5ce81ce7ea7423bf49fe752829de5373f75;p=old-jaiswallab-svn%2F.git Working copy with JHeatChart.java. Puts the species axis at the bottom of the graph rather than the top. svn path=/; revision=367 --- diff --git a/Personnel/miles/2/.classpath b/Personnel/miles/2/.classpath index 4e8113d..435c452 100644 --- a/Personnel/miles/2/.classpath +++ b/Personnel/miles/2/.classpath @@ -3,5 +3,6 @@ + diff --git a/Personnel/miles/2/Lib/jheatchart-0.6.jar b/Personnel/miles/2/Lib/jheatchart-0.6.jar new file mode 100644 index 0000000..889b45b Binary files /dev/null and b/Personnel/miles/2/Lib/jheatchart-0.6.jar differ diff --git a/Personnel/miles/2/src/Heatmap.java b/Personnel/miles/2/src/Heatmap.java index ee58794..2a77845 100644 --- a/Personnel/miles/2/src/Heatmap.java +++ b/Personnel/miles/2/src/Heatmap.java @@ -1,12 +1,18 @@ import java.sql.*; + import java.io.*; import java.util.Properties; import java.util.Scanner; import java.awt.*; +import java.awt.image.BufferedImage; +import java.awt.image.BufferedImageOp; import java.awt.image.ImageProducer; +import java.awt.image.RenderedImage; +import javax.imageio.ImageIO; import javax.swing.*; +import org.tc33.jheatchart.HeatChart; /** * @author miles @@ -15,6 +21,7 @@ import javax.swing.*; public class Heatmap { String[] species; static int[] allClusterIDs; + static int maxCount = 0; public static void main(String[] args) { String[] geneIDs = new String[] {"mgf009407m", "GRMZM2G457201_T02", "LOC_Os03g53530.1", "Bradi3g44220.1"}; @@ -25,12 +32,103 @@ public class Heatmap { generateDelimitedTable(species); // IF asked for image - // generateHeatMapImage(species) + generateHeatMapImage(species); // IF asked for webpage //displayHeatChart(species); } + private static void generateHeatMapImage(Species[] species) { + /* + File output = new File("heatMap"); + + int heatMapHeight = 25*allClusterIDs.length; + int heatMapWidth = 25*species.length; + int imageType = (false ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_3BYTE_BGR); + + BufferedImage heatMapImage = new BufferedImage(heatMapWidth, heatMapHeight, imageType); + Graphics2D heatMapGraphics = heatMapImage.createGraphics(); + + for (int s = 0; s < species.length; s ++) { + for (int c = 0; c < allClusterIDs.length; c ++) { + int frequency = species[s].findClusterFrequency(allClusterIDs[c]); + // -1 means that species contains no genes in the specified cluster, but can't have negative color + // ensure that frequency is never negative + if (frequency == -1) { + frequency = 0; + } + Color cellColor = new Color((255/maxCount) * frequency, 0, 0); + heatMapGraphics.setColor(cellColor); + heatMapGraphics.fillRect(s*25, c*25, 25, 25); + } + } + + + int chartWidth = heatMapWidth + 50; + int chartHeight = heatMapHeight + 300; + BufferedImage chartImage = new BufferedImage(chartWidth, chartHeight, imageType); + Graphics2D chartGraphics = chartImage.createGraphics(); + Color background = new Color(255, 255, 255); + chartGraphics.setColor(background); + chartGraphics.fillRect(0, 0, chartWidth, chartHeight); + chartGraphics.drawImage(heatMapImage, 50, 300, null); + chartGraphics.setColor(Color.black); + chartGraphics.drawString("Heat Map", 25, 25); + + for (int c = 0; c < allClusterIDs.length; c ++) { + String clusterID = Integer.toString(allClusterIDs[c]); + chartGraphics.drawString(clusterID, 5, (315 + c*25)); + } + + try { + ImageIO.write((RenderedImage) chartImage, "gif", output); + } catch (IOException e) { + System.out.println("Unable to write Heat Map Image"); + } + */ + + + + double[][] heatMapVals = new double[allClusterIDs.length][species.length]; + for (int s = 0; s < species.length; s ++) { + for (int c = 0; c < allClusterIDs.length; c ++) { + int frequency = species[s].findClusterFrequency(allClusterIDs[c]); + // -1 means that species contains no genes in the specified cluster, but can't have negative color + // ensure that frequency is never negative + if (frequency == -1) { + frequency = 0; + } + heatMapVals[c][s] = frequency; + } + } + String[] names = new String[species.length]; + for (int s = 0; s < species.length; s ++) { + names[s] = species[s].name; + } + HeatChart heatChart = new HeatChart(heatMapVals); + Color axisColor = new Color(0, 0, 0); + Color background = new Color(255, 255, 255); + Dimension cellSize = new Dimension(20, 20); + heatChart.setAxisColour(axisColor); + heatChart.setBackgroundColour(background); + heatChart.setCellSize(cellSize); + heatChart.setXValues(names); + heatChart.setTitle("Heat Map"); + heatChart.setYAxisLabel("Cluster ID"); + heatChart.setXAxisLabel("Species"); + + Image heatMap = heatChart.getChartImage(); + + File output = new File("heatMap"); + + try { + ImageIO.write((RenderedImage) heatMap, "gif", output); + } catch (IOException e) { + System.out.println("Unable to write image to file"); + } + + } + private static void generateDelimitedTable(Species[] species) { System.out.println("About to create a new tab delimited file with data requested." + "\n" + "Please name new file:"); Scanner in = new Scanner(System.in); @@ -82,9 +180,9 @@ public class Heatmap { } printWriter.print("\t" + frequency); } - - } } + printWriter.close(); + } // creates a connection, distributes work and information to the various helper functions // gathers the final list of species and their geneMaps in one place @@ -184,6 +282,9 @@ public class Heatmap { rawSpecies[speciesCount].setName(currentSpecies); rawSpecies[speciesCount].addCluster(rs.getInt(1), rs.getInt(3)); while(rs.next()) { + if(rs.getInt(3) > maxCount) { + maxCount = rs.getInt(3); + } String name = rs.getString(2); if (name == currentSpecies) { rawSpecies[speciesCount].addCluster(rs.getInt(1), rs.getInt(3)); diff --git a/Personnel/miles/2/tmp b/Personnel/miles/2/tmp index 723c2d7..4e5b43b 100644 --- a/Personnel/miles/2/tmp +++ b/Personnel/miles/2/tmp @@ -1,32 +1,4 @@ -Species Cluster 0 Cluster 1 Cluster 2 - -Arabidopsis_lyrata 1 1 1 -Arabidopsis_thaliana 0 1 1 -Batrachochytrium_distachyon 0 0 1 -Brachypodium_distachyon 1 2 1 -Caenorhabditis_elegans 0 0 1 -Carica_papaya 0 0 1 -Chlamydomonas_reinhardtii 0 0 1 -Cucumis_sativus 0 1 2 -Danio_rerio 0 0 1 -Drosophila_melanogaster 0 0 1 -Ectocarpus_siliculosus 0 1 1 -Fragaria_vesca 0 1 1 -Glycine_max 0 4 2 -Homo_sapiens 0 0 2 -Laccaria_bicolor 0 0 1 -Magnaporthe_grissa 0 0 1 -Manihot_esculenta 0 1 1 -Mimulus_guttatus 0 1 1 -Neurospora_crassa 0 0 1 -Oryza_sativa 2 1 1 -Pediculus_humanus 1 0 1 -Physcomitrella_patens 0 0 1 -Populus_trichocarpa 0 2 1 -Prunus_persica 0 1 1 -Rattus_norvegicus 0 1 1 -Ricinus_communis 0 1 1 -Saccharomyces_cerevisiae 0 0 1 -Selaginella_moellendorffii 0 1 1 -Sorghum_bicolor 0 1 1 -Vitis_vinifera 0 1 1 \ No newline at end of file +Cluster id Arabidopsis_lyrata Arabidopsis_thaliana Batrachochytrium_distachyon Brachypodium_distachyon Caenorhabditis_elegans Carica_papaya Chlamydomonas_reinhardtii Cucumis_sativus Danio_rerio Drosophila_melanogaster Ectocarpus_siliculosus Fragaria_vesca Glycine_max Homo_sapiens Laccaria_bicolor Magnaporthe_grissa Manihot_esculenta Mimulus_guttatus Neurospora_crassa Oryza_sativa Pediculus_humanus Physcomitrella_patens Populus_trichocarpa Prunus_persica Rattus_norvegicus Ricinus_communis Saccharomyces_cerevisiae Selaginella_moellendorffii Sorghum_bicolor Vitis_vinifera +0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 0 +1 1 1 0 2 0 0 0 1 0 0 1 1 4 0 0 0 1 1 0 1 0 0 2 1 1 1 0 1 1 1 +2 1 1 1 1 1 1 1 2 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \ No newline at end of file