Egaz/Command/fas2vcf.pm

filter: Statementstmt 46.7% 36 / 77 Branchbran 22.2% 4 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 100% 6 / 6 totaltotal 47.8% 56 / 117 CC 16 SCAR 40.5
CC
16
Cov
41%
execute
132.0
validate_args
6.0
BEGIN
1.0
CRAP 57.6
« Egaz/Command/repeatmasker.pm ↑ Summary Egaz/Command/blastlink.pm »
1 package App::Egaz::Command::fas2vcf;
2 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use App::Egaz -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use App::Egaz::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶sub abstract {
Subroutine abstract: called CC=1 SCAR=0.0
Pod documented
10 2 return 'list variations in blocked fasta file';
11 }
12  
13 4 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
14 return (
15 4 [ "outfile|o=s", "Output filename. [stdout] for screen", { default => "stdout" }, ],
16 [ "list=s", "a list of names to keep, one per line", ],
17 [ "verbose|v", "verbose mode", ],
18 { show_defaults => 1, }
19 );
20 }
21  
22 4 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
23 4 return "egaz fas2vcf [options] <infile> <chr.sizes>";
24 }
25  
26 1 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
27 1 my $desc;
28 1 $desc .= ucfirst(abstract) . ".\n";
29 1 $desc .= <<MARKDOWN;
30  
31 * infile == stdin means reading from STDIN
32 * Steps:
33 1. split .fas to a temp dir by `fasops split`
34 2. convert each fasta files to .vcf by `snp-sites`
35 3. concat every .vcf files by `bcftools`
36  
37 MARKDOWN
38  
39 1 return $desc;
40 }
41  
42 3 ▶sub validate_args {
Subroutine validate_args: called CC=6 SCAR=18.0
Pod documented
43 3 my ( $self, $opt, $args ) = @_;
44  
45 3 ▶ if ( @{$args} != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args;} != 2) 2 1 3
46 2 my $message = "This command need two input files.\n\tIt found";
47 2 $message .= sprintf " [%s]", $_ for @{$args};
48 2 $message .= ".\n";
49 2 $self->usage_error($message);
50 }
51 1 for ( @{$args} ) {
52 1 ▶ next if lc $_ eq "stdin";
Branch: 1 branch
BranchTrueFalseTotal
if lc $_ eq "stdin" 0 1 1
53 1 ▶ if ( !Path::Tiny::path($_)->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Path::Tiny::path($_)->is_file) 1 0 1
54 1 $self->usage_error("The input file [$_] doesn't exist.");
55 }
56 }
57  
58 }
59  
60 0 ▶sub execute {
Subroutine execute: not called CC=11 SCAR=48.8
Pod documented
61 0 my ( $self, $opt, $args ) = @_;
62  
63 0 my $tempdir = Path::Tiny->tempdir( TEMPLATE => "fas2vcf_XXXXXXXX", );
64 0 my $length_of = App::RL::Common::read_sizes( $args->[1] );
65  
66 { # fasops split
67 0 my $cmd = "";
68 0 ▶ if ( $opt->{list} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'list'}) { } 0 0 0
69 0 $cmd .= " fasops subset $args->[0] $opt->{list} --required -o stdout";
70 0 $cmd .= " |";
71 0 $cmd .= " fasops split stdin --simple -o $tempdir";
72 }
73 else {
74 0 $cmd .= "fasops split $args->[0] --simple -o $tempdir";
75 }
76 0 App::Egaz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
77 }
78  
79 { # snp-sites
80 0 my @files = $tempdir->children(qr/\.fas$/);
81 0 ▶ printf STDERR " Find %d .fas files\n", scalar @files if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 0 0 0
82  
83 0 for my Path::Tiny $f (@files) {
84 0 my ( $name, $chr_name, $chr_strand, $chr_pos ) = split /\./, $f->basename(".fas");
85 0 my ( $chr_start, $chr_end ) = split /\-/, $chr_pos;
86 0 my $chr_length = $length_of->{$chr_name};
87  
88 0 my $cmd = "snp-sites -v $f";
89 0 my @lines = split /\n/, `$cmd`;
90  
91 0 for my $l (@lines) {
92 0 ▶ if ( $l =~ /^\#\#contig\=\<ID\=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /^\#\#contig\=\<ID\=/) 0 0 0
93 0 $l = "##contig=<ID=$chr_name,length=$chr_length>";
94 }
95  
96 # jvarkit/biostar94573.jar uses chrUn; snp-sites uses 1
97 0 ▶ if ( $l =~ /^chrUn\t/ or $l =~ /^1\t/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /^chrUn\t/ or $l =~ /^1\t/) 0 0 0
Condition0%0/3
$l =~ /^chrUn\t/ or $l =~ /^1\t/
l!l&&r!l&&!r
000
MC/DC0%0/2
$l =~ /^chrUn\t/ or $l =~ /^1\t/
$l =~ /^chrUn\t/ $l =~ /^1\t/
Truth table0%0/3
A or B
A= $l =~ /^chrUn\t/B= $l =~ /^1\t/
ABresult
1X1
011
000
98 0 my @fields = split /\t/, $l;
99 0 $fields[0] = $chr_name;
100  
101 # vcf position is 1-based
102 0 $fields[1] = $chr_start + $fields[1] - 1;
103 0 $l = join "\t", @fields;
104 }
105 }
106  
107 0 ▶ if ( scalar grep { !/^#/ } @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({not /^#/;} @lines)) 0 0 0
108 0 Path::Tiny::path( $f . ".vcf" )->spew( map { $_ . "\n" } @lines );
109 }
110 }
111 }
112  
113 { # bcftools
114 0 my $temp_list = Path::Tiny->tempfile( TEMPLATE => "fas2vcf_XXXXXXXX", );
115 0 for my Path::Tiny $f ( $tempdir->children(qr/\.vcf$/) ) {
116 0 $temp_list->append( $f . "\n" );
117 }
118  
119 0 my $cmd = "bcftools concat -f $temp_list";
120 0 ▶ if ( lc $opt->{outfile} ne "stdout" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $opt->{"outfile"} ne "stdout") 0 0 0
121 0 $cmd .= " -o $opt->{outfile}";
122 }
123 0 App::Egaz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
124 }
125  
126 0 return;
127 }
128  
129 19 1;
« Egaz/Command/repeatmasker.pm ↑ Summary Egaz/Command/blastlink.pm »