« blib/lib/Bio/Roary/CommandLine/UniqueGenesPerSample.pm
↑ Summary
lib/Bio/Roary/CommandLine/QueryRoary.pm »
| 1 | package Bio::Roary::Output::GroupsMultifastaNucleotide; | ||||||||||||||||||||||||||||
| 2 | 3 | $Bio::Roary::Output::GroupsMultifastaNucleotide::VERSION = '3.13.0'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Take in a GFF files and a groups file and output one multifasta file per group with nucleotide sequences. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Bio::SeqIO; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use File::Path qw(make_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use File::Copy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use File::Temp qw/ tempfile /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Bio::Roary::Exceptions; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Bio::Roary::AnalyseGroups; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Bio::Tools::GFF; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | with 'Bio::Roary::BedFromGFFRole'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | has 'gff_file' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 18 | 3 | has 'group_names' => ( is => 'ro', isa => 'ArrayRef', required => 0 ); | |||||||||||||||||||||||||||
| 19 | 3 | has 'output_directory' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 20 | 3 | has 'pan_reference_groups_seen' => ( is => 'rw', isa => 'HashRef', required => 1 ); | |||||||||||||||||||||||||||
| 21 | 3 | has 'number_of_gff_files' => ( is => 'ro', isa => 'Int', required => 1 ); | |||||||||||||||||||||||||||
| 22 | 3 | has 'pan_reference_filename' => ( is => 'ro', isa => 'Str',default => 'pan_genome_reference.fa' ); | |||||||||||||||||||||||||||
| 23 | 3 | has 'dont_delete_files' => ( is => 'ro', isa => 'Bool',default => 0 ); | |||||||||||||||||||||||||||
| 24 | 3 | has 'core_definition' => ( is => 'ro', isa => 'Num', default => 1.0 ); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 3 | has 'annotate_groups' => ( is => 'ro', isa => 'Bio::Roary::AnnotateGroups', required => 1 ); | |||||||||||||||||||||||||||
| 27 | 3 | has 'output_multifasta_files' => ( is => 'ro', isa => 'Bool', default => 0 ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | has 'fasta_file' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_fasta_file' ); | |||||||||||||||||||||||||||
| 30 | 3 | has '_input_seqio' => ( is => 'ro', isa => 'Bio::SeqIO', lazy => 1, builder => '_build__input_seqio' ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 3 | has 'output_filename' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_output_filename' ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub _build_output_filename | ||||||||||||||||||||||||||||
| 35 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_build_output_filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 37 | 1 | my ( $filename, $directories, $suffix ) = fileparse($self->gff_file); | |||||||||||||||||||||||||||
| 38 | 1 | return join('/',($self->output_directory, $filename.'.tmp_nuc_sequences.fa' )); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | sub _build__input_seqio { | ||||||||||||||||||||||||||
|
Subroutine
_build__input_seqio: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 43 | 1 | return Bio::SeqIO->new( -file => $self->fasta_file, -format => 'Fasta' ); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 3 | ▶ | sub _bed_output_filename { | ||||||||||||||||||||||||||
|
Subroutine
_bed_output_filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 48 | 3 | return join( '.', ( $self->output_filename, 'intermediate.bed' ) ); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | sub populate_files { | ||||||||||||||||||||||||||
|
Subroutine
populate_files: called CC=7 SCAR=38.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 53 | 1 | ▶ | while ( my $input_seq = $self->_input_seqio->next_seq() ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( $self->annotate_groups->_ids_to_groups->{$input_seq->display_id} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||
| 57 | 0 | my $current_group = $self->annotate_groups->_ids_to_groups->{$input_seq->display_id}; | |||||||||||||||||||||||||||
| 58 | 0 | my $gene_name = $self->annotate_groups->_groups_to_consensus_gene_names->{$current_group}; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if(! defined($self->pan_reference_groups_seen->{$current_group})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||
| 62 | 0 | my $pan_output_seq = $self->_pan_genome_reference_io_obj($current_group); | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | $pan_output_seq->write_seq(Bio::Seq->new( -display_id => $input_seq->display_id, -desc => ($gene_name ? $gene_name : $current_group), -seq => $input_seq->seq ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | $self->pan_reference_groups_seen->{$current_group} = 1; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my $number_of_genes = @{$self->annotate_groups->_groups_to_id_names->{$current_group}}; | |||||||||||||||||||||||||||
| 68 | # Theres no need to align noncore files | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | next if($self->dont_delete_files == 0 && $number_of_genes < ($self->core_definition * $self->number_of_gff_files )); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->dont_delete_files == 0 and $number_of_genes < $self->core_definition * $self->number_of_gff_files
MC/DC0%0/2
$self->dont_delete_files == 0 and $number_of_genes < $self->core_definition * $self->number_of_gff_files
$self->dont_delete_files == 0
$number_of_genes < $self->core_definition * $self->number_of_gff_files
Truth table0%0/3
A and B
A= $self->dont_delete_files == 0B= $number_of_genes < $self->core_definition * $self->number_of_gff_files
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my $output_seq = $self->_group_seq_io_obj($current_group,$number_of_genes); | |||||||||||||||||||||||||||
| 72 | 0 | $output_seq->write_seq($input_seq); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | unlink($self->fasta_file); | |||||||||||||||||||||||||||
| 77 | 0 | 1; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | sub _group_file_name | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_group_file_name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 82 | 0 | my ($self,$group_name,$num_group_genes) = @_; | |||||||||||||||||||||||||||
| 83 | 0 | my $annotated_group_name = $self->annotate_groups->_groups_to_consensus_gene_names->{$group_name}; | |||||||||||||||||||||||||||
| 84 | 0 | $annotated_group_name =~ s!\W!_!gi; | |||||||||||||||||||||||||||
| 85 | 0 | my $filename = $annotated_group_name.'.fa'; | |||||||||||||||||||||||||||
| 86 | 0 | my $group_file_name = join('/',($self->output_directory, $filename )); | |||||||||||||||||||||||||||
| 87 | 0 | return $group_file_name; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub _pan_genome_reference_io_obj | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_pan_genome_reference_io_obj: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 93 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 94 | 0 | return Bio::SeqIO->new( -file => ">>".$self->pan_reference_filename, -format => 'Fasta' ); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub _group_seq_io_obj | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_group_seq_io_obj: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 100 | 0 | my ($self,$group_name,$num_group_genes) = @_; | |||||||||||||||||||||||||||
| 101 | 0 | my $filename = $self->_group_file_name($group_name,$num_group_genes); | |||||||||||||||||||||||||||
| 102 | 0 | return Bio::SeqIO->new( -file => ">>".$filename, -format => 'Fasta' ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | ▶ | sub _extracted_nucleotide_fasta_file_from_bed_filename { | ||||||||||||||||||||||||||
|
Subroutine
_extracted_nucleotide_fasta_file_from_bed_filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 108 | 2 | return join( '.', ( $self->output_filename, 'intermediate.extracted.fa' ) ); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | sub _create_nucleotide_fasta_file_from_gff { | ||||||||||||||||||||||||||
|
Subroutine
_create_nucleotide_fasta_file_from_gff: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 112 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | open(my $input_fh, $self->gff_file); | |||||||||||||||||||||||||||
| 115 | 1 | open(my $output_fh, '>', $self->_nucleotide_fasta_file_from_gff_filename); | |||||||||||||||||||||||||||
| 116 | 1 | my $at_sequence = 0; | |||||||||||||||||||||||||||
| 117 | 1 | ▶ | while(<$input_fh>) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||||||
| 119 | 271 | my $line = $_; | |||||||||||||||||||||||||||
| 120 | 271 | ▶ | if($line =~/^>/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||||||
| 122 | 1 | $at_sequence = 1; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 271 | ▶ | if($at_sequence == 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | { | ||||||||||||||||||||||||||||
| 127 | 252 | print {$output_fh} $line; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 1 | close($input_fh); | |||||||||||||||||||||||||||
| 131 | 1 | close($output_fh); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 4 | ▶ | sub _nucleotide_fasta_file_from_gff_filename { | ||||||||||||||||||||||||||
|
Subroutine
_nucleotide_fasta_file_from_gff_filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 136 | 4 | return join( '.', ( $self->output_filename, 'intermediate.fa' ) ); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | ▶ | sub _extract_nucleotide_regions { | ||||||||||||||||||||||||||
|
Subroutine
_extract_nucleotide_regions: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 140 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1 | $self->_create_nucleotide_fasta_file_from_gff; | |||||||||||||||||||||||||||
| 143 | 1 | $self->_create_bed_file_from_gff; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 1 | my $cmd = | |||||||||||||||||||||||||||
| 146 | 'bedtools getfasta -s -fi ' | ||||||||||||||||||||||||||||
| 147 | . $self->_nucleotide_fasta_file_from_gff_filename | ||||||||||||||||||||||||||||
| 148 | . ' -bed ' | ||||||||||||||||||||||||||||
| 149 | . $self->_bed_output_filename . ' -fo ' | ||||||||||||||||||||||||||||
| 150 | . $self->_extracted_nucleotide_fasta_file_from_bed_filename | ||||||||||||||||||||||||||||
| 151 | . ' -name > /dev/null 2>&1'; | ||||||||||||||||||||||||||||
| 152 | 1 | system($cmd); | |||||||||||||||||||||||||||
| 153 | 1 | unlink( $self->_nucleotide_fasta_file_from_gff_filename ); | |||||||||||||||||||||||||||
| 154 | 1 | unlink( $self->_bed_output_filename ); | |||||||||||||||||||||||||||
| 155 | 1 | unlink( $self->_nucleotide_fasta_file_from_gff_filename . '.fai' ); | |||||||||||||||||||||||||||
| 156 | 1 | return $self->_extracted_nucleotide_fasta_file_from_bed_filename; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | ▶ | sub _cleanup_fasta { | ||||||||||||||||||||||||||
|
Subroutine
_cleanup_fasta: called CC=5 SCAR=29.9
| |||||||||||||||||||||||||||||
| 160 | 1 | my ($self,$infile) = @_; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | my($fh, $outfile) = tempfile(); | |||||||||||||||||||||||||||
| 163 | 1 | ▶ | return unless ( -e $infile ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | open( my $in, '<', $infile ); | |||||||||||||||||||||||||||
| 166 | 0 | open( my $out, '>', $outfile ); | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | while ( my $line = <$in> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | chomp $line; | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | if ( $line =~ /^>/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | 0 | $line =~ s/"//g ; | |||||||||||||||||||||||||||
| 172 | # newer versions of Bedtools add (-) or (+) to the end of the sequence name, remove them | ||||||||||||||||||||||||||||
| 173 | 0 | $line =~ s!\([-+]\)!!; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | if($line =~ /^(>[^:]+)/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | { | ||||||||||||||||||||||||||||
| 178 | 0 | $line = $1; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 0 | print $out "$line\n"; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | 0 | close $in; | |||||||||||||||||||||||||||
| 183 | 0 | close $out; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | move( $outfile, $infile); | |||||||||||||||||||||||||||
| 186 | 0 | return $infile; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 1 | ▶ | sub _build_fasta_file { | ||||||||||||||||||||||||||
|
Subroutine
_build_fasta_file: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 191 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 192 | 1 | my $fasta_filename = $self->_extract_nucleotide_regions; | |||||||||||||||||||||||||||
| 193 | 1 | return $self->_cleanup_fasta($fasta_filename); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 3 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 197 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 3 | 1; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | __END__ | ||||||||||||||||||||||||||||