Merge branch 'master' of github.com:solgenomics/sgn
[sgn.git] / lib / CXGN / Cview / Map / SGN / Contig.pm
blob4c784e47c2aa6f1e88aa800f8dbef521aa68abe9
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 AUTHOR(S)
13 Lukas Mueller <lam87@cornell.edu>
15 =head1 FUNCTIONS
17 This class implements the following functions:
19 =cut
21 package CXGN::Cview::Map::SGN::Contig;
22 use strict;
23 use warnings;
25 use File::Spec;
26 use CXGN::Cview::MapFactory;
27 use CXGN::Cview::Map::SGN::Genetic;
28 use CXGN::Cview::Chromosome::Physical;
29 use CXGN::Cview::Marker::Physical;
31 use CatalystX::GlobalContext '$c';
33 use base qw | CXGN::Cview::Map::SGN::Genetic |;
35 =head2 function new
37 Synopsis:
38 Arguments: a database handle (preferably generated through
39 CXGN::DB::Connection) and a map id. Currently,
40 only one map_id is supported, with the alpha-
41 numeric id of "contig".
42 Returns:
43 Side effects:
44 Description:
46 =cut
48 sub new {
49 my $class = shift;
50 my $dbh = shift;
51 my $id = shift;
52 my $args = shift;
54 my $db_version_id = get_db_id($dbh, $id);
55 my $self = $class -> SUPER::new($dbh, $db_version_id);
57 $self->set_preferred_chromosome_width(18);
58 $self->set_short_name($args->{short_name});
59 $self->set_long_name($args->{long_name});
60 $self->{gbrowse_fpc} = $args->{gbrowse_fpc};
61 $self->{temp_dir} = $args->{temp_dir} || '/tmp';
62 $self->set_abstract($args->{abstract});
63 $self->{marker_link} = $args->{marker_link};
64 $self->set_id($id);
66 return $self;
70 =head2 function get_chromosome()
72 Synopsis:
73 Arguments:
74 Returns:
75 Side effects:
76 Description:
78 =cut
80 sub get_chromosome {
81 my $self = shift;
82 my $chr_nr = shift;
84 my $map_factory = CXGN::Cview::MapFactory->new($self->get_dbh());
85 my $id = get_db_id($self->get_dbh(), $self->get_id());
86 my $genetic_map = $map_factory->create({map_version_id=>$id});
87 my $genetic=$genetic_map->get_chromosome($chr_nr);
88 my $chromosome = CXGN::Cview::Chromosome::Physical->new();
90 my $largest_offset = 0;
92 my @gff = $self->{gbrowse_fpc}->databases()
93 or $c->throw(
94 public_message => "Map not found.",
95 developer_message => 'No gbrowse_fpc databases found',
96 notify => 1,
97 is_server_error => 0,
98 is_client_error => 0,
101 if ( @gff > 1 ) { die "Can't deal with multiple databases right now..." }
103 my ($gff) = @gff;
105 for my $m ($genetic->get_markers()) {
106 $m->set_chromosome($chromosome);
107 $chromosome->add_marker($m);
108 my $offset = $m->get_offset();
109 if ($offset > $largest_offset) {
110 $largest_offset=$offset;
112 $m->hide();
114 my @gff_markers = $gff->features(-method => 'marker',
115 -attributes => { Name => $m->get_name() },
117 my @contigs = ();
118 for my $gm (@gff_markers) {
119 @contigs = $gm->refseq();
121 my $count = 0;
122 for my $c (@contigs) {
123 my $contig = CXGN::Cview::Marker::Physical->new();
124 $contig->set_chromosome($chromosome);
125 $contig->set_name($c);
127 #my $url = "/gbrowse/gbrowse/sanger_tomato_fpc/?name=$c";
128 my $url = $self->{gbrowse_fpc}->view_url({ name => $c });
129 $contig->set_marker_name($c);
130 $contig->set_marker_type("contig");
131 $contig->set_url($url);
132 $contig->set_offset($m->get_offset());
133 $contig->get_label()->set_name($c);
134 $contig->get_label()->set_url($url);
135 $contig->set_tooltip("Contig: $c. Anchored to: ".($m->get_name()).".");
136 $chromosome -> add_marker($contig);
137 $count++;
140 $chromosome->set_length($largest_offset);
141 $self->{chr}->[$chr_nr]=$chromosome;
142 return $chromosome;
146 =head2 function get_overview_chromosome()
148 Synopsis:
149 Arguments:
150 Returns:
151 Side effects:
152 Description:
154 =cut
156 sub get_overview_chromosome {
157 my $self = shift;
158 my $chr_nr = shift;
160 my $bargraph = CXGN::Cview::Chromosome::BarGraph->new();
162 my $largest_offset = 0;
164 my $chromosome = $self->get_chromosome($chr_nr);
166 for my $m ($chromosome->get_markers()) {
167 if ($m->get_marker_type() eq "contig") {
168 my $offset = $m->get_offset();
169 $bargraph -> add_association("manual", $offset, 1);
170 if ($offset>$largest_offset) { $largest_offset = $offset; }
173 return $bargraph;
176 =head2 function get_chromosome_connections()
178 Synopsis:
179 Arguments:
180 Returns:
181 Side effects:
182 Description:
184 =cut
186 sub get_chromosome_connections {
187 my $self = shift;
188 my $chr_nr = shift;
189 my @list = ();
190 # this map has no connections.!!!!
191 # 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" };
192 return @list;
195 sub get_db_id {
196 my $dbh = shift;
197 my $id = shift;
198 my $db_id = $id;
199 $db_id=~s/^.*(\d+)$/$1/;
200 return CXGN::Cview::Map::Tools::find_current_version($dbh, $db_id);
204 sub can_zoom {
205 return 0;
208 sub get_marker_link {
209 my $self = shift;
210 my $clone_id= shift;
211 if ($clone_id) { return ""; }
212 else { return ""; }
215 sub get_marker_count {
216 my $self = shift;
217 my $chr_nr = shift;
219 my $tmp_file = "";
221 my @lengths = $self->cache_marker_counts();
222 return $lengths[$chr_nr-1];
225 sub cache_marker_counts {
226 my $self = shift;
228 my @lengths = ();
229 my $temp_file = File::Spec->catfile($self->{temp_dir}, 'contig'.$self->get_id()."_marker_counts.txt");
231 if (! -e ($temp_file)) {
232 open(my $TEMP, '>', $temp_file) or die "Can't open $temp_file for writing: $!";
233 for my $c (1..12) {
234 my $count = 0;
235 my $chr = $self->get_chromosome($c);
236 for my $m ($chr->get_markers()) {
237 if ($m->get_marker_name()=~ /^ctg/) {
238 $count++;
241 print $TEMP "$c\t$count\n";
246 else {
247 open(my $TEMP, '<', $temp_file) or die "Can't open $temp_file for reading: $!";
248 while (<$TEMP>) {
249 chomp;
250 my ($c, $length) = split /\t/;
251 push @lengths, $length;
255 return @lengths;
258 sub get_map_stats {
259 my $self = shift;
261 my $count = 0;
262 for my $c (1..12) {
263 $count += $self->get_marker_count($c);
266 return "$count contigs have been assigned to this map";
269 =head2 get_abstract
271 Usage:
272 Desc:
273 Ret:
274 Args:
275 Side Effects:
276 Example:
278 =cut