Thursday, 12 January 2012

Javascript code to reload css files and js scripts and MySQL commands to disable constraints

I found this snippet of jQuery JavaScript on StackOverflow and it has been really useful. It reloads the stylesheets in a page without reloading the whole page itself. I just paste the code into Firebug and run it and it works its magic.

var queryString = '?reload=' + new Date().getTime();
$('link[rel="stylesheet"]').each(function () {
    this.href = this.href.replace(/\?.*|$/, queryString);
});

I tried to write something similar for Javascript where it reloads the <script> elements in the page but it was buggy. I think the script is double loaded into the page rather than replaced and I think this means it only really works for simple scripts that us mere mortals write but when you try to reload something complex like jQuery with all its handlers it goes a little funny. Here's the broken code anyway and I've set it to only look for and load mysimplescript.js only.

var queryString = '?reload=' + new Date().getTime();
$('head script').each(function(){
    if(/mysimplescript\.js/.test(this.src))
        $.getScript(this.src.replace(/\?.*|$/, queryString));
});

And another thing that really useful when you're dealing with tables with foreign key constraints and you're trying to reload them and it's giving you "MySQL constraint fails" errors - you can temporarily switch off constraint checking:


SET foreign_key_checks = 0;
INSERT INTO tableB SELECT * FROM tableA;
SET foreign_key_checks = 1;

But don't get too used to doing that hack. Constraints are put there for a reason.

Sunday, 8 January 2012

Submitted a new PHP class: IsPrime

I posted a new PHP class on phpclasses.org called IsPrime and it just tests to see if a number is prime or not using some simple tests/sieves before it does the usual processor-intensive test. Most solutions just quickly jump into the processor intensive method while this approach should prove a much faster way to determine if a number is prime or not.

Test 1: is the number positive - primes must be positive
Test2: is the number a single digit prime - 2,3,5,7
Test3: does the number end in 1, 3, 7 or 9 as all prime numbers do
Test4: is the number divisible by 3 or 7 - most numbers that end with 1, 3, 7 or 9 are divisible by those and are therefore not prime
Test5: is the number divisible by another prime number - if it is then it's not prime. This is the processor intensive test.

If it passes those five steps then it's prime. Here's the class: http://www.phpclasses.org/package/7281-PHP-Check-whether-a-number-is-prime.html

Sunday, 18 December 2011

The Very Simple JavaScript Formula Class

A small Javascript program I wrote a while back but didn't get round to releasing until now.

The Very Simple JavaScript Formula class takes a simple string of characters written with a standard keyboard and translates it into HTML code that attempts to visually represent the mathematical notation. The purpose of this class is to make maths on the web easier to write, display and understand - users won't need to learn LaTeX or other markup languages or use desktop/server-side applications to create visual Formulas. All you need to do now is type the formula and VSJSFormula will display the rendered formula. The source code is at:
http://code.google.com/p/vsjsformula/

Friday, 11 November 2011

Phenotype Ontology search resources

I just spent a while looking for these so I thought I'd share my findings.

This is a list of website URI's associated with ontologies, mouse/mammalian phenotype ontology term ids specifically. Where I couldn't find a specific website that shows the details of the ontology, I used the EBI's general ontology lookup search.

For each of the term types, replace the %s with a full term id like MP:0010053. You can use the sprintf() function to do this :-).

MP:
http://www.informatics.jax.org/searches/Phat.cgi?id=%s
BSPO:
http://www.ebi.ac.uk/ontology-lookup/?termId=%s
CHEBI:
http://www.ebi.ac.uk/chebi/searchId.do?chebiId=%s
CL:
http://www.ebi.ac.uk/ontology-lookup/?termId=%s
ENVO:
http://www.ebi.ac.uk/ontology-lookup/?termId=%s
GO:
http://amigo.geneontology.org/cgi-bin/amigo/term_details?term=%s
IMR:
http://www.ebi.ac.uk/ontology-lookup/?termId=%s
MA:
http://www.informatics.jax.org/searches/AMA.cgi?id=%s
PATO:
http://www.ebi.ac.uk/ontology-lookup/?termId=%s

Friday, 4 November 2011

Quick JQuery expanding vertical menu

I'm just posting this as a reference for myself primarily, but if it helps anyone else then that's great. This is a super quick expanding vertical menu. You can animate it by passing 'slow' or 'fast' as an argument to the toggle() method.

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('.branch').click(function(){
        $(this).children('.subbranch').each(function(){
            return $(this).toggle();
        });
    });
});
</script>


<ul class='tree'>
<li class='branch'><a href='#'>Super Title 1</a>
<ul class='subbranch' style='display:none'>
    <li><a href="#">Item 1</a></li>
    <li><a href="#">Item 2</a></li>
    <li><a href="#">Item 3</a></li>
</ul></li>
<li class='branch'><a href='#'>Super Title 2</a>
<ul class='subbranch' style='display:none'>
    <li><a href="#">Item 4</a></li>
    <li><a href="#">Item 5</a></li>
    <li><a href="#">Item 6</a></li>
</ul></li>
</ul>

Thursday, 13 October 2011

New Largest Virus Discovered

