How to read fasta sequences from a file using PHP?

//

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

HOW TO CITE THIS ARTICLE Tariq Abdullah (2019). How to read fasta sequences from a file using PHP?. Bioinformatics Review, 5 (05)
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

Willing to stay updated?

By investing less than 30 seconds you can start recieving all our new articles in your mailbox. Stay updated with latest Bioinformatics Research, trends and tools of trade.

 

Lost your password? Please enter your email address. You will receive mail with link to set new password.

0 $0.00