-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathfastaselect.pl
executable file
·112 lines (77 loc) · 2.32 KB
/
fastaselect.pl
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/perl
# miRDeep2 FASTA-select perl script
# Copyright (C) 2008 - 2011 Marc Friedländer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
use warnings;
use strict;
use Getopt::Std;
my $usage =
"$0 file_fasta idfile (-a)
This script only prints out the fastaentries that has an id
that is present in the idfile. Using the option -a only prints
out entries that has an id that is not present in the id file.
";
my $file_fasta=shift or die $usage;
my $file_ids=shift or die $usage;
my %options=();
getopts("a",\%options);
my %hash;
parse_file_ids(\$file_ids,\%hash);
parse_fasta(\$file_fasta,\%hash);
exit;
sub parse_fasta{
my ($file,$hash) = @_;
my ($id, $desc, $sequence) = ();
open (FASTA, "<$$file") or die "can not open $$file\n";
while (<FASTA>)
{
chomp;
if (/^>(\S+)(.*)/)
{
$id = $1;
$desc = $2;
$sequence = "";
while (<FASTA>){
chomp;
if (/^>(\S+)(.*)/){
if((defined $$hash{$id} and not $options{a}) or (not defined $$hash{$id} and $options{a})){
print ">$id$desc\n$sequence\n";
}
$id = $1;
$desc = $2;
$sequence = "";
next;
}
$sequence .= $_;
}
}
}
if((defined $$hash{$id} and not $options{a}) or (not defined $$hash{$id} and $options{a})){
print ">$id$desc\n$sequence\n";
}
close FASTA;
return;
}
sub parse_file_ids{
my ($file,$hash) = @_;
open (FILE, "<$$file") or die "can not open $$file\n";
while (my $line=<FILE>){
if($line=~/^(\S+)/){
my $id=$1;
$$hash{$id}=1;
}
}
return;
}