Connect with us

Algorithms

How to read fasta sequences from a file using PHP?

Avatar

Published

on

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.

Advertisement
Click to comment

You must be logged in to post a comment Login

Leave a Reply

Algorithms

VS-Analysis: A Python Script to Analyze Virtual Screening Results of Autodock Vina

Dr. Muniba Faiza

Published

on

VS-Analysis: A Python Script to Analyze Virtual Screening Results of Autodock Vina

The output files obtained as a result of virtual screening (VS) using Autodock Vina may be large in number. It is difficult or quite impossible to analyze them manually. Therefore, we are providing a Python script to fetch top results (i.e., compounds showing low binding affinities). (more…)

Continue Reading

Algorithms

How to search motif pattern in FASTA sequences using Perl hash?

Dr. Muniba Faiza

Published

on

Here is a simple Perl script to search for motif patterns in a large FASTA file with multiple sequences.

(more…)

Continue Reading

Algorithms

How to read fasta sequences as hash using perl?

Avatar

Published

on

This is a simple Perl script to read a multifasta file as a hash. (more…)

Continue Reading

LATEST ISSUE

ADVERT

Feedback
Feedback
How Would You Rate the Design of Bioinformatics Review Website?
Do you have any additional comment?
Next
Enter your email if you'd like us to contact you regarding with your feedback.
Back
Submit
Thank you for submitting your feedback!