-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpick_seq_from_fasta_neo.pl
96 lines (89 loc) · 2.29 KB
/
pick_seq_from_fasta_neo.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
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use Getopt::Long;
Getopt::Long::GetOptions(
'help|h' => sub { Getopt::Long::HelpMessage(0) },
'in|i=s' => \my $in_list,
'fa|f=s' => \my $in_fa,
'stdin' => \my $stdin,
) or Getopt::Long::HelpMessage(1);
sub SEQ_REV_COMP {
my $SEQ = reverse shift;
$SEQ =~ tr/Uu/Tt/;
return ( $SEQ =~ tr/AGCTagct/TCGAtcga/r );
}
sub SEQ_TR_TU {
my $SEQ = shift;
return ( $SEQ =~ tr/Uu/Tt/r );
}
my %fasta;
my $title_name;
open my $FA, "<", $in_fa;
while (<$FA>) {
if (/^>(\S+)/) {
$title_name = $1;
}
else {
$_ =~ s/\r?\n//;
$fasta{$title_name} .= $_;
}
}
close($FA);
if ( defined($in_list) ) {
open( my $SEG, "<", $in_list );
while (<$SEG>) {
s/\r?\n//;
my ( $chr, $start, $end, $dir, $name ) = split( /\s+/, $_ );
if ( exists( $fasta{$chr} ) ) {
my $length = abs( $end - $start ) + 1;
my $seq = substr( $fasta{$chr}, $start - 1, $length );
if ( $dir eq "-" ) {
$seq = SEQ_REV_COMP($seq);
}
else {
$seq = SEQ_TR_TU($seq);
}
if ( defined($name) ) {
print ">$chr:$start-$end($dir)$name\n$seq\n";
}
else {
print ">$chr:$start-$end($dir)\n$seq\n";
}
}
else {
warn("Sorry, there is no such a segment: $_\n");
}
}
close($SEG);
}
elsif ( defined($stdin) ) {
while (<>) {
s/\r?\n//;
my ( $chr, $start, $end, $dir, $name ) = split( /\s+/, $_ );
if ( exists( $fasta{$chr} ) ) {
my $length = abs( $end - $start ) + 1;
my $seq = substr( $fasta{$chr}, $start - 1, $length );
if ( $dir eq "-" ) {
$seq = SEQ_REV_COMP($seq);
}
else {
$seq = SEQ_TR_TU($seq);
}
if ( defined($name) ) {
print ">$chr:$start-$end($dir)$name\n$seq\n";
}
else {
print ">$chr:$start-$end($dir)\n$seq\n";
}
}
else {
warn("Sorry, there is no such a segment: $_\n");
}
}
}
else {
die("==> You should provide the FASTA!!\n");
}
__END__