« lib/Bio/Roary/CommandLine/ParallelAllAgainstAllBlastp.pm
↑ Summary
lib/Bio/Roary/ExtractCoreGenesFromSpreadsheet.pm »
| 1 | package Bio::Roary::ExtractProteomeFromGFF; | ||||||||||
| 2 | 26 | $Bio::Roary::ExtractProteomeFromGFF::VERSION = '3.13.0'; | |||||||||
| 3 | # ABSTRACT: Take in a GFF file and create protein sequences in FASTA format | ||||||||||
| 4 | |||||||||||
| 5 | |||||||||||
| 6 | 26 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 26 | ▶ | use Bio::SeqIO; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 26 | ▶ | use Cwd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 26 | ▶ | use Bio::Roary::Exceptions; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 26 | ▶ | use File::Basename; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 26 | ▶ | use File::Temp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 26 | ▶ | use File::Copy; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 26 | ▶ | use Bio::Tools::GFF; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 26 | with 'Bio::Roary::JobRunner::Role'; | |||||||||
| 15 | 26 | with 'Bio::Roary::BedFromGFFRole'; | |||||||||
| 16 | |||||||||||
| 17 | 26 | has 'gff_file' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||
| 18 | 26 | has 'apply_unknowns_filter' => ( is => 'rw', isa => 'Bool', default => 1 ); | |||||||||
| 19 | 26 | has 'maximum_percentage_of_unknowns' => ( is => 'ro', isa => 'Num', default => 5 ); | |||||||||
| 20 | 26 | has 'output_filename' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_output_filename' ); | |||||||||
| 21 | 26 | has 'fasta_file' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_fasta_file' ); | |||||||||
| 22 | 22 | ▶ | has '_working_directory' => ( is => 'ro', isa => 'File::Temp::Dir', default => sub { File::Temp->newdir( DIR => getcwd, CLEANUP => 1 ); } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 26 | has '_working_directory_name' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build__working_directory_name' ); | |||||||||
| 24 | 26 | has 'translation_table' => ( is => 'rw', isa => 'Int', default => 11 ); | |||||||||
| 25 | |||||||||||
| 26 | 22 | ▶ | sub _build_fasta_file { | ||||||||
|
Subroutine
_build_fasta_file: called CC=1 SCAR=1.7
| |||||||||||
| 27 | 22 | my ($self) = @_; | |||||||||
| 28 | 22 | $self->_extract_nucleotide_regions; | |||||||||
| 29 | 22 | $self->_convert_nucleotide_to_protein; | |||||||||
| 30 | 0 | $self->_cleanup_fasta; | |||||||||
| 31 | 0 | $self->_cleanup_intermediate_files; | |||||||||
| 32 | 0 | $self->_filter_fasta_sequences( join('/',($self->output_directory,$self->output_filename)) ); | |||||||||
| 33 | 0 | return join('/',($self->output_directory,$self->output_filename)); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | sub _build__working_directory_name { | ||||||||
|
Subroutine
_build__working_directory_name: not called CC=1 SCAR=6.9
| |||||||||||
| 37 | 0 | my ($self) = @_; | |||||||||
| 38 | 0 | return $self->_working_directory->dirname(); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | sub _build_output_filename { | ||||||||
|
Subroutine
_build_output_filename: not called CC=1 SCAR=6.9
| |||||||||||
| 42 | 0 | my ($self) = @_; | |||||||||
| 43 | 0 | my ( $filename, $directories, $suffix ) = fileparse( $self->gff_file, qr/\.[^.]*/ ); | |||||||||
| 44 | 0 | return join( '/', ( $self->_working_directory_name, $filename . '.faa' ) ); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | 0 | ▶ | sub _cleanup_intermediate_files { | ||||||||
|
Subroutine
_cleanup_intermediate_files: not called CC=1 SCAR=6.9
| |||||||||||
| 50 | 0 | my ($self) = @_; | |||||||||
| 51 | 0 | unlink( $self->_unfiltered_output_filename ); | |||||||||
| 52 | 0 | unlink( $self->_fastatranslate_filename ); | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 88 | ▶ | sub _nucleotide_fasta_file_from_gff_filename { | ||||||||
|
Subroutine
_nucleotide_fasta_file_from_gff_filename: called CC=1 SCAR=0.0
| |||||||||||
| 56 | 88 | my ($self) = @_; | |||||||||
| 57 | 88 | return join('/',($self->output_directory,join( '.', ( $self->output_filename, 'intermediate.fa' ) ))); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 44 | ▶ | sub _extracted_nucleotide_fasta_file_from_bed_filename { | ||||||||
|
Subroutine
_extracted_nucleotide_fasta_file_from_bed_filename: called CC=1 SCAR=0.0
| |||||||||||
| 61 | 44 | my ($self) = @_; | |||||||||
| 62 | 44 | return join('/',($self->output_directory,join( '.', ( $self->output_filename,'intermediate.extracted.fa' ) ))); | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 22 | ▶ | sub _unfiltered_output_filename { | ||||||||
|
Subroutine
_unfiltered_output_filename: called CC=1 SCAR=0.0
| |||||||||||
| 66 | 22 | my $self = shift; | |||||||||
| 67 | 22 | return join('/',($self->output_directory,join( '.', ( $self->output_filename, 'unfiltered.fa' ) ))); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | |||||||||||
| 71 | 22 | ▶ | sub _create_nucleotide_fasta_file_from_gff { | ||||||||
|
Subroutine
_create_nucleotide_fasta_file_from_gff: called CC=4 SCAR=13.9
| |||||||||||
| 72 | 22 | my ($self) = @_; | |||||||||
| 73 | |||||||||||
| 74 | 22 | open(my $input_fh, $self->gff_file); | |||||||||
| 75 | 22 | open(my $output_fh, '>', $self->_nucleotide_fasta_file_from_gff_filename); | |||||||||
| 76 | 22 | my $at_sequence = 0; | |||||||||
| 77 | 22 | ▶ | while(<$input_fh>) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | { | ||||||||||
| 79 | 5657 | my $line = $_; | |||||||||
| 80 | 5657 | ▶ | if($line =~/^>/) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | { | ||||||||||
| 82 | 28 | $at_sequence = 1; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 5657 | ▶ | if($at_sequence == 1) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | { | ||||||||||
| 87 | 5194 | print {$output_fh} $line; | |||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | 22 | close($input_fh); | |||||||||
| 91 | 22 | close($output_fh); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 22 | ▶ | sub _extract_nucleotide_regions { | ||||||||
|
Subroutine
_extract_nucleotide_regions: called CC=1 SCAR=0.0
| |||||||||||
| 95 | 22 | my ($self) = @_; | |||||||||
| 96 | |||||||||||
| 97 | 22 | $self->_create_nucleotide_fasta_file_from_gff; | |||||||||
| 98 | 22 | $self->_create_bed_file_from_gff; | |||||||||
| 99 | |||||||||||
| 100 | 22 | my $cmd = | |||||||||
| 101 | 'bedtools getfasta -s -fi ' | ||||||||||
| 102 | . $self->_nucleotide_fasta_file_from_gff_filename | ||||||||||
| 103 | . ' -bed ' | ||||||||||
| 104 | . $self->_bed_output_filename . ' -fo ' | ||||||||||
| 105 | . $self->_extracted_nucleotide_fasta_file_from_bed_filename | ||||||||||
| 106 | . ' -name > /dev/null 2>&1'; | ||||||||||
| 107 | |||||||||||
| 108 | 22 | $self->logger->debug($cmd); | |||||||||
| 109 | 22 | system($cmd); | |||||||||
| 110 | 22 | unlink( $self->_nucleotide_fasta_file_from_gff_filename ); | |||||||||
| 111 | 22 | unlink( $self->_bed_output_filename ); | |||||||||
| 112 | 22 | unlink( $self->_nucleotide_fasta_file_from_gff_filename . '.fai' ); | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 0 | ▶ | sub _cleanup_fasta { | ||||||||
|
Subroutine
_cleanup_fasta: not called CC=5 SCAR=34.0
| |||||||||||
| 116 | 0 | my $self = shift; | |||||||||
| 117 | 0 | my $infile = $self->_unfiltered_output_filename; | |||||||||
| 118 | 0 | my $outfile = join('/',($self->output_directory,$self->output_filename)); | |||||||||
| 119 | 0 | ▶ | return unless ( -e $infile ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | |||||||||||
| 121 | 0 | open( my $in, '<', $infile ); | |||||||||
| 122 | 0 | open( my $out, '>', $outfile ); | |||||||||
| 123 | 0 | ▶ | while ( my $line = <$in> ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 0 | chomp $line; | |||||||||
| 125 | 0 | ▶ | if ( $line =~ /^>/ ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | { | ||||||||||
| 127 | 0 | $line =~ s/"//g; | |||||||||
| 128 | # newer versions of Bedtools add (-) or (+) to the end of the sequence name, remove them | ||||||||||
| 129 | 0 | $line =~ s!\([-+]\)!!; | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 0 | ▶ | if($line =~ /^(>[^:]+)/) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | { | ||||||||||
| 134 | 0 | $line = $1; | |||||||||
| 135 | } | ||||||||||
| 136 | 0 | print $out "$line\n"; | |||||||||
| 137 | } | ||||||||||
| 138 | 0 | close $in; | |||||||||
| 139 | 0 | close $out; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | 0 | ▶ | sub _fastatranslate_filename { | ||||||||
|
Subroutine
_fastatranslate_filename: not called CC=1 SCAR=6.9
| |||||||||||
| 143 | 0 | my ($self) = @_; | |||||||||
| 144 | 0 | return join('/',($self->output_directory,join( '.', ( $self->output_filename, 'intermediate.translate.fa' ) ))); | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 22 | ▶ | sub _fastatranslate { | ||||||||
|
Subroutine
_fastatranslate: called CC=2 SCAR=14.3
| |||||||||||
| 148 | 22 | my ( $self, $inputfile, $outputfile ) = @_; | |||||||||
| 149 | |||||||||||
| 150 | 22 | my $input_fasta_file_obj = Bio::SeqIO->new( -file => $inputfile, -format => 'Fasta' ); | |||||||||
| 151 | 0 | my $output_protein_file_obj = Bio::SeqIO->new( -file => ">" . $outputfile, -format => 'Fasta', -alphabet => 'protein' ); | |||||||||
| 152 | |||||||||||
| 153 | 0 | my %protein_sequence_objs; | |||||||||
| 154 | 0 | ▶ | while ( my $seq = $input_fasta_file_obj->next_seq ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | 0 | $seq->desc(undef); | |||||||||
| 156 | 0 | my $protseq = $seq->translate( -codontable_id => $self->translation_table ); | |||||||||
| 157 | 0 | $output_protein_file_obj->write_seq($protseq); | |||||||||
| 158 | } | ||||||||||
| 159 | 0 | return 1; | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | 22 | ▶ | sub _convert_nucleotide_to_protein { | ||||||||
|
Subroutine
_convert_nucleotide_to_protein: called CC=1 SCAR=0.4
| |||||||||||
| 163 | 22 | my ($self) = @_; | |||||||||
| 164 | 22 | $self->_fastatranslate( $self->_extracted_nucleotide_fasta_file_from_bed_filename, $self->_unfiltered_output_filename ); | |||||||||
| 165 | 0 | unlink( $self->_extracted_nucleotide_fasta_file_from_bed_filename ); | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | 0 | ▶ | sub _does_sequence_contain_too_many_unknowns { | ||||||||
|
Subroutine
_does_sequence_contain_too_many_unknowns: not called CC=2 SCAR=17.9
| |||||||||||
| 169 | 0 | my ( $self, $sequence_obj ) = @_; | |||||||||
| 170 | 0 | my $maximum_number_of_Xs = int( ( $sequence_obj->length() * $self->maximum_percentage_of_unknowns ) / 100 ); | |||||||||
| 171 | 0 | my $number_of_Xs_found = () = $sequence_obj->seq() =~ /X/g; | |||||||||
| 172 | 0 | ▶ | if ( $number_of_Xs_found > $maximum_number_of_Xs ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 0 | return 1; | |||||||||
| 174 | } | ||||||||||
| 175 | else { | ||||||||||
| 176 | 0 | return 0; | |||||||||
| 177 | } | ||||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | 0 | ▶ | sub _filter_fasta_sequences { | ||||||||
|
Subroutine
_filter_fasta_sequences: not called CC=4 SCAR=30.0
| |||||||||||
| 181 | 0 | my ( $self, $filename ) = @_; | |||||||||
| 182 | 0 | my $temp_output_file = $filename . '.tmp.filtered.fa'; | |||||||||
| 183 | 0 | my $out_fasta_obj = Bio::SeqIO->new( -file => ">" . $temp_output_file, -format => 'Fasta' ); | |||||||||
| 184 | 0 | my $fasta_obj = Bio::SeqIO->new( -file => $filename, -format => 'Fasta' ); | |||||||||
| 185 | |||||||||||
| 186 | 0 | my $sequence_found = 0; | |||||||||
| 187 | |||||||||||
| 188 | 0 | ▶ | while ( my $seq = $fasta_obj->next_seq() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | 0 | ▶ | if ( $self->_does_sequence_contain_too_many_unknowns($seq) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | 0 | next; | |||||||||
| 191 | } | ||||||||||
| 192 | 0 | $seq->desc(undef); | |||||||||
| 193 | 0 | $out_fasta_obj->write_seq($seq); | |||||||||
| 194 | 0 | $sequence_found = 1; | |||||||||
| 195 | } | ||||||||||
| 196 | |||||||||||
| 197 | 0 | ▶ | if ( $sequence_found == 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 198 | 0 | $self->logger->error( "Could not extract any protein sequences from " | |||||||||
| 199 | . $self->gff_file | ||||||||||
| 200 | . ". Does the file contain the assembly as well as the annotation?" ); | ||||||||||
| 201 | } | ||||||||||
| 202 | |||||||||||
| 203 | # Replace the original file. | ||||||||||
| 204 | 0 | move( $temp_output_file, $filename ); | |||||||||
| 205 | 0 | return 1; | |||||||||
| 206 | } | ||||||||||
| 207 | |||||||||||
| 208 | 26 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 209 | 26 | __PACKAGE__->meta->make_immutable; | |||||||||
| 210 | |||||||||||
| 211 | 26 | 1; | |||||||||
| 212 | |||||||||||
| 213 | __END__ | ||||||||||