add option to link by marker name instead of marker id (to link to GBrowse etc.)
[cview.git] / lib / CXGN / Cview / MapFactory / Cacao.pm
blob8786615fa09153acf715b3d90dfe0604ec61d4d8
2 =head1 NAME
4 CXGN::Cview::MapFactory - a factory object for CXGN::Cview::Map objects
6 =head1 SYNOPSYS
8 my $map_factory = CXGN::Cview::MapFactory->new($dbh);
9 $map = $map_factory->create({map_version_id=>"u1"});
11 =head1 DESCRIPTION
13 see L<CXGN::Cview::MapFactory>.
15 The MapFactory constructor takes a database handle (preferably constructed using CXGN::DB::Connection object). The map objects can then be constructed using the create function, which takes a hashref as a parameter, containing either map_id or map_version_id as a key (but not both). map_ids will be converted to map_version_ids immediately. Map_version_ids are then analyzed and depending on its format, CXGN::Cview::Map object of the proper type is returned.
17 The function get_all_maps returns all maps as list of appropriate CXGN::Cview::Map::* objects.
20 =head1 AUTHOR(S)
22 Lukas Mueller (lam87@cornell.edu)
24 =head1 VERSION
26 1.0, March 2007
28 =head1 LICENSE
30 Refer to the L<CXGN::LICENSE> file.
32 =head1 FUNCTIONS
34 This class implements the following functions:
36 =cut
38 use strict;
40 package CXGN::Cview::MapFactory::Cacao;
42 use base qw| CXGN::DB::Object |;
44 use Scalar::Util qw/blessed/;
46 use CXGN::Cview::Map::SGN::Genetic;
47 #use CXGN::Cview::Map::SGN::User;
48 use CXGN::Cview::Map::SGN::Fish;
49 use CXGN::Cview::Map::SGN::Sequence;
50 use CXGN::Cview::Map::SGN::IL;
51 use CXGN::Cview::Map::SGN::Physical;
52 use CXGN::Cview::Map::SGN::ProjectStats;
53 use CXGN::Cview::Map::SGN::AGP;
54 #use CXGN::Cview::Map::SGN::ITAG;
55 use CXGN::Cview::Map::SGN::Contig;
56 use CXGN::Cview::Map::SGN::Scaffold;
57 use CXGN::Cview::Map::SGN::Image;
58 use CXGN::Cview::Map::SGN::QTL;
60 =head2 function new()
62 Synopsis: constructor
63 Arguments: a database handle
64 Returns: a CXGN::Cview::MapFactory::SGN object
65 Side effects: none
66 Description: none
68 =cut
70 sub new {
71 my $class = shift;
72 my $dbh = shift;
73 my $context = shift;
75 unless( blessed($context) && $context->isa('SGN::Context') ) {
76 require SGN::Context;
77 $context = SGN::Context->new();
79 my $self = $class->SUPER::new($dbh);
81 $self->{context}=$context;
84 return $self;
87 =head2 function create()
89 Description: creates a map based on the hashref given, which
90 should either contain the key map_id or map_version_id
91 and an appropriate identifier. The function returns undef
92 if a map of the given id cannot be found/created.
93 Example:
95 =cut
97 sub create {
98 my $self = shift;
99 my $hashref = shift;
100 #print STDERR "Hashref = map_id => $hashref->{map_id}, map_version_id => $hashref->{map_version_id}\n";
102 my $c = $self->{context};
103 my $temp_dir = $c->path_to( $c->config->{tempfiles_subdir} );
105 if (!exists($hashref->{map_id}) && !exists($hashref->{map_version_id})) {
106 die "[CXGN::Cview::MapFactory] Need either a map_id or map_version_id.\n";
108 if ($hashref->{map_id} && $hashref->{map_version_id}) {
109 die "[CXGN::Cview::MapFactory] Need either a map_id or map_version_id - not both.\n";
111 if ($hashref->{map_id}) {
112 $hashref->{map_version_id}=CXGN::Cview::Map::Tools::find_current_version($self->get_dbh(), $hashref->{map_id});
115 # now, we only deal with map_versions...
117 my $id = $hashref->{map_version_id};
119 #print STDERR "MapFactory: dealing with id = $id\n";
121 # if the map_version_id is purely numeric,
122 # check if the map is in the maps table and generate the
123 # appropriate map
125 if ($id=~/^\d+$/) {
126 my $query = "SELECT map_version_id, map_type, map_id, short_name FROM sgn.map join sgn.map_version using(map_id) WHERE map_version_id=?";
127 my $sth = $self->get_dbh()->prepare($query);
128 $sth->execute($id);
129 my ($id, $map_type) = $sth->fetchrow_array();
130 $map_type ||= '';
131 if ($map_type =~ /genetic/i) {
132 return CXGN::Cview::Map::SGN::Genetic->new($self->get_dbh(), $id);
136 elsif ($map_type =~ /seq/) {
137 print STDERR "Creating a seq map...($map_type, $id)\n";
138 my $map = CXGN::Cview::Map::SGN::Sequence->new($self->get_dbh(), $id);
139 $map->set_marker_link("http://gbrowse-cacao.sgn.cornell.edu/gb2/gbrowse/cacao1/?q=");
140 $map->set_link_by_name(1);
141 return $map;
145 return;
149 =head2 function get_all_maps()
151 Synopsis:
152 Arguments: none
153 Returns: a list of all maps currently defined, as
154 CXGN::Cview::Map objects (and subclasses)
155 Side effects: Queries the database for certain maps
156 Description:
158 =cut
160 sub get_all_maps {
161 my $self = shift;
163 my @system_maps = $self->get_system_maps();
164 my @user_maps = $self->get_user_maps();
165 my @maps = (@system_maps, @user_maps);
166 return @maps;
171 =head2 get_system_maps
173 Usage: my @system_maps = $map_factory->get_system_maps();
174 Desc: retrieves a list of system maps (from the sgn
175 database) as a list of CXGN::Cview::Map objects
176 Ret:
177 Args:
178 Side Effects:
179 Example:
181 =cut
183 sub get_system_maps {
184 my $self = shift;
186 my @maps = ();
188 my $query = "SELECT map.map_id FROM sgn.map LEFT JOIN sgn.map_version USING(map_id) LEFT JOIN sgn.accession on(parent_1=accession.accession_id) LEFT JOIN sgn.organism USING(organism_id) LEFT JOIN common_name USING(common_name_id) WHERE current_version='t' ORDER by common_name.common_name";
189 my $sth = $self->get_dbh()->prepare($query);
190 $sth->execute();
192 while (my ($map_id) = $sth->fetchrow_array()) {
193 my $map = $self->create({ map_id => $map_id });
194 if ($map) { push @maps, $map; }
197 # push il, physical, contig, and agp map
199 # foreach my $id ("il6.5", "il6.9", "p9", "c9", "agp", "pachy") {
200 # my $map = $self->create( {map_id=>$id} );
201 # if ($map) { push @maps, $map; }
204 return @maps;
209 =head2 get_user_maps
211 Status: DEPRECATED. Does nothing now, as user maps have been disabled.
212 Usage:
213 Desc: retrieves the current user maps of the logged in user.
214 Ret: a list of CXGN::Cview::Map objects
215 Args: none
216 Side Effects: none
217 Example:
219 =cut
221 sub get_user_maps {
222 my $self = shift;
223 # push the maps that are specific to that user and not public, if somebody is logged in...
225 my @maps = ();
226 # my $login = CXGN::Login->new($self->get_dbh());
227 # my $user_id = $login->has_session();
228 # if ($user_id) {
229 # my $q3 = "SELECT user_map_id FROM sgn_people.user_map WHERE obsolete='f' AND sp_person_id=?";
230 # my $h3 = $self->get_dbh()->prepare($q3);
231 # $h3->execute($user_id);
232 # while (my ($user_map_id) = $h3->fetchrow_array()) {
233 # my $map = $self->create( {map_id=>"u".$user_map_id} );
235 # if ($map) { push @maps, $map; }
238 return @maps;
242 sub get_db_ids {
243 my $self = shift;
244 my $id = shift;
246 my $population_id = 6;
247 my $reference_map_id=5;
249 if ($id=~/il(\d+)\.?(\d*)?/) {
250 $population_id=$1;
251 $reference_map_id=$2;
253 if (!$reference_map_id) { $reference_map_id=5; }
254 if (!$population_id) { $population_id=6; }
255 #print STDERR "Population ID: $population_id, reference_map_id = $reference_map_id\n";
257 return ($population_id, $reference_map_id);
260 return 1;