-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_primers_2013.pl
executable file
·143 lines (129 loc) · 4.71 KB
/
check_primers_2013.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/perl -w
use strict;
my $file = shift;
my $db = shift;
my $uniq_only = defined $ARGV[2] ? $ARGV[2] : 0 ;
my %primers;
if (!defined $file){
die "please provide a fasta file of primers in the following format:
primersetName PRIMER_foward PRIMER_rev
primersetName PRIMER_foward PRIMER_rev
primersetName PRIMER_foward PRIMER_rev
";
}
if (!defined $db){
die "please provide a fasta to use as a db
";
}
open IN , $file or die "Can't open $file $!";
open OUT , ">$file.fa" or die "Can't open $file.fa for writting $!";
while (my $line = <IN>){
my ($id , $primer1,$primer2 ) = split /\s+/ , $line;
$primers{$id}{p1}{seq}=$primer1;
$primers{$id}{p2}{seq}=$primer2;
$primers{$id}{p1}{len}=length ($primer1);
$primers{$id}{p2}{len}=length ($primer2);
my $seq_for_blast = $primer1 . "NNNNNNNNNN" . $primer2;
$primers{$id}{found}=0;
print OUT ">$id\n$seq_for_blast\n";
}
`blastall -F F -p blastn -i $file.fa -d $db -o $file.blastout -m8` if !-e "$file.blastout";
my $count;
open BLASTOUT, "$file.blastout" or die "Can't open $file.blastout";
#1 Chr11 100.00 21 0 0 45 65 21964649 21964629 0.004 42.1
#1 Chr11 100.00 16 0 0 1 16 21963880 21963895 3.6 32.2
while ( my $line = <BLASTOUT> ) {
$count++;
chomp $line;
my (
$primer_pair, $subject, $perId, $alignLen,
$mm, $gapOpenings, $qStart, $qEnd,
$sStart, $sEnd, $e, $score
) = split /\t/, $line;
my $p1_len = $primers{$primer_pair}{p1}{len};
my $p2_len = $primers{$primer_pair}{p2}{len};
next if (($alignLen != $p1_len) and ($alignLen != $p2_len));
next if ($perId != 100);
next if ($mm > 0);
next if ($gapOpenings > 0);
my $strand = ($qEnd - $qStart > 0) ? 1 : -1 ;
my $p;
if ($qStart < $p1_len) {
## this is p1 aligned
next if $alignLen < $p1_len;
$p = 'p1';
}else{
## ths is p2 aligned
next if $alignLen < $p2_len;
$p = 'p2';
}
## mayabe i can use strand and p1 p2 identity to simplify proper primer finding
$primers{$primer_pair}{blast}{$count}{$subject}{strand} = $strand;
$primers{$primer_pair}{blast}{$count}{$subject}{alnLen} = $alignLen;
$primers{$primer_pair}{blast}{$count}{$subject}{perID} = $perId;
$primers{$primer_pair}{blast}{$count}{$subject}{qStart} = $qStart;
$primers{$primer_pair}{blast}{$count}{$subject}{qEnd} = $qEnd;
$primers{$primer_pair}{blast}{$count}{$subject}{sStart} = $sStart;
$primers{$primer_pair}{blast}{$count}{$subject}{sEnd} = $sEnd;
$primers{$primer_pair}{blast}{$count}{$subject}{e} = $e;
$primers{$primer_pair}{blast}{$count}{$subject}{line} = $line;
$primers{$primer_pair}{subject}{$subject}=1;
$primers{$primer_pair}{found}=1;
}
my %warn;
print "primer_set\tproductSize\thit:start..end\tin_range\tmaps\tprimer1\tprimer2\n";
my %matches;
foreach my $primer_set ( keys %primers ) {
#my @subjects = keys %{$primers{$primer_set}{subject}};
#next if @subjects > 1;
my %hits;
my @values;
my @lines;
foreach my $count (keys %{$primers{$primer_set}{blast}}){
foreach my $sub (keys %{$primers{$primer_set}{blast}{$count}}){
push @{ $hits{$sub}} , $primers{$primer_set}{blast}{$count}{$sub}{sStart} , $primers{$primer_set}{blast}{$count}{$sub}{sEnd};
}
}
my ($loc_chr,$loc_start,$loc_end) = (0,0,0);
if ($primer_set =~ /\b(\w+):(\d+)..(\d+)/){
($loc_chr,$loc_start,$loc_end) = ($1, $2, $3);
}
foreach my $sub (keys %hits){
my @sorted_values = sort {$a <=> $b} @{$hits{$sub}};
my $maps;
if (@sorted_values < 4){
#2 starts, 2 ends ## just needs to map completely
# ========== ===========
# S E S E
$maps = "both primers do not map";
}elsif (@sorted_values == 4){
#2 starts, 2 ends ## exactly maps
$maps = "both primers map uniquely";
}elsif (@sorted_values > 4){
#more than only 2 starts and 2 ends
$maps = "primers do not map uniquely";
}
if ($uniq_only){
next if @sorted_values != 4; #exactly 2 starts, 2 ends ## needs to map uniquely
}
my $smallest = shift @sorted_values;
my $largest = pop @sorted_values;
my $p1 = uc $primers{$primer_set}{p1}{seq};
my $p2 = uc $primers{$primer_set}{p2}{seq};
my $product_size = $largest-$smallest+1;
my $in_range = 'N/A';
if ($sub eq $loc_chr and $loc_start > $smallest and $loc_end < $largest){
$in_range = 'yes';
}else{
$in_range = 'no';
}
print "$primer_set\t$product_size\t$sub:$smallest..$largest\t$in_range\t$maps\t$p1\t$p2\n";# if $product_size < 10000 and $product_size > 100;
#print "$primer_set\t",$largest-$smallest+1,"\t$subjects[0]:$smallest..$largest\t$p1\t$p2\n";
}
}
foreach my $id (keys %primers){
my $found = $primers{$id}{found};
if (!$found){
print "$id\tNo blast hit found\n";
}
}