-
Notifications
You must be signed in to change notification settings - Fork 10
/
faget
executable file
·60 lines (54 loc) · 1.02 KB
/
faget
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/perl
# Select sequences from a FASTA file by identifier
# Copyright 2012 Shaun Jackman
use strict;
use Getopt::Std qw'getopts';
my %opt;
getopts 'f:p:v', \%opt;
my $opt_f = $opt{'f'};
my $opt_prefix = $opt{'p'};
my $opt_invert = $opt{'v'};
# Reverse complement.
sub rc($)
{
my $seq = $_[0];
($seq = reverse $seq) =~ tr/ACGTacgt/TGCAtgca/;
return $seq;
}
my %f;
sub insert($)
{
my $id = shift;
if ($id =~ /[+-]$/) {
my $sense = chop $id;
$f{$id} = $sense;
} else {
$f{$id} = 1;
}
}
if (defined $opt_f) {
open F, "<$opt_f" or die;
chomp, insert $_ while <F>;
close F;
} else {
insert $_ for split ',| |\|', shift;
}
while (<>) {
next if /^#/;
die unless s/^>//;
chomp;
my ($id, $comment) = split ' ', $_, 2;
chomp (my $seq = <>);
if ($opt_invert) {
print ">$_\n$seq\n" unless $f{$id};
} else {
my $sense = $f{$id};
$seq = rc $seq if $sense eq '-';
next if !$f{$id};
if ($sense =~ /[+-]/) {
print ">$opt_prefix$id$sense $comment\n$seq\n";
} else {
print ">$opt_prefix$_\n$seq\n";
}
}
}