KaryotypeBandAdaptor.pm 10.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
44
45
#
# Ensembl module for Bio::EnsEMBL::DBSQL::KaryotypeBandAdaptor
#
# Cared for by James Stalker <jws@sanger.ac.uk>
#
# Copyright James Stalker
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::EnsEMBL::DBSQL::KaryotypeBandAdaptor

=head1 SYNOPSIS

Give standard usage here

=head1 DESCRIPTION

Database adaptor to provide access to KaryotypeBand objects

=head1 AUTHOR

James Stalker

This modules is part of the Ensembl project http://www.ensembl.org

=head1 CONTACT

Email jws@sanger.ac.uk

=head1 APPENDIX

The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _

=cut


# Let the code begin...


package Bio::EnsEMBL::DBSQL::KaryotypeBandAdaptor;
use vars qw(@ISA);
Web Admin's avatar
Web Admin committed
46
use Bio::EnsEMBL::KaryotypeBand;
47
48
49
50
51
52
53
54
55
56
57
use strict;

# Object preamble - inherits from Bio::Root::RootI

use Bio::EnsEMBL::DBSQL::BaseAdaptor;

@ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);

# inherit new from BaseAdaptor


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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
=head2 fetch_by_chromosome_start_end

 Title   : fetch_by_chromosome_start_end
 Usage   : $band_obj = $kary_adp->fetch_by_chromosome_position('chr1',10000, 2000);
 Function: Retrieves KaryotypeBand objects by chromosome ('chrN' notation)
           and its absolute "golden-path" start/end on that chromosome.
 Example :
 Returns : A KaryotypeBand object list
 Args    : Chromosome id (chrN notation) and start, end in absolute basepairs

=cut

sub fetch_by_chromosome_start_end{
    my ($self,$chr,$start,$end) = @_;

    $self->throw("Need both chromosome and start/end") unless (defined $start && defined $end);

    my $sth = $self->prepare("	SELECT	chr_start,
					chr_end,
					band,
					stain
				FROM	karyotype 
				WHERE	chr_name = '$chr'
				AND	$start <= chr_end 
				AND	$end > chr_start 
			     ");

    $sth->execute;
	my @bands = ();
	my ($chr_start,$chr_end,$band,$stain) = ();
		
    while (($chr_start,$chr_end,$band,$stain) = $sth->fetchrow_array()){
    	last unless defined $band;
    	my $band_obj = Bio::EnsEMBL::KaryotypeBand->new();
    	$band_obj->name($band);
    	$band_obj->chromosome($chr);
    	$band_obj->start($chr_start);
    	$band_obj->end($chr_end);
    	$band_obj->stain($stain);
		#print STDERR "Kary Get: $chr_start,$chr_end,$band,$stain\n";
		push (@bands, $band_obj);
	}

    return @bands;
}


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
=head2 fetch_by_chromosome_position

 Title   : fetch_by_chromosome_position
 Usage   : $band_obj = $kary_adp->fetch_by_chromosome_position('chr1',10000);
 Function: Retrieves a KaryotypeBand obj by its chromosome ('chrN' notation)
           and its absolute "golden-path" position on that chromosome.
 Example :
 Returns : A KaryotypeBand object
 Args    : Chromosome id (chrN notation) and position in absolute basepairs

=cut

sub fetch_by_chromosome_position{
    my ($self,$chr,$position) = @_;

    $self->throw("Need both chromosome and position") unless defined $position;

    my $sth = $self->prepare("	SELECT	chr_start,
					chr_end,
					band,
					stain
				FROM	karyotype 
				WHERE	chr_name = '$chr'
				AND	$position <= chr_end 
				AND	$position > chr_start 
			     ");

    $sth->execute;
133
134
	
	my ($chr_start,$chr_end,$band,$stain)  = $sth->fetchrow_array();
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
162
163
164
165
    return undef unless defined $band;

    my $band_obj = Bio::EnsEMBL::KaryotypeBand->new();
    $band_obj->name($band);
    $band_obj->chromosome($chr);
    $band_obj->start($chr_start);
    $band_obj->end($chr_end);
    $band_obj->stain($stain);

    return $band_obj;
}



=head2 fetch_by_chromosome_name

 Title   : fetch_by_chromosome_name
 Usage   : $band_obj = $kary_adp->fetch_by_chromosome_name('chr1','q23.1');
 Function: Retrieves a KaryotypeBand obj by its chromosome ('chrN' notation)
           and its band name (e.g. 'q23.1').
 Example :
 Returns : A KaryotypeBand object
 Args    : Chromosome id (chrN notation) and band name 

=cut

sub fetch_by_chromosome_name{
    my ($self,$chr,$name) = @_;

    $self->throw("Need both chromosome and name") unless defined $name;

166
167
168
169
170
171
    my $sth = $self->prepare(
        "select	chr_start, chr_end, stain
           from karyotype 
          where chr_name = ? and band = ?"
    );
    $sth->execute( $chr, $name );
172
173
174

    my ($chr_start,$chr_end,$stain) = $sth->fetchrow_array;

Web Admin's avatar
Web Admin committed
175
    return undef unless defined $chr_start;
176
177
178
179
180
181
182
183

    my $band_obj = Bio::EnsEMBL::KaryotypeBand->new();
    $band_obj->name($name);
    $band_obj->chromosome($chr);
    $band_obj->start($chr_start);
    $band_obj->end($chr_end);
    $band_obj->stain($stain);

184
185
186
187
188
189
190
191
192
    return $band_obj;
}

=head2 fetch_by_chromosome_name_virtual

 Title   : fetch_by_chromosome_name_virtual
 Usage   : $band_obj = $kary_adp->fetch_by_chromosome_name('chr1','q23.1');
 Function: Retrieves a KaryotypeBand obj by its chromosome ('chrN' notation)
           and its band name (e.g. 'q23.1'). { can also use a non-fully declared band:
193
	   e.g. q23, q23.3
194
 Example :
195
196
197
 Returns : A KaryotypeBand object BUT DOES NOT RETURN STAIN INFORMATION!!!!!
         : (as we can not get this from the information we retrieve)
 Args    : Chromosome id (chrN notation) and (partial?) band name 
198
199
200

=cut

201
sub fetch_by_chromosome_name_virtual {
202
203
204
205
206
    my ($self,$chr,$name) = @_;

    $self->throw("Need both chromosome and name") unless defined $name;

    my $sth;
207
208
209
210
211
212
213
    $sth = $self->prepare(
        "select	chr_start, chr_end
           from karyotype 
          where chr_name = ? and band = ?"
    );
    $sth->execute( $chr, $name );
    my ( $chr_start, $chr_end ) = $sth->fetchrow_array;
214
215

	unless($chr_start) {
216
217
218
219
220
	    $sth = $self->prepare(
            "select min(chr_start), max(chr_end)
               from karyotype 
              where chr_name = '$chr' and band like '$name\%'"
        );
221
	    $sth->execute;
222
	    ( $chr_start, $chr_end ) = $sth->fetchrow_array;
223
224
225
226
227
228
229
230
231
232
233
	}

    return undef unless defined $chr_start;

    my $band_obj = Bio::EnsEMBL::KaryotypeBand->new();
    $band_obj->name($name);
    $band_obj->chromosome($chr);
    $band_obj->start($chr_start);
    $band_obj->end($chr_end);
    $band_obj->stain('white');

234
235
    return $band_obj;
}
236
=head2 fetch_by_name
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
 Title   : fetch_by_name
 Usage   : $band_obj = $kary_adp->fetch_by_chromosome_name('q23.1');
 Function: Retrieves a KaryotypeBand obj by its band name (e.g. 'q23.1').
 Example :
 Returns : A KaryotypeBand object
 Args    : Band name 

=cut


sub fetch_by_name{
    my ($self,$chr,$name) = @_;

    $self->throw("Need both name") unless defined $name;

    my $sth = $self->prepare(
        "select	chr_name, chr_start, chr_end, stain
           from karyotype 
          where band = ?"
    );
    $sth->execute( $name );

    my ($chr, $chr_start,$chr_end,$stain) = $sth->fetchrow_array;

    return undef unless defined $chr_start;

    my $band_obj = Bio::EnsEMBL::KaryotypeBand->new();
    $band_obj->name($name);
    $band_obj->chromosome($chr);
    $band_obj->start($chr_start);
    $band_obj->end($chr_end);
    $band_obj->stain($stain);

    return $band_obj;
}

=head2 fetch_by_name_virtual

 Title   : fetch_by_name_virtual
 Usage   : $band_obj = $kary_adp->fetch_by_name('q23.1');
 Function: Retrieves a KaryotypeBand obj by its 
           and its band name (e.g. 'q23.1'). { can also use a non-fully declared band:
	   e.g. q23, q23.3 }
 Example :
 Returns : A KaryotypeBand object BUT DOES NOT RETURN STAIN INFORMATION!!!!!
         : (as we can not get this from the information we retrieve)
 Args    : (partial?) band name 

=cut

sub fetch_by_name_virtual {
    my ($self,$chr,$name) = @_;

    $self->throw("Need both chromosome and name") unless defined $name;

    my $sth;
    $sth = $self->prepare(
        "select	chr_name, chr_name, chr_start, chr_end
           from karyotype 
          where band = ?"
    );
    $sth->execute( $name );
    my ( $chr, $chr_start, $chr_end ) = $sth->fetchrow_array;

	unless($chr_start) {
	    $sth = $self->prepare(
            "select chr_name, min(chr_start), max(chr_end)
               from karyotype 
              where band like '$name\%'"
        );
	    $sth->execute;
	    ( $chr, $chr_start, $chr_end ) = $sth->fetchrow_array;
	}

    return undef unless defined $chr_start;

    my $band_obj = Bio::EnsEMBL::KaryotypeBand->new();
    $band_obj->name($name);
    $band_obj->chromosome($chr);
    $band_obj->start($chr_start);
    $band_obj->end($chr_end);
    $band_obj->stain('white');

    return $band_obj;
}
323

324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
=head2 fetch_chromosome_length

 Title   : fetch_chromosome_length
 Usage   : 
 Function: Returns length of a chromosome ('chrN' notation)
 Example :
 Returns : SV
 Args    : Chromosome id (chrN notation) 

=cut

sub fetch_chromosome_length {
    my ($self,$chr) = @_;

    $self->throw("Need a chromosome") unless defined $chr;

	# return a cached copy of the chromosome bands
341
342
	if (exists $self->{"_karyotype_band_cache_$chr"}){
		my @tmp = @{$self->{"_karyotype_band_cache_$chr"}};
343
344
345
346
347
348
349
350
351
352
353
354
355
356
		return $tmp[-1]->end();
	}

    my $sth = $self->prepare("	SELECT
											max(chr_end)
								FROM		karyotype 
								WHERE		chr_name = '$chr' 
			     			");

    $sth->execute;
    my ($chr_end) = $sth->fetchrow_array();
	return($chr_end);

}
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374


=head2 fetch_all_by_chromosome

 Title   : fetch_all_by_chromosome
 Usage   : @band_obj = $kary_adp->fetch_all_by_chromosome('chr1');
 Function: Retrieves all KaryotypeBand objects on a chromosome ('chrN' notation)
 Example :
 Returns : An array of KaryotypeBand objects
 Args    : Chromosome id (chrN notation) 

=cut

sub fetch_all_by_chromosome{
    my ($self,$chr) = @_;

    $self->throw("Need a chromosome") unless defined $chr;

375
    # return a cached copy of the chromosome bands
376
377
    if (exists $self->{"_karyotype_band_cache_$chr"}){
 	return(@{$self->{"_karyotype_band_cache_$chr"}});
378
    }
379

380
381
382
383
384
385
    my $sth = $self->prepare("	SELECT	chr_start,
					chr_end,
					band,
					stain
				FROM	karyotype 
				WHERE	chr_name = '$chr' 
Web Admin's avatar
Web Admin committed
386
				ORDER BY chr_start
387
388
389
390
			     ");

    $sth->execute;
    my ($chr_start,$chr_end,$band,$stain);
391
    $sth->bind_columns(undef,\$chr_start,\$chr_end,\$band,\$stain);
392
393
394
395
396
    
    my @bands;

    while ($sth->fetch()){
	my $band_obj = Bio::EnsEMBL::KaryotypeBand->new();
Web Admin's avatar
Web Admin committed
397
	$band_obj->name($band);
398
399
400
401
402
403
404
405
	$band_obj->chromosome($chr);
	$band_obj->start($chr_start);
	$band_obj->end($chr_end);
	$band_obj->stain($stain);

	push @bands,$band_obj;
    }

406
	# save a copy in the local cache
407
	$self->{"_karyotype_band_cache_$chr"} = \@bands;
408

409
410
411
412
    return @bands;
}

1;