[BioPython] PropertyManager

Andrew Dalke dalke@acm.org
Wed, 19 Apr 2000 01:13:16 -0600


Jeff:
> You're disinclined to [add attributes to objects] because of
> your C++ background...

>but not [storing the properties in an external table]?  ;)


The C++ idea bouncing around the back of my head for the latter
is something like:

class ProteinAlphabet {
 ...
};

class DNAAlphabet {
 ...
};

template <class T>
class Seq {
public:
  T alphabet;
  string data;
  Seq(new_alphabet, new_data) : alphabet(new_alphabet), data(new_data) {
  }
  ...
};

class WeightTable {
 public:
  float operator[](char c);
};
protein_weights = WeightTable( ... values ...);
ambiguous_dna_weights = WeightTable( ... values ...);

# Specializations to map alphabet types to default weights.
# I haven't done C++ programming in a while, so I don't know
# if this is a good idea in that world.
template <ProteinAlphabet T>
typedef DefaultWeightTable {
  static WeightTable& table = protein_weights;
};
template <DNAAlphabet T>
typedef DefaultWeightTable {
  static WeightTable& table = ambiguous_dna_weights;
};


template <class T>
float weight(Seq<T>) {
  WeightTable table = DefaultWeightTable<T>::table;
  float sum = 0.0f;
  for (Seq<T>::const_iterator c = Seq.begin(); c; c++) {
    sum += table[c];
  }
  return sum;
}

Seq<DNAAlphabet> dna("ATCGTCAATGCA");
Seq<ProteinAlphabet> protein;
Translate::standard_table(dna, protein);
cout << "The dna weight is " << weight(dna) << endl;
cout << "The protein weight is " << weight(dna) << endl;


Modulo errors for not having done serious C++ programming in
3 years.

                    Andrew
                    dalke@acm.org