Login | Register

Info | Home

BioPHP - Complement of a DNA or RNA sequence

Original code submitted by joseba
Code bellow is covered by GNU GPL v2 license.

Description

Last change: 2010/10/18 17:04 | Edit description | Recent Changes | Original description
Gets the genetic complement of a DNA or RNA sequence. 

Code

Last change: 2010/10/18 17:04 | Edit Code | Recent Changes | Download | Original code
function complement($seq, $moltype)
        {
        if (isset($moltype) == FALSE)
                if (isset($this->moltype) == TRUE) $moltype = $this->moltype;
                else $moltype = "DNA"; // default to DNA.
        
        $dna_complements = array("A" => "T",
                                 "T" => "A",
                                 "G" => "C",
                                 "C" => "G");
        $rna_complements = array("A" => "U",
                                 "U" => "A",
                                 "G" => "C",
                                 "C" => "G");
        $moltype = strtoupper($moltype);
        if ($moltype == "DNA") $comp_r = $dna_complements;
        elseif ($moltype == "RNA") $comp_r = $rna_complements;
        $seqlen = strlen($seq);
        $compseq = "";
        for($i = 0; $i < $seqlen; $i++)
                {
                $symbol = substr($seq, $i, 1);
                $compseq .= $comp_r[$symbol];
                }
        return $compseq;
        }