| 1 | package HackaMol::Roles::SelectionRole; | ||||||||||||||
| 2 | 14 | $HackaMol::Roles::SelectionRole::VERSION = '0.053'; | |||||||||||||
| 3 | #ABSTRACT: Atom selections in molecules | ||||||||||||||
| 4 | 14 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 14 | ▶ | use HackaMol::AtomGroup; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 14 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | |||||||||||||||
| 9 | 14 | my %common_selection = ( | |||||||||||||
| 10 | 'sidechain' => '$_->record_name eq "ATOM" and not $_->name =~ /^(N|CA|C|O|OXT)$/', | ||||||||||||||
| 11 | 'backbone' => '$_->record_name eq "ATOM" and $_->name =~ /^(N|CA|C|O)$/', # backbone restricted to ATOM to avoid HETATM weirdness, e.g. het cys in 1v1q | ||||||||||||||
| 12 | 'water' => '$_->resname =~ m/HOH|TIP|H2O/ and $_->record_name eq "HETATM"', | ||||||||||||||
| 13 | 'protein' => '$_->record_name eq "ATOM"', | ||||||||||||||
| 14 | 'ligands' => '($_->resname !~ m/HOH|TIP|H2O/ ) and $_->record_name eq "HETATM"', | ||||||||||||||
| 15 | 'metals' => '$_->symbol =~ m/^(Li|Be|Na|Mg|K|Ca|Sc|Ti|V|Cr|Mn|Fe|Co|Ni|Cu|Zn|Rb|Sr|Y|Zr|Nb|Mo|Tc|Ru|Rh|Pd|Ag|Cd|Cs|Ba|La|Ce|Pr|Nd|Pm|Sm|Eu|Gd|Tb|Dy|Ho|Er|Tm|Yb|Lu|Hf|Ta|W|Re|Os|Ir|Pt|Au|Hg)$/', | ||||||||||||||
| 16 | ); | ||||||||||||||
| 17 | |||||||||||||||
| 18 | has 'selection' => ( | ||||||||||||||
| 19 | traits => ['Hash'], | ||||||||||||||
| 20 | is => 'ro', | ||||||||||||||
| 21 | isa => 'HashRef[Str]', | ||||||||||||||
| 22 | lazy => 1, | ||||||||||||||
| 23 | 1 | ▶ | default => sub { {} }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 14 | handles => { | |||||||||||||
| 25 | get_selection => 'get', | ||||||||||||||
| 26 | set_selection => 'set', | ||||||||||||||
| 27 | has_selection => 'count', | ||||||||||||||
| 28 | keys_selection => 'keys', | ||||||||||||||
| 29 | delete_selection => 'delete', | ||||||||||||||
| 30 | has_selection => 'exists', | ||||||||||||||
| 31 | }, | ||||||||||||||
| 32 | ); | ||||||||||||||
| 33 | |||||||||||||||
| 34 | |||||||||||||||
| 35 | has 'selections_cr' => ( | ||||||||||||||
| 36 | traits => ['Hash'], | ||||||||||||||
| 37 | is => 'ro', | ||||||||||||||
| 38 | isa => 'HashRef[CodeRef]', | ||||||||||||||
| 39 | 6 | ▶ | default => sub { {} }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | 14 | lazy => 1, | |||||||||||||
| 41 | handles => { | ||||||||||||||
| 42 | get_selection_cr => 'get', | ||||||||||||||
| 43 | set_selection_cr => 'set', | ||||||||||||||
| 44 | has_selections_cr => 'count', | ||||||||||||||
| 45 | keys_selection_cr => 'keys', | ||||||||||||||
| 46 | delete_selection_cr => 'delete', | ||||||||||||||
| 47 | has_selection_cr => 'exists', | ||||||||||||||
| 48 | }, | ||||||||||||||
| 49 | ); | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 37 | ▶ | sub select_group { | ||||||||||||
|
Subroutine
select_group: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 37 | my $self = shift; | |||||||||||||
| 54 | 37 | my $selection = shift; | |||||||||||||
| 55 | 37 | my $method; | |||||||||||||
| 56 | |||||||||||||||
| 57 | 37 | ▶ | if ($self->has_selection_cr($selection)){ #attr takes priority so user can change | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 58 | 1 | $method = $self->get_selection_cr($selection); | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | elsif ( exists( $common_selection{$selection} ) ) { | ||||||||||||||
| 61 | 6 | $method = eval("sub{ grep{ $common_selection{$selection} } \@_ }"); | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | else { | ||||||||||||||
| 64 | 30 | $method = _regex_method($selection); | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | #grep { &{ sub{ $_%2 } }($_)} 1..10 | ||||||||||||||
| 68 | |||||||||||||||
| 69 | my $group = | ||||||||||||||
| 70 | 37 | HackaMol::AtomGroup->new( atoms => [ &{$method}( $self->all_atoms ) ], ); | |||||||||||||
| 71 | |||||||||||||||
| 72 | 37 | return ($group); | |||||||||||||
| 73 | |||||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | # $mol->select_group('(chain A .or. (resname TYR .and. chain B)) .and. occ .within. 1') | ||||||||||||||
| 77 | # becomes grep{($_->chain eq A or ($_->resname eq TYR and $_->chain eq 'B')) and $_->occ <= 1.0} | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 30 | ▶ | sub _regex_method { | ||||||||||||
|
Subroutine
_regex_method: called CC=4 SCAR=13.9
| |||||||||||||||
| 80 | 30 | my $str = shift; | |||||||||||||
| 81 | |||||||||||||||
| 82 | # allow and or .and. .or. ... does this cause other problems with names? | ||||||||||||||
| 83 | 30 | $str =~ s/\sand\s/ \.and\. /g; | |||||||||||||
| 84 | 30 | $str =~ s/\sor\s/ \.or\. /g; | |||||||||||||
| 85 | 30 | $str =~ s/\snot\s/ \.not\. /g; | |||||||||||||
| 86 | |||||||||||||||
| 87 | #print "$str not implemented yet"; return(sub{0}); | ||||||||||||||
| 88 | #my @parenth = $str =~ /(\(([^()]|(?R))*\))/g | ||||||||||||||
| 89 | |||||||||||||||
| 90 | # ranges resid 1+3-10+20 -> resid =~ /^(1|3|4|5|6|7|8|9|10|20)$/ | ||||||||||||||
| 91 | 30 | my @ranges = $str =~ /(\w+\s+(?:\w+|\d+)(?:\+|\-)[^\s]+)/g; | |||||||||||||
| 92 | 30 | foreach my $range (@ranges){ | |||||||||||||
| 93 | 6 | my ($attr,$sel) = split(/\s+/, $range); | |||||||||||||
| 94 | #$range =~ s/\+/\\+/g; | ||||||||||||||
| 95 | #$range =~ s/\-/\\-/g; | ||||||||||||||
| 96 | 6 | ▶ | my $gsel = join '|',map{/(.+)-(.+)/ ? ($1 .. $2) : $_ } split('\+', $sel ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | 6 | $str =~ s/\Q$range\E/\$\_->$attr =~ \/^($gsel)\$\//g; | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 30 | $str =~ s/(\w+)\s+(\d*[A-Za-z]+\d*)/\$\_->$1 eq \'$2\'/g; # resnames must have at least 1 letter | |||||||||||||
| 101 | 30 | $str =~ s/(\w+)\s+(-?\d+)/\$\_->$1 eq $2/g; | |||||||||||||
| 102 | 30 | $str =~ s/(\w+)\s+\.within\.\s+(\d+)/\$\_->$1 <= $2/g; | |||||||||||||
| 103 | 30 | $str =~ s/(\w+)\s+\.beyond\.\s+(\d+)/\$\_->$1 >= $2/g; | |||||||||||||
| 104 | 30 | $str =~ s/$_/\($common_selection{$_}\)/g foreach keys %common_selection; | |||||||||||||
| 105 | 30 | $str =~ s/\.and\./and/g; | |||||||||||||
| 106 | 30 | $str =~ s/\.or\./or/g; | |||||||||||||
| 107 | 30 | $str =~ s/\.not\./not/g; | |||||||||||||
| 108 | |||||||||||||||
| 109 | 30 | return ( eval("sub{ grep{ $str } \@_ }") ); | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | |||||||||||||||
| 113 | |||||||||||||||
| 114 | 14 | ▶ | no Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 115 | |||||||||||||||
| 116 | 14 | 1; | |||||||||||||
| 117 | |||||||||||||||
| 118 | __END__ | ||||||||||||||