Connect with us

Algorithms

How to read fasta sequences from a file using PHP?

Tariq Abdullah

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 CEO 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

MOCCA- A New Suite to Model cis- regulatory Elements for Motif Occurrence Combinatorics

Tariq Abdullah

Published

on

MOCCA- A New Suite to Model cis- regulatory Elements for Motif Occurrence Combinatorics

cis-regulatory elements are DNA sequence segments that regulate gene expression. cis-regulatory elements consist of some regions such as promoters, enhancers, and so on. These regions consist of specific sequence motifs. (more…)

Continue Reading

Algorithms

vs_Analysis.py: 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

LATEST ISSUE

ADVERT