Connect with us

Algorithms

How to read fasta sequences as hash using perl?

Tariq Abdullah

Published

on

This is a simple Perl script to read a multifasta file as a hash.

Suppose, your multifasta file is “input.fasta”, which you want to read as the hash.

#! /usr/bin/perl
use warnings;
use strict;
my $infile = "input.fasta";
my %sequences;

open( FH, '<', $infile ) or die $!;

while( my $line = <FH> ){
      chomp $line;
      if ( $line =~ /^(>.*)$/ )){
           my $id = $1;
      }
      elseif ( $line !~ /^\s*$/ ){
           $sequences{$id} .= $line;
      }
}

close (FH);
exit;

If you want to write a subroutine for reading a fasta file, then you can do like this:

#! /usr/bin/perl
use warnings;
use strict 'vars';

my $infile = "input.fasta";
my %seqs = %{ read_fasta_as_hash($infile) };#call the subroutine
          #your code goes here

sub read_fasta_as_hash{
  my $inputfile = shift;
  my $id = '';
  my %sequences;
  open( INFILE, '<', $inputfile ) or die $!;
        
  while( my $line = <INFILE> ){ 
        chomp $line;
        if ( $line =~ /^(>.*)$/ )){
            my $id = $1;
        }
        elseif ( $line !~ /^\s*$/ ){
             $sequences{$id} .= $line;
        }
   }
   close (INFILE);
   return %sequences;
}

exit;

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.

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