check_mapping.pl 8.63 KB
Newer Older
1
#!/usr/bin/env perl
2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright [1999-2013] Wellcome Trust Sanger Institute and the 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.

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

=head1 NAME

check_mapping.pl - script to check whole genome alignment between two
assemblies.

=head1 SYNOPSIS

check_mapping.pl [arguments]

Required arguments:

  --dbname, db_name=NAME              database name NAME
  --host, --dbhost, --db_host=HOST    database host HOST
  --port, --dbport, --db_port=PORT    database port PORT
  --user, --dbuser, --db_user=USER    database username USER
  --pass, --dbpass, --db_pass=PASS    database passwort PASS
  --assembly=ASSEMBLY                 assembly version ASSEMBLY

  --altdbname=NAME                    alternative database NAME
  --altassembly=ASSEMBLY              alternative assembly version ASSEMBLY

Kieron Taylor's avatar
Kieron Taylor committed
38 39 40 41
  --nolog                             outside of a webserver environment, 
                                      logging needs to be handled manually.
                                      Use in conjunction with shell redirects.

42 43 44 45 46 47 48
Optional arguments:

  --althost=HOST                      alternative databases host HOST
  --altport=PORT                      alternative database port PORT
  --altuser=USER                      alternative database username USER
  --altpass=PASS                      alternative database password PASS

49
  --chromosomes, --chr=LIST           only process LIST toplevel seq_regions
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

  --conffile, --conf=FILE             read parameters from FILE
                                      (default: conf/Conversion.ini)

  --logfile, --log=FILE               log to FILE (default: *STDOUT)
  --logpath=PATH                      write logfile to PATH (default: .)
  --logappend, --log_append           append to logfile (default: truncate)

  -v, --verbose=0|1                   verbose logging (default: false)
  -i, --interactive=0|1               run script interactively (default: true)
  -n, --dry_run, --dry=0|1            don't write results to database
  -h, --help, -?                      print help (this message)

=head1 DESCRIPTION

This script checks if the whole genome alignment between two assemblies is
correct. It does so by comparing the sequence in the reference database with
the sequence of the projected fragments in the alternative database.

=head1 RELATED FILES

The whole process of creating a whole genome alignment between two assemblies
is done by a series of scripts. Please see

  ensembl/misc-scripts/assembly/README

for a high-level description of this process, and POD in the individual scripts
for the details.


=head1 CONTACT

Please post comments/questions to the Ensembl development list
Magali Ruffier's avatar
Magali Ruffier committed
83
<http://lists.ensembl.org/mailman/listinfo/dev>
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

=cut

use strict;
use warnings;
no warnings 'uninitialized';

use FindBin qw($Bin);
use vars qw($SERVERROOT);

BEGIN {
    $SERVERROOT = "$Bin/../../..";
    unshift(@INC, "$SERVERROOT/ensembl/modules");
    unshift(@INC, "$SERVERROOT/bioperl-live");
}

use Getopt::Long;
use Pod::Usage;
use Bio::EnsEMBL::Utils::ConversionSupport;

Kieron Taylor's avatar
Kieron Taylor committed
104 105
use Algorithm::Diff qw(diff);

106 107 108 109 110 111 112 113 114 115 116 117
$| = 1;

my $support = new Bio::EnsEMBL::Utils::ConversionSupport($SERVERROOT);

# parse options
$support->parse_common_options(@_);
$support->parse_extra_options(
    'assembly=s',
    'altdbname=s',
    'altassembly=s',
    'althost=s',
    'altport=n',
118
    'altpass=s',
119 120 121 122 123 124 125 126 127
    'chromosomes|chr=s@',
);
$support->allowed_params(
    $support->get_common_params,
    'assembly',
    'altdbname',
    'altassembly',
    'althost',
    'altport',
128
    'altpass',
129 130 131 132 133 134 135 136 137 138 139
    'chromosomes',
);

if ($support->param('help') or $support->error) {
    warn $support->error if $support->error;
    pod2usage(1);
}

$support->comma_to_list('chromosomes');

# ask user to confirm parameters to proceed
140
#$support->confirm_params;
141 142 143 144 145 146 147 148 149 150 151 152

# get log filehandle and print heading and parameters to logfile
$support->init_log;

$support->check_required_params(
    'assembly',
    'altdbname',
    'altassembly'
);

# first set connection parameters for alternative db if not different from
# reference db
153
map { $support->param("alt$_", $support->param($_)) unless ($support->param("alt$_")) } qw(host port user);
154 155 156 157 158 159 160 161 162

# reference database
my $R_dba = $support->get_database('ensembl');
my $R_sa = $R_dba->get_SliceAdaptor;

# database containing the alternative assembly
my $A_dba = $support->get_database('core', 'alt');
my $A_sa = $A_dba->get_SliceAdaptor;

163
$support->log("Looping over toplevel seq_regions...\n\n");
164

Kieron Taylor's avatar
Kieron Taylor committed
165 166
my @global_diff_bins;

167 168
foreach my $chr ($support->sort_chromosomes) {
  $support->log_stamped("Toplevel seq_region $chr...\n", 1);
169

170 171 172 173 174 175 176 177 178
  my $R_slice = $R_sa->fetch_by_region('toplevel', $chr);
  my $A_slice = $A_sa->fetch_by_region('toplevel', $chr);

  unless ($A_slice) {
    $support->log("Not found in alternative db. Skipping.\n", 2);
    next;
  }
  
  my $cs_name = $A_slice->coord_system_name;
179 180

  # compare reference and alternative sequence
181
  my @segments = @{ $R_slice->project($cs_name, $support->param('altassembly')) };
182 183
  
  my $i;
184
  my $k;
185 186 187 188 189 190 191 192

  foreach my $seg (@segments) {
    # reference sequence
    my $R_sub_slice = $R_slice->sub_Slice($seg->from_start, $seg->from_end);
    my $R_seq = $R_sub_slice->seq;
    
    # alternative sequence
    my $A_proj_slice = $seg->to_Slice;
193 194 195 196
    
    # ignore PAR region (i.e. we project to the symlinked seq_region)
    next if ($A_proj_slice->seq_region_name ne $chr);
    
197 198 199 200 201 202 203 204 205 206
    my $A_sub_slice = $A_slice->sub_Slice($A_proj_slice->start, $A_proj_slice->end, $A_proj_slice->strand);
    my $A_seq = $A_sub_slice->seq;

    # compare
    if ($R_seq eq $A_seq) {
      # sequences are identical -> ok
      $support->log_verbose("Sequence match at ".$R_sub_slice->name."\n", 2);

    } else {
      # not identical -> something is wrong
207
      $support->log("Sequence mismatch at ".$R_sub_slice->name."\n", 2);
208 209 210 211 212 213 214 215 216 217 218 219 220

      my $R_sub_seq;
      my $A_sub_seq;
      
      if ($R_sub_slice->length > 20) {
        $R_sub_seq = substr($R_seq, 0, 10)."...".substr($R_seq, -10, 10);
        $A_sub_seq = substr($A_seq, 0, 10)."...".substr($A_seq, -10, 10);
      } else {
        $R_sub_seq = substr($R_seq, 0, 20);
        $A_sub_seq = substr($A_seq, 0, 20);
      }
      
      $support->log("Ref: $R_sub_seq\n", 3);
221
      $support->log("Alt: $A_sub_seq\n\n", 3);
222 223

      $i++;
Kieron Taylor's avatar
Kieron Taylor committed
224
      
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
      
      if (length($R_seq) == length($A_seq)){
          #my $diffs = ($R_seq ^ $A_seq) =~ tr/\0//c;  # A concatenation of differences
          # this approach is x10 faster than relying on Algorithm::Diff, as long as there
          # are no InDels, and the lengths are comparable.
          my $mask = ($R_seq ^ $A_seq);
          my @diffs = split (//,$mask);
          my ($in_change,$change_start,$change_end);
          for (my $x=0; $x<scalar(@diffs); $x++) {
              if ($in_change) {
                  if ($diffs[$x] eq "\0") {
                      $in_change = 0;
                      $change_end = $x;
                      
                      my $length = $change_end - $change_start + 1;
                      $global_diff_bins[$length]++;
                  }
                  else {
                      next;
                  }                  
              } elsif ($diffs[$x] ne "\0") {
                  $in_change = 1;
                  $change_start = $x;
              }
              
          }
      } else {
          my @Ref = split(//,$R_seq);
          my @Alt = split(//,$A_seq);
          my @diffs = diff( \@Ref, \@Alt );
          foreach (@diffs) {
              my $length = 0;
              foreach my $desc (@{$_}) {
                  if ($desc->[0] eq '+') {$length++;}
                  if ($desc->[0] eq '-') {$length--;}
              };
              $global_diff_bins[$length]++;
          }
      }
      
265
    }
266 267

    $k++;
268 269 270
  }

  if ($i) {
271
    $support->log("Total: $i (of $k) alignments contain sequence mismatches.\n", 2);
272
  } else {
273
    $support->log("All $k alignments ok.\n", 2);
274
  }
275 276 277 278

  $support->log_stamped("Done.\n\n", 1);
}

Kieron Taylor's avatar
Kieron Taylor committed
279 280 281 282 283 284 285 286
$support->log("Summary of changes across all chromosomes\n\n",1);
$support->log("|Bin  |Frequency\n",2);
for (my $bin = 0; $bin < scalar(@global_diff_bins); $bin++) {
    if (defined ($global_diff_bins[$bin])) {
        $support->log("|$bin  |$global_diff_bins[$bin]\n");
    }
}

287 288 289
# finish logfile
$support->finish_log;