lib/Bio/Roary/ReformatInputGFFs.pm

filter: Statementstmt 93.5% 130 / 139 Branchbran 68.1% 30 / 44 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 1 totaltotal 88.3% 182 / 206 CC 26 SCAR 17.8
CC
26
Cov
88%
_add_suffix_to_gene_ids_and_return_new_file
10.0
_fix_gff_file_extension
9.3
fix_duplicate_gene_ids
8.0
CRAP 5.9
« lib/Bio/Roary/External/IterativeCdhit.pm ↑ Summary lib/Bio/Roary/AccessoryBinaryFasta.pm »
1 package Bio::Roary::ReformatInputGFFs;
2 2 $Bio::Roary::ReformatInputGFFs::VERSION = '3.13.0';
3 # ABSTRACT: Take in gff files and add suffix where a gene id is seen twice
4  
5  
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Bio::Roary::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Bio::Tools::GFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Digest::MD5::File qw(file_md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 has 'gff_files' => ( is => 'ro', isa => 'ArrayRef', required => 1 );
17 2 has 'logger' => ( is => 'ro', lazy => 1, builder => '_build_logger' );
18 2 has '_tags_to_filter' => ( is => 'ro', isa => 'Str', default => 'CDS' );
19 2 has 'output_directory' => ( is => 'ro', isa => 'Str', default => 'fixed_input_files' );
20 2 has 'suffix_counter' => ( is => 'rw', isa => 'Int', default => 1 );
21  
22 5 ▶has 'fixed_gff_files' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 3 ▶sub _build_logger {
Subroutine _build_logger: called CC=1 SCAR=0.0
25 3 my ($self) = @_;
26 3 Log::Log4perl->easy_init( $ERROR );
27 3 my $logger = get_logger();
28 3 return $logger;
29 }
30  
31 4 ▶sub fix_duplicate_gene_ids {
Subroutine fix_duplicate_gene_ids: called CC=8 SCAR=20.8
Pod undocumented
32 4 my ($self) = @_;
33  
34 4 my %gene_ids_seen_before;
35
36 my %file_md5s;
37
38 4 for my $file ( @{ $self->gff_files } ) {
39 9 my $digest = file_md5_hex($file);
40
41 9 ▶ if(defined($file_md5s{$digest}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_md5s{$digest}) { } 1 8 9
42 {
43 $self->logger->warn(
44 1 "Input files have identical MD5 hashes, only using the first file: ".$file_md5s{$digest}." == ".$file
45 );
46 1 next;
47 }
48 else
49 {
50 8 $file_md5s{$digest} = $file;
51 }
52
53 8 my $ids_seen = 0;
54 8 my $ids_from_file = $self->_get_ids_for_gff_file($file);
55  
56 8 ▶ if ( @{$ids_from_file} < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$ids_from_file;} < 1) { } 0 8 8
57 0 $self->logger->error(
58 "Input GFF file doesnt contain annotation we can use so excluding it from the analysis: $file"
59 );
60 }
61 else {
62 8 for my $gene_id ( @{$ids_from_file} ) {
63 31 ▶ if ( $gene_ids_seen_before{$gene_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gene_ids_seen_before{$gene_id}) 2 29 31
64 2 $self->logger->error(
65 "Input file contains duplicate gene IDs, attempting to fix by adding a unique suffix, new GFF in the fixed_input_files directory: $file "
66 );
67 2 my $updated_file = $self->_add_suffix_to_gene_ids_and_return_new_file($file, $digest);
68 2 ▶ push( @{ $self->fixed_gff_files }, $updated_file ) if ( defined($updated_file) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $updated_file 2 0 2
69 2 $ids_seen = 1;
70 2 last;
71 }
72 29 $gene_ids_seen_before{$gene_id}++;
73 }
74
75 # We know its a valid GFF file since we could open it and extract IDs.
76 # We need to make sure the filenames end in .gff. If it contained duplicate IDs, then they are fixed so nothing to do, but
77 # if they didnt, then we have to double check and repair if necessary.
78 8 ▶ if ( $ids_seen == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ids_seen == 0) 6 2 8
79
80
81 6 push( @{ $self->fixed_gff_files }, $self->_fix_gff_file_extension($file) );
82 }
83 }
84 }
85 4 return 1;
86 }
87  
88 sub _fix_gff_file_extension
89 6 ▶{
Subroutine _fix_gff_file_extension: called CC=4 SCAR=22.3
90 6 my ( $self, $input_file ) = @_;
91
92 6 my ( $filename, $directories, $suffix ) = fileparse( $input_file, qr/\.[^.]*/ );
93 6 ▶ return $input_file if($suffix eq '.gff');
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq ".gff" 6 0 6
94
95
96 0 ▶ make_path( $self->output_directory ) if ( !( -d $self->output_directory ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->output_directory 0 0 0
97 0 my $output_file = $self->output_directory . '/' . $filename . '.gff';
98 0 ▶ copy($input_file, $output_file) or $self->logger->error("Couldnt copy file with invalid gff extention: $input_file -> $output_file");
Branch: 1 branch
BranchTrueFalseTotal
copy($input_file, $output_file) or $self->logger->error("Couldnt copy file with invalid gff extention: $input_file -> $output_file") 0 0 0
99 0 return $output_file;
100 }
101  
102  
103 3 ▶sub _add_suffix_to_gene_ids_and_return_new_file {
Subroutine _add_suffix_to_gene_ids_and_return_new_file: called CC=10 SCAR=23.1
104 3 my ( $self, $input_file, $digest ) = @_;
105 3 my ( $filename, $directories, $suffix ) = fileparse( $input_file, qr/\.[^.]*/ );
106 3 ▶ make_path( $self->output_directory ) if ( !( -d $self->output_directory ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->output_directory 3 0 3
107 3 my $output_file = $self->output_directory . '/' . $filename . '.gff';
108  
109 3 open( my $input_gff_fh, $input_file );
110 3 open( my $out_gff_fh, '>', $output_file );
111
112 # There is a chance that there can be a collision here, but its remote.
113 3 my $random_locus_tag = "".$digest;
114
115 3 $self->logger->warn(
116 "Renamed GFF file from: $input_file -> $output_file" );
117 3 $self->logger->warn(
118 "Locus tag used is '$random_locus_tag' for file: $input_file" );
119  
120 3 my $found_fasta = 0;
121 3 my $gene_counter = 1;
122 3 ▶ while (<$input_gff_fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $input_gff_fh)) 663 3 666
123 663 my $line = $_;
124  
125 663 ▶ if ( $line =~ /^\#\#FASTA/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\#\#FASTA/) 3 660 663
126 3 $found_fasta = 1;
127 }
128  
129 663 ▶ if ( $line =~ /\#/ || $found_fasta == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\#/ or $found_fasta == 1) 645 18 663
Condition100%3/3
$line =~ /\#/ or $found_fasta == 1
l!l&&r!l&&!r
963618
MC/DC100%2/2
$line =~ /\#/ or $found_fasta == 1
$line =~ /\#/ $found_fasta == 1
Truth table100%3/3
A or B
A= $line =~ /\#/B= $found_fasta == 1
ABresult
1X1
011
000
130 645 print {$out_gff_fh} $line;
131 645 next;
132 }
133  
134 18 my @cells = split( /\t/, $line );
135 18 my @tags = split( /;/, $cells[8] );
136 18 my $found_id = 0;
137 18 ▶ for ( my $i = 0 ; $i < @tags ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @tags) 52 8 60
138 52 ▶ if ( $tags[$i] =~ /^(ID=["']?)([^;"']+)(["']?)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tags[$i] =~ /^(ID=["']?)([^;"']+)(["']?)/) 10 42 52
139 10 my $current_id = $2;
140 10 $current_id .= '___' . $self->suffix_counter;
141 10 $tags[$i] = $1 .$random_locus_tag.'_'. $gene_counter . $3;
142 10 $gene_counter++;
143 10 $found_id++;
144 10 last;
145 }
146 }
147 18 ▶ if ( $found_id == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_id == 0) 8 10 18
148 8 unshift( @tags, 'ID=' . $random_locus_tag.'_'. $gene_counter );
149 8 $gene_counter++;
150 }
151 18 $cells[8] = join( ';', @tags );
152 18 print {$out_gff_fh} join( "\t", @cells );
153 }
154  
155 3 ▶ if ( $found_fasta == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_fasta == 0) 0 3 3
156 0 $self->logger->warn(
157 "Input GFF file doesnt appear to have the FASTA sequence at the end of the file so is being excluded from the analysis: $input_file" );
158 0 return undef;
159 }
160 3 close($out_gff_fh);
161 3 close($input_gff_fh);
162 3 return $output_file;
163 }
164  
165 10 ▶sub _get_ids_for_gff_file {
Subroutine _get_ids_for_gff_file: called CC=4 SCAR=13.9
166 10 my ( $self, $file ) = @_;
167 10 my @gene_ids;
168 10 my $tags_regex = $self->_tags_to_filter;
169 10 my $gffio = Bio::Tools::GFF->new( -file => $file, -gff_version => 3 );
170 10 ▶ while ( my $feature = $gffio->next_feature() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $feature = $gffio->next_feature) 100 10 110
171 100 ▶ next if !( $feature->primary_tag =~ /$tags_regex/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $feature->primary_tag =~ /$tags_regex/ 51 49 100
172 49 my $gene_id = $self->_get_feature_id($feature);
173 49 ▶ push( @gene_ids, $gene_id ) if ( defined($gene_id) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $gene_id 49 0 49
174 }
175 10 return \@gene_ids;
176 }
177  
178 49 ▶sub _get_feature_id {
Subroutine _get_feature_id: called CC=4 SCAR=16.1
179 49 my ( $self, $feature ) = @_;
180 49 my ( $gene_id, @junk );
181 49 ▶ if ( $feature->has_tag('ID') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($feature->has_tag('ID')) { } 49 0 49
elsif ($feature->has_tag('locus_tag')) { } 0 0 0
182 49 ( $gene_id, @junk ) = $feature->get_tag_values('ID');
183 }
184 elsif ( $feature->has_tag('locus_tag') ) {
185 0 ( $gene_id, @junk ) = $feature->get_tag_values('locus_tag');
186 }
187 else {
188 0 return undef;
189 }
190 49 $gene_id =~ s!["']!!g;
191 49 ▶ return undef if ( $gene_id eq "" );
Branch: 1 branch
BranchTrueFalseTotal
if $gene_id eq "" 0 49 49
192 49 return $gene_id;
193 }
194  
195 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
196 2 __PACKAGE__->meta->make_immutable;
197  
198 2 1;
199  
200 __END__
« lib/Bio/Roary/External/IterativeCdhit.pm ↑ Summary lib/Bio/Roary/AccessoryBinaryFasta.pm »