Multidimensional Arrays in Perl 5.0: A Boon to Bioperl (Part 1)
Perl 5.0 is Now Equipped to Handle Multidimensional Arrays
In a nutshell
One can use brackets or back slashes to "convert" data structures like arrays and hashes to scalars and references, scalar values, are used to access these data structures.
Example:
@array = [ 0,3, 6, 9, 12, 15] ;
$newarray = \ @array ; #( reference)
When does a biologist need multi-dimensional arrays?
When one is working with bioinformatics, the question is more like: when does one NOT need multi-dimensional arrays?
Starting from a list of all experiments with microarray data related to diferent types of disease to pathway analysis information , one needs to provide for a variety of "table of tables" , "hashes of arrays", "arrays of arrays" (scary-cool ! !) and so on.
Sample:
I would like to look at all data from experiments related to kidney cancer before I proceed to work on it.
Here are some experiments that I have found in public databases.
PlatformA: Microarray data from men and women over fifty with kidney cancer in metros with a high incidence of carcinogenic dyes in food. ( hypothetical) Six replicates, two with control data from healthy kidneys and two from each gender; treated by drug GROW
PlatformB: Microarray data from drug companyX of patients with Kidney cancer , some showing fewer cancerous cells in the kidneys of patients treated with Drugs M, N and B (several files, 2 replicates and one control ); treated by drug MNB
PlatformC: Microarray data from mice with kidney cancer induced via carcinogens. Two different mice models used to extract the most well defined set of genes upregulated in cancer. Categories: Four control sets, one treated with a drug, one with a diet high in antioxidants prior to induction, male, female and so on; treated by carcinogen and drug GHI
Experiment A : treated by the drug GROW kidney cancer fifty ; six files/datasets, three categories
Experiment B : treated by the drug MNB ; more than eight files/datasets, more than three categories
ExperimentC : treated by a carcinogen and the drug GHI ; ten files/datasets, more than four categories
Experiment D : treated by the drug FNB, four files/datasets, two categories.
Here is a hash created from the above information.
Experiment A, platform 1
Experiment B, platform 2
Experiment C, platform 4
Experiment D, platform 3
The "boon"/ solution:
The above example provides us with an instance of a "Hash". Here, the drug is the key.
We know that a hash is a scalar value in Perl. We need to be able to access the values in arrays, arrays of hashes or hashes of arrays. For this we will use a
"Reference".
A Reference is a scalar value too.
The other data structure we will use is an array.
There are several ways of storing values and processing them. References can be created in two ways.
1) THE BACKSLASH METHOD FOR MULTI-DIMENSIONAL ARRAYS
We now have to store our array in a scalar.
When you put a "\" in front of an array, you get a reference to it.
Our original array :
@newdrugarray.;
$newref = \ @newdrugarray;
Remember how we can make a table for the platform used and the experiment?
Here goes:
ExperimentA, platform1
ExperimentB, platform2
ExperimentC, platform3
ExperimentD, platform1
The information from the hash can be stored in the reference, say , $new_h_ref
Using the "Backslash" , "\", we have:
$new_h_ref = \%Exptinfo
Or, if you want a more realistic, useful hash, here's another example.
Experiment A : treated by the drug GROW
Experiment B : treated by the drug MNB
Experiment C : the drug GHI
Experiment D : treated by the drug FNB,
Experiment Name of the drug
Experiment A GROW
Experiment B MNB
Experiment C GHI
Experiment D FNB
Fig 1.2 Table showing a hash of experiment and drug information. Here drug is the key.
Can you create a reference using the information in the table , fig 1.2?
Remember, a reference is a scalar.
2) The BRACKET METHOD FOR MULTI-DIMENSIONAL ARRAYS
Here is the tricky part. We use diiferent brackets for different data structures.
"[ ]" are used for arrays. "{}" are used for hashes.
This makes it easy to handle multi-dimensional arrays such as an array of hashes or hash of arrays or an array of arrays.
$Newref = [elements of @Newdrugarray ] ;
#Note the square brackets.
(This is the same as
$Newref = \ @Newdrugarray )
We store the elements of a @Newdrugarray in one step in the scalar value $Newref.
Here we do it all in one step.
How does it work for a hash?
We use curly brackets, " { }" to make references to hashes.
Lets refer back to our example of a hash.. Our R.A. has come up with a hash for the sake of convenience.
ExperimentA, platform1
ExperimentB, platform2
ExperimentC, platform3
ExperimentD, platform1
$New_hash_ref = { Experiment A => platform 1, Experiment B => platform 2};
# $ New_hash_ref now holds a reference to a hash.
# Note the curly brackets.
While the demo here is for a very straight forward hash, we could use this for a hash of arrays or tables.
Now you will say: "Hey! You merely assigned stored it in a scalar variable."
Yes I did !
That's all one needs to do.
However, storing multi-dimensional arrays is only half the story.
To be able to sucessfully use multi-dimensional arrays , one has to be able to retrieve data, print them and update them.
I look forward to covering the use of references and printing references.
Published by josie bennett
I work with medical and scientific literature. Other interests include drug discovery, informatics and biobases. View profile
- Evolution of Icons in the Byzantine ChurchArt has always been a vehicle for people to express their ideas, their beliefs, their values and themselves. Throughout time it has been used to serve a myriad of varying objectives ranging from decoration to educati...
- Motion Picture Purgatory - Comic Strip Reviews by Rick TremblesRick Trembles took it upon himself to try something a bit different and do his iconoclastic movie reviews in the form of a comic strip which he does on a weekly basis.
- Health Care Goes MobileImagine a world with less paperwork, real-time access to patient data, improved workflow, and enhanced outcomes.
- Doteasy Domain Name Registration & Hosting ServiceDoteasy provides a low-cost, easy to use domain registration and web hosting service for basic websites. For more advanced users, Doteasy supplies options to upgrade the hosting to allow for scripting support and mor...
MSN Messenger Icons: Where to Download Them, How to Make ThemMSN Icons are huge. From the buddy icon to the emoticons, or smilies, that you can make show up in the middle of your conversation, there is an msn icon to suit every mood.
- An Introduction to Programming in Perl
- Context in Writing
- The Process of Writing
- The Writing Process: How to Write a Novel
- A Guide to Common Acronyms for the Web
- Italian Market Analysis of the Automotive Industry
- Hosting Your Own Web Server: Things to Consider
- Multidimensional arrays are now seen in every area of bioinformatics.
- Perl 5.0 can handle multidimensional arrays.
- References to multidimensional arrays help one manipulate them and they are scalar values.
What more could a bioinformaticist ask for?



