{"id":739,"date":"2017-02-21T20:16:31","date_gmt":"2017-02-21T20:16:31","guid":{"rendered":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/?page_id=739"},"modified":"2017-05-04T16:04:58","modified_gmt":"2017-05-04T16:04:58","slug":"php-programming-language-basics-built-in-predefined-functions-strings-and-biological-sequences-manipulation","status":"publish","type":"page","link":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-4-adding-a-dynamic-layer-introducing-the-php-programming-language\/php-programming-language-basics-built-in-predefined-functions-strings-and-biological-sequences-manipulation\/","title":{"rendered":"4-6: PHP programming language basics &#8211; built-in predefined functions, strings and biological sequences manipulation"},"content":{"rendered":"<p>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.<\/p>\n<p>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!<\/p>\n<p>Before starting, we should mention some generic information on functions calls.<\/p>\n<p>A function call is performed by writing the name of the function followed by round brackets. Let&#8217;s imagine to have a function called &#8220;bestfunction&#8221;. We can call it like this:<\/p>\n<p>bestfunction();<\/p>\n<p>If the function returns something we need to collect, we can write:<\/p>\n<p>$function_output = bestfunction();<\/p>\n<p>so that the bestfunction() output is now stored to the $function_output variable.<\/p>\n<p>It it not uncommon for functions to need &#8220;arguments&#8221; 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.<\/p>\n<p>Here are a few sample calls to a function:<\/p>\n<p>$function_output = bestfunction($argument1); \/\/ If the argument is passed as a variable name, the variable is actually automatically interpolated to it&#8217;s value &#8220;under the hood&#8221; and then passed to the function. The variable that is passed as argument should have of course been defined in the script code <strong>before<\/strong> the function call.<\/p>\n<p>$function_output = bestfunction(&#8220;gaattc&#8221;); \/\/ In this case we are passing a value directly, rather than a variable<\/p>\n<p>$function_output = bestfunction($argument1,$argument2); \/\/ A function called with 2 arguments<\/p>\n<p>$function_output = bestfunction(&#8220;GTCTAGTGA&#8221;, 2); \/\/ Depending on the function, arguments may be of different types: numbers, strings, arrays, boolean values etc&#8230;<\/p>\n<h2>Listing all available built-in PHP functions<\/h2>\n<p>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 &#8220;internal&#8221; functions) and those eventually defined by the user.<\/p>\n<p>This function is <strong>get_defined_functions()<\/strong>. A call to it, without arguments, will return an array with two keys, &#8220;internal&#8221; and &#8220;user&#8221;. As you may guess, the key &#8220;internal&#8221; provides access to an array with the names of PHP built-in functions, while the &#8220;user&#8221; key provides access to an array with all the names of the functions defined by the user (you, the programmer), if any.<\/p>\n<p>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.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$available_functions = get_defined_functions();\r\n\r\n$builtin_functions = $available_functions[\"internal\"];\r\n\r\necho \"<p><ul>\\n\";\r\nforeach($builtin_functions as $available_function){\r\n    echo \"<li>$available_function<\/li>\\n\";\r\n}\r\necho \"<\/ul><\/p>\\n\";\r\n\r\n?>\r\n<\/code><\/pre>\n<p>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.<\/p>\n<h2>strtoupper() and strtolower()<\/h2>\n<p>The strtoupper(), <strong>string-to-upper<\/strong>, and strtolower(), <strong>string-to-lower<\/strong> functions allow us to convert strings passed as arguments to all uppercase or all lowercase, respectively.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$myseq = \"gtcctagatgctg\";\r\n\r\n$myseq_upper = strtoupper($myseq);\r\n\r\necho \"<p>$myseq_upper<\/p>\\n\"; \/\/ Will output GTCCTAGATGCTG\r\n\r\n$myseq2 = \"GTcctaGatGCtg\";\r\n\r\n$myseq2_upper = strtoupper($myseq); \r\n$myseq2_lower = strtolower($myseq); \r\n\r\necho \"<p>$myseq2_upper<\/p>\\n\"; \/\/ Will output GTCCTAGATGCTG\r\n\r\necho \"<p>$myseq2_lower<\/p>\\n\"; \/\/ Will output gtcctagatgctg\r\n\r\n?>\r\n<\/code><\/pre>\n<h2>Reverse strings with strrev()<\/h2>\n<p>The strrev() <strong>string reverse<\/strong> function takes a string as an argument and reverses it, as simple as that. mystring will become gnirtsym.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$myseq = \"atcctagatgctg\";\r\n\r\n$myseq_reversed = strrev($myseq);\r\n\r\necho \"<p>The reverse of the $myseq sequence is $myseq_reversed<\/p>\\n\";\r\n\/\/ The reverse of the atcctagatgctg sequence is gtcgtagatccta\r\n\r\n?>\r\n<\/code><\/pre>\n<h2>strlen()<\/h2>\n<p>The strlen() (<strong>string length<\/strong>) function takes a string as argument and returns the number of characters composing it, as an integer.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$myseq = \"gtcctagatgctg\";\r\n\r\n$myseq_len = strlen($myseq);\r\n\r\necho '<p>The $myseq string length is '.$myseq_len.\" characters<\/p>\\n\";\r\n\/\/ The $myseq string length is 13 characters\r\n\r\n?>\r\n<\/code><\/pre>\n<h2>count()<\/h2>\n<p>count() takes an array as argument and returns the number of elements it contains, as an integer.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$fruits = array(\"apples\",\"pears\",\"bananas\");\r\n\r\n$fruits_count = count($fruits); \/\/ 3\r\n\r\necho '<p>The $fruits array contains '.$fruits_count.' elements<\/p>\\n'; \/\/ The $fruits array contains 3 elements\r\n\r\n$complement_dict = array(\r\n    \"A\" => \"T\",\r\n    \"T\" => \"A\",\r\n    \"G\" => \"C\",\r\n    \"C\" => \"G\"\r\n);\r\n\r\n$comp_count = count($complement_dict);\r\n\r\necho \"<p>There are $comp_count elements in the complement dictionary<\/p>\";\r\n\/\/ There are 4 elements in the complement dictionary\r\n\r\n?>\r\n<\/code><\/pre>\n<h2 id=\"round\">round()<\/h2>\n<p>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. <\/p>\n<p>The &#8220;precision&#8221; 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 (&#960;) will actually return 3.1415926535898. <\/p>\n<p>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.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n$a = 5.325675;\r\n$b = round($a, 2); \/\/ $b is now 5.33\r\n\r\n$c = round(5\/3, 3); \/\/ $c is now 1.667\r\n\r\necho pi(); \/\/ 3.1415926535898\r\n\r\n$pi_rounded = round(pi(), 2); \/\/ 3.14\r\n?>\r\n<\/code><\/pre>\n<h2 id=\"trim\">trim()<\/h2>\n<p>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:<\/p>\n<p>&#8221; &#8221;    ordinary space<br \/>\n&#8220;\\n&#8221;   newline<br \/>\n&#8220;\\r&#8221;   carriage return<br \/>\n&#8220;\\t&#8221;   tab<br \/>\n&#8220;\\0&#8221;   the null byte<br \/>\n&#8220;\\x0B&#8221; vertical tab<\/p>\n<p>trim() can be called with a second argument that specifies which characters are to be included in the remove list.   <\/p>\n<p>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.<\/p>\n<p>Let&#8217;s quote <a href=\"https:\/\/en.wikipedia.org\/wiki\/Newline\">Wikipedia<\/a>:<\/p>\n<p><em>&#8220;Most textual Internet protocols (including HTTP, SMTP, FTP, IRC, and many others) mandate the use of ASCII CR+LF (&#8216;\\r\\n&#8217;, 0x0D 0x0A) on the protocol level, but recommend that tolerant applications recognize lone LF (&#8216;\\n&#8217;, 0x0A) as well. Despite the dictated standard, many applications erroneously use the C newline escape sequence &#8216;\\n&#8217; (LF) instead of the correct combination of carriage return escape and newline escape sequences &#8216;\\r\\n&#8217; (CR+LF) &#8220;<\/em><\/p>\n<p>You can split a text file ($text) into lines ($lines) with a command such as:<\/p>\n<pre lang=\"php\"><code>\r\n\r\n$lines = preg_split(\"\\n\", $text);\r\n\r\n<\/code><\/pre>\n<p>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.<\/p>\n<p>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:<\/p>\n<pre lang=\"php\"><code>\r\nforeach($lines as $line){\r\n    $line = trim($line);\r\n    \/\/ Do stuff with the nicely trimmed line...\r\n}\r\n<\/code><\/pre>\n<p>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:<\/p>\n<p>$mystring = Hello, World&#8230;  ..<br \/>\n$mystring1 = Hello, World.  ,<br \/>\n$mystring2 = .Hello, World,<\/p>\n<p>By calling a line like: <\/p>\n<pre lang=\"php\"><code>\r\n\r\n$cleaned = trim($mystring, \" .,\")\r\n\r\n<\/code><\/pre>\n<p>on any of the above, the value of $cleaned will always be &#8220;Hello, World&#8221;.<\/p>\n<p>So now you can&#8217;t say we didn&#8217;t tell you to always trim your lines. Maybe there is a trailing \\r, maybe there isn&#8217;t (you may never know for sure), just trim and be an happier coder \ud83d\ude42<\/p>\n<h2 id=\"file_get_contents\">file_get_contents() and how to retrieve biological sequences from the Internet<\/h2>\n<p>file_get_contents() turns the contents of a local or online file, whose path is passed to it as an argument, to a string. <\/p>\n<p>The only required argument is the path of the file, however <a href=\"https:\/\/www.w3schools.com\/php\/func_filesystem_file_get_contents.asp\" target=\"_blank\">more optional arguments are supported<\/a>.<\/p>\n<p>We have already used it in the <a href=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-4-adding-a-dynamic-layer-introducing-the-php-programming-language\/dynamic-web-pages-with-php-a-simple-yet-useful-example\/\">first section of this chapter<\/a> to generate a dynamic webpage from contents stored in different files. <\/p>\n<p>Let&#8217;s now use it to retrieve a FASTA sequence from the <a href=\"http:\/\/www.uniprot.org\/\" target=\"_blank\">UniProt<\/a> database instead, for instance the sequence of the human protein ABL1 (<a href=\"http:\/\/www.uniprot.org\/uniprot\/P00519\" target=\"_blank\">Uniprot ID: P00519<\/a>).<\/p>\n<p>You can reach any FASTA sequence text file on the UniProt web site at an address composed as follows:<\/p>\n<p>http:\/\/www.uniprot.org\/uniprot\/(sequence ID here).fasta<\/p>\n<p>For example:<\/p>\n<p><a href=\"http:\/\/www.uniprot.org\/uniprot\/P00519.fasta\" target=\"_blank\">http:\/\/www.uniprot.org\/uniprot\/P00519.fasta<\/a><\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$abl1_fasta = file_get_contents(\"http:\/\/www.uniprot.org\/uniprot\/P00519.fasta\");\r\n\r\n\/\/ The FASTA sequence of ABL1 is now stored on the $abl1_fasta variable\r\n\/\/ However if you try to display it on the page with a simple echo statement it will not look very nice\r\n\/\/ because the newlines (\\n) present in the sequence will not render in HTML, you will\r\n\/\/ see the whole sequence on a single line, including the first header line.\r\n\/\/ To display it nicely in a web page you can either enclose it within \"pre\" tags, which we cannot show here\r\n\/\/ for technical reasons, or manipulate it to replace the \\n with break tags, more on this later on.\r\n\r\n\/\/ You have been warned, let's do it anyway. Try this example yourself.\r\n\r\necho \"<p>The ABL1 sequence:<\/p><p>$abl1_fasta<\/p>\";\r\n?>\r\n<\/code><\/pre>\n<p>Here&#8217;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. <\/p>\n<p>We will get the sequences corresponding to the following 5 ids:<\/p>\n<p>P21333<br \/>\nP00533<br \/>\nP68133<br \/>\nP35222<br \/>\nO75369<\/p>\n<p>We highly recommend you try this example yourself on your server.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$uniprot_ids_array = array(\"P21333\",\"P00533\",\"P68133\",\"P35222\",\"O75369\");\r\n$fasta_seqs_array = array(); \/\/ An empty array. It will get populated in the foreach cycle that follows\r\n\r\nforeach($uniprot_ids_array as $id){\r\n    $fasta_seq_url = \"http:\/\/www.uniprot.org\/uniprot\/\".$id.\".fasta\"; \/\/computing the URL of the sequence\r\n    $fasta_seqs_array[] = file_get_contents($fasta_seq_url); \/\/ Adding the sequence to the $fasta_seqs_array\r\n}\r\n\r\n\/\/ We can now output the sequences with the limitations outlined in the previous code example\r\n\/\/ So the sequences will not look well formatted on your web page, but you will be able\r\n\/\/ to see that they are indeed there\r\n\r\n$i = 1; \/\/ A counter variable used in the output below\r\n\r\nforeach($fasta_seqs_array as $fasta_seq){\r\n    echo \"<p>Sequence $i:<br>$fasta_seq<\/p>\";\r\n    \/\/ we increment the counter variable $i by one, each time we switch to the next sequence\r\n    \/\/ within the foreach cycle\r\n    $i++; \/\/ This is equivalent to writing $i = $i + 1\r\n}\r\n\r\n?>\r\n<\/code><\/pre>\n<p>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 <a href=\"http:\/\/www.uniprot.org\/help\/programmatic_access\" target=\"_blank\">according to their guidelines for programmatic access<\/a>. They do however specify the following, which is quite a mild statement:<\/p>\n<p><em>&#8220;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.&#8221;<\/em><\/p>\n<p>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.<\/p>\n<h2>explode() and implode() &#8211; How to format FASTA sequences for the web<\/h2>\n<p>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.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$uniprot_ids = \"P21333,P00533,P68133,P35222,O75369\"; \/\/ A string with the 5 IDs separated by commas\r\n\r\n$uniprot_ids_array = explode(',',$uniprot_ids); \/\/ This array now contains the 5 IDs as elements\r\n\r\n$ids_number = count($uniprot_ids_array);\r\n\r\necho \"<p>These are the $ids_number UniProt IDs<\/p><p><ul>\";\r\n\r\nforeach($uniprot_ids_array as $id){\r\n    echo \"<li>$id<\/li>\";\r\n}\r\n\r\necho \"<\/ul><\/p>\";\r\n\r\n?>\r\n<\/code><\/pre>\n<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.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$uniprot_ids_array = array(\"P21333\",\"P00533\",\"P68133\",\"P35222\",\"O75369\");\r\n\r\n$uniprot_ids_as_string = implode(',',$uniprot_ids_array);\r\n\r\necho $uniprot_ids_as_string; \/\/ P21333,P00533,P68133,P35222,O75369\r\n\r\n?>\r\n<\/code><\/pre>\n<p>We can combine explode and implode sequentially in a single expression to perform a quick &#8220;<strong>search and replace&#8221; within a string<\/strong>. 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.<\/p>\n<p>Before getting to the FASTA sequences, try this simpler example in which we replace commas with hyphens surrounded by spaces in a string.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$uniprot_ids = \"P21333,P00533,P68133,P35222,O75369\";\r\n\r\n$new_format = implode(' - ',explode(',',$uniprot_ids));\r\n\r\necho $newformat; \/\/ P21333 - P00533 - P68133 - P35222 - O75369\r\n\r\n?>\r\n<\/code><\/pre>\n<p>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 <span style=\"font-family:courier;\">monospace font family (courier)<\/span>. 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.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$uniprot_ids_array = array(\"P21333\",\"P00533\",\"P68133\",\"P35222\",\"O75369\");\r\n$fasta_seqs_array = array();\r\n\r\nforeach($uniprot_ids_array as $id){\r\n    $fasta_seq_url = \"http:\/\/www.uniprot.org\/uniprot\/\".$id.\".fasta\";\r\n    $fasta_seqs_array[] = file_get_contents($fasta_seq_url);\r\n}\r\n\r\n\/\/ We can now output the sequences. This time, they will look good!\r\n\r\n$i = 1; \/\/ A counter variable used in the output below\r\n\r\nforeach($fasta_seqs_array as $fasta_seq){\r\n\r\n    \/\/ WE NOW DO THE MAGIC SEARCH AND REPLACE WITH EXPLODE AND IMPLODE APPLIED SEQUENTIALLY\r\n    $formatted_seq = implode(\"<br>\",explode(\"\\n\",$fasta_seq));\r\n    \r\n    echo \"<p><strong>Sequence $i:<\/strong><br>\\n<span style=\\\"font-family:courier;\\\">$formatted_seq<\/span><\/p>\\n\";\r\n    \/\/ Note that we have embedded the sequence in a <span> tag and given courier as font to it with CSS\r\n    \/\/ we increment the counter variable $i by one, each time we switch to the next sequence\r\n    \/\/within the foreach cycle\r\n    $i++; \r\n}\r\n\r\n?>\r\n<\/code><\/pre>\n<p>Please run this example on your server. Find a part of the output of this script below:<\/p>\n<p><strong>Sequence 1:<\/strong><br \/>\n<span style=\"font-family:courier;\">>sp|P21333|FLNA_HUMAN Filamin-A OS=Homo sapiens G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FTARVTGDDSMRMSHLKVGSAADIPINISETDL<br \/>SLLTATVVPPSGREEPCLLKRLRNGHVGISFVPKETGEHLVHVKKNGQHVASSPIPVVIS<br \/>QSEIGDASRVRVSGQGLHEGHTFEPAEFIIDTRDAGYGGLSLSIEGPSKVDINTEDLEDG<br \/>TCRVTYCPTEPGNYIINIKFADQHVPGSPFSVKVTGEGRVKESITRRRRAPSVANVGSHC<br \/>DLSLKIPEISIQDMTAQVTSPSGKTHEAEIVEGENHTYCIRFVPAEMGTHTVSVKYKGQH<br \/>VPGSPFQFTVGPLGEGGAHKVRAGGPGLERAEAGVPAEFSIWTREAGAGGLAIAVEGPSK<br \/>AEISFEDRKDGSCGVAYVVQEPGDYEVSVKFNEEHIPDSPFVVPVASPSGDARRLTVSSL<br \/>QESGLKVNQPASFAVSLNGAKGAIDAKVHSPSGALEECYVTEIDQDKYAVRFIPRENGVY<br \/>LIDVKFNGTHIPGSPFKIRVGEPGHGGDPGLVSAYGAGLEGGVTGNPAEFVVNTSNAGAG<br \/>ALSVTIDGPSKVKMDCQECPEGYRVTYTPMAPGSYLISIKYGGPYHIGGSPFKAKVTGPR<br \/>LVSNHSLHETSSVFVDSLTKATCAPQHGAPGPGPADASKVVAKGLGLSKAYVGQKSSFTV<br \/>DCSKAGNNMLLVGVHGPRTPCEEILVKHVGSRLYSVSYLLKDKGEYTLVVKWGDEHIPGS<br \/>PYRVVVP<br \/><\/span><\/p>\n<p><strong>Sequence 2:<\/strong><br \/>\n<span style=\"font-family:courier;\">>sp|P00533|EGFR_HUMAN Epidermal growth factor receptor OS=Homo sapiens GN=EGFR PE=1 SV=2<br \/>MRPSGTAGAALLALLAALCPASRALEEKKVCQGTSNKLTQLGTFEDHFLSLQRMFNNCEV<br \/>VLGNLEITYVQRNYDLSFLKTIQEVAGYVLIALNTVERIPLENLQIIRGNMYYENSYALA<br \/>VLSNYDANKTGLKELPMRNLQEILHGAVRFSNNPALCNVESIQWRDIVSSDFLSNMSMDF<br \/>QNHLG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RV<br \/>APQSSEFIGA<br \/><\/span><\/p>\n<h2>Searching a substring or character inside a string with strrchr()<\/h2>\n<p>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 (<strong>string research<\/strong>) 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 &#8220;false&#8221; if the needle is not found in the haystack. Therefore <a href=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-4-adding-a-dynamic-layer-introducing-the-php-programming-language\/php-programming-language-basics-conditional-statements-if-elseif-else\/\">the result will equal (==)<\/a> true if the substring is found and false if not found.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$haystack_string = \"Hello, world! It is a beautiful day.\";\r\n$needle_string = \"world\";\r\n\r\n$search_result = strrchr($haystack_string,$needle_string);\r\n\r\necho \"<p>The search result is \\\"$search_result\\\"<\/p>\";\r\n\r\n?>\r\n<\/code><\/pre>\n<p>Let&#8217;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.<\/p>\n<pre lang=\"php\"><code>\r\n<?php\r\n\r\n$nonpolar =\"FLIMVPAWG\"; \/\/ A string made by all the nonpolar amino-acids in single letter notation\r\n$polar = \"STYCQN\"; \/\/ Polar amino-acids\r\n$basic = \"HKR\"; \/\/ Basic amino-acids\r\n$acidic = \"DE\"; \/\/ Acidic amino-acids\r\n\r\n\/\/ In the $aminoacid_nature variable we will store a word (string) describing the amino-acid class\r\n\/\/ Options will be: \"nonpolar\", \"polar\", \"basic\" or \"acidic\"\r\n$aminoacid_nature = \"\"; \r\n\r\n$aminoacid = \"K\";\r\n\r\n\/\/ We now start to look for our $aminoacid, the needle, in the 4 classes of \r\n\/\/ amino-acids defined above as strings, $nonpolar, $polar etc..., the haystacks\r\n\r\nif(strrchr($nonpolar, $aminoacid)){\r\n    $aminoacid_nature = \"nonpolar\";\r\n}\r\nelseif(strrchr($polar, $aminoacid)){\r\n    $aminoacid_nature = \"polar\";\r\n}\r\nelseif(strrchr($basic, $aminoacid)){\r\n    $aminoacid_nature = \"basic\";\r\n}\r\nelseif(strrchr($acidic, $aminoacid)){\r\n    $aminoacid_nature = \"acidic\";\r\n}\r\n\r\necho \"<p>Amino-acid <strong>$aminoacid<\/strong> is <strong>$aminoacid_nature<\/strong><\/p>\";\r\n\r\n?>\r\n<\/code><\/pre>\n<p>This is the output of this script:<\/p>\n<p>Amino-acid <strong>K<\/strong> is <strong>basic<\/strong><\/p>\n<p>In the next section we will look at a few <a href=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-4-adding-a-dynamic-layer-introducing-the-php-programming-language\/php-programming-language-basics-more-on-strings-and-biological-sequences-manipulation-with-predefined-functions\/\">more built-in functions that will allow us further strings and biological sequences manipulation with PHP<\/a>. Stay tuned!<\/p>\n<div class=\"google-ad\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- bioinfo web dev 2 --><br \/>\n<ins class=\"adsbygoogle\" style=\"display: inline-block; width: 728px; height: 90px;\" data-ad-client=\"ca-pub-0159360445983090\" data-ad-slot=\"3442176918\"><\/ins><br \/>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/div>\n<h2>Chapter Sections<\/h2>\n<p>[pagelist include=&#8221;435&#8243;]<\/p>\n<p>[siblings]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/chapter-4-adding-a-dynamic-layer-introducing-the-php-programming-language\/php-programming-language-basics-built-in-predefined-functions-strings-and-biological-sequences-manipulation\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;4-6: PHP programming language basics &#8211; built-in predefined functions, strings and biological sequences manipulation&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":435,"menu_order":6,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-739","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/comments?post=739"}],"version-history":[{"count":124,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/739\/revisions"}],"predecessor-version":[{"id":2071,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/739\/revisions\/2071"}],"up":[{"embeddable":true,"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/pages\/435"}],"wp:attachment":[{"href":"http:\/\/www.cellbiol.com\/bioinformatics_web_development\/wp-json\/wp\/v2\/media?parent=739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}