Source code of BlastXML.php class
<?php /** * @file BlastXML.php * Provides a class for handling Blast XML files. * Orginally writen by Riadh Hammami */ class BlastXML { private $data; private $params = array(); private $Iterations = array(); public function __construct(SimpleXMLElement $BlastXMLData = NULL) { if ($BlastXMLData) { $this--->setData($BlastXMLData); } } public function setData(SimpleXMLElement $BlastXMLData) { $this->data = $BlastXMLData->data; $this->parameters(); //$BlastXMLData->asXML(); $this->SetIterations(); return $this; } private function SetIterations() { $iterations = $this->data->xpath('//Iteration'); if (isset($iterations)) { foreach ($iterations as $iteration) { $this->Iterations[] = $this->fetch_iteration($iteration); } } } private function fetch_iteration(SimpleXMLElement $iteration) { $id = $iteration->{"Iteration_query-ID"}; $query = $iteration->{"Iteration_query-def"}; $length = $iteration->{"Iteration_query-len"}; $hits = $iteration->xpath('.//Iteration_hits/Hit'); $Hits_array = array(); foreach ($hits as $hit) { $Hit_id = $hit->{"Hit_id"}; $Hit_def = $hit->{"Hit_def"}; $Hit_accession = $hit->{"Hit_accession"}; $Hit_len = $hit->{"Hit_len"}; $Hsps = $hit->xpath('.//Hsp'); $Hsp_array = array(); foreach ($Hsps as $Hsp) { $Hsp_num = $Hsp->{"Hsp_num"}; $Hsp_bit_score = $Hsp->{"Hsp_bit-score"}; $Hsp_score = $Hsp->{"Hsp_score"}; $Hsp_evalue = $Hsp->{"Hsp_evalue"}; $Hsp_query_from = $Hsp->{"Hsp_query-from"}; $Hsp_query_to = $Hsp->{"Hsp_query-to"}; $Hsp_hit_from = $Hsp->{"Hsp_hit-from"}; $Hsp_hit_to = $Hsp->{"Hsp_hit-to"}; $Hsp_query_frame = $Hsp->{"Hsp_query-frame"}; $Hsp_hit_frame = $Hsp->{"Hsp_hit-frame"}; $Hsp_identity = $Hsp->{"Hsp_identity"}; $Hsp_positive = $Hsp->{"Hsp_positive"}; $Hsp_gaps = $Hsp->{"Hsp_gaps"}; $Hsp_align_len = $Hsp->{"Hsp_align-len"}; $Hsp_qseq = $Hsp->{"Hsp_qseq"}; $Hsp_hseq = $Hsp->{"Hsp_hseq"}; $Hsp_midline = $Hsp->{"Hsp_midline"}; $Hsp_array[] = array("Hsp_num" => (int)$Hsp_num[0], "Hsp_bit-score" => (float)$Hsp_bit_score[0], "Hsp_score" => (float)$Hsp_score[0], "Hsp_evalue" => (float)$Hsp_evalue[0], "Hsp_query-from" => (int)$Hsp_query_from[0], "Hsp_query-to" => (int)$Hsp_query_to[0], "Hsp_hit-from" => (int)$Hsp_hit_from[0], "Hsp_hit-to" => (int)$Hsp_hit_to[0], "Hsp_query-frame"=> (int)$Hsp_query_frame[0], "Hsp_hit-frame" => (int)$Hsp_hit_frame[0], "Hsp_identity" => (int)$Hsp_identity[0], "Hsp_positive" => (int)$Hsp_positive[0], "Hsp_gaps" => (int)$Hsp_gaps[0], "Hsp_align-len" => (int)$Hsp_align_len[0], "Hsp_qseq" => (string)$Hsp_qseq[0], "Hsp_hseq" => (string)$Hsp_hseq[0], "Hsp_midline" => (string)$Hsp_midline[0]); } $Hits_array[] = array ("Hit_id"=> (string)$Hit_id[0], "Hit_def"=> (string)$Hit_def[0], "Hit_accession"=> (string)$Hit_accession[0], "Hit_len"=> (int)$Hit_len[0], "Hit_hsps"=> $Hsp_array); } $stats = $iteration->xpath('.//Iteration_stat/Statistics'); $Statistics_db_num = $stats->{"Statistics_db-num"}; $Statistics_db_len = $stats->{"Statistics_db-len"}; $Statistics_hsp_len = $stats->{"Statistics_hsp-len"}; $Statistics_eff_space = $stats->{"Statistics_eff-space"}; $Statistics_kappa = $stats->{"Statistics_kappa"}; $Statistics_lambda = $stats->{"Statistics_lambda"}; $Statistics_entropy = $stats->{"Statistics_entropy"}; $stats_array = array ("Statistics_db-num" => (int)$Statistics_db_num[0], "Statistics_db-len" => (int)$Statistics_db_len[0], "Statistics_hsp-len" => (int)$Statistics_hsp_len[0], "Statistics_eff-space" => (float)$Statistics_eff_space[0], "Statistics_kappa" => (float)$Statistics_kappa[0], "Statistics_lambda" => (float)$Statistics_lambda[0], "Statistics_entropy" => (float)$Statistics_entropy[0]); $It_message = $iteration->{"Iteration_message"}; $iteration_message = NULL; if (isset($It_message)) $iteration_message=(string)$It_message[0]; return array( "ID" => (string)$id[0], "query" => (string)$query[0], "length" => (int)$length[0], "Hits" => $Hits_array, "Statistics" => $stats_array, "Iteration_message" => $iteration_message); //return NULL; } private function parameters() { $program = $this->data->xpath('//BlastOutput_program'); $version = $this->data->xpath('//BlastOutput_version'); $reference = $this->data->xpath('//BlastOutput_reference'); $db = $this->data->xpath('//BlastOutput_db'); $matrix = $this->data->xpath('//Parameters_matrix'); $expect = $this->data->xpath('//Parameters_expect'); $gap_open = $this->data->xpath('//Parameters_gap-open'); $gap_extend = $this->data->xpath('//Parameters_gap-extend'); $filter = $this->data->xpath('//Parameters_filter'); $this->params = array("program" => (string)$program[0], "version" => (string)$version[0], "reference" => (string)$reference[0], "db" => (string)$db[0], "matrix" => (string)$matrix[0], "expect" => (int)$expect[0], "gap-open" => (int)$gap_open[0], "gap-extend" => (int)$gap_extend[0], "filter" => (string)$filter[0]); return $this->params; } public function program() { return $this->params["program"];} public function version() { return $this->params["version"];} public function reference() { return $this->params["reference"];} public function db() { return $this->params["db"];} public function matrix() { return $this->params["matrix"];} public function Results() {return $this->Iterations;} } ?>
Demo
<?php include("BlastXML.php"); $filename="results.xml"; //XML Blast output file $xml = simplexml_load_file($filename); $blast = new BlastXML($xml); //echo "Program :" . $blast->program() . "<br>"; echo "Version:" . $blast->version() . "<br>"; //echo "Reference:" . $blast->reference() . "<br>"; //echo "DB:" . $blast->db() . "<br>"; //echo "Matrix:" . $blast->matrix() . "<br>"; $Results = $blast->Results(); foreach($Results as $Result){ //var_dump($Results); // for debug purpose echo "<b>Query= " . $Result["query"]. "</b><br>"; echo "<i>Length= " . $Result["length"]. "</i><br>"; if (count($Result["Hits"])>0) { echo '<table width="700">'; echo "<tr><td align=\"center\"><b><br>Sequences</td><td><b><br>producing significant alignments:</td><td align=\"center\"><b>Score<br>(bits)</td><td align=\"center\"><b>E<br>Value</b></td></tr>"; foreach ($Result["Hits"] as $hit) { $id = $hit["Hit_id"]; $Name = $hit["Hit_def"]; $Score = $hit["Hit_hsps"][0]["Hsp_bit-score"]; $Evalue = $hit["Hit_hsps"][0]["Hsp_evalue"]; echo "<td align=\"center\"><div id='".$id."Table'>" ."<a href='" . $id . "'>" .$id . "</a></div></td>" ."<td><a href=\"" . $id . "\">" .$Name . "</a>" ."</td>" ."<td align=\"center\">" . $Score ."</td>" ."<td>".$Evalue ."</td>" ."</tr>"; } echo "</table>"; } else { echo $Result["Iteration_message"]; } } ?>