-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgenerate_dennis_alignments.pl
executable file
·103 lines (86 loc) · 2.45 KB
/
generate_dennis_alignments.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
#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use English;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;
use File::Basename;
$OUTPUT_AUTOFLUSH = 1;
my %args;
$args{evalue} = 1e-50;
my $options_okay =
GetOptions( 'assembly=s' => \$args{assembly}, 'evalue=s' => \$args{evalue} );
croak "No assembly file! Please specify -a $OS_ERROR\n"
if ( $args{assembly} eq "" );
my $blastfile = basename( $args{assembly} );
$blastfile = "dennis_refs." . $blastfile;
$blastfile =~ s/\.fa$/\.blastn\.out/;
open my $blast, '<', $blastfile
or croak
"Can't open BLAST output $blastfile in current directory! $OS_ERROR\n";
my %contig;
my %hit;
while ( my $hit = <$blast> ) {
chomp $hit;
my @f = split /\t/, $hit;
next if $f[10] > $args{evalue};
$contig{ $f[1] }++;
$hit{ $f[0] }{ $f[1] }{qstart} = $f[6];
$hit{ $f[0] }{ $f[1] }{qend} = $f[7];
$hit{ $f[0] }{ $f[1] }{sstart} = $f[8];
$hit{ $f[0] }{ $f[1] }{send} = $f[9];
}
close $blast;
open my $assembly, '<', $args{assembly}
or croak "Can't open assembly file $args{assembly}! $OS_ERROR\n";
my $contig = 0;
my %conseq;
while ( my $fasta = <$assembly> ) {
if ( $fasta =~ /^>(.+)$/ ) {
$contig = defined $contig{$1} ? $1 : "";
}
else {
if ($contig) {
chomp $fasta;
$conseq{$contig} .= $fasta;
}
}
}
close $assembly;
my $scaffoldfile = $blastfile;
$scaffoldfile =~ s/\.blastn\.out$/.scaffold.fa/;
open my $scaffold, '>', $scaffoldfile or croak "Can't open scaffold file $scaffoldfile! $OS_ERROR\n";
while ( my $ref = each %hit ) {
my $header = "$ref";
my $ref_assembly = "";
my $gapstart = 0;
foreach my $contig (
sort { $hit{$ref}{$a}{qstart} <=> $hit{$ref}{$b}{qstart} }
keys %{ $hit{$ref} }
)
{
my $seq = $conseq{$contig};
$header .= " $contig";
if ( $hit{$ref}{$contig}{sstart} > $hit{$ref}{$contig}{send} ) {
$seq = revcomp($seq);
$header .= "-RC";
}
$ref_assembly .= $seq;
if ( $gapstart != 0 ) {
my $gapsize = $hit{$ref}{$contig}{qend} - $gapstart + 1;
$gapsize = 1 if ( $gapsize < 1 );
$ref_assembly .= 'N' x $gapsize;
}
$gapstart = $hit{$ref}{$contig}{qend};
}
print $scaffold ">$header\n$ref_assembly\n";
}
close $scaffold;
sub revcomp {
my $dna = shift;
my $rc = reverse $dna;
$rc =~ tr/ACGTacgt/TGCAtgca/;
return $rc;
}