<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Copyright [2016-2022] EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=head1 CONTACT

 Ensembl &lt;http://www.ensembl.org/info/about/contact/index.html&gt;

=cut

=head1 NAME

 mutfunc

=head1 SYNOPSIS

 mv mutfunc.pm ~/.vep/Plugins
 ./vep -i variations.vcf --plugin mutfunc,motif=1,extended=1,db=/FULL_PATH_TO/mutfunc_data.db
 ./vep -i variations.vcf --plugin mutfunc,db=/FULL_PATH_TO/mutfunc_data.db

=head1 DESCRIPTION

 A VEP plugin that retrieves data from mutfunc db predicting destabilization of protein structure, interaction interface, and motif.

 Please cite the mutfunc publication alongside the VEP if you use this resource:
 http://msb.embopress.org/content/14/12/e8430

 Pre-requisites:

 1) The data file. mutfunc SQLite db can be downloaded from - 
 https://ftp.ensembl.org/pub/current_variation/mutfunc/mutfunc_data.db
 
 Options are passed to the plugin as key=value pairs:

 By default all the fields (motif, int, mod, and exp) are added in the output. But if you want to have some selected fields and not all of
 them just select the relevant options. The default behavior will then go away outputting only the selected fields.

 db			  : (mandatory) Path to SQLite database containing data for other analysis.
 motif    : Select this option to have mutfunc motif analysis in the output
 int      : Select this option to have mutfunc protein interection analysis in the output
 mod      : Select this option to have mutfunc protein structure analysis in the output
 exp      : Select this option to have mutfunc protein structure (experimental) analysis in the output
 extended : By default mutfunc outputs the most significant field for any analysis. Select this option to get more verbose output.

=cut

package mutfunc;

use strict;
use warnings;
use DBI;
use Compress::Zlib;
use Digest::MD5 qw(md5_hex);
use List::MoreUtils qw(first_index);

use Bio::EnsEMBL::Utils::Exception qw(throw warning);
use Bio::EnsEMBL::Variation::Utils::Sequence qw(get_matched_variant_alleles);
use Bio::EnsEMBL::Variation::Utils::BaseVepPlugin;

use base qw(Bio::EnsEMBL::Variation::Utils::BaseVepPlugin);

my @ALL_AAS = qw(A C D E F G H I K L M N P Q R S T V W Y);

my $field_order = {
  motif =&gt; ["elm", "lost"],
  int   =&gt; ["evidence", "dG_wt", "dG_mt", "ddG", "dG_wt_sd", "dG_mt_sd", "ddG_sd"],
  mod   =&gt; ["dG_wt", "dG_mt", "ddG", "dG_wt_sd", "dG_mt_sd", "ddG_sd"],
  exp   =&gt; ["dG_wt", "dG_mt", "ddG", "dG_wt_sd", "dG_mt_sd", "ddG_sd"]
};

sub new {
  my $class = shift;

  my $self = $class-&gt;SUPER::new(@_);

  my $param_hash = $self-&gt;params_to_hash();

  # default behavior is to output all field
  $param_hash-&gt;{all} = 1 if (
    (!defined $param_hash-&gt;{motif}) &amp;&amp;
    (!defined $param_hash-&gt;{int}) &amp;&amp;
    (!defined $param_hash-&gt;{mod}) &amp;&amp;
    (!defined $param_hash-&gt;{exp})
  );

  $self-&gt;{motif} = 1 if $param_hash-&gt;{motif} || $param_hash-&gt;{all};
  $self-&gt;{int} = 1 if $param_hash-&gt;{int} || $param_hash-&gt;{all};
  $self-&gt;{mod} = 1 if $param_hash-&gt;{mod} || $param_hash-&gt;{all};
  $self-&gt;{exp} = 1 if $param_hash-&gt;{exp} || $param_hash-&gt;{all};

  die "ERROR: please provide the SQLite database using 'db' parameter\n" if (
    !(defined $param_hash-&gt;{db})
  );
  $self-&gt;{db} = $param_hash-&gt;{db};

  $self-&gt;{extended} = 1 if $param_hash-&gt;{extended};

  if( ($self-&gt;{config}-&gt;{output_format} eq "json") || $self-&gt;{config}-&gt;{rest}){
    $self-&gt;{output_json} = 1;
  }

  $self-&gt;{initial_pid} = $$;

  $self-&gt;{dbh} = DBI-&gt;connect("dbi:SQLite:dbname=".$self-&gt;{db},"","");
  $self-&gt;{get_sth} = $self-&gt;{dbh}-&gt;prepare("SELECT species, md5, item, matrix FROM consequences WHERE md5 = ?");

  $self-&gt;{species} = $self-&gt;config-&gt;{species};

  return $self;
}

sub feature_types {
  return ['Transcript'];
}

sub get_header_info {
  my ($self) = shift;

  my %header;

  if (defined $self-&gt;{motif}){
    $header{mutfunc_motif} = "Nonsynonymous mutations impact on linear motif from mutfunc db. Output field(s) include: ";
    if (defined $self-&gt;{extended}){
      $header{mutfunc_motif} .= $self-&gt;{config}-&gt;{output_format} eq "vcf" ? "(fields are separated by '&amp;') " : "(fields are separated by ',') ";
    }
    $header{mutfunc_motif} .= "elm - ELM accession of the linear motif, " if defined $self-&gt;{extended};
    $header{mutfunc_motif} .= "lost - '1' if the mutation causes the motif to be lost and '0' otherwise";
  }

  foreach (qw(int mod exp)){
    if (defined $self-&gt;{$_}) {
      my $key = "mutfunc_" . $_;
      $header{$key} = "Protein interaction interface destabilization analysis from mutfunc db. Output field(s) include: " if $_ eq "int";
      $header{$key} = "Protein structure destabilization analysis from mutfunc db. Output field(s) include: " if $_ eq "mod";
      $header{$key} = "Protein structure destabilization analysis (experimental) from mutfunc db. Output field(s) include: " if $_ eq "exp";

      if (defined $self-&gt;{extended}){
        $header{$key} .= $self-&gt;{config}-&gt;{output_format} eq "vcf" ? "(fields are separated by '&amp;') " : "(fields are separated by ',') ";
      }
      $header{$key} .= "evidence - 'EXP' for experimental model and 'MDL' for homology models and 'MDD' for domain-domain homology models, " if ($_ eq "int" &amp;&amp; (defined $self-&gt;{extended}));
      $header{$key} .= "dG_wt - reference interface energy (kcal/mol), " if defined $self-&gt;{extended};
      $header{$key} .= "dG_mt - mutated interface energy (kcal/mol), " if defined $self-&gt;{extended};
      $header{$key} .= "ddG - change in interface stability between mutated and reference structure (kcal/mol) mutations where ddG &gt;= 2 kcal/mol can be considered deleterious, ";
      $header{$key} .= "dG_wt_sd - dG_wt standard deviation (kcal/mol), " if defined $self-&gt;{extended};
      $header{$key} .= "dG_mt_sd - dG_mt standard deviation (kcal/mol), " if defined $self-&gt;{extended};
      $header{$key} .= "ddG_sd - ddG standard deviation (kcal/mol), " if defined $self-&gt;{extended};
    }
  }

  return \%header;
}

sub expand_matrix {
  my ($matrix) = @_;
  my $expanded_matrix = Compress::Zlib::memGunzip($matrix) or
    throw("Failed to gunzip: $gzerrno");

  return $expanded_matrix;
}

sub retrieve_item_value {
  my ($matrix, $pos, $aa, $tot_packed_len) = @_;

  my $item_value = (substr $matrix, $pos * $tot_packed_len * 20 + $aa * $tot_packed_len, $tot_packed_len );

  return $item_value;
}

sub parse_motif {
  my ($item_value) = @_;

  my ($elm, $lost) = unpack "A24v", $item_value;

  $elm = undef if $elm eq "undefined";
  $lost = undef if $lost == 0xFFFF;

  return $elm, $lost;
}

sub parse_destabilizers {
  my ($item_value, $item) = @_;
  my @evidence_lval = qw(EXP MDD MDL);
  my $evidence_val;

  # only int item type have evidence
  if ($item eq "int"){
    my $evidence = unpack "v", $item_value;

    # now omit the evidence part from item value
    $item_value = substr $item_value, 2;

    # get the value of the evidence
    $evidence = undef if $evidence == 0xFFFF;
    $evidence_val = defined $evidence ? $evidence_lval[$evidence] : undef;
  }

  # get the rest
  my ($dG_wt, $ddG, $dG_wt_sd, $dG_mt_sd, $ddG_sd) = unpack "A8A8A8A8A8", $item_value;

  $dG_wt = undef if $dG_wt eq "10000000";
  $ddG = undef if $ddG eq "10000000";
  $dG_wt_sd = undef if $dG_wt_sd eq "10000000";
  $dG_mt_sd = undef if $dG_mt_sd eq "10000000";
  $ddG_sd = undef if $ddG_sd eq "10000000";

  return $evidence_val, $dG_wt, $ddG, $dG_wt_sd, $dG_mt_sd, $ddG_sd if $item eq "int";
  return $dG_wt, $ddG, $dG_wt_sd, $dG_mt_sd, $ddG_sd;
}

sub format_output{
  my ($self, $data, $item) = @_;

  my $result = {};

  if ($self-&gt;{output_json}){
    my %hash;
    if( $self-&gt;{extended}){
      %hash = map { $_ =&gt; $data-&gt;{$_} } @{ $field_order-&gt;{$item} };
    }
    else{
      %hash = map { $_ =&gt; $data-&gt;{$_} } keys %$data;
    }
    $result-&gt;{$item} = \%hash;
  }
  else{
    my $key = "mutfunc_" . $item;
    if( $self-&gt;{extended}){
      $result-&gt;{$key} = join(",", map { $data-&gt;{$_} } @{ $field_order-&gt;{$item} });
    }
    else {
      $result-&gt;{$key} = join(",", map { $data-&gt;{$_} } keys %$data );
    }
  }

  return $result;
}

sub process_from_db {
  my ($self, $tva) = @_;

  # get the trascript related to the variant
  my $tr = $tva-&gt;transcript;

  # get the translation
  my $translation = $tr-&gt;translate;
  return {} unless $translation;

  # get the md5 hash of the peptide sequence
  my $md5 = md5_hex($translation-&gt;seq);

  # forked, reconnect to DB
  if($$ != $self-&gt;{initial_pid}) {
    $self-&gt;{dbh} = DBI-&gt;connect("dbi:SQLite:dbname=".$self-&gt;{db},"","");
    $self-&gt;{get_sth} = $self-&gt;{dbh}-&gt;prepare("SELECT species, md5, item, matrix FROM consequences WHERE md5 = ?");

    # set this so only do once per fork
    $self-&gt;{initial_pid} = $$;
  }
  $self-&gt;{get_sth}-&gt;execute($md5);

  my $result_from_db = {};
  while (my $arrayref = $self-&gt;{get_sth}-&gt;fetchrow_arrayref) {
    # skip if species does not match
    my $species = $arrayref-&gt;[0];
    next unless $species eq $self-&gt;{species};

    my $item = $arrayref-&gt;[2];
    my $matrix = $arrayref-&gt;[3];

    # expand the compressed matrix
    my $expanded_matrix = expand_matrix($matrix);

    # position and peptide to retrieve value from matrix
    my $pos = $tva-&gt;transcript_variation-&gt;translation_start;
    my $peptide = $tva-&gt;peptide;

    next unless $peptide;

    # in matrix position is 0 indexed
    $pos--;

    # we need position of peptide in the ALL_AAS array
    my $peptide_number = (first_index { $_ eq $peptide } @ALL_AAS);

    # get matrix for each item value and parse them
    # motif
    if ($item eq "motif" &amp;&amp; $self-&gt;{motif}) {
      # get the item value for specific pos and amino acid from matrix
      my $tot_packed_len = 26;
      my $item_value = retrieve_item_value($expanded_matrix, $pos, $peptide_number, $tot_packed_len);

      if ($item_value){
        # parse the item value from matrix
        my ($elm, $lost) = parse_motif($item_value);

        # format the output
        if(defined $elm || defined $lost){
          my $data = $self-&gt;{extended} ? {
            elm   =&gt; $elm,
            lost  =&gt; $lost
          } : {
            lost  =&gt; $lost
          };

          my $formatted_output = $self-&gt;format_output($data, $item);
          @$result_from_db{ keys %$formatted_output } = values %$formatted_output;
        }
      }
    }
    # int and mod and exp
    elsif ($item eq "int" || $item eq "mod" || $item eq "exp") {
      if ($self-&gt;{$item}){
        # get the item value for specific pos and amino acid from matrix
        my $tot_packed_len = ($item eq "int") ? 42 : 40;
        my $item_value = retrieve_item_value($expanded_matrix, $pos, $peptide_number, $tot_packed_len);

        if ($item_value){
          # parse the item value from matrix
          my ($evidence, $dG_wt, $ddG, $dG_wt_sd, $dG_mt_sd, $ddG_sd);
          if ($item eq "int"){
            ($evidence, $dG_wt, $ddG, $dG_wt_sd, $dG_mt_sd, $ddG_sd) = parse_destabilizers($item_value, $item);
          }
          else{
            ($dG_wt, $ddG, $dG_wt_sd, $dG_mt_sd, $ddG_sd) = parse_destabilizers($item_value, $item);
          }

          # dG_mt can be calculated from dG_wt and ddG
          my $dG_mt = (defined $dG_wt &amp;&amp; defined $ddG) ? $dG_wt + $ddG : undef;

          # format the output
          if(defined $evidence || defined $dG_wt || defined $ddG || defined $dG_wt_sd || defined $dG_mt_sd || defined $ddG_sd){
            my $data = $self-&gt;{extended} ? {
              dG_wt   =&gt; $dG_wt,
              dG_mt  =&gt; $dG_mt,
              ddG   =&gt; $ddG,
              dG_wt_sd  =&gt; $dG_wt_sd,
              dG_mt_sd   =&gt; $dG_mt_sd,
              ddG_sd  =&gt; $ddG_sd
            } : {
              ddG   =&gt; $ddG
            };

            $data-&gt;{evidence} = $evidence if (defined $evidence &amp;&amp; $item eq "int" &amp;&amp; $self-&gt;{extended});

            my $formatted_output = $self-&gt;format_output($data, $item);
            @$result_from_db{ keys %$formatted_output } = values %$formatted_output;
          }
        }
      }
    }
  }

  return $result_from_db;
}

sub run {
  my ($self, $tva) = @_;

  my $result = {};

  my $hash_from_db = $self-&gt;process_from_db($tva);
  @$result{ keys %$hash_from_db } = values %$hash_from_db;

  return {} unless %$result;
  return $self-&gt;{output_json} ? {"mutfunc" =&gt; $result} : $result;
}

1;
</pre></body></html>