On writing PHP code, a lot of functions are available to us that are built-in into the language, or if you prefer, predefined: you can use them without having to write them, and are usually quite solid code you can rely on.
In this section and the next we hope to show you how the use of a few readily available PHP functions can go a long way in manipulating strings and biological sequences data. This knowledge will be a strong base for building great web applications in the field of biology and bioinformatics. Keep reading!
Before starting, we should mention some generic information on functions calls.
A function call is performed by writing the name of the function followed by round brackets. Let’s imagine to have a function called “bestfunction”. We can call it like this:
bestfunction();
If the function returns something we need to collect, we can write:
$function_output = bestfunction();
so that the bestfunction() output is now stored to the $function_output variable.
It it not uncommon for functions to need “arguments” in order to perform their duty. Some functions do not require arguments at all, others can optionally take arguments and other absolutely require one or more arguments in order to work. Arguments are passed to the function within the round brackets that follow the function name. If there are several arguments (more than one), they are separated by commas.
Here are a few sample calls to a function:
$function_output = bestfunction($argument1); // If the argument is passed as a variable name, the variable is actually automatically interpolated to it’s value “under the hood” and then passed to the function. The variable that is passed as argument should have of course been defined in the script code before the function call.
$function_output = bestfunction(“gaattc”); // In this case we are passing a value directly, rather than a variable
$function_output = bestfunction($argument1,$argument2); // A function called with 2 arguments
$function_output = bestfunction(“GTCTAGTGA”, 2); // Depending on the function, arguments may be of different types: numbers, strings, arrays, boolean values etc…
Listing all available built-in PHP functions
If you want to list all PHP built-in functions, there is a special predefined function for that, as well, that allows you see the names of all the functions available during the execution of a script, both the predefined functions (also called “internal” functions) and those eventually defined by the user.
This function is get_defined_functions(). A call to it, without arguments, will return an array with two keys, “internal” and “user”. As you may guess, the key “internal” provides access to an array with the names of PHP built-in functions, while the “user” key provides access to an array with all the names of the functions defined by the user (you, the programmer), if any.
Please try to run the following code on your web server and check out the result for a complete listing of all available PHP built-in functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $available_functions = get_defined_functions(); $builtin_functions = $available_functions["internal"]; echo "<p><ul>\n"; foreach($builtin_functions as $available_function){ echo "<li>$available_function</li>\n"; } echo "</ul></p>\n"; ?> |
At the time of this writing 1115 PHP built-in functions are available on our Linux test server. For the purposes of this book, we will need just a few of those that we will review in this section. This will not necessarily be a comprehensive review of the predefined PHP functions used in this book, more of them might be described in subsequent sections, as we come to use them.
strtoupper() and strtolower()
The strtoupper(), string-to-upper, and strtolower(), string-to-lower functions allow us to convert strings passed as arguments to all uppercase or all lowercase, respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $myseq = "gtcctagatgctg"; $myseq_upper = strtoupper($myseq); echo "<p>$myseq_upper</p>\n"; // Will output GTCCTAGATGCTG $myseq2 = "GTcctaGatGCtg"; $myseq2_upper = strtoupper($myseq); $myseq2_lower = strtolower($myseq); echo "<p>$myseq2_upper</p>\n"; // Will output GTCCTAGATGCTG echo "<p>$myseq2_lower</p>\n"; // Will output gtcctagatgctg ?> |
Reverse strings with strrev()
The strrev() string reverse function takes a string as an argument and reverses it, as simple as that. mystring will become gnirtsym.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $myseq = "atcctagatgctg"; $myseq_reversed = strrev($myseq); echo "<p>The reverse of the $myseq sequence is $myseq_reversed</p>\n"; // The reverse of the atcctagatgctg sequence is gtcgtagatccta ?> |
strlen()
The strlen() (string length) function takes a string as argument and returns the number of characters composing it, as an integer.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $myseq = "gtcctagatgctg"; $myseq_len = strlen($myseq); echo '<p>The $myseq string length is '.$myseq_len." characters</p>\n"; // The $myseq string length is 13 characters ?> |
count()
count() takes an array as argument and returns the number of elements it contains, as an integer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php $fruits = array("apples","pears","bananas"); $fruits_count = count($fruits); // 3 echo '<p>The $fruits array contains '.$fruits_count.' elements</p>\n'; // The $fruits array contains 3 elements $complement_dict = array( "A" => "T", "T" => "A", "G" => "C", "C" => "G" ); $comp_count = count($complement_dict); echo "<p>There are $comp_count elements in the complement dictionary</p>"; // There are 4 elements in the complement dictionary ?> |
round()
The round() function allows to round the number of digits after the decimal point in float numbers. It can be used to adjust the precision of a number deriving from a fraction, for example.
The “precision” directive (number of handled digits in a float number) in the php.ini configuration file defaults to 14. For example, the pi() function that returns the pi number (π) will actually return 3.1415926535898.
round() can be called with two arguments, the first is the float we wish to round while the second is the desired precision. The last digit is rounded to the next or previous digit depending on what follows. For example round(5.324, 2) returns 5.32 while round(5.326, 2) returns 5.33. Interestingly, round(5.325, 2) returns 5.33, so there is a bias toward the upper digit.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $a = 5.325675; $b = round($a, 2); // $b is now 5.33 $c = round(5/3, 3); // $c is now 1.667 echo pi(); // 3.1415926535898 $pi_rounded = round(pi(), 2); // 3.14 ?> |
trim()
The trim() function removes unwanted characters at the beginning and end of a string. By default, when called with just one argument (the string to be trimmed), it will remove the following characters:
” ” ordinary space
“\n” newline
“\r” carriage return
“\t” tab
“\0” the null byte
“\x0B” vertical tab
trim() can be called with a second argument that specifies which characters are to be included in the remove list.
One reason it is sometimes a great idea to trim strings is that there are at least two widely used ways to generate a newline. Some protocols or application will use \r\n. This is a legacy of how manual typewriters worked: to get a new line you first returned the carriage to the start position (\r), and then moved the paper up by one position to start writing a newline (\n). Hence the double character \r\n for a newline. However, many protocols and applications dropped the \r and produce a newline by using just \n.
Let’s quote Wikipedia:
“Most textual Internet protocols (including HTTP, SMTP, FTP, IRC, and many others) mandate the use of ASCII CR+LF (‘\r\n’, 0x0D 0x0A) on the protocol level, but recommend that tolerant applications recognize lone LF (‘\n’, 0x0A) as well. Despite the dictated standard, many applications erroneously use the C newline escape sequence ‘\n’ (LF) instead of the correct combination of carriage return escape and newline escape sequences ‘\r\n’ (CR+LF) “
You can split a text file ($text) into lines ($lines) with a command such as:
1 2 3 4 5 |
$lines = preg_split("\n", $text); |
This will always work nicely. However if the text used \r\n to specify newlines, there you now have a trailing \r on each line. Believe it or not, this little tiny \r at the end of each line can cause lots of troubles later in your code. Since you normally do not see this character, you may end up having weird consequences without having a clue of what is going on.
It is therefore an excellent idea, when you come to use these lines later on in the code, to trim them with trim() so as to get rid of the (potential) trailing \r:
1 2 3 4 5 6 |
foreach($lines as $line){ $line = trim($line); // Do stuff with the nicely trimmed line... } |
By specifying the second argument, trim() becomes a powerful tool for text manipulation. Say you have lines with a variable number of dots, commas or spaces at the end that you would like to remove:
$mystring = Hello, World… ..
$mystring1 = Hello, World. ,
$mystring2 = .Hello, World,
By calling a line like:
1 2 3 4 5 |
$cleaned = trim($mystring, " .,") |
on any of the above, the value of $cleaned will always be “Hello, World”.
So now you can’t say we didn’t tell you to always trim your lines. Maybe there is a trailing \r, maybe there isn’t (you may never know for sure), just trim and be an happier coder 🙂
file_get_contents() and how to retrieve biological sequences from the Internet
file_get_contents() turns the contents of a local or online file, whose path is passed to it as an argument, to a string.
The only required argument is the path of the file, however more optional arguments are supported.
We have already used it in the first section of this chapter to generate a dynamic webpage from contents stored in different files.
Let’s now use it to retrieve a FASTA sequence from the UniProt database instead, for instance the sequence of the human protein ABL1 (Uniprot ID: P00519).
You can reach any FASTA sequence text file on the UniProt web site at an address composed as follows:
http://www.uniprot.org/uniprot/(sequence ID here).fasta
For example:
http://www.uniprot.org/uniprot/P00519.fasta
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $abl1_fasta = file_get_contents("http://www.uniprot.org/uniprot/P00519.fasta"); // The FASTA sequence of ABL1 is now stored on the $abl1_fasta variable // However if you try to display it on the page with a simple echo statement it will not look very nice // because the newlines (\n) present in the sequence will not render in HTML, you will // see the whole sequence on a single line, including the first header line. // To display it nicely in a web page you can either enclose it within "pre" tags, which we cannot show here // for technical reasons, or manipulate it to replace the \n with break tags, more on this later on. // You have been warned, let's do it anyway. Try this example yourself. echo "<p>The ABL1 sequence:</p><p>$abl1_fasta</p>"; ?> |
Here’s a slightly more sophisticated example in which we get FASTA sequences for 5 different proteins, store them into an array and then provide an output.
We will get the sequences corresponding to the following 5 ids:
P21333
P00533
P68133
P35222
O75369
We highly recommend you try this example yourself on your server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php $uniprot_ids_array = array("P21333","P00533","P68133","P35222","O75369"); $fasta_seqs_array = array(); // An empty array. It will get populated in the foreach cycle that follows foreach($uniprot_ids_array as $id){ $fasta_seq_url = "http://www.uniprot.org/uniprot/".$id.".fasta"; //computing the URL of the sequence $fasta_seqs_array[] = file_get_contents($fasta_seq_url); // Adding the sequence to the $fasta_seqs_array } // We can now output the sequences with the limitations outlined in the previous code example // So the sequences will not look well formatted on your web page, but you will be able // to see that they are indeed there $i = 1; // A counter variable used in the output below foreach($fasta_seqs_array as $fasta_seq){ echo "<p>Sequence $i:<br>$fasta_seq</p>"; // we increment the counter variable $i by one, each time we switch to the next sequence // within the foreach cycle $i++; // This is equivalent to writing $i = $i + 1 } ?> |
A note on the code sample above. We are accessing sequences on a third party server, through URLs. UniProt seems to be perfectly OK with that according to their guidelines for programmatic access. They do however specify the following, which is quite a mild statement:
“Please consider to provide your email address as part of the User-Agent header that your programs set. This will allow us to contact you in case of problems.”
Indeed sending requests to a server generates a load. If this load is excessive, the server owner may have a problem with that, and eventually ban your IP address. It is therefore a good idea, if you plan to send lots of automated (software generated) requests to a particular server, to check the server guidelines and eventually get in contact to check with them if they are OK with what you plan to do.
explode() and implode() – How to format FASTA sequences for the web
The explode() function allows us to split a string in parts based on a delimiter and store the parts as elements of an array. It takes the delimiter as first argument and the string as second argument. Mind that the delimiter cannot be an empty string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $uniprot_ids = "P21333,P00533,P68133,P35222,O75369"; // A string with the 5 IDs separated by commas $uniprot_ids_array = explode(',',$uniprot_ids); // This array now contains the 5 IDs as elements $ids_number = count($uniprot_ids_array); echo "<p>These are the $ids_number UniProt IDs</p><p><ul>"; foreach($uniprot_ids_array as $id){ echo "<li>$id</li>"; } echo "</ul></p>"; ?> |
implode() does the opposite: it take a delimiter as first argument and an array as second argument and will join the elements of the array into a string, placing the delimiter in between.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $uniprot_ids_array = array("P21333","P00533","P68133","P35222","O75369"); $uniprot_ids_as_string = implode(',',$uniprot_ids_array); echo $uniprot_ids_as_string; // P21333,P00533,P68133,P35222,O75369 ?> |
We can combine explode and implode sequentially in a single expression to perform a quick “search and replace” within a string. This comes very handy right now. Remember we had a problem with the formatting of the FASTA sequences for display in a web page, as the newlines that split the FASTA sequence in several lines in the FASTA text file is totally ignored in HTML, and each FASTA sequence was rendered in a single line when given in output in a web page as we did in previous examples in this section? We have a nice fix.
Before getting to the FASTA sequences, try this simpler example in which we replace commas with hyphens surrounded by spaces in a string.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $uniprot_ids = "P21333,P00533,P68133,P35222,O75369"; $new_format = implode(' - ',explode(',',$uniprot_ids)); echo $newformat; // P21333 - P00533 - P68133 - P35222 - O75369 ?> |
And now try the following example in which the newlines \n in FASTA sequences are replaced by break tags before being shown in the web page. This happens in the output part of the script, the second foreach cycle. We have also embedded the sequence in a span tag that was given a monospace font family (courier). This makes so that every character in the sequence has the same width resulting in an orderly appearance. The use of monospace fonts for sequences also has important implications for alignments, but this is a topic we will discuss in another section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php $uniprot_ids_array = array("P21333","P00533","P68133","P35222","O75369"); $fasta_seqs_array = array(); foreach($uniprot_ids_array as $id){ $fasta_seq_url = "http://www.uniprot.org/uniprot/".$id.".fasta"; $fasta_seqs_array[] = file_get_contents($fasta_seq_url); } // We can now output the sequences. This time, they will look good! $i = 1; // A counter variable used in the output below foreach($fasta_seqs_array as $fasta_seq){ // WE NOW DO THE MAGIC SEARCH AND REPLACE WITH EXPLODE AND IMPLODE APPLIED SEQUENTIALLY $formatted_seq = implode("<br>",explode("\n",$fasta_seq)); echo "<p><strong>Sequence $i:</strong><br>\n<span style=\"font-family:courier;\">$formatted_seq</span></p>\n"; // Note that we have embedded the sequence in a <span> tag and given courier as font to it with CSS // we increment the counter variable $i by one, each time we switch to the next sequence //within the foreach cycle $i++; } ?> |
Please run this example on your server. Find a part of the output of this script below:
Sequence 1:
>sp|P21333|FLNA_HUMAN Filamin-A OS=Homo sapiens GN=FLNA PE=1 SV=4
MSSSHSRAGQSAAGAAPGGGVDTRDAEMPATEKDLAEDAPWKKIQQNTFTRWCNEHLKCV
SKRIANLQTDLSDGLRLIALLEVLSQKKMHRKHNQRPTFRQMQLENVSVALEFLDRESIK
LVSIDSKAIVDGNLKLILGLIWTLILHYSISMPMWDEEEDEEAKKQTPKQRLLGWIQNKL
PQLPITNFSRDWQSGRALGALVDSCAPGLCPDWDSWDASKPVTNAREAMQQADDWLGIPQ
VITPEEIVDPNVDEHSVMTYLSQFPKAKLKPGAPLRPKLNPKKARAYGPGIEPTGNMVKK
RAEFTVETRSAGQGEVLVYVEDPAGHQEEAKVTANNDKNRTFSVWYVPEVTGTHKVTVLF
AGQHIAKSPFEVYVDKSQGDASKVTAQGPGLEPSGNIANKTTYFEIFTAGAGTGEVEVVI
QDPMGQKGTVEPQLEARGDSTYRCSYQPTMEGVHTVHVTFAGVPIPRSPYTVTVGQACNP
SACRAVGRGLQPKGVRVKETADFKVYTKGAGSGELKVTVKGPKGEERVKQKDLGDGVYGF
EYYPMVPGTYIVTITWGGQNIGRSPFEVKVGTECGNQKVRAWGPGLEGGVVGKSADFVVE
AIGDDVGTLGFSVEGPSQAKIECDDKGDGSCDVRYWPQEAGEYAVHVLCNSEDIRLSPFM
ADIRDAPQDFHPDRVKARGPGLEKTGVAVNKPAEFTVDAKHGGKAPLRVQVQDNEGCPVE
ALVKDNGNGTYSCSYVPRKPVKHTAMVSWGGVSIPNSPFRVNVGAGSHPNKVKVYGPGVA
KTGLKAHEPTYFTVDCAEAGQGDVSIGIKCAPGVVGPAEADIDFDIIRNDNDTFTVKYTP
RGAGSYTIMVLFADQATPTSPIRVKVEPSHDASKVKAEGPGLSRTGVELGKPTHFTVNAK
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VTDHGSCVRACGADSYEMEEDGVRKCKKCEGPCRKVCNGIGIGEFKDSLSINATNIKHFK
NCTSISGDLHILPVAFRGDSFTHTPPLDPQELDILKTVKEITGFLLIQAWPENRTDLHAF
ENLEIIRGRTKQHGQFSLAVVSLNITSLGLRSLKEISDGDVIISGNKNLCYANTINWKKL
FGTSGQKTKIISNRGENSCKATGQVCHALCSPEGCWGPEPRDCVSCRNVSRGRECVDKCN
LLEGEPREFVENSECIQCHPECLPQAMNITCTGRGPDNCIQCAHYIDGPHCVKTCPAGVM
GENNTLVWKYADAGHVCHLCHPNCTYGCTGPGLEGCPTNGPKIPSIATGMVGALLLLLVV
ALGIGLFMRRRHIVRKRTLRRLLQERELVEPLTPSGEAPNQALLRILKETEFKKIKVLGS
GAFGTVYKGLWIPEGEKVKIPVAIKELREATSPKANKEILDEAYVMASVDNPHVCRLLGI
CLTSTVQLITQLMPFGCLLDYVREHKDNIGSQYLLNWCVQIAKGMNYLEDRRLVHRDLAA
RNVLVKTPQHVKITDFGLAKLLGAEEKEYHAEGGKVPIKWMALESILHRIYTHQSDVWSY
GVTVWELMTFGSKPYDGIPASEISSILEKGERLPQPPICTIDVYMIMVKCWMIDADSRPK
FRELIIEFSKMARDPQRYLVIQGDERMHLPSPTDSNFYRALMDEEDMDDVVDADEYLIPQ
QGFFSSPSTSRTPLLSSLSATSNNSTVACIDRNGLQSCPIKEDSFLQRYSSDPTGALTED
SIDDTFLPVPEYINQSVPKRPAGSVQNPVYHNQPLNPAPSRDPHYQDPHSTAVGNPEYLN
TVQPTCVNSTFDSPAHWAQKGSHQISLDNPDYQQDFFPKEAKPNGIFKGSTAENAEYLRV
APQSSEFIGA
Searching a substring or character inside a string with strrchr()
It is often useful to be able to check if a sequence of characters, for example an oligonucleotide or short peptide sequence, or a single character, for example a single base or amino-acid, is present within a longer sequence. The strrchr() function (string research) can help. it takes 2 arguments: the first is the string in which to search (haystack), the second the substring to be searched (needle). It returns a portion of the haystack that starts with the first occurrence of the needle to the end of the haystack or “false” if the needle is not found in the haystack. Therefore the result will equal (==) true if the substring is found and false if not found.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $haystack_string = "Hello, world! It is a beautiful day."; $needle_string = "world"; $search_result = strrchr($haystack_string,$needle_string); echo "<p>The search result is \"$search_result\"</p>"; ?> |
Let’s try a slightly more sophisticated and useful example in which we will check if a given amino-acid is nonpolar, polar, basic or acidic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php $nonpolar ="FLIMVPAWG"; // A string made by all the nonpolar amino-acids in single letter notation $polar = "STYCQN"; // Polar amino-acids $basic = "HKR"; // Basic amino-acids $acidic = "DE"; // Acidic amino-acids // In the $aminoacid_nature variable we will store a word (string) describing the amino-acid class // Options will be: "nonpolar", "polar", "basic" or "acidic" $aminoacid_nature = ""; $aminoacid = "K"; // We now start to look for our $aminoacid, the needle, in the 4 classes of // amino-acids defined above as strings, $nonpolar, $polar etc..., the haystacks if(strrchr($nonpolar, $aminoacid)){ $aminoacid_nature = "nonpolar"; } elseif(strrchr($polar, $aminoacid)){ $aminoacid_nature = "polar"; } elseif(strrchr($basic, $aminoacid)){ $aminoacid_nature = "basic"; } elseif(strrchr($acidic, $aminoacid)){ $aminoacid_nature = "acidic"; } echo "<p>Amino-acid <strong>$aminoacid</strong> is <strong>$aminoacid_nature</strong></p>"; ?> |
This is the output of this script:
Amino-acid K is basic
In the next section we will look at a few more built-in functions that will allow us further strings and biological sequences manipulation with PHP. Stay tuned!
Chapter Sections
[pagelist include=”435″]
[siblings]