The largest virus yet discovered has been isolated from ocean water pulled up off the coast of Chile.
Called Megavirus chilensis, it is 10 to 20 times wider than the average virus.
It just beats the previous record holder, Mimivirus, which was found in a water cooling tower in the UK in 1992.
Scientists tell the journal PNAS that Megavirus probably infects amoebas, single-celled organisms that are floating free in the sea.
The particle measures about 0.7 micrometres (thousandths of a millimetre) in diameter.
"It is bigger than some bacteria," explained Prof Jean-Michel Claverie, from Aix-Marseille University, Marseille, France.
"You don't need an electron microscope to see it; you can see it with an ordinary light microscope," he told BBC News.
Viruses cannot copy themselves; they need to invade a host cell if they want to replicate.
Like Mimivirus, Megavirus has hair-like structures, or fibrils, on the exterior of its shell, or capsid, that probably attract unsuspecting amoebas looking to prey on bacteria displaying similar features.
A study of the giant virus's DNA shows it to have more than a thousand genes, the biochemical instructions it uses to build the systems it requires to replicate once inside its host.
In the lab experiments conducted by Professor Claverie and colleagues, in which they infected fresh-water amoebas, Megavirus was seen to construct large trojan organelles - the "cells within cells" that would produce new viruses to infect other amoebas.
"Everything is initiated from a single particle, and then grows and grows to become this virion factory," explained Prof Claverie. "That's why it needs all these genes."
Megavirus was found off the coast of Las Cruces, central Chile. It was recovered as part of a general trawl in the ocean for biology of interest.
"This is a new way of doing virology," said Prof Claverie.
"Previously, we only discovered viruses because they caused disease in humans, or animals and plants. But now we are initiating what might be called environmental virology and we are looking for viruses everywhere.
"You just go to lakes, seas and oceans and pick up the water, and then you filter it, and try to rescue the virus by co-cultivating it with some potential host."
More generally, there is interest in ocean viruses because they have a major influence on populations of plankton, the microscopic organisms that form the base of many marine food chains. And when they kill plankton, viruses are also helping to regulate the planet's geochemical cycles as the dead organisms sink into the deep, locking away their carbon for aeons.

http://www.bbc.co.uk/news/science-environment-15242386

Friday, 26 August 2011

Copying radio button values to a group of radio buttons

My javascript is a little rusty and I've just spent a few hours trying to solve this problem: How do you copy the checked value of a radio button to a set/group of radio buttons?

I have this radio button group as the master group, the one that when I change what's checked I want it to modify the rest of the other set of radio buttons when a button was clicked:

<input type="radio" name="master" value="1"> Yes
<input type="radio" name="master" value="0"> No
<input type="radio" name="master" value=""> --
<input type="button" name="radiocopy" onclick="copydown()" value="Change All" ></button>

And the other groups are found in some rows of data and one of the columns requires a choice to be made: Yes, No, or missing.

<input type="radio" name="slaveRow1" value="1"> Yes
<input type="radio" name="slaveRow1" value="0"> No
<input type="radio" name="slaveRow1" value=""> --

<input type="radio" name="slaveRow2" value="1"> Yes
<input type="radio" name="slaveRow2" value="0"> No
<input type="radio" name="slaveRow2" value=""> --

<input type="radio" name="slaveRow3" value="1"> Yes
<input type="radio" name="slaveRow3" value="0"> No
<input type="radio" name="slaveRow3" value=""> --

Now what I wanted was that when I set the master to Yes, the rest of the radio buttons would change to Yes as well when I click the Change All button. Here's what I wrote:

function copydown(){
    var fm = document.forms["thenameofyourform"];

    //find out which master radio button is checked and assign value to masterValue
    var masterValue = null;
    for(i = 0; i < fm.elements['master'].length; i++){
        if(fm.elements['master'][i].checked == true)
            masterValue = fm.elements['master'][i].value;
    }

    //loop through all the form elements in the page
    for(i = 0; i < fm.elements.length; i++){
        //select only the radio buttons
        if( fm.elements[i].type == "radio" && fm.elements[i].name.indexOf("slaveRow") != -1){
            //check the value of each slave radio button and if it matches the
            //checked value of the master then check it
            if(fm.elements[i].value == masterValue)
                fm.elements[i].checked = true;
        }
    }
}

That seemed to do the trick. I must admit my Javascript is rusty. If you don't want to implement this with a button you can add a listener to the master radio buttons group.

Monday, 8 August 2011

Enums in PHP and Java

This is mainly for my personal reference. Enums are quite useful to use in projects and save typing and make sure Strings/Integers match, because it's not uncommon that, for example, you type "list" instead of the expected word "List" or misspell things and other such tirivial issues.

PHP:

<?php

//PHP has no native Enum type so you need to manipulate
//a normal class to work how you want it to
final class Season
{
    //class constants are inherantly public and static
    const SPRING = 'Spring';
    const SUMMER = 'Summer';
    const AUTUMN = 'Autumn';
    const WINTER = 'Winter';

    public function __toString()
    {
        //default return
        return self::SPRING;
    }
}

//Test the enum... prints out Spring.
echo ( new Season() );
//And if I wanted to choose just Summer
echo Season::SUMMER;


Java:

public enum Season {
    //define the values
    SPRING, SUMMER, AUTUMN, WINTER;
   
    //for extra useability have it return a string of the chosen enum when needed
    @Override
    public String toString(){
        switch(this){
            case SPRING: return "Spring";
            case SUMMER: return "Summer";
            case AUTUMN: return "Autumn";
            case WINTER: return "Winter";
            default: return "Spring";
        }
    }
}

//usage - prints out Summer
System.out.println( Season.SUMMER );

Friday, 29 July 2011

String DB

RAS, RAF, MEK, MAPK... or something like that. That's what I remember as being the chain of gene activation to activate an oncogene leading to cancer (P53 getting its ass kicked) . No I can't remember the details but these activation/interactions form a tree of what gene up or downregulates the function of another and it's quite a complicated part of genetics and bioinformatics. But I found a website recently that I hadn't seen before and thought of sharing it because apart from getting a nice little description of what the gene does in different species, it has nice spider diagrams like this showing gene relationships:





http://string-db.org/
A search example, PAX6: http://string-db.org/newstring_cgi/show_network_section.pl?caller_identity=expasy_api&identifier=pax6
If you click continue at the bottom of that page you see the diagram.