-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractFASTA_fromBED.pl
69 lines (58 loc) · 1.81 KB
/
extractFASTA_fromBED.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
#!/usr/bin/perl
#Extract sequences from stitchPositions BED file with sequences and output FASTA
#Usage: perl extractFASTA_fromBED.pl --bed <input BED> [--output <output FASTA filename>]
use strict;
use warnings;
use Cwd; use Cwd qw(abs_path);
use File::Basename;
use Getopt::Long;
my %inputs = ();
my $cwd;
my $basename;
my $outputfile;
GetOptions( \%inputs, 'bed=s', 'output=s');
if ( (!exists $inputs{bed}) ) {
print "Usage: perl extractFASTA_fromBED.pl --bed <input BED> [--output <output FASTA>]\n";
exit 0;
}
else {
$cwd = abs_path($inputs{bed});
$cwd = dirname($cwd);
$basename = basename($inputs{bed});
$basename =~ s/\.[^.]+$//;
}
if( defined $inputs{output} and !-e $inputs{output}) {
$outputfile = abs_path($inputs{output});
}
elsif ( defined $inputs{output} and -e $inputs{output}) {
print "Output file $inputs{output} already exists! Using default output filename...\n";
$outputfile = $cwd."/".$basename.".fasta";
}
else {
$outputfile = $cwd."/".$basename.".fasta"; #if output file not given use $basename as prefix
}
if( -e $outputfile ) {
die "ERROR: Output file ", $outputfile, " already exists. Please specify alternate filename. $!\n";
}
foreach my $key ("bed") {
if (exists $inputs{$key}) { $inputs{$key} = abs_path($inputs{$key}); }
if ( ! -e $inputs{$key} ) { die "Input file $inputs{$key} does not exists: $!\n"; }
}
print "\n";
print "\tInput BED file: $inputs{bed}\n";
print "\tOutput FASTA file: $outputfile\n";
print "\n";
open OUT, ">$outputfile" or die "ERROR: Could not open output file $outputfile: $!\n";
open BED, "<$inputs{bed}" or die "ERROR: Could not open input BED: $!\n";
while (<BED>) {
my $line = $_;
chomp($line);
if ($line =~ /^#/) {
next;
}
else {
my @s = split("\t",$line);
my $fastaHeader = ">$s[0]_$s[1]_$s[2]_$s[3]";
print OUT "$fastaHeader\n$s[9]\n";
}
}