/contrib/gls.pl

https://bitbucket.org/cistrome/cistrome-harvard/ · Perl · 204 lines · 170 code · 30 blank · 4 comment · 8 complexity · 86442bf296dbea9b13c65eea468a0e43 MD5 · raw file

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