コード例 #1
0
ファイル: Seq.py プロジェクト: nuin/biopython
    def __cmp__(self, other):
        """Compare the sequence for to another sequence or a string.

        If compared to another sequence the alphabets must be compatible.
        Comparing DNA to RNA, or Nucleotide to Protein will raise an
        exception.

        Otherwise only the sequence itself is compared, not the precise
        alphabet.

        This method indirectly supports ==, < , etc."""
        if hasattr(other, "alphabet") :
            #other should be a Seq or a MutableSeq
            if not Alphabet._check_type_compatible([self.alphabet,
                                                    other.alphabet]) :
                raise TypeError("Incompatable alphabets %s and %s" \
                                % (repr(self.alphabet), repr(other.alphabet)))
            #They should be the same sequence type (or one of them is generic)
            if isinstance(other, MutableSeq):
                #See test_GAQueens.py for an historic usage of a non-string
                #alphabet!  Comparing the arrays supports this.
                return cmp(self.data, other.data)
            else :
                return cmp(str(self), str(other))
        elif isinstance(other, basestring) :
            return cmp(str(self), other)
        else :
            raise TypeError
コード例 #2
0
ファイル: Seq.py プロジェクト: manucorreia/biopython
    def __cmp__(self, other):
        """Compare the sequence for to another sequence or a string.

        If compared to another sequence the alphabets must be compatible.
        Comparing DNA to RNA, or Nucleotide to Protein will raise an
        exception.

        Otherwise only the sequence itself is compared, not the precise
        alphabet.

        This method indirectly supports ==, < , etc."""
        if hasattr(other, "alphabet"):
            #other should be a Seq or a MutableSeq
            if not Alphabet._check_type_compatible(
                [self.alphabet, other.alphabet]):
                raise TypeError("Incompatable alphabets %s and %s" \
                                % (repr(self.alphabet), repr(other.alphabet)))
            #They should be the same sequence type (or one of them is generic)
            if isinstance(other, MutableSeq):
                #See test_GAQueens.py for an historic usage of a non-string
                #alphabet!  Comparing the arrays supports this.
                return cmp(self.data, other.data)
            else:
                return cmp(str(self), str(other))
        elif isinstance(other, basestring):
            return cmp(str(self), other)
        else:
            raise TypeError
コード例 #3
0
ファイル: Seq.py プロジェクト: manucorreia/biopython
 def __radd__(self, other):
     if hasattr(other, "alphabet"):
         #other should be a Seq or a MutableSeq
         if not Alphabet._check_type_compatible(
             [self.alphabet, other.alphabet]):
             raise TypeError("Incompatable alphabets %s and %s" \
                             % (repr(self.alphabet), repr(other.alphabet)))
         #They should be the same sequence type (or one of them is generic)
         a = Alphabet._consensus_alphabet([self.alphabet, other.alphabet])
         return self.__class__(str(other) + str(self), a)
     elif isinstance(other, basestring):
         #other is a plain string - use the current alphabet
         return self.__class__(other + str(self), self.alphabet)
     else:
         raise TypeError
コード例 #4
0
ファイル: Seq.py プロジェクト: nuin/biopython
 def __radd__(self, other):
     if hasattr(other, "alphabet") :
         #other should be a Seq or a MutableSeq
         if not Alphabet._check_type_compatible([self.alphabet,
                                                 other.alphabet]) :
             raise TypeError("Incompatable alphabets %s and %s" \
                             % (repr(self.alphabet), repr(other.alphabet)))
         #They should be the same sequence type (or one of them is generic)
         a = Alphabet._consensus_alphabet([self.alphabet, other.alphabet])
         return self.__class__(str(other) + str(self), a)
     elif isinstance(other, basestring) :
         #other is a plain string - use the current alphabet
         return self.__class__(other + str(self), self.alphabet)
     else :
         raise TypeError
コード例 #5
0
ファイル: Seq.py プロジェクト: manucorreia/biopython
 def __radd__(self, other):
     if hasattr(other, "alphabet"):
         #other should be a Seq or a MutableSeq
         if not Alphabet._check_type_compatible(
             [self.alphabet, other.alphabet]):
             raise TypeError("Incompatable alphabets %s and %s" \
                             % (repr(self.alphabet), repr(other.alphabet)))
         #They should be the same sequence type (or one of them is generic)
         a = Alphabet._consensus_alphabet([self.alphabet, other.alphabet])
         if isinstance(other, MutableSeq):
             #See test_GAQueens.py for an historic usage of a non-string
             #alphabet!  Adding the arrays should support this.
             return self.__class__(other.data + self.data, a)
         else:
             return self.__class__(str(other) + str(self), a)
     elif isinstance(other, basestring):
         #other is a plain string - use the current alphabet
         return self.__class__(str(other) + str(self), self.alphabet)
     else:
         raise TypeError
コード例 #6
0
ファイル: Seq.py プロジェクト: manucorreia/biopython
    def _get_seq_str_and_check_alphabet(self, other_sequence):
        """string/Seq/MutableSeq to string, checking alphabet (PRIVATE).

        For a string argument, returns the string.

        For a Seq or MutableSeq, it checks the alphabet is compatible
        (raising an exception if it isn't), and then returns a string.
        """
        try:
            other_alpha = other_sequence.alphabet
        except AttributeError:
            #Assume other_sequence is a string
            return other_sequence

        #Other should be a Seq or a MutableSeq
        if not Alphabet._check_type_compatible([self.alphabet, other_alpha]):
            raise TypeError("Incompatable alphabets %s and %s" \
                            % (repr(self.alphabet), repr(other_alpha)))
        #Return as a string
        return str(other_sequence)
コード例 #7
0
ファイル: Seq.py プロジェクト: nuin/biopython
 def __radd__(self, other):
     if hasattr(other, "alphabet") :
         #other should be a Seq or a MutableSeq
         if not Alphabet._check_type_compatible([self.alphabet,
                                                 other.alphabet]) :
             raise TypeError("Incompatable alphabets %s and %s" \
                             % (repr(self.alphabet), repr(other.alphabet)))
         #They should be the same sequence type (or one of them is generic)
         a = Alphabet._consensus_alphabet([self.alphabet, other.alphabet])
         if isinstance(other, MutableSeq):
             #See test_GAQueens.py for an historic usage of a non-string
             #alphabet!  Adding the arrays should support this.
             return self.__class__(other.data + self.data, a)
         else :
             return self.__class__(str(other) + str(self), a)
     elif isinstance(other, basestring) :
         #other is a plain string - use the current alphabet
         return self.__class__(str(other) + str(self), self.alphabet)
     else :
         raise TypeError
コード例 #8
0
ファイル: Seq.py プロジェクト: nuin/biopython
    def _get_seq_str_and_check_alphabet(self, other_sequence) :
        """string/Seq/MutableSeq to string, checking alphabet (PRIVATE).

        For a string argument, returns the string.

        For a Seq or MutableSeq, it checks the alphabet is compatible
        (raising an exception if it isn't), and then returns a string.
        """
        try :
            other_alpha = other_sequence.alphabet
        except AttributeError :
            #Assume other_sequence is a string
            return other_sequence

        #Other should be a Seq or a MutableSeq
        if not Alphabet._check_type_compatible([self.alphabet, other_alpha]) :
            raise TypeError("Incompatable alphabets %s and %s" \
                            % (repr(self.alphabet), repr(other_alpha)))
        #Return as a string
        return str(other_sequence)