Bioinformatics ReviewBioinformatics Review
Notification Show More
Font ResizerAa
  •  Home
  • Docking
  • MD Simulation
  • Tools
  • More Topics
    • Softwares
    • Sequence Analysis
    • Algorithms
    • Bioinformatics Programming
    • Bioinformatics Research Updates
    • Drug Discovery
    • Phylogenetics
    • Structural Bioinformatics
    • Editorials
    • Tips & Tricks
    • Bioinformatics News
    • Featured
    • Genomics
    • Bioinformatics Infographics
  • Community
    • BiR-Research Group
    • Community Q&A
    • Ask a question
    • Join Telegram Channel
    • Join Facebook Group
    • Join Reddit Group
    • Subscription Options
    • Become a Patron
    • Write for us
  • About Us
    • About BiR
    • BiR Scope
    • The Team
    • Guidelines for Research Collaboration
    • Feedback
    • Contact Us
    • Recent @ BiR
  • Subscription
  • Account
    • Visit Dashboard
    • Login
Font ResizerAa
Bioinformatics ReviewBioinformatics Review
Search
Have an existing account? Sign In
Follow US
Bioinformatics Programming

How to extract fasta sequences from a multi-fasta file based on matching headers in a separate file?

Tariq Abdullah
Last updated: May 20, 2020 5:48 pm
Tariq Abdullah
Share
1 Min Read
SHARE

This is a simple Perl script to extract FASTA sequences from a large fasta file depending on the matching fasta headers present in another file.

For example, your fasta sequences are present in a file named, “input.fa” and the headers are in another file called “headers.txt”.

#! /usr/bin/perl
use warnings;
use strict;
my $headerfile = 'headers.txt';
my $input = 'input.fa';
open( HEADERFILE, '<', $headerfile ) or die $!;
chomp ( my @headers = map { split } <$headerfile> );    #splitting lines on whitespaces.
close HEADERFILE;
my %seqs;
open( INPUTFILE, '<', $input ) or die $!;
{
local $/ = '';         #Reading until blank line
while ( <$input> ) {
     my ( $header, $sequence ) = m/>\s*(\S+)\n(.*)/ms;
     $sequences{$header} = $sequence;
}
open( my $seqsfile, ">", "input.fa" );
foreach my $header (@headers) {
             if ( $sequences{$header} ) {
                       print $header, "\n";
                       print $sequences{$header}, "\n";
             }
}

close( $seqsfile );
}

close INPUTFILE;
exit;
TAGGED:headeridsmultifastaPerlsequences
Share This Article
Facebook Copy Link Print
ByTariq Abdullah
Tariq is founder of Bioinformatics Review and Lead 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 Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

ai tools vs traditional tools in bioinformatics
AI Tools vs Traditional Tools in Bioinformatics- Which one to select?
Algorithms Artificial Intelligence Machine Learning Software Tools
AI vs Physics in Molecular Docking
AI vs Physics in Molecular Docking: Towards Faster and More Accurate Pose Prediction
Artificial Intelligence Drug Discovery Machine Learning
10 years of Bioinformatics Review: From a Blog to a Bioinformatics Knowledge Hub!
Editorial
Starting in Bioinformatics? Do This First!
Starting in Bioinformatics? Do This First!
Tips & Tricks

You Might Also Like

tanimoto_similarities_one_vs_all.py – Python script to calculate Tanimoto Similarities of a compound with multiple compounds
Bioinformatics ProgrammingPython

tanimoto_similarities_one_vs_all.py – Python script to calculate Tanimoto Similarities of multiple compounds

July 18, 2022
AlgorithmsBioinformatics ProgrammingSoftware

BETSY: A new backward-chaining expert system for automated development of pipelines in Bioinformatics

May 20, 2020
DockingAnalyzer.py-A Python script to identify ligand binding in protein pockets.
Bioinformatics ProgrammingBioinformatics Research UpdatesDockingDrug DiscoveryFeaturedGitHubPython

DockingAnalyzer.py: A New Python script to Identify Ligand Binding in Protein Pockets.

March 13, 2025
Bioinformatics ProgrammingTools

Perl one-liners for bioinformaticians

December 8, 2015
Copyright 2024 IQL Technologies
  • Journal
  • Customer Support
  • Contact Us
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Cookie Policy
  • Sitemap
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?

Not a member? Sign Up