PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Bio/Pipeline/Utils/Iterator.pm

https://github.com/bioperl/bioperl-pipeline
Perl | 168 lines | 107 code | 48 blank | 13 comment | 9 complexity | d826e4c5d86365cb34e8e340a59f04dd MD5 | raw file
Possible License(s): LGPL-2.0
  1. #
  2. # BioPerl module for Bio::Pipeline::Utils::Iterator
  3. #
  4. # Please direct questions and support issues to <bioperl-l@bioperl.org>
  5. #
  6. # Cared for by Shawn Hoon <shawnh@fugu-sg.org>
  7. #
  8. #
  9. # You may distribute this module under the same terms as perl itself
  10. #
  11. # POD documentation - main docs before the code
  12. #
  13. =head1 NAME
  14. Bio::Pipeline::Utils::Iterator
  15. =head1 SYNOPSIS
  16. use Bio::Pipeline::Utils::Iterator;
  17. use Bio::AlignIO;
  18. my $itr = Bio::Pipeline::Utils::Iterator->new();
  19. my $aio = Bio::AlignIO->new(-file=>$ARGV[0],-format=>"phylip");
  20. my $obj = $itr->run($aio); #$obj is a array ref of Bio::SimpleAlign
  21. =head1 DESCRIPTION
  22. Iterator module plugged into a transformer allows fetching of all Iterator
  23. type objects at once to be passed to the runnable. This allows one
  24. to make use of the set of IO type modules like:
  25. Bio::AlignIO
  26. Bio::SeqIO
  27. Bio::MapIO
  28. Bio::SearchIO
  29. Bio::ClusterIO
  30. Bio::TreeIO
  31. =head1 FEEDBACK
  32. =head2 Mailing Lists
  33. User feedback is an integral part of the evolution of this and other
  34. Bioperl modules. Send your comments and suggestions preferably to one
  35. of the Bioperl mailing lists. Your participation is much appreciated.
  36. bioperl-pipeline@bioperl.org - General discussion
  37. http://bioperl.org/wiki/Mailing_lists - About the mailing lists
  38. =head2 Support
  39. Please direct usage questions or support issues to the mailing list:
  40. L<bioperl-l@bioperl.org>
  41. rather than to the module maintainer directly. Many experienced and
  42. reponsive experts will be able look at the problem and quickly
  43. address it. Please include a thorough description of the problem
  44. with code and data examples if at all possible.
  45. =head2 Reporting Bugs
  46. Report bugs to the Bioperl bug tracking system to help us keep track
  47. the bugs and their resolution. Bug reports can be submitted via email
  48. or the web:
  49. bioperl-bugs@bio.perl.org
  50. http://bugzilla.open-bio.org/
  51. =head1 AUTHOR - Shawn Hoon
  52. Email shawnh@fugu-sg.org
  53. =head1 APPENDIX
  54. The rest of the documentation details each of the object methods. Internal metho
  55. ds are usually preceded with a _
  56. =cut
  57. # Let the code begin...
  58. package Bio::Pipeline::Utils::Iterator;
  59. use vars qw(@ISA);
  60. use strict;
  61. use Bio::Root::Root;
  62. @ISA = qw(Bio::Root::Root);
  63. my %method = ("Bio::AlignIO" =>"next_aln",
  64. "Bio::SeqIO" => "next_seq",
  65. "Bio::MapIO" =>"next_map",
  66. "Bio::SearchIO"=>"next_result",
  67. "Bio::ClusterIO"=>"next_cluster",
  68. "Bio::TreeIO" => "next_tree");
  69. =head2 new
  70. Title : new
  71. Usage : my $Iterator = Bio::Pipeline::Utils::Iterator->new('-module'=>$module);
  72. Function: constructor for Iterator object
  73. Returns : a new Iterator object
  74. Args : module, the list of Iterator modules found in Bio::Pipeline::Utils::Iterator::*
  75. =cut
  76. sub new {
  77. my ($caller ,@args) = @_;
  78. my $class = ref($caller) || $caller;
  79. my ($self) = $class->SUPER::new(@args);
  80. $self->method(\%method);
  81. return $self;
  82. }
  83. =head2 run
  84. Title : run
  85. Usage : $self->run();
  86. Function: abstract method for running the Iterator
  87. Returns :
  88. Args :
  89. =cut
  90. sub run {
  91. my ($self,$iterator) = @_;
  92. my %method = %{$self->method};
  93. foreach my $key (%method){
  94. if ($iterator->isa($key)){
  95. my $itr = $method{$key};
  96. $iterator->can($itr) || $self->throw("$itr cannot be called on ". ref $iterator);
  97. my @obj;
  98. while (my $obj = $iterator->$itr){
  99. push @obj, $obj;
  100. }
  101. return \@obj;
  102. }
  103. }
  104. $self->throw (ref $iterator ." is not supported by Bio::Pipeline::Utils::Iterator");
  105. }
  106. =head2 method
  107. Title : method
  108. Usage : $inc->method
  109. Function: get set method for the Iterator method to use
  110. Returns : a hash of object to method mapping
  111. Args : a hash of object to method mapping
  112. =cut
  113. sub method {
  114. my ($self,$method) = @_;
  115. if($method){
  116. $self->{'_method'} = $method;
  117. }
  118. return $self->{'_method'};
  119. }
  120. 1;