Template.pm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package Template;
  2. use strict;
  3. use utilities;
  4. use vars qw($AUTOLOAD);
  5. use Carp;
  6. {
  7. my %_attr_data = # DEFAULT ACCESS
  8. (
  9. _Filt => ['###', 'read/write'],
  10. _No => ['###', 'read'],
  11. _Hit => ['###', 'read'],
  12. _Prob => ['###', 'read'],
  13. _Eval => ['###', 'read'],
  14. _Pval => ['###', 'read'],
  15. _Score => ['###', 'read/write'],
  16. _SS => ['###', 'read'],
  17. _Cols => ['###', 'read'],
  18. _Qstart => ['###', 'read'],
  19. _Qend => ['###', 'read'],
  20. _Tstart => ['###', 'read'],
  21. _Tend => ['###', 'read'],
  22. _HMM => ['###', 'read'],
  23. _Sim => ['###', 'read/write'],
  24. _Ident => ['###', 'read/write'],
  25. _SumProbL=> ['###', 'read/write'],
  26. _ss_dssp => ['', 'read/write'],
  27. _conf => ['', 'read/write'],
  28. _predTM => ['###', 'read/write']
  29. );
  30. sub _accessible {
  31. my ($self, $attr, $mode) = @_;
  32. $_attr_data{$attr}[1] =~ /$mode/;
  33. }
  34. sub _default_for {
  35. my ($self, $attr) = @_;
  36. $_attr_data{$attr}[0];
  37. }
  38. sub _standard_keys {
  39. keys %_attr_data;
  40. }
  41. }
  42. ## constructor
  43. sub new {
  44. my ($caller, %arg) = @_;
  45. my $caller_is_obj = ref($caller);
  46. my $class = $caller_is_obj || $caller;
  47. my $self = bless {}, $class;
  48. foreach my $attrname ($self->_standard_keys() ) {
  49. my ($argname) = ($attrname =~ /^_(.*)/);
  50. if (exists $arg{$argname}) {
  51. $self->{$attrname} = $arg{$argname};
  52. } elsif ($caller_is_obj) {
  53. $self->{$attrname} = $caller->{$attrname};
  54. } else {
  55. $self->{$attrname} = $self->_default_for($attrname);
  56. }
  57. }
  58. return $self;
  59. }
  60. ## automatically generated getters and setters:
  61. ## $AUTOLOAD contains full name of a routine and is checked for name/accessiblity
  62. ## then an anonymous routine (names e.g. get_name) is created and stored
  63. ## in table for future use
  64. sub AUTOLOAD {
  65. no strict "refs";
  66. my ($self, $newval) = @_;
  67. if ($AUTOLOAD =~ /.*::get(_\w+)/ && $self->_accessible($1, 'read')) {
  68. my $attr_name = $1;
  69. *{$AUTOLOAD} = sub { return $_[0]->{$attr_name} };
  70. return $self->{$attr_name}
  71. }
  72. if ($AUTOLOAD =~ /.*::set(_\w+)/ && $self->_accessible($1, 'write')) {
  73. my $attr_name = $1;
  74. *{$AUTOLOAD} = sub { $_[0]->{$attr_name} = $_[1]; return };
  75. $self->{$1} = $newval;
  76. return
  77. }
  78. ## mistaken?
  79. croak("No such method: $AUTOLOAD");
  80. }
  81. sub DESTROY {
  82. }
  83. sub to_string {
  84. my $self = shift;
  85. my $spacer = shift;
  86. my $out = "";
  87. $out .= defined($spacer) ? "$self->{_Filt}$spacer" : "$self->{_Filt}\t";
  88. $out .= defined($spacer) ? "$self->{_No}$spacer" : "$self->{_No}\t";
  89. $out .= defined($spacer) ? "$self->{_Hit}$spacer" : "$self->{_Hit}\t";
  90. $out .= defined($spacer) ? "$self->{_Prob}$spacer" : "$self->{_Prob}\t";
  91. $out .= defined($spacer) ? "$self->{_Eval}$spacer" : "$self->{_Eval}\t";
  92. $out .= defined($spacer) ? "$self->{_Pval}$spacer" : "$self->{_Pval}\t";
  93. $out .= defined($spacer) ? "$self->{_Score}$spacer" : "$self->{_Score}\t";
  94. $out .= defined($spacer) ? "$self->{_SS}$spacer" : "$self->{_SS}\t";
  95. $out .= defined($spacer) ? "$self->{_Cols}$spacer" : "$self->{_Cols}\t";
  96. $out .= defined($spacer) ? "$self->{_Qstart}$spacer" : "$self->{_Qstart}\t";
  97. $out .= defined($spacer) ? "$self->{_Qend}$spacer" : "$self->{_Qend}\t";
  98. $out .= defined($spacer) ? "$self->{_Tstart}$spacer" : "$self->{_Tstart}\t";
  99. $out .= defined($spacer) ? "$self->{_Tend}$spacer" : "$self->{_Tend}\t";
  100. $out .= defined($spacer) ? "$self->{_HMM}$spacer" : "$self->{_HMM}\t";
  101. $out .= defined($spacer) ? "$self->{_Ident}$spacer" : "$self->{_Ident}\t";
  102. $out .= defined($spacer) ? "$self->{_Sim}$spacer" : "$self->{_Sim}\t";
  103. $out .= defined($spacer) ? "$self->{_SumProbL}$spacer" : "$self->{_SumProbL}\t";
  104. $out .= defined($spacer) ? "$self->{_predTM}$spacer" : "$self->{_predTM}\t";
  105. return $out;
  106. }
  107. ## check whether two templates have same keys and values
  108. sub equals {
  109. my ($self, $template) = @_;
  110. my %cmp = map { $_ => 1 } keys %{$self};
  111. for my $key (keys %{$template}) {
  112. last unless exists $cmp{$key};
  113. last unless $self->{$key} eq $template->{$key};
  114. delete $cmp{$key};
  115. }
  116. if (%cmp) {
  117. return 0;
  118. } else {
  119. return 1;
  120. }
  121. }
  122. 1;