Gene Expression Data Using Hashes

IT-SC 173 Finally, this returns 1: ZZZ cmp AAA; This algorithm is called a binary search, and it considerably speeds up the process of searching in an array, for example, to search 30,000 genes takes only about 15 times through the loop, maximum. As compared to 15,000 comparisons, average, for the unsorted array. Of course, you also have to sort the list, which might take awhile. If you need to keep adding elements, you have to either insert them in the right place or add them to the end and sort the array again. All that inserting or sorting might slow things down considerably. But if youre just sorting it once and then doing lots of lookups, a binary search might be worth doing. While were at it, lets look at how to sort an array. Heres how to sort an array of strings alphabetically: array = sort array; Heres how to sort an array of numbers in ascending order: array = sort { a = b } array; Many other kinds of sorting can be done, but these are the most common. For more details, see the Perl documentation for the sort function.

8.2.4 Gene Expression Data Using Hashes

You can also use hashes to find a gene in your data. To do so, you can load the hash so that the keys are the gene names and the values are the expression measurement. Then a single call on the hash, with the name of the desired gene as a key, returns the results of the experiment for that gene, and youve got your answer. This process is also cleaner than storing the gene name and the expression result in one scalar string; here the key is a scalar, and the value is a separate scalar. Furthermore, due to how hashes are made, you get an answer back very quickly, because decent hashes dont have to search hard to find the value of a key. Using hashes is typically faster than binary searches. Plus, youd know if the gene being searched for was in the data, because you can explicitly ask if a hash value is defined by saying something like: if defined myhash{mykey} { ... } Also, youll get an error message if you have warnings turned on, and you refer to an undefined value. Another advantage of hashes over binary searching is that you can add or subtract elements to hashes without resorting the entire array. Finally, because hashes are built into Perl as a basic datatype, they are easy to use, and IT-SC 174 you wont have to do much programming to accomplish your goal. It is usually the case that its more important to save time writing a program then it is to save time running it. I mention this in Chapter 3 , but its worth emphasizing. To a programmer, the lazy way is often the most efficient way: let the machine do the work Dont get the idea that hashes are always the right way to go, however. For instance, they dont store their elements in a sorted order, so if you need to look at the data that way, you have to explicitly sort it, like so: sorted_keys = sort keys my_hash; This is do-able, but it can be a bit slow on a large array. You could also sort the values, of course. To conclude the discussion of data structures for our expression data example, heres an informal survey of the properties of some different data structures in Perl for searching, adding and deleting, and maintaining sorted order in a set of gene names: Use a hash if you just need to see if something is in a set and dont need to list the set in order. A sorted array combined with a binary search algorithm will do if you need an ordered set and pretty fast lookup and dont need to add or subtract elements very often. An array, in conjunction with the Perl functions push and pop, works well if you dont need to sort the elements but do need to quickly get at the most recently added element. A Perl array with the functions push and shift will serve if you dont need the elements sorted but need to add elements. Its especially useful to always remove the oldest element the element that has been in the array the longest. For more information, see Appendix A and especially Mastering Algorithms with Perl published by OReilly.

8.2.5 Relational Databases