| 1 | package App::Dazz::Command::merge; | ||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 11 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 11 | ▶ | use App::Dazz - command; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 11 | ▶ | use App::Dazz::Common; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 11 | ▶ | use constant abstract => "merge overlapped unitigs"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 4 | ▶ | sub opt_spec { | ||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 12 | return ( | ||||||||||
| 13 | 4 | [ "outfile|o=s", "output filename, [stdout] for screen", ], | |||||||||
| 14 | [ "len|l=i", "minimal length of overlaps", { default => 1000 }, ], | ||||||||||
| 15 | [ "idt|i=f", "minimal identity of overlaps", { default => 0.98 }, ], | ||||||||||
| 16 | [ "tmp=s", "user defined tempdir", ], | ||||||||||
| 17 | [ "parallel|p=i", "number of threads", { default => 8 }, ], | ||||||||||
| 18 | [ "verbose|v", "verbose mode", ], | ||||||||||
| 19 | [ "png", "write a png file via graphviz", ], | ||||||||||
| 20 | { show_defaults => 1, } | ||||||||||
| 21 | ); | ||||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 4 | ▶ | sub usage_desc { | ||||||||
|
Subroutine
usage_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 25 | 4 | return "dazz merge [options] <infile>"; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 1 | ▶ | sub description { | ||||||||
|
Subroutine
description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 29 | 1 | my $desc; | |||||||||
| 30 | 1 | $desc .= ucfirst(abstract) . ".\n"; | |||||||||
| 31 | 1 | $desc .= "\tAll operations are running in a tempdir and no intermediate files are kept.\n"; | |||||||||
| 32 | 1 | return $desc; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 3 | ▶ | sub validate_args { | ||||||||
|
Subroutine
validate_args: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||
| 36 | 3 | my ( $self, $opt, $args ) = @_; | |||||||||
| 37 | |||||||||||
| 38 | 3 | ▶ | if ( @{$args} != 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 1 | my $message = "This command need one input file.\n\tIt found"; | |||||||||
| 40 | 1 | $message .= sprintf " [%s]", $_ for @{$args}; | |||||||||
| 41 | 1 | $message .= ".\n"; | |||||||||
| 42 | 1 | $self->usage_error($message); | |||||||||
| 43 | } | ||||||||||
| 44 | 2 | for ( @{$args} ) { | |||||||||
| 45 | 2 | ▶ | if ( !Path::Tiny::path($_)->is_file ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 1 | $self->usage_error("The input file [$_] doesn't exist."); | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 1 | ▶ | if ( !exists $opt->{outfile} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . ".merge.fasta"; | |||||||||
| 52 | } | ||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 1 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: called CC=26 SCAR=60.8
Pod
documented
| |||||||||||
| 56 | 1 | my ( $self, $opt, $args ) = @_; | |||||||||
| 57 | |||||||||||
| 58 | # absolute pathes as we will chdir to tempdir later | ||||||||||
| 59 | 1 | my $infile = Path::Tiny::path( $args->[0] )->absolute->stringify; | |||||||||
| 60 | |||||||||||
| 61 | 1 | ▶ | if ( lc $opt->{outfile} ne "stdout" ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 0 | $opt->{outfile} = Path::Tiny::path( $opt->{outfile} )->absolute->stringify; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | # record cwd, we'll return there | ||||||||||
| 66 | 1 | my $cwd = Path::Tiny->cwd; | |||||||||
| 67 | 1 | my $tempdir; | |||||||||
| 68 | 1 | ▶ | if ( $opt->{tmp} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | $tempdir = Path::Tiny->tempdir( | ||||||||||
| 70 | TEMPLATE => "dazz_merge_XXXXXXXX", | ||||||||||
| 71 | DIR => $opt->{tmp}, | ||||||||||
| 72 | 0 | ); | |||||||||
| 73 | } | ||||||||||
| 74 | else { | ||||||||||
| 75 | 1 | $tempdir = Path::Tiny->tempdir("dazz_merge_XXXXXXXX"); | |||||||||
| 76 | } | ||||||||||
| 77 | 1 | chdir $tempdir; | |||||||||
| 78 | |||||||||||
| 79 | { # overlaps | ||||||||||
| 80 | 1 | my $cmd; | |||||||||
| 81 | 1 | $cmd .= "dazz overlap"; | |||||||||
| 82 | 1 | $cmd .= " --len $opt->{len} --idt $opt->{idt} --parallel $opt->{parallel}"; | |||||||||
| 83 | 1 | $cmd .= " $infile"; | |||||||||
| 84 | 1 | $cmd .= " -o merge.ovlp.tsv"; | |||||||||
| 85 | 1 | App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } ); | |||||||||
| 86 | |||||||||||
| 87 | 1 | ▶ | if ( !$tempdir->child("merge.ovlp.tsv")->is_file ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 1 | Carp::croak "Failed: create merge.ovlp.tsv\n"; | |||||||||
| 89 | } | ||||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 0 | my $graph = Graph->new( directed => 1 ); | |||||||||
| 93 | { # build graph | ||||||||||
| 94 | 0 | my %seen_pair; | |||||||||
| 95 | 0 | for my $line ( $tempdir->child("merge.ovlp.tsv")->lines( { chomp => 1, } ) ) { | |||||||||
| 96 | 0 | my $info = App::Dazz::Common::parse_ovlp_line($line); | |||||||||
| 97 | |||||||||||
| 98 | # ignore self overlapping | ||||||||||
| 99 | 0 | ▶ | next if $info->{f_id} eq $info->{g_id}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | |||||||||||
| 101 | # ignore poor overlaps | ||||||||||
| 102 | 0 | ▶ | next if $info->{ovlp_idt} < $opt->{idt}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | ▶ | next if $info->{ovlp_len} < $opt->{len}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | |||||||||||
| 105 | # we've orient overlapped sequences to the same strand | ||||||||||
| 106 | 0 | ▶ | next if $info->{g_strand} == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | |||||||||||
| 108 | # skip duplicated overlaps | ||||||||||
| 109 | 0 | my $pair = join( "-", sort ( $info->{f_id}, $info->{g_id} ) ); | |||||||||
| 110 | 0 | ▶ | next if $seen_pair{$pair}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | $seen_pair{$pair}++; | |||||||||
| 112 | |||||||||||
| 113 | # contained anchors have been removed | ||||||||||
| 114 | 0 | ▶ | if ( $info->{f_B} > 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | ▶ | if ( $info->{f_E} == $info->{f_len} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | |||||||||||
| 117 | # f.B f.E | ||||||||||
| 118 | # f ========+----------> | ||||||||||
| 119 | # g -----------+=======> | ||||||||||
| 120 | # g.B g.E | ||||||||||
| 121 | $graph->add_weighted_edge( $info->{f_id}, $info->{g_id}, | ||||||||||
| 122 | 0 | $info->{g_len} - $info->{g_E} ); | |||||||||
| 123 | } | ||||||||||
| 124 | } | ||||||||||
| 125 | else { | ||||||||||
| 126 | 0 | ▶ | if ( $info->{g_E} == $info->{g_len} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | |||||||||||
| 128 | # f.B f.E | ||||||||||
| 129 | # f -----------+=======> | ||||||||||
| 130 | # g ========+----------> | ||||||||||
| 131 | # g.B g.E | ||||||||||
| 132 | $graph->add_weighted_edge( $info->{g_id}, $info->{f_id}, | ||||||||||
| 133 | 0 | $info->{f_len} - $info->{f_E} ); | |||||||||
| 134 | } | ||||||||||
| 135 | } | ||||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | # Remove cyclic nodes | ||||||||||
| 139 | 0 | ▶ | while ( $graph->has_a_cycle ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | 0 | my @cycles = $graph->find_a_cycle; | |||||||||
| 141 | |||||||||||
| 142 | 0 | for my $v (@cycles) { | |||||||||
| 143 | 0 | for my $s ( $graph->successors($v) ) { | |||||||||
| 144 | 0 | ▶ | $graph->delete_edge( $v, $s ) if $graph->has_edge( $v, $s ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 0 | for my $p ( $graph->predecessors($v) ) { | |||||||||
| 148 | 0 | ▶ | $graph->delete_edge( $p, $v ) if $graph->has_edge( $p, $v ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | } | ||||||||||
| 150 | 0 | $graph->delete_vertex($v); | |||||||||
| 151 | } | ||||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | # Branching nodes will stay | ||||||||||
| 155 | # | ||||||||||
| 156 | # Remove branching nodes | ||||||||||
| 157 | # for my $v ( $graph->vertices ) { | ||||||||||
| 158 | # if ( $graph->out_degree($v) > 1 or $graph->in_degree($v) > 1 ) { | ||||||||||
| 159 | # $graph->delete_vertex($v); | ||||||||||
| 160 | # } | ||||||||||
| 161 | # } | ||||||||||
| 162 | |||||||||||
| 163 | 0 | $tempdir->child("overlapped.txt")->spew( map {"$_\n"} $graph->vertices ); | |||||||||
| 164 | |||||||||||
| 165 | 0 | ▶ | if ( $opt->{png} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | 0 | App::Dazz::Common::g2gv( $graph, $infile . ".png" ); | |||||||||
| 167 | } | ||||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | { # Output non-overlapped | ||||||||||
| 171 | 0 | my $cmd; | |||||||||
| 172 | 0 | $cmd .= "faops some -i -l 0 $infile overlapped.txt non-overlapped.fasta"; | |||||||||
| 173 | 0 | App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } ); | |||||||||
| 174 | } | ||||||||||
| 175 | |||||||||||
| 176 | #----------------------------# | ||||||||||
| 177 | # loading sequences | ||||||||||
| 178 | #----------------------------# | ||||||||||
| 179 | 0 | my $seq_of = App::Fasops::Common::read_fasta($infile); | |||||||||
| 180 | |||||||||||
| 181 | #----------------------------# | ||||||||||
| 182 | # merge | ||||||||||
| 183 | #----------------------------# | ||||||||||
| 184 | { | ||||||||||
| 185 | 0 | my @topo_sorted = $graph->topological_sort; | |||||||||
| 186 | |||||||||||
| 187 | 0 | my %merge_of; | |||||||||
| 188 | 0 | my $count = 1; | |||||||||
| 189 | 0 | for my $wcc ( $graph->weakly_connected_components ) { | |||||||||
| 190 | 0 | my @pieces = @{$wcc}; | |||||||||
| 191 | 0 | my $copy = scalar @pieces; | |||||||||
| 192 | 0 | ▶ | next if $copy == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 193 | |||||||||||
| 194 | 0 | my %idx_of; | |||||||||
| 195 | 0 | for my $p (@pieces) { | |||||||||
| 196 | 0 | ▶ | $idx_of{$p} = App::Fasops::Common::firstidx { $_ eq $p } @topo_sorted; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | 0 | @pieces = map { $_->[0] } | |||||||||
| 200 | 0 | sort { $a->[1] <=> $b->[1] } | |||||||||
| 201 | 0 | map { [ $_, $idx_of{$_} ] } @pieces; | |||||||||
| 202 | |||||||||||
| 203 | 0 | my $flag_start = 1; | |||||||||
| 204 | 0 | for my $i ( 0 .. $#pieces - 1 ) { | |||||||||
| 205 | 0 | my $anchor_0 = $pieces[$i]; | |||||||||
| 206 | 0 | my $anchor_1 = $pieces[ $i + 1 ]; | |||||||||
| 207 | |||||||||||
| 208 | 0 | ▶ | if ($flag_start) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 209 | 0 | $merge_of{$count} = $seq_of->{$anchor_0}; | |||||||||
| 210 | 0 | $flag_start = 0; | |||||||||
| 211 | } | ||||||||||
| 212 | |||||||||||
| 213 | 0 | ▶ | if ( $graph->has_edge( $anchor_0, $anchor_1 ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 214 | 0 | my $weight = $graph->get_edge_weight( $anchor_0, $anchor_1 ); | |||||||||
| 215 | 0 | $merge_of{$count} .= substr $seq_of->{$anchor_1}, -$weight; | |||||||||
| 216 | } | ||||||||||
| 217 | else { | ||||||||||
| 218 | 0 | $count++; | |||||||||
| 219 | 0 | $merge_of{$count} = $seq_of->{$anchor_1}; | |||||||||
| 220 | } | ||||||||||
| 221 | } | ||||||||||
| 222 | |||||||||||
| 223 | 0 | $count++; | |||||||||
| 224 | } | ||||||||||
| 225 | |||||||||||
| 226 | 0 | my @contents = map { ( ">merge_$_", $merge_of{$_}, ) } sort { $a <=> $b } keys %merge_of; | |||||||||
| 227 | 0 | $tempdir->child("merged.fasta")->spew( map {"$_\n"} @contents ); | |||||||||
| 228 | } | ||||||||||
| 229 | |||||||||||
| 230 | { # Outputs. stdout is handeld by faops | ||||||||||
| 231 | 0 | my $cmd; | |||||||||
| 232 | 0 | $cmd .= "cat non-overlapped.fasta merged.fasta"; | |||||||||
| 233 | 0 | $cmd .= " | faops filter -l 0 stdin"; | |||||||||
| 234 | 0 | $cmd .= " $opt->{outfile}"; | |||||||||
| 235 | 0 | App::Dazz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } ); | |||||||||
| 236 | } | ||||||||||
| 237 | |||||||||||
| 238 | 0 | chdir $cwd; | |||||||||
| 239 | } | ||||||||||
| 240 | |||||||||||
| 241 | 11 | 1; | |||||||||