How to read fasta sequences from a file using PHP?

Tariq Abdullah
0 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; 
}
Share This Article
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.
Leave a Comment

Leave a Reply