-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelineate_consensus.pl
129 lines (115 loc) · 3.3 KB
/
delineate_consensus.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use strict;
use warnings;
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
package IdSequence;
sub new {
my ($class, $id, $sequence) = @_;
my $self = {
_id => $id,
_sequence => $sequence,
};
return bless($self, $class);
};
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
package main;
my $path = shift @ARGV;
my $opt = shift @ARGV;
my $file = $path =~ s/.*\///r;
my ($file_name, $file_extension) = $file =~ /^(.+)\.([^.]+)$/;
open my $input, "<:utf8", $path or die;
my (@IdSequence_array, $id);
while (<$input>) {
chomp;
if (m/\A>/) {
($id) = m/\A>(.+)/;
} else {
my $a_IdSequence = IdSequence->new($id, uc $_);
push @IdSequence_array, $a_IdSequence;
};
last if eof $input;
};
close $input;
sub na_delineate {
my $seq = $_[0];
my %hash = (
"R" => ["A","G"],
"Y" => ["C","T"],
"S" => ["G","C"],
"W" => ["A","T"],
"K" => ["G","T"],
"M" => ["A","C"],
"B" => ["C","G","T"],
"D" => ["A","G","T"],
"H" => ["A","C","T"],
"V" => ["A","C","G"],
"N" => ["A","T","G","C"],
);
my $sols = 1;
$sols *= $_ for my @entrs = map { 0 + @{$hash{$_}} } my @keys = sort keys %hash;
my @pairs = map {
my $i = $_;
[ map {
[$keys[$_], $hash{$keys[$_]}[$i % $entrs[$_]]],
($i = int($i / $entrs[$_])) x 0
} 0 .. $#entrs];
} 0 .. $sols - 1;
my @res;
for my $pair (@pairs) {
my $temp = $seq;
for my $repl (@{$pair}) {
$temp =~ s/@{$repl}[0]/@{$repl}[1]/g;
};
push @res, $temp;
};
return uniq(@res);
};
sub aa_delineate {
my $seq = $_[0];
# my @subs = $seq =~ /(?<=\[)\b\w+\b(?=[\]])/g; # match content inside the square brackets
# my @not_subs = $seq =~ /(?<!\[)\b\w+\b(?![\]])/g; # match content outside the square brackets
my @subs = $seq =~ /\[[^]]*\]/g;
my %hash;
for my $sub (@subs) {
my @chars = split("", $sub =~ s/[][]//gr);
$hash{$sub} = [@chars] unless $hash{$sub};
};
my $sols = 1;
$sols *= $_ for my @entrs = map { 0 + @{$hash{$_}} } my @keys = sort keys %hash;
my @pairs = map {
my $i = $_;
[ map {
[$keys[$_], $hash{$keys[$_]}[$i % $entrs[$_]]],
($i = int($i / $entrs[$_])) x 0
} 0 .. $#entrs];
} 0 .. $sols - 1;
my @res;
for my $pair (@pairs) {
my $temp = $seq;
for my $repl (@{$pair}) {
@{$repl}[0] =~ s/\[/\\\[/g;
@{$repl}[0] =~ s/\]/\\\]/g;
$temp =~ s/@{$repl}[0]/@{$repl}[1]/g;
};
push @res, $temp;
};
return uniq(@res);
};
open my $output, ">:utf8", "delineated_${file_name}.${file_extension}" or die;
for my $a_IdSequence (@IdSequence_array) {
my @delin;
if ($opt eq "na") {
@delin = na_delineate($a_IdSequence->{_sequence});
} elsif ($opt eq "aa") {
@delin = aa_delineate($a_IdSequence->{_sequence});
};
for (my $i = 0; $i < scalar @delin; $i++) {
print $output ">" . $a_IdSequence->{_id} . "_" . ($i + 1) . "\n";
print $output $delin[$i] . "\n";
};
};
sub uniq {
my %seen;
grep !$seen{$_}++, @_;
# grep !$seen{$_->[0]}{$_->[1]}++, @_;
# grep !$seen{"$_->[0],$_->[1]"}++, @_;
};