compare_assemblies.pl 7.82 KB
Newer Older
1
#!/usr/bin/env perl
2
# Copyright [1999-2016] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
3 4 5 6 7 8 9 10 11 12 13 14 15
# 
# 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 38 39 40 41 42 43

=head1 NAME

compare_assemblies.pl - compare two assemblies

=head1 SYNOPSIS

compare_assemblies.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

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

44
  --chromosomes, --chr=LIST           only process LIST toplevel seq_regions
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

  --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 compares two assemblies. At the moment all it does is to list
61
clones that are on different toplevel seq_regions in the two assemblies.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

=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 AUTHOR

Patrick Meidl <meidl@ebi.ac.uk>, Ensembl core API team

=head1 CONTACT

Please post comments/questions to the Ensembl development list
Magali Ruffier's avatar
Magali Ruffier committed
81
<http://lists.ensembl.org/mailman/listinfo/dev>
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

=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;

$| = 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',
    'chromosomes|chr=s@',
);
$support->allowed_params(
    $support->get_common_params,
    'assembly',
    'altdbname',
    'altassembly',
    'althost',
    'altport',
    '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
$support->confirm_params;

# 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
map { $support->param("alt$_", $support->param($_)) unless ($support->param("alt$_")) } qw(host port user pass);

# 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;

my $fmt1 = "%-20s%-12s%-12s\n";
my $fmt2 = "%-35s%10.0f\n";
my @diff_total;
my %stats_total;

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

164
foreach my $R_chr ($support->sort_chromosomes($support->get_chrlength)) {
165 166
    $support->log_stamped("\nChromosome $R_chr...\n", 1);

167 168
    # fetch toplevel seq_region slice and project to clones
    my $R_slice = $R_sa->fetch_by_region('toplevel', $R_chr);
169 170 171 172 173 174 175 176 177 178
    my @R_clones = @{ $R_slice->project('clone') };

    # loop over reference clones
    my @diff;
    my %stats;
    foreach my $R_seg (@R_clones) {
        $stats{'num_clones'}++;
        my $R_clone = $R_seg->to_Slice;
        my $R_clone_name = $R_clone->seq_region_name;

179
        # fetch clone from alternative db and project to toplevel seq_region
180 181
        my $A_clone = $A_sa->fetch_by_region('clone', $R_clone_name);
        if ($A_clone) {
182
            my ($A_seg) = @{ $A_clone->project('toplevel') };
183 184 185
            if ($A_seg) {
                my $A_slice = $A_seg->to_Slice;

186
                # compare toplevel seq_region names
187 188 189 190 191 192
                my $A_chr = $A_slice->seq_region_name;
                unless ($R_chr eq $A_chr) {
                    push @diff, [$R_clone_name, $R_chr, $A_chr];
                }
            } else {
                $stats{'does_not_project'}++;
193
                $support->log_verbose("Clone $R_clone_name doesn't project to toplevel seq_region.\n", 2);
194 195 196 197 198 199 200 201 202 203
            }
        } else {
            $stats{'not_in_alt'}++;
            $support->log_verbose("Clone $R_clone_name not in alternative db.\n", 2);
        }
    }
    push @diff_total, @diff;
    map { $stats_total{$_} += $stats{$_} }
        qw(num_clones does_not_project not_in_alt);

204 205
    # print results for this toplevel seq_region
    $support->log("\nStats for toplevel seq_region $R_chr:\n\n", 2);
206 207 208 209
    $support->log(sprintf($fmt2, "Total number of clones:", $stats{'num_clones'}), 3);
    $support->log(sprintf($fmt2, "Clones not in alternative db:", $stats{'not_in_alt'}), 3);
    $support->log(sprintf($fmt2, "Clones not in alternative assembly:", $stats{'does_not_project'}), 3);
    if (@diff) {
210
        $support->log("\nClones on different toplevel seq_regions in the 2 assemblies:\n\n", 3);
211 212 213 214 215 216
        $support->log(sprintf($fmt1, qw(CLONE R_CHR A_CHR)), 4);
        $support->log(('-'x44)."\n", 4);
        foreach my $d (@diff) {
            $support->log(sprintf($fmt1, @{ $d }), 4);
        }
    } else {
217
        $support->log("\nAll matching clones on same toplevel seq_region in the 2 assemblies.\n", 3);
218 219 220
    }
}

Patrick Meidl's avatar
Patrick Meidl committed
221
$support->log_stamped("Done.\n\n");
222 223 224 225 226 227 228

# print overall results
$support->log("\nOverall stats:\n");
$support->log(sprintf($fmt2, "Total number of clones:", $stats_total{'num_clones'}), 1);
$support->log(sprintf($fmt2, "Clones not in alternative db:", $stats_total{'not_in_alt'}), 1);
$support->log(sprintf($fmt2, "Clones not in alternative assembly:", $stats_total{'does_not_project'}), 1);
if (@diff_total) {
229
    $support->log("\nClones on different toplevel seq_regions in the 2 assemblies:\n\n", 1);
230 231 232 233 234 235
    $support->log(sprintf($fmt1, qw(CLONE R_CHR A_CHR)), 2);
    $support->log(('-'x44)."\n", 2);
    foreach my $d (@diff_total) {
        $support->log(sprintf($fmt1, @{ $d }), 2);
    }
} else {
236
    $support->log("\nAll clones on same toplevel seq_region in the 2 assemblies.\n", 2);
237 238 239 240 241 242
}
$support->log("\n");

# finish logfile
$support->finish_log;