How to read fasta sequences from a file using PHP?

//
1 min read

Here is a simple function in PHP to read fasta sequences from a file.

Your multifasta input file is “input.fasta”.

function read_fastas($filename){

  $fh = fopen($filename, 'r');
  $i= 0 ;
 
 $sequences = array();
  while($line = fgets($fh)){
    $i++;
    if($i%2==1){
      $sequence['header'] = $line;
    }
    else{
     $sequence['sequence'] = $line;
     array_push($sequences, $sequence);
    }

  }

 return $sequences;

}

The rest of the script would go like this:

<?php
$filename = "input.fasta";

//Define function
function read_fastas($filename){

  $fh = fopen($filename, 'r');
  if (filesize($x) == 0) { //check if file is empty or not
    echo "Input file is empty!";
  }
  else{
  $i= 0 ;
 
 $sequences = array();
  while($line = fgets($fh)){
    $i++;
    if($i%2==1){
      $sequence['header'] = $line;
    }
    else{
     $sequence['sequence'] = $line;
     array_push($sequences, $sequence);
    }

  }

 return $sequences;
}
}

//Call the function
read_fastas();
//do something with your fasta
?>

 

Tariq is founder of Bioinformatics Review and a professional Software Developer at IQL Technologies. His areas of expertise include algorithm design, phylogenetics, MicroArray, Plant Systematics, and genome data analysis. If you have questions, reach out to him via his homepage.

Leave a Reply

Previous Story

A workshop on Genome Editing Tools and Techniques

Next Story

"Congrats! it is a nice work!"

Latest from Algorithms

miRBase: Explained

Micro RNAs (miRNAs) are the short endogenous RNAs (~22 nucleotides) and originate from the non-coding RNAs

0 $0.00