TemplateStruct.pm 4.6 KB

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