Rice University logo
Top blue bar image
Computer Scientist at the Department of BioSciences, Rice University

Archive for the ‘Java’ Category

Simplest biology heatmap in php

April 23rd, 2014 by Boanerges Aleman-Meza

Biology heatmaps are useful to summarize results, such as in this example:

Need: create a heatmap from values in a database table. The table has three fields, one for names of rows, another for names of columns, and value.

  • Make a table in HTML
  • Set the background color of each cell depending upon the value of each database record.
  • The color coding preferred by my boss is from blue to black and then to yellow:
  • Find out what the minimum and maximum value are supposed to be. In my case it was 0.9 and 1.1
  • Make a list of colors such as colors.php (if you use this file, rename it from .php.txt to .php, I appended .txt for easy display/view on browser).
  • Use a formula that will get you a given color depending upon an input value.
    • My formula requires to know the ‘start’ value, in this case 0.9; it also needs to know ‘half’ of the heatmap, in this case 0.1
    • Example of getting the index value in php, where $value is the input value:
    • $index = round( ( ( $value ) – $start ) / ( $half * 2 / ( count( $colors ) – 1 ) ) )
    • Then the color to use as background color in each cell would be: $color = $colors[ $index ]
  • Display a ‘legend’ that tells the user the range of colors for given values.
    • My legend is automatically generated using ‘start’ and ‘half’ values, here’s an example for a legend that ranges from -1 to 1
    • Actually, I have it as a file that gets included into the file containing the heatmap. Legend php file: legend.php
    • I should mention that the file containing the heatmap must include the colors.php before including legend.php
  • When things go OK, heatmap should look pretty. Example:
  • Missing values should use a different color, such as gray.
  • Last note: it was not necessary to care about efficiency when displaying the heatmap. One query was used to remember the rows and columns. Then nested loops performed queries to retrieve the value of each cell in the table. That is, for ‘c’ columns and ‘r’ rows, there were c * r queries to the database, which was OK in heatmaps on the range of 100 rows by 20 columns.

Later, the database table had to be updated via a text file. These steps were done to update it:

  • Truncate the existing table
  • Make sure that there is no header line (such as cross1 cross2 value)
  • Since we’re in linux, make sure that end-of-line characters are not an issue (‘vi’ tells me if file is DOS, if so, do: set ff=unix).
  • Load the new file into the table via SQL command: load data local infile ‘heatmap.txt’ into table heatmap
  • Verify the minimum and maximum value in the table so that the heatmap can display colors correctly

Computer programming in Java doing GUIs, then image processing, then databases, then some PHP

December 20th, 2013 by Boanerges Aleman-Meza

I do lots of computer programming. I like it a lot.

However, switching context is required in my current job. My programming can change, even in the same week, among the following:

  • Graphical user interface in Java
  • Crunching numbers in Java (often via commons-math from Apache.org)
  • Loading data into databases via Java (minor cleaning and validations are the norm)
  • Simple web pages that use PHP to query databases
  • Basic image processing stuff (imageJ is often enough)

Java dialog for yes-no question

October 12th, 2012 by Boanerges Aleman-Meza

Java Dialog for giving user option of YES/NO in a GUI, the default is the second option in this case (i.e., the NO). I’d like to quickly find this code when needed, but also important is how the question needs to be made, and the text of each button needs to be worded so that user can better know what each button is going to do.

String[] options = { "Yes, discard changes", "No, go back to where I was" };
int n = JOptionPane.showOptionDialog( dialog, "Changes have not been saved!\nDiscard Changes?",
        "Unsaved changes", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[ 1 ] );
if( n == 0 ) {
dialog.setVisible( false );

Maybe I could have proposed in this way, but setting the default to ‘YES’. Fun, but my wife likely wont think it is fun, heheh.

Note: the question has a mistake, it should say “Changes have not been saved”

Extra note: simple html can be used, such as

"<html>Yes,<br>discard changes</html>"