-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnp4fasta.pl
78 lines (74 loc) · 1.97 KB
/
snp4fasta.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
#!/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);
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 ( $title, $pos, $origin, $snp ) = split( /\s+/, $_ );
if ( exists( $fasta{$title} ) ) {
my @seq = split( "", $fasta{$title} );
if ( $origin eq $seq[ $pos - 1 ] ) {
splice( @seq, $pos - 1, 1, $snp );
print(">$title-$pos-$origin-$snp\n");
print( join( "", @seq ) );
print("\n");
}
else {
warn("Sorry, $title-$pos-$origin doesn't match $seq[$pos - 1]\n"
);
}
}
else {
warn("Sorry, there is no such a segment: $_\n");
}
}
close($SEG);
}
elsif ( defined($stdin) ) {
while (<>) {
s/\r?\n//;
my ( $title, $pos, $origin, $snp ) = split( /\s+/, $_ );
if ( exists( $fasta{$title} ) ) {
my @seq = split( "", $fasta{$title} );
if ( $origin eq $seq[ $pos - 1 ] ) {
splice( @seq, $pos - 1, 1, $snp );
print(">$title-$pos-$origin-$snp\n");
print( join( "", @seq ) );
print("\n");
}
else {
warn("Sorry, $title-$pos-$origin doesn't match $seq[$pos - 1]\n"
);
}
}
else {
warn("Sorry, there is no such a segment: $_\n");
}
}
}
else {
die("==> You should provide the LIST!!\n");
}
__END__