Last active
February 11, 2016 11:59
-
-
Save sestaton/889cba88b5279a58d997 to your computer and use it in GitHub Desktop.
Fetch FASTA records by sequence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use File::Basename; | |
my $usage = "perl ".basename($0)." seqsi.fas seqsj.fas > seqs_out.fas"; | |
my $infilei = shift or die $usage; | |
my $infilej = shift or die $usage; | |
my %hash; | |
open my $ini, '<', $infilei or die $!; | |
while (my ($id, $seq) = fasta_it(\*$ini)) { | |
$hash{$seq} = $id; | |
} | |
close $ini; | |
open my $inj, '<', $infilej or die $!; | |
while (my ($id, $seq) = fasta_it(\*$inj)) { | |
if (exists $hash{$seq}) { | |
print join "\n", ">".$hash{$seq}, "$seq\n"; | |
} | |
} | |
close $inj; | |
sub fasta_it { | |
my ($fh) = @_; | |
local $/ = "\n>"; | |
return unless my $entry = $fh->getline; | |
chomp $entry; | |
my ($id, $seq) = split /\n/, $entry, 2; | |
defined $id && $id =~ s/>//g; | |
return ($id, $seq); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment