-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfind_unique_blocks.pl
executable file
·155 lines (130 loc) · 4.1 KB
/
find_unique_blocks.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
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use English;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;
use Term::ExtendedColor qw/:all/;
use Number::Format qw/:subs :vars/;
use List::Util qw/min max/;
$OUTPUT_AUTOFLUSH = 1;
my %args;
$args{markerfile} = "";
$args{lengthfile} = "";
$args{geneticsfile} = "";
my $options_okay = GetOptions(
'markers=s' => \$args{markerfile},
'lengths=s' => \$args{lengthfile},
'genetics=s' => \$args{geneticsfile},
);
croak "No marker file! Please specify -m $OS_ERROR\n"
if ( $args{markerfile} eq "" );
croak "No lengths file! Please specify -l $OS_ERROR\n"
if ( $args{lengthfile} eq "" );
croak "No genetics file! Please specify -g $OS_ERROR\n"
if ( $args{geneticsfile} eq "" );
open my $lengthfile, '<', $args{lengthfile}
or croak "Can't open $args{lengthfile}! $OS_ERROR\n";
my %scflen;
my $genomelen = 0;
while ( my $lengthline = <$lengthfile> ) {
chomp $lengthline;
my ( $scf, $len ) = split /\t/, $lengthline;
$scflen{$scf} = $len;
$genomelen += $len;
}
close $lengthfile;
my $scfnum = keys %scflen;
print "Genome has $scfnum scaffolds and is $genomelen bp long\n";
my %genetics;
open my $geneticsfile, '<', $args{geneticsfile}
or croak "Can't open $args{geneticsfile}! $OS_ERROR\n";
my $infoline;
while ( my $infoline = <$geneticsfile> ) {
last if ( $infoline =~ /Type/ );
}
while ( my $marker_type = <$geneticsfile> ) {
chomp $marker_type;
my ( $parents, $males, $females, $type, $corrections ) = split /\t/,
$marker_type;
# next if $type !~ 'Maternal';
$genetics{$type}{$parents}{males} = $males;
$genetics{$type}{$parents}{females} = $females;
}
close $geneticsfile;
open my $markerfile, '<', $args{markerfile}
or croak "Can't open $args{markerfile}! $OS_ERROR\n";
my %scflines;
my $prev_scf;
my %scf;
my %marker;
my $scf_count = 0;
my $marker_count = 0;
while ( my $marker = <$markerfile> ) {
next
if ( $marker =~ /^(-+)$/ );
chomp $marker;
my (
$scf, $pos, $type, $parents, $mq,
$fs, $p, $rmsobs, $rmspat, $orig,
$edge, $cons, $corrected
)
= split /\t/,
uncolor($marker);
next if $type eq 'Reject';
# next if $type !~ 'Maternal';
$marker_count++;
$prev_scf = $scf if !defined $prev_scf;
if ( $scf ne $prev_scf ) {
$scf_count++;
print STDERR "." if ( $scf_count % 10 == 0 );
print STDERR "$scf_count\n" if ( $scf_count % 100 == 0 );
process_scf( $prev_scf, \%scflines, \%genetics );
%scflines = ();
}
$prev_scf = $scf;
$scflines{$pos}{type} = $type;
$scflines{$pos}{pattern} = $corrected;
}
process_scf( $prev_scf, \%scflines );
close $markerfile;
print "\n";
sub process_scf {
my ( $scf, $scflines, $genetics ) = @_;
printf "%8s\t%8s\t%8s\t%8s", 'Scaffold', 'Start', 'End', 'Length';
map { printf "\t%-69s", $_; } sort keys %genetics;
print "\n";
my %curpat;
for my $type ( keys %genetics ) {
$curpat{$type} = " " x 69;
}
my $prevpos = 1;
for my $pos ( sort { $a <=> $b } keys %{$scflines} ) {
my $type = $scflines->{$pos}{type};
if ( $curpat{$type} ne $scflines->{$pos}{pattern} ) {
printf "%8s\t%8d\t%8d\t%8d", $scf, $prevpos, $pos - 1,
$pos - $prevpos;
for my $printtype ( sort keys %curpat ) {
if ( $type eq $printtype ) {
print "\t";
my @cur = split //, $curpat{$printtype};
my @new = split //, $scflines->{$pos}{pattern};
for my $i ( 0 .. $#cur ) {
my $col = $cur[$i] eq $new[$i] ? 'black' : 'red1';
print fg $col, $cur[$i];
}
}
else {
print "\t$curpat{$printtype}";
}
}
print "\n";
$curpat{$type} = $scflines->{$pos}{pattern};
$prevpos = $pos;
}
}
printf "%8s\t%8d\t%8d\t%8d\n", $scf, $prevpos, $scflen{$scf},
$scflen{$scf} - $prevpos + 1;
}