PageRenderTime 108ms CodeModel.GetById 58ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/gls.pl

https://bitbucket.org/cistrome/cistrome-harvard/
Perl | 204 lines | 191 code | 11 blank | 2 comment | 2 complexity | 86442bf296dbea9b13c65eea468a0e43 MD5 | raw file
  1#/!/usr/bin/env perl -w
  2
  3=head1 NAME
  4
  5 gls
  6
  7=head1 DESCRIPTION
  8
  9 Display the files generated by the current user within the local instance of Galaxy.
 10 Information is grouped by user's Galaxy histories and ordered by date/time
 11 
 12=head1 OPTIONS
 13
 14	- i|info        = show more info about file [default = no]
 15	- e|error       = show error files [default = no]
 16	- d|dirname     = show files from this dirname (ie, history name) only (NOTE. if history name contains spaces, it should be quoted)
 17	- n|nocontent   = show empty files [default = no]
 18	- a|altuser     = supply an alternative username (nb, admin only)
 19	- h|help        = help
 20	- m|man         = man
 21
 22=head1 AUTHOR
 23
 24 Simon McGowan, CBRG [Computational Biology Research Group, Oxford University, UK]
 25
 26=head1 Update Record
 27
 28 23/09/2010  001  S.McGowan  first written
 29
 30=cut
 31
 32
 33
 34use strict;
 35use Data::Dumper;
 36use DBI;
 37use Getopt::Long;
 38use Pod::Usage;
 39
 40my $show_file_info = 0;
 41my $show_error_files = 0;
 42my $show_empty_files = 0;
 43my $help = 0;
 44my $man = 0;
 45my $alt_user;
 46my $selected_dir;
 47
 48GetOptions(
 49	'h|help'=>\$help,
 50	'm|man'=>\$man,
 51	'i|info'=>\$show_file_info,
 52	'e|error'=>\$show_error_files,
 53	'a|altuser=s'=>\$alt_user,
 54	'n|nocontent'=>\$show_empty_files,
 55	'd|dirname=s'=>\$selected_dir
 56); 
 57
 58pod2usage(1) if $help;
 59pod2usage(-verbose=>2) if $man;
 60
 61my %history_data;
 62my %file_data;
 63
 64
 65############  CONFIG  ########################################
 66# list of admin usernames:
 67my %admin;
 68$admin{simonmcg} = '';
 69$admin{stevetay} = '';
 70
 71# institute email domain
 72my $email_domain = '@molbiol.ox.ac.uk';
 73
 74# mysql db
 75my $mysql_database = 'galaxy';
 76my $mysql_host = 'xxxxxxxx';
 77my $mysql_username = 'xxxxxxxx';
 78my $mysql_password = 'xxxxxxxx';
 79
 80# file path
 81my $db_root_dir = '/wwwdata/galaxy-prod/database/files/';
 82##############################################################
 83
 84#-----------------------------------------------------------------------------------
 85
 86my $current_user = getlogin();  
 87
 88# allow admin to list any user's galaxy files:
 89if (exists($admin{$current_user}))
 90{
 91	if ($alt_user)  {$current_user = $alt_user;}
 92} 
 93
 94&get_data;
 95
 96&print_galaxy_data;
 97
 98exit();
 99
100#-----------------------------------------------------------------------------------
101
102sub get_data
103{
104	my $dbh = DBI->connect("DBI:mysql:database=$mysql_database;host=" . $mysql_host, $mysql_username, $mysql_password, {'RaiseError' => 1});
105	
106	my $sql = "SELECT h.id, h.name, h.create_time, hda.dataset_id, hda.update_time, hda.name, hda.info, hda.blurb, hda.extension, d.file_size
107	FROM history h, history_dataset_association hda, galaxy_user g, dataset d
108	WHERE g.email = '$current_user$email_domain'
109	AND g.id = h.user_id
110	AND h.id = hda.history_id
111	AND hda.dataset_id = d.id";	
112	
113	my $sth = $dbh->prepare($sql) or die("Failed to prepare statement $sql\n"); 
114	$sth->execute() or die("Can't perform SQL $sql : $DBI::errstr\n");	
115	while (my $ref = $sth->fetch)
116	{
117		my ($history_id, $history_name, $create_time, $dataset_id, $dataset_time, $dataset_name, $info, $blurb, $ext, $file_size) = @{$ref};
118		
119		#print "$history_id, $history_name, $create_time, $dataset_id, $dataset_time, $dataset_name, $info, $blurb, $ext, $file_size\n\n";
120		
121		$history_data{$history_id}{create_time} = $create_time;
122		$history_data{$history_id}{history_name} = $history_name;
123		
124		$file_data{$history_id}{$dataset_id}{dataset_update_time} = $dataset_time;
125		$file_data{$history_id}{$dataset_id}{dataset_name} = $dataset_name;
126		$file_data{$history_id}{$dataset_id}{info} = $info;
127		$file_data{$history_id}{$dataset_id}{blurb} = $blurb;		
128		$file_data{$history_id}{$dataset_id}{file_size} = $file_size;		
129		$file_data{$history_id}{$dataset_id}{ext} = $ext;
130	}	
131	$sth->finish;
132}
133
134sub print_galaxy_data
135{
136	foreach my $hist_id (sort numerically keys %history_data)
137	{
138		my $hist_name = $history_data{$hist_id}{history_name};
139		if ($selected_dir)
140		{
141			# if the user has opted to see just one dir...
142			unless($hist_name eq $selected_dir) {next;}
143		}
144		
145		my $hist_date = $history_data{$hist_id}{create_time};
146		
147		print "\n";
148		print "$hist_date - $hist_name\n";
149		
150		foreach my $dataset_id (sort numerically keys %{$file_data{$hist_id}})
151		{
152			my $dataset_time = $file_data{$hist_id}{$dataset_id}{dataset_update_time};
153			my $dataset_name = $file_data{$hist_id}{$dataset_id}{dataset_name};
154			my $info = $file_data{$hist_id}{$dataset_id}{info};
155			my $blurb = $file_data{$hist_id}{$dataset_id}{blurb};
156			my $file_size = $file_data{$hist_id}{$dataset_id}{file_size};
157			my $ext = $file_data{$hist_id}{$dataset_id}{ext};
158			my $file_path = &derive_file_path($dataset_id);
159			
160			if (($blurb) and ($blurb eq 'empty'))
161			{
162				unless ($show_empty_files) {next;}
163			}
164			if (($blurb) and ($blurb eq 'error'))
165			{
166				unless ($show_error_files) {next;}
167			}
168			
169			print "\t$dataset_time - $dataset_name";
170			print " $file_path";
171			if ($show_file_info)
172			{
173				print " [size:$file_size; type:$ext;";
174				if (($info) and ($blurb)) { print " $info; $blurb"; }
175				elsif ($info) { print " $info"; }
176				elsif ($blurb) { print " $blurb"; }
177				print ']';
178			}
179			print "\n";
180		}
181	}
182}
183
184sub derive_file_path
185{
186	my ($dataset_id) = @_;
187	my $dir = sprintf("%06d", $dataset_id);
188	$dir =~ s/\d\d\d$//;
189	my $full_path = $db_root_dir . $dir . '/dataset_' . $dataset_id . '.dat';
190	return ($full_path);	
191}
192
193sub numerically
194{
195	$a <=> $b;
196}
197
198
199
200
201
202
203
204