Wednesday 29 August 2012

Reduce PHP array or arrays with keys to just their values

Note: If you are using PHP5.5+ then ignore the rest of this post. You should use array_column() instead.

When you are running a query to get data from a database table like this using a framework like CodeIgniter with code like this:

$result = $this->db->select('id')->from('table')->order_by('field_n')->get()->result_array();

The $result tends to look something like this:

$result = array(
    array('id' => 1),
    array('id' => 2),
    array('id' => 3),
    ...
);

What I would like is an array of values like this:

$result = array(1, 2, 3);

I've found a good way to achieve this is to run this code (PHP5.3+):

$result = array_map(function($v){return array_shift($v);}, $result);

Doing a print_r of $result shows the code produces the flat array we want:

Array(
    [0] => 1,
    [1] => 2,
    [2] => 3,
    ...
);

There is also an added benefit in that we can select which field we want to return if there is more than one column in the table. Consider the following query:

$result = $this->db->select('id','name')->from('table')->order_by('name')->get()->result_array();

which puts these values into results:

$result = array(
    array('id' => 3, 'name' => 'Aaron'),
    array('id' => 1, 'name' => 'Brian'),
    array('id' => 2, 'name' => 'Steve'),
    ...
);

What if we just want to get the names from the array? Here's the code and the output:

$result = array_map(function($v){return $v['name'];}, $result);

Array(
    [0] => 'Aaron',
    [1] => 'Brian',
    [2] => 'Steve',
    ...
);

Monday 13 August 2012

MySQL Constraints and Foreign Keys

This is primarily for my reference alone. I continually find myself using these three SQL commands when working with constraints and foreign keys.

-- Add a foreign key constraint. I can also SET NULL, NO ACTION or RESTRICT instead of cascade
ALTER TABLE sop_version_ancestry ADD CONSTRAINT fk_sop_version_ancestry_sop
FOREIGN KEY (sop_id) REFERENCES sop(sop_id)
ON DELETE CASCADE ON UPDATE CASCADE

-- Show what constraints exist in the table (because phpmyadmin doesn't show ought)
SHOW CREATE TABLE sop_version_ancestry

-- Delete a constraint once I know what it's called from the command above
ALTER TABLE sop_version_ancestry DROP FOREIGN KEY fk_sop_version_ancestry_sop

Monday 30 April 2012

That's one wierd-looking chicken! - Bilateral gynandromorphism

In birds there is a rare mutation that creates a sexual chimera. Birds have a different system to our XY sex-determining chromosome system and this allows the above to happen. In the picture above, the right side is the female side (hen), and the left side is male (cockrel) - so you have a 50/50 male/female split. Isn't it amazing how it's split down the middle exactly!? More about this here.

Tuesday 24 April 2012

Ancient virus DNA thrives in us


Traces of ancient viruses which infected our ancestors millions of years ago are more widespread in us than previously thought.
A study shows how extensively viruses from as far back as the dinosaur era still thrive in our genetic material.
It sheds light on the origins of a big proportion of our genetic material, much of which is still not understood.
The scientists investigated the genomes of 38 mammals including humans, mice, rats, elephants and dolphins.
The research was carried out at Oxford University, the Aaron Diamond AIDS Research Centre in New York and the Rega Institute in Belgium.
It is reported in the journal Proceedings of the National Academy of Sciences.
One of the viruses was found to have invaded the genome of a common ancestor around 100 million years ago with its remnants discovered in almost every mammal in the study.
Another infected an early primate with the result that it was found in apes, humans and other primates as well.
The work established that many of these viruses lost the ability to transfer from one cell to another.
Instead they evolved to stay within their host cell where they have profilerated very effectively - spending their entire life cycle within the cell.
Forced choice The researchers found evidence of the viruses multiplying so extensively within mammals' genomes that they have been compared to an outbreak of disease.
The senior author of the study, Dr Robert Belshaw from Oxford University's Zoology Department, said: "This is the story of an epidemic within every animal's genome, a story which has been going on for 100 million years and which continues today.

Much of the dark matter in our genome plays by its own rules”

Gkikas Magiorkinis Oxford University
"We suspect that these viruses are forced to make a choice: either to keep their 'viral' essence and spread between animals and species. Or to commit to one genome and then spread massively within it."
The study shows that the viruses involved have lost a gene called Env, which is responsible for transmission between cells.
Known as endogenous retroviruses, these micro-organisms have gone on to become 30 times more abundant in their host cells.
The study is one of many attempting to understand the full complexity of the human genome.
Astonishingly, only 1.5% of the genetic material in our cells codes for human life. Half of the rest is sometimes described as "junk DNA" with no known function, and the other half consist of genes introduced by viruses and other parasites.
Positive services According to the lead author, Dr Gkikas Magiorkinis, "much of the dark matter in our genome plays by its own rules, in the same way as an epidemic of an infectious disease but operating over millions of years.
"Learning the rules of this ancient game will help us understand their role in health and disease."
This raises the extraordinary scenario of our DNA serving as an environment in which viruses can evolve - a micro-ecology within the double-helix of our genetic material.
There is evidence that they can provide positive services. For example the protein syncytin - derived from a virus - helps develop the placenta.
Dr Belshaw says that endogenous retroviruses (ERVs) are not known to have any obvious or direct health effects.
"But there could be effects we're not picking up on or things we could even take advantage of if we detect ERVs moving around or expressing proteins as a result of cancer or infection."
The study was supported by the Wellcome Trust and the Royal Society.

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

Saturday 14 January 2012

Synthetic biology and the rise of the 'spider-goats'

Freckles looks like a perfectly normal kid. She has bright eyes, a healthy white pelt and gambols happily with Pudding, Sweetie and her five other siblings, exactly as you might imagine young goats do. Until I fend her off, she's very keen on chewing my trousers. To the casual observer, and to goatherds, she shows no signs that she is not a perfectly normal farmyard goat.
But Freckles is a long way from normal. She is an extraordinary creation, an animal that could not have existed at any point in history before the 21st century. She is all goat, but she has something extra in every one of her cells: Freckles is also part spider.
That is what we can now do with genetics: extreme crossbreeding. If 20th-century biology was about taking living things apart to find out how they work, the current era is defined by putting them back together, but not necessarily as evolution decreed, and certainly without the clumsy constraints of mating. Freckles is the result of genetic engineering. But our mastery of manipulating DNA has evolved into an even more extreme form of tinkering, broadly called "synthetic biology". I've been tracking this emerging field since finishing my PhD in genetics 10 years ago, but intensely in the last year as a presenter for the BBC's flagship science strand, Horizon.
Freckles is the creation of Randy Lewis, a professor of genetics at Utah State University. The farm is a university outpost where they research modern farming techniques, teach animal husbandry and raise what are inevitably referred to as "spider-goats".Randy, like many of the other scientists here in Logan, Utah, has farming in his blood. So although a creature that is part goat, part spider might seem like an idea born of science fiction, as far as Randy is concerned it's simply advanced farming: breeding animals to produce things that we want.
"We're interested in dragline silk – the silk that spiders catch themselves with when they fall," he tells me in his midwest lilt. "It's stronger than Kevlar. It really has some amazing properties for any kind of a fibre."
In a sense, spider-goats are an extension of the farming we've been doing for 10,000 years. All livestock and arable has been carefully bred, each cross being a genetic experiment of its own. "The trouble is, you can't farm spiders," Randy says with an almost comic deadpan face. "They're very cannibalistic." He and his team took the gene that encodes dragline silk from an orb-weaver spider and placed it among the DNA that prompts milk production in the udders. This genetic circuit was then inserted in an egg and implanted into a mother goat. Now, when Freckles lactates, her milk is full of spider-silk protein.
We milk Freckles together and process it in the lab to leave only the silk proteins. With a glass rod, we delicately lift out a single fibre of what is very obviously spider silk and spool it on to a reel. It has amazing, and desirable, properties, which is why Randy's seemingly bizarre research is so robustly funded. "In the medical field, we already know that we can produce spider silk that's good enough to be used in ligament repair," he tells me. "We already know we can make it strong enough as an elastic. We've done some studies that show that you can put it in the body and you don't get inflammation and get ill. We hope within a couple of years that we're going to be testing to see exactly the best designs and the best materials we can make from it."
The instructions for all creatures that have ever lived (as far as we know) are written in the code of DNA tucked away in the heart of living cells. Given the bewildering diversity of life on Earth, this system is incredibly conservative. All life is based on an alphabet of just four letters, which, when arranged in the right order, spell out proteins. And all life is made of, or by, proteins. So what this means is that the code for making silk in a spider is written in exactly the same language as the code for making goats' milk.
Since the advent of genetic engineering, we have been able to exploit the universality of this code and cut and paste bits of DNA from any one species into any other. Identifying the genetic basis of all cancers and inherited diseases came from this technology: human or mouse genes have been spliced into bacteria so we could study and experiment on those damaged bits of code. Now, this editing technology has progressed to the extent that all bits of DNA code are effectively interchangeable between all species. In fact, Freckles and the other spider-goats are not even on the cutting edge. The loosely defined field of synthetic biology has come to incorporate even more extreme forms of genetic tinkering.
The most striking headlines so far came when American biologist Craig Venter announced in 2010 that he had created the world's first synthetic life form. Synthia, aka Mycoplasma mycoides JCVI-syn 1.0, was a cell whose genetic code, copied and modified from an existing bacterium, had been assembled not by its parent, but by a computer. That code, including literary quotations and website addresses, was then jammed into the eviscerated chassis of another similar cell and the whole thing booted up. It did live and it hadn't lived before.
But to say that he had "created life" is a stretch that Venter – a master of PR as well as an accomplished scientist – allowed to foment and the press lapped up. It's more accurate to say that he rebooted life, his aim being to create a living template on to which new genetic functions could be built. Nevertheless, it remains an astonishing technical achievement, showing our dominance over DNA; not only can we modify one or two genes, we can make enough to power up a living thing.
The scientists who work in synthetic biology often take a perfunctory, reductionist view of what they do. Massachusetts Institute of Technology professor Ron Weiss is a founding father of this field, a purist who started fiddling with the code of life while coding computers. "I decided to take what we understand in computing and apply that to programming biology. To me, that's really the essence of synthetic biology."
This may sound glib. Life forms are complex at every level. If there is one concrete thing we have learned from the billions spent on reading our own genetic code, it's that biology is messy. Scientists are often confounded by baffling "noise" in the molecules that make up living organisms, unpredictable variation set among unfathomable sophistication. Weiss and his comrades at the BioBricks Foundation want to strip out all the noise in biology and turn it into pure engineering, where organisms can be treated like machines and their inner workings are component parts.
Genes have evolved over millions of years to bestow survival on their hosts by having very specific functions. By standardising these genetic elements in an online registry, anyone can piece them together in any order to create biological circuits with entirely designed purpose. Even the language used is more the stuff of electrical engineering than traditional biology.
"Imagine a program, a piece of DNA that goes into a cell and says, 'If cancer, then make a protein that kills the cancer cell; if not, just go away.' That's a kind of program that we're able to write and implement and test in living cells right now." What Ron Weiss is describing is a study his team published last autumn showing that, by using the logic of computer circuits combined with BioBricks parts, they had built a cancer assassin cell. The logic of the genetic circuit initially distinguishes a cancer cell from a healthy cell using a set of five criteria. It then destroys the tumour cell if it satisfied those conditions. This sniper targeting is the opposite of the blunderbuss approach of chemotherapy, which can destroy both tumour and healthy cells with reckless abandon.
Over the last few years, BioBricks has grown into a global phenomenon. The Registry of Standard Biological Parts currently contains thousands of bits of DNA, all freely available, and this democratisation of science is built into the BioBricks ethos. Every year, undergraduate students compete in an international competition to think of a problem and design and build its solution, using only the parts available in the registry. 2011's European champions, from Imperial College London, designed a system for preventing soil erosion and the conversion of land into desert. There is a remix culture within these teams; it's serious play (the grand prize is a silver Lego brick) and they come from diverse backgrounds – maths, engineering, even astrophysics – unfettered by the narrowly defined science disciplines under which I did my DNA research.
The ease of access to this bleeding-edge technology is breathtaking. Last summer, in suburban Sunnyvale, California, I hung out at a gathering of synthetic biology weekend hobbyists, self-styled as "bio-hackers" with the excellent name BioCurious. There, high-school students were learning about biology by introducing fluorescent proteins from deep-sea jellyfish into bacteria to make them glow in the dark. In 2009, three scientists won Nobel prizes for this work. Already, it is literally child's play.
As with any great revolutions, there are those who stand to make a killing after the doors are kicked open. At the other end of the scale from the open-source, open-access utopia of BioBricks, synthetic biology commercial enterprises are emerging. The tech may be new, but the fields are not. With synthetic biology only a few years old, the most intense areas of commercialised synthetic biology are in fuel and drug production. California biotech companies such as LS9 and Amyris have ploughed millions of dollars into developing synthetic organisms that will produce diesel. In its futuristic labs in Emeryville, Amyris has modified brewer's yeast so that instead of fermenting sugar to produce alcohol, diesel seeps out of every cell. This synthetic biodiesel is already used to power trucks in Brazil. Amyris's ambition is to scale up from pilot plants to industrial-scale production. When I ask chief science officer Jack Newman if they envisage their biofuel replacing natural oil, he is suspiciously coy: "I'll be excited about a billion litres."
One significant fear has less to do with the science and more to do with the shifting balance of economic power. Technology watchdogs and campaign groups such as Friends of the Earth and ETC Group initially called unrealistically for a total ban on synthetic biology, even though it lacked a workable definition. ETC has modified its stance to focus on the industrialisation of these processes, and specifically the fact that synthetic biodiesel organisms need food.
Jim Thomas, who works for ETC, passionately feels that the control of fuel production is simply shifting from one set of corporate giants to another. "Large companies are buying up bits pieces of land so that they can grow sugarcane and then they're feeding it to vats of synthetic microbes to make fuels," he tells me. "Synthetic organisms at this point should not be out there in the environment; they shouldn't be out there in industry. That's irresponsible and inappropriate."
The culture of biology is rapidly changing and scientists and the public need to keep up. Synthetic biology has the potential to generate a new industrial revolution. It is perhaps the defining technology for the 21st century and it is happening now. Without an informed public discourse, fear of this unprecedented and sometimes unsettling technology may hinder the world-changing promise it harbours.
"Prediction is very difficult, especially about the future," as the great physicist Niels Bohr once said. But science fiction never got close to the outlook that came with the advent of synthetic biology. It is now easy to picture a world in which your torn ligaments are replaced with ones made from spider-silk produced by goats; where medicine is served by living programmable machines that seek and destroy only the cells that cause the disease; and where you will drive a car powered by diesel grown by brewer's yeast. Welcome to the future.
by Adam Rutherford, a science writer and broadcaster 

 http://www.guardian.co.uk/science/2012/jan/14/synthetic-biology-spider-goat-genetics

Friday 13 January 2012

How to setup PEAR on WAMP2.1 and Windows 7

This has been bugging me for ages now and I gave up trying to get it to work on my laptop but now I tried again at work and finally got it working. Yay!

Visit this page in your browser: http://pear.php.net/go-pear.phar

It will start a download. Move the go-pear.phar file to your C:\wamp\bin\php\php5.3.5\ directory

Now you need to put the pear executable into your system path. Right click on (My) Computer which can be accessed from the Desktop or Start button and select Properties. Click on Advanced System Settings and select the Advanced tab. Click the Environmental Variables button. Scroll down to find the Path variable and double click the value. Add a semicolon ";" at the end if there isn't one and then add this path: C:\wamp\bin\php\php5.3.5\

Save your changes.

Now open up the command line (Press the Start button and type in cmd) and go to C:\wamp\bin\php\php5.3.5\

Type in php go-pear.phar

Read the questions it asks you and type the stuff it tells you to continue. At the moment we're not too bothered with the variables it sets. When it's done it will write to the php.ini file in the current directory. This is NOT the php.ini file that WAMP uses. To see where the php.ini file is that WAMP uses you can create a phpinfo.php file in your www folder with <?php phpinfo(); ?> inside it and then run it from the browser, but I'll just tell you where it is instead - C:\wamp\bin\apache\Apache2.2.17\bin.

Open up the php.ini file that is in the current php5.3.5 directory with a text editor and scroll to the bottom to find these lines:

;***** Added by go-pear
include_path=".;C:\wamp\bin\php\php5.3.5\pear"
;*****

Copy and paste those to the bottom of the php.ini file in the Apache-based directory. Save your changes and  restart WAMP if you have it running.

Back in the command line type in pear config-create \wamp\bin\php\php5.3.5 pear.ini

Note the space between the path and the file name.

Now you may wish to set the paths of some of the configuration files if these are not already correct. To view the pear config settings type in pear config-show

This will give you an idea of which paths to amend. It will display a list in three columns, the first is the description/name of the setting, the second is its key and the third column displays the path.

In order to set the path correctly you need to supply the key as the first argument and the folder path as the second argument like so: pear config-set www_dir C:\wamp\bin\php\php5.3.5\www

Set the correct path for the keys www, data, cfg, docs and tests.

So now you are ready to run PEAR with your php.

First, have a go at installing a PEAR package like PHPUnit which is used in unit testing. The commands are:

pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit

Hopefully that should work fine. But if it doesn't like when I got back home to try installing it on my laptop. Try these commands and give it another go.

pear clear-cache
pear update-channels

With pear working, pear packages installed and your PHP include path including the pear directory you can write some good code. To include a pear-installed module type in include 'PHPExcel/PHPExcel.php'; for example.

I hope this stuff works for you too. I can't provide any support if it doesn't work for you because I don't know much about this stuff and I'm surprised I got it to work.

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