fixed a typo
[cview.git] / lib / CXGN / Cview / Map / SGN / Contig.pm
blobd28acbd7030501ac6efaa6f0df7950e5b21ea1b4
1 =head1 NAME
3 CXGN::Cview::Map::SGN::Contig - a class to generate maps of contigs aligned to the genome
5 =head1 SYNOPSYS
7 my $map = CXGN::Cview::Map::SGN::Contig->new($dbh, $id);
8 my $chr = $map->get_chromosome(1);
9 # etc...
11 =head1 DESCRIPTION
13 The FPC data is currently stored in a berkeleydb database. It is accessed using the following methods:
15 my $db = Bio::DB::GFF->new( -adaptor => 'berkeleydb',
16 -dsn => '/data/local/cxgn/core/sgn/documents/gbrowse/databases/fpc/SGN_2009');
18 and then you can do all the Bio::DB::GFF things with it, do 'perldoc Bio::DB::GFF' to see.
20 =head1 AUTHOR(S)
22 Lukas Mueller <lam87@cornell.edu>
24 =head1 FUNCTIONS
26 This class implements the following functions:
28 =cut
30 package CXGN::Cview::Map::SGN::Contig;
31 use strict;
33 use Bio::DB::GFF;
34 use File::Spec;
35 use CXGN::Cview::Map::SGN::Genetic;
36 use CXGN::Cview::Chromosome::Physical;
37 use CXGN::Cview::Marker::Physical;
40 use base qw | CXGN::Cview::Map::SGN::Genetic |;
42 =head2 function new
44 Synopsis:
45 Arguments: a database handle (preferably generated through
46 CXGN::DB::Connection) and a map id. Currently,
47 only one map_id is supported, with the alpha-
48 numeric id of "contig".
49 Returns:
50 Side effects:
51 Description:
53 =cut
55 sub new {
56 my $class = shift;
57 my $dbh = shift;
58 my $id = shift;
59 my $args = shift;
61 my $db_version_id = get_db_id($dbh, $id);
62 my $self = $class -> SUPER::new($dbh, $db_version_id);
64 # if (!defined($self)) { return undef; }
65 $self->set_preferred_chromosome_width(18);
66 $self->set_short_name($args->{short_name});
67 $self->set_long_name($args->{long_name});
68 $self->{gbrowse_fpc} = $args->{gbrowse_fpc};
69 $self->{temp_dir} = $args->{temp_dir} || '/tmp';
70 $self->set_abstract($args->{abstract});
71 $self->{marker_link} = $args->{marker_link};
72 $self->set_id($id);
76 return $self;
80 =head2 function get_chromosome()
82 Synopsis:
83 Arguments:
84 Returns:
85 Side effects:
86 Description:
88 =cut
90 sub get_chromosome {
91 my $self = shift;
92 my $chr_nr = shift;
94 my $map_factory = CXGN::Cview::MapFactory->new($self->get_dbh());
95 my $id = get_db_id($self->get_dbh(), $self->get_id());
96 #print STDERR "get_chromosome $id.\n";
97 my $genetic_map = $map_factory->create({map_version_id=>$id});
98 my $genetic=$genetic_map->get_chromosome($chr_nr);
99 my $chromosome = CXGN::Cview::Chromosome::Physical->new();
101 my $largest_offset = 0;
104 # my $gff = Bio::DB::GFF->new(
105 # -adaptor => 'berkeleydb',
106 # -dsn => $self->{berkeley_db_path},
107 # );
108 my (@gff) = $self->{gbrowse_fpc}->databases();
109 if (@gff > 1) { die "Can't deal with multiple databases right now..."; }
110 if (!@gff) { die "No database found!"; }
111 my ($gff) = @gff;
113 foreach my $m ($genetic->get_markers()) {
114 $m->set_chromosome($chromosome);
115 $chromosome->add_marker($m);
116 my $offset = $m->get_offset();
117 if ($offset > $largest_offset) {
118 $largest_offset=$offset;
120 $m->hide();
122 my @gff_markers = $gff->features(-method => 'marker',
123 -attributes => { Name => $m->get_name() },
125 my @contigs = ();
126 foreach my $gm (@gff_markers) {
127 @contigs = $gm->refseq();
129 my $count = 0;
130 foreach my $c (@contigs) {
131 my $contig = CXGN::Cview::Marker::Physical->new();
132 $contig->set_chromosome($chromosome);
133 $contig->set_name($c);
135 #my $url = "/gbrowse/gbrowse/sanger_tomato_fpc/?name=$c";
136 my $url = $self->{gbrowse_fpc}->view_url({ name => $c });
137 $contig->set_marker_name($c);
138 $contig->set_marker_type("contig");
139 $contig->set_url($url);
140 $contig->set_offset($m->get_offset());
141 $contig->get_label()->set_name($c);
142 $contig->get_label()->set_url($url);
143 $contig->set_tooltip("Contig: $c. Anchored to: ".($m->get_name()).".");
144 $chromosome -> add_marker($contig);
145 $count++;
148 $chromosome->set_length($largest_offset);
149 $self->{chr}->[$chr_nr]=$chromosome;
150 return $chromosome;
154 =head2 function get_overview_chromosome()
156 Synopsis:
157 Arguments:
158 Returns:
159 Side effects:
160 Description:
162 =cut
164 sub get_overview_chromosome {
165 my $self = shift;
166 my $chr_nr = shift;
168 my $bargraph = CXGN::Cview::Chromosome::BarGraph->new();
170 my $largest_offset = 0;
172 my $chromosome = $self->get_chromosome($chr_nr);
174 foreach my $m ($chromosome->get_markers()) {
175 if ($m->get_marker_type() eq "contig") {
176 my $offset = $m->get_offset();
177 $bargraph -> add_association("manual", $offset, 1);
178 if ($offset>$largest_offset) { $largest_offset = $offset; }
181 return $bargraph;
184 =head2 function get_chromosome_connections()
186 Synopsis:
187 Arguments:
188 Returns:
189 Side effects:
190 Description:
192 =cut
194 sub get_chromosome_connections {
195 my $self = shift;
196 my $chr_nr = shift;
197 my @list = ();
198 # this map has no connections.!!!!
199 # push @list, { map_version_id=>CXGN::Cview::Map::Tools::find_current_version($self->get_dbh(), CXGN::Cview::Map::Tools::current_tomato_map_id), lg_name=>$chr_nr, marker_count=>"?", short_name=>"F2-2000" };
200 return @list;
203 sub get_db_id {
204 my $dbh = shift;
205 my $id = shift;
206 my $db_id = $id;
207 $db_id=~s/^.*(\d+)$/$1/;
208 return CXGN::Cview::Map::Tools::find_current_version($dbh, $db_id);
212 sub can_zoom {
213 return 0;
216 sub get_marker_link {
217 my $self = shift;
218 my $clone_id= shift;
219 if ($clone_id) { return ""; }
220 else { return ""; }
223 sub get_marker_count {
224 my $self = shift;
225 my $chr_nr = shift;
227 my $tmp_file = "";
229 my @lengths = $self->cache_marker_counts();
230 return $lengths[$chr_nr-1];
233 sub cache_marker_counts {
234 my $self = shift;
236 my @lengths = ();
237 my $temp_file = File::Spec->catfile($self->{temp_dir}, 'contig'.$self->get_id()."_marker_counts.txt");
239 if (! -e ($temp_file)) {
240 open(my $TEMP, ">$temp_file") || die "Can't open $temp_file for writing.";
241 for my $c (1..12) {
242 my $count = 0;
243 my $chr = $self->get_chromosome($c);
244 foreach my $m ($chr->get_markers()) {
245 if ($m->get_marker_name()=~ /^ctg/) {
246 $count++;
249 print $TEMP "$c\t$count\n";
254 else {
255 open(my $TEMP, "<$temp_file") || die "Can't open $temp_file for reading.";
256 while (<$TEMP>) {
257 chomp;
258 my ($c, $length) = split /\t/;
259 push @lengths, $length;
263 return @lengths;
266 sub get_map_stats {
267 my $self = shift;
269 my $count = 0;
270 foreach my $c (1..12) {
271 $count += $self->get_marker_count($c);
274 return "$count contigs have been assigned to this map";
277 =head2 get_abstract
279 Usage:
280 Desc:
281 Ret:
282 Args:
283 Side Effects:
284 Example:
286 =cut