Extracting Multiple FASTA Sequences using PHP

Multiple FASTA Sequences

This is a simple tutorial which explains how to safely extract multiple sequences from a FASTA file using PHP script. I have used four functions to perform different tasks: read_fas_file() - to check invalid file, fas_check() - to check FASTA file format, get_seq() - to retrieve sequence and sequence name pairs, and fas_get() - to extract and display multiple FASTA file formatted sequences. The full source code and multiple protein sequences in FASTA file format used in this tutorial is given below.

Source Code

function read_fas_file($x) { // Check for Empty File
 if (!file_exists($x)) {
  print "File Not Exist!!";
  exit();
 } else {
  $fh = fopen($x, 'r');
  if (filesize($x) == 0) {
   print "File is Empty!!";
   fclose($fh);
   exit();
  } else {
   $f = fread($fh, filesize($x));
   fclose($fh);
   return $f;
  }
 }
}

function fas_check($x) { // Check FASTA File Format
 $gt = substr($x, 0, 1);
 if ($gt != ">") {
  print "Not FASTA File!!";
  exit();
 } else {
  return $x;
 }
}

function get_seq($x) { // Get Sequence and Sequence Name
 $fl = explode(PHP_EOL, $x);
 $sh = trim(array_shift($fl));
 if($sh == null) {
  $sh = "UNKNOWN SEQUENCE";
 }
 $fl = array_filter($fl);
 $seq = "";
 foreach($fl as $str) {
  $seq .= trim($str);
 }
 $seq = strtoupper($seq);
 $seq = preg_replace("/[^ACDEFGHIKLMNPQRSTVWY]/i", "", $seq);
 if ((count($fl) < 1) || (strlen($seq) == 0)) {
  print "Sequence is Empty!!";
  exit();
 } else {
  return array($sh, $seq);
 }
}

function fas_get($x) { // Read Multiple FASTA Sequences
 $gtr = substr($x, 1);
 $sqs = explode(">", $gtr);
 if (count($sqs) > 1) {
  foreach ($sqs as $sq) {
   $spair = get_seq($sq);
   $spairs[$spair[0]] = $spair[1];
  }
  return $spairs;
 } else {
  $spair = get_seq($gtr);
  return array($spair[0] => $spair[1]);
 }
}

$file = "pdt.fas";
$content = read_fas_file($file);
$fasta = fas_check($content);
$seq = fas_get($fasta);
foreach($seq as $x => $y) {
 print $x . "\n" . $y;
 print "\n\n";
}

FASTA File (pdt.fas)

>pdt|pdtdbt00001|pdb|1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NEB Bill Calculator (New)

Technical Questions