blib/lib/Bioinfo/App/Cmd/Fasta/Cmd/Split.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 75 CC 6 SCAR 31.7
CC
6
Cov
0%
execute
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 23.8
1 package Bioinfo::App::Cmd::Fasta::Cmd::Split;
2 use Modern::Perl;
3 use Moo;
4 use MooX::Cmd;
5 use MooX::Options prefer_commandline => 1;
6 use IO::All;
7  
8 our $VERSION = '0.1.11'; # VERSION:
9 # ABSTRACT: my perl module and CLIs for Biology
10  
11  
12 option input => (
13 is => 'ro',
14 required => 1,
15 format => 's',
16 short => 'i',
17 doc => 'a file of fasta format'
18 );
19  
20  
21 option num => (
22 is => 'ro',
23 format => 'i',
24 short => 'n',
25 doc => ''
26 );
27  
28  
29 option outdir => (
30 is => 'ro',
31 format => 's',
32 short => 'o',
33 doc => '',
34 );
35  
36  
37 option prefix => (
38 is => 'ro',
39 format => 's',
40 short => 'p',
41 default => sub { '' },
42 doc => 'the prefix of the split file',
43 );
44  
45  
46 sub execute {
47 my ($self, $args_ref, $chain_ref) = @_;
48 $self->options_usage unless (@$args_ref);
49 my $input = $self->input;
50 my $outdir = $self->outdir;
51 my $num = $self->num;
52 my $prefix = $self->prefix;
53 system("mkdir -p $outdir") unless -e $outdir;
54 my %id2seq;
55 my $io_fa = io($input)->chomp;
56 my ($total_base, $seqid) = (0, '');
57 while (defined (my $line = $io_fa->getline)) {
58 if ($line =~/^>/) {
59 $seqid = $line;
60 } else {
61 $id2seq{$seqid} .= $line;
62 $total_base += length($line);
63 }
64 }
65 say "number of files:$num\t prefix:$prefix\toutdir:$outdir";
66 say "total base: $total_base";
67 my $file_base = int($total_base / $num) + 2;
68 my ($file_base_tmp, $index_tmp) = (0, 1);
69 my $single_file_name = "$outdir/$prefix$index_tmp.fa";
70 my $single_file_content = "";
71 for my $id (keys %id2seq) {
72 if ($file_base_tmp < $file_base) {
73 $single_file_content .= sprintf("%s\n%s\n", $id, $id2seq{$id});
74 $file_base_tmp += length($id2seq{$id});
75 } else {
76 io($single_file_name)->print("$single_file_content");
77 say "$single_file_name\t$file_base_tmp";
78  
79 # set next file parameter
80 $single_file_content = sprintf("%s\n%s\n", $id, $id2seq{$id});
81 $file_base_tmp = length($id2seq{$id});
82 $index_tmp++;
83 $single_file_name = "$outdir/$prefix$index_tmp.fa";
84 }
85 }
86  
87 # the last one file
88 if ($file_base_tmp < $file_base) {
89 $single_file_name = "$outdir/$prefix$index_tmp.fa";
90 io($single_file_name)->print("$single_file_content");
91 say "$single_file_name\t$file_base_tmp";
92 }
93 }
94  
95 1;
96  
97 __END__
98  
99 =pod
100  
101 =encoding UTF-8
102  
103 =head1 NAME
104  
105 Bioinfo::App::Cmd::Fasta::Cmd::Split - my perl module and CLIs for Biology
106  
107 =head1 VERSION
108  
109 version 0.1.11
110  
111 =head1 SYNOPSIS
112  
113 use Bioinfo::App::Cmd::Fasta;
114 ...
115  
116 =head1 DESCRIPTION
117  
118 =head1 ATTRIBUTES
119  
120 =head2 input
121  
122 The input file is a file of fasta format
123  
124 =head2 num
125  
126 number of files that the fasta file will be split
127  
128 =head2 outdir
129  
130 =head2 prefix
131  
132 the prefix of the split file
133  
134 =head1 METHODS
135  
136 =head2 execute
137  
138 =head1 AUTHOR
139  
140 Yan Xueqing <yanxueqing621@163.com>
141  
142 =head1 COPYRIGHT AND LICENSE
143  
144 This software is copyright (c) 2017 by Yan Xueqing.
145  
146 This is free software; you can redistribute it and/or modify it under
147 the same terms as the Perl 5 programming language system itself.
148  
149 =cut