Back to index

python-biopython  1.60
Public Member Functions | Private Member Functions | Static Private Attributes
test_Seq_objs.StringMethodTests Class Reference

List of all members.

Public Member Functions

def test_str_count
def test_str_find
def test_str_rfind
def test_str_startswith
def test_str_endswith
def test_str_strip
def test_str_rstrip
def test_str_split
def test_str_rsplit
def test_str_lsplit
def test_str_length
def test_str_upper
def test_str_upper
def test_str_getitem
def test_tostring
def test_tomutable
def test_toseq
def test_the_complement
def test_the_reverse_complement
def test_the_transcription
def test_the_back_transcription
def test_the_translate
def test_the_translation_of_stops
def test_the_translation_of_invalid_codons
def test_the_translation_of_ambig_codons
def test_init_typeerror

Private Member Functions

def _test_method

Static Private Attributes

list _examples
list _start_end_values = [0, 1, 2, 1000, -1, -2, -999]

Detailed Description

Definition at line 64 of file test_Seq_objs.py.


Member Function Documentation

def test_Seq_objs.StringMethodTests._test_method (   self,
  method_name,
  pre_comp_function = None,
  start_end = False 
) [private]
Check this method matches the plain string's method.

Definition at line 96 of file test_Seq_objs.py.

00096 
00097     def _test_method(self, method_name, pre_comp_function=None, start_end=False):
00098         """Check this method matches the plain string's method."""
00099         self.assertTrue(isinstance(method_name, str))
00100         for example1 in self._examples:
00101             if not hasattr(example1, method_name):
00102                 #e.g. MutableSeq does not support find
00103                 continue
00104             str1 = str(example1)
00105 
00106             for example2 in self._examples:
00107                 if not hasattr(example2, method_name):
00108                     #e.g. MutableSeq does not support find
00109                     continue
00110                 str2 = str(example2)
00111 
00112                 i = getattr(example1,method_name)(str2)
00113                 j = getattr(str1,method_name)(str2)
00114                 if pre_comp_function:
00115                     i = pre_comp_function(i)
00116                     j = pre_comp_function(j)
00117                 if i != j:
00118                     raise ValueError("%s.%s(%s) = %i, not %i" \
00119                                      % (repr(example1),
00120                                         method_name,
00121                                         repr(str2),
00122                                         i,
00123                                         j))
00124 
00125                 try:
00126                     i = getattr(example1,method_name)(example2)
00127                     j = getattr(str1,method_name)(str2)
00128                     if pre_comp_function:
00129                         i = pre_comp_function(i)
00130                         j = pre_comp_function(j)
00131                     if i != j:
00132                         raise ValueError("%s.%s(%s) = %i, not %i" \
00133                                          % (repr(example1),
00134                                             method_name,
00135                                             repr(example2),
00136                                             i,
00137                                             j))
00138                 except TypeError:
00139                     #TODO - Check the alphabets do clash!
00140                     pass
00141 
00142                 if start_end:
00143                     for start in self._start_end_values:
00144                         i = getattr(example1,method_name)(str2, start)
00145                         j = getattr(str1,method_name)(str2, start)
00146                         if pre_comp_function:
00147                             i = pre_comp_function(i)
00148                             j = pre_comp_function(j)
00149                         if i != j:
00150                             raise ValueError("%s.%s(%s, %i) = %i, not %i" \
00151                                              % (repr(example1),
00152                                                 method_name,
00153                                                 repr(str2),
00154                                                 start,
00155                                                 i,
00156                                                 j))
00157                         
00158                         for end in self._start_end_values:
00159                             i = getattr(example1,method_name)(str2, start, end)
00160                             j = getattr(str1,method_name)(str2, start, end)
00161                             if pre_comp_function:
00162                                 i = pre_comp_function(i)
00163                                 j = pre_comp_function(j)
00164                             if i != j:
00165                                 raise ValueError("%s.%s(%s, %i, %i) = %i, not %i" \
00166                                                  % (repr(example1),
00167                                                     method_name,
00168                                                     repr(str2),
00169                                                     start,
00170                                                     end,
00171                                                     i,
00172                                                     j))

Here is the caller graph for this function:

Check Seq __init__ gives TypeError exceptions.

Definition at line 522 of file test_Seq_objs.py.

00522 
00523     def test_init_typeerror(self):
00524         """Check Seq __init__ gives TypeError exceptions."""
00525         #Only expect it to take strings and unicode - not Seq objects!
00526         self.assertRaises(TypeError, Seq, (1066))
00527         self.assertRaises(TypeError, Seq, (Seq("ACGT", generic_dna)))

Check matches the python string count method.

Definition at line 173 of file test_Seq_objs.py.

00173 
00174     def test_str_count(self):
00175         """Check matches the python string count method."""
00176         self._test_method("count", start_end=True)

Here is the call graph for this function:

Check matches the python string endswith method.

Definition at line 213 of file test_Seq_objs.py.

00213 
00214     def test_str_endswith(self):
00215         """Check matches the python string endswith method."""
00216         self._test_method("endswith", start_end=True)
00217 
00218         try:
00219             self.assertTrue("ABCDE".endswith(("ABE","OBE","CDE")))
00220         except TypeError:
00221             #Base string only supports this on Python 2.5+, skip this
00222             return
00223 
00224         #Now check with a tuple of sub sequences
00225         for example1 in self._examples:
00226             if not hasattr(example1, "endswith"):
00227                 #e.g. MutableSeq does not support this
00228                 continue
00229             subs = tuple([example1[start:start+2] for start \
00230                           in range(0, len(example1)-2,3)])
00231             subs_str = tuple([str(s) for s in subs])
00232 
00233             self.assertEqual(str(example1).endswith(subs_str),
00234                              example1.endswith(subs))
00235             self.assertEqual(str(example1).startswith(subs_str),
00236                              example1.startswith(subs_str)) #strings!
00237             self.assertEqual(str(example1).endswith(subs_str,3),
00238                              example1.endswith(subs,3))
00239             self.assertEqual(str(example1).endswith(subs_str,2,6),
00240                              example1.endswith(subs,2,6))

Here is the call graph for this function:

Check matches the python string find method.

Definition at line 177 of file test_Seq_objs.py.

00177 
00178     def test_str_find(self):
00179         """Check matches the python string find method."""
00180         self._test_method("find", start_end=True)

Here is the call graph for this function:

Check slicing and indexing works like a string.

Definition at line 287 of file test_Seq_objs.py.

00287 
00288     def test_str_getitem(self):
00289         """Check slicing and indexing works like a string."""
00290         for example1 in self._examples:
00291             str1 = str(example1)
00292             for i in self._start_end_values:
00293                 if abs(i) < len(example1):
00294                     self.assertEqual(str(example1[i]), str1[i])
00295                 self.assertEqual(str(example1[:i]), str1[:i])
00296                 self.assertEqual(str(example1[i:]), str1[i:])
00297                 for j in self._start_end_values:
00298                     self.assertEqual(str(example1[i:j]), str1[i:j])
00299                     for step in range(-3,4):
00300                         if step == 0:
00301                             try:
00302                                 print example1[i:j:step]
00303                                 self._assert(False) #Should fail!
00304                             except ValueError:
00305                                 pass
00306                         else:
00307                             self.assertEqual(str(example1[i:j:step]), \
00308                                              str1[i:j:step])

Check matches the python string __len__ method.

Definition at line 267 of file test_Seq_objs.py.

00267 
00268     def test_str_length(self):
00269         """Check matches the python string __len__ method."""
00270         for example1 in self._examples:
00271             str1 = str(example1)
00272             self.assertEqual(len(example1), len(str1))

Check matches the python string rstrip method.

Definition at line 261 of file test_Seq_objs.py.

00261 
00262     def test_str_lsplit(self):
00263         """Check matches the python string rstrip method."""
00264         #Calling (r)split should return a list of Seq-like objects, we'll
00265         #just apply str() to each of them so it matches the string method
00266         self._test_method("rstrip", pre_comp_function=lambda x : map(str,x))

Here is the call graph for this function:

Check matches the python string rfind method.

Definition at line 181 of file test_Seq_objs.py.

00181 
00182     def test_str_rfind(self):
00183         """Check matches the python string rfind method."""
00184         self._test_method("rfind", start_end=True)

Here is the call graph for this function:

Check matches the python string rstrip method.

Definition at line 255 of file test_Seq_objs.py.

00255 
00256     def test_str_rsplit(self):
00257         """Check matches the python string rstrip method."""
00258         #Calling (r)split should return a list of Seq-like objects, we'll
00259         #just apply str() to each of them so it matches the string method
00260         self._test_method("rstrip", pre_comp_function=lambda x : map(str,x))

Here is the call graph for this function:

Check matches the python string rstrip method.

Definition at line 245 of file test_Seq_objs.py.

00245 
00246     def test_str_rstrip(self):
00247         """Check matches the python string rstrip method."""
00248         self._test_method("rstrip", pre_comp_function=str)

Here is the call graph for this function:

Check matches the python string rstrip method.

Definition at line 249 of file test_Seq_objs.py.

00249 
00250     def test_str_split(self):
00251         """Check matches the python string rstrip method."""
00252         #Calling (r)split should return a list of Seq-like objects, we'll
00253         #just apply str() to each of them so it matches the string method
00254         self._test_method("rstrip", pre_comp_function=lambda x : map(str,x))

Here is the call graph for this function:

Check matches the python string startswith method.

Definition at line 185 of file test_Seq_objs.py.

00185 
00186     def test_str_startswith(self):
00187         """Check matches the python string startswith method."""
00188         self._test_method("startswith", start_end=True)
00189 
00190         try:
00191             self.assertTrue("ABCDE".startswith(("ABE","OBE","ABC")))
00192         except TypeError:
00193             #Base string only supports this on Python 2.5+, skip this
00194             return
00195         
00196         #Now check with a tuple of sub sequences
00197         for example1 in self._examples:
00198             if not hasattr(example1, "startswith"):
00199                 #e.g. MutableSeq does not support this
00200                 continue
00201             subs = tuple([example1[start:start+2] for start \
00202                           in range(0, len(example1)-2,3)])
00203             subs_str = tuple([str(s) for s in subs])
00204 
00205             self.assertEqual(str(example1).startswith(subs_str),
00206                              example1.startswith(subs))
00207             self.assertEqual(str(example1).startswith(subs_str),
00208                              example1.startswith(subs_str)) #strings!
00209             self.assertEqual(str(example1).startswith(subs_str,3),
00210                              example1.startswith(subs,3))
00211             self.assertEqual(str(example1).startswith(subs_str,2,6),
00212                              example1.startswith(subs,2,6))        

Here is the call graph for this function:

Check matches the python string strip method.

Definition at line 241 of file test_Seq_objs.py.

00241 
00242     def test_str_strip(self):
00243         """Check matches the python string strip method."""
00244         self._test_method("strip", pre_comp_function=str)

Here is the call graph for this function:

Check matches the python string upper method.

Definition at line 273 of file test_Seq_objs.py.

00273 
00274     def test_str_upper(self):
00275         """Check matches the python string upper method."""
00276         for example1 in self._examples:
00277             if isinstance(example1, MutableSeq) : continue
00278             str1 = str(example1)
00279             self.assertEqual(str(example1.upper()), str1.upper())

Here is the caller graph for this function:

Check matches the python string lower method.

Definition at line 280 of file test_Seq_objs.py.

00280 
00281     def test_str_upper(self):
00282         """Check matches the python string lower method."""
00283         for example1 in self._examples:
00284             if isinstance(example1, MutableSeq) : continue
00285             str1 = str(example1)
00286             self.assertEqual(str(example1.lower()), str1.lower())

Here is the call graph for this function:

Check obj.back_transcribe() method.

Definition at line 405 of file test_Seq_objs.py.

00405 
00406     def test_the_back_transcription(self):
00407             """Check obj.back_transcribe() method."""
00408             mapping = ""
00409             for example1 in self._examples:
00410                 if isinstance(example1, MutableSeq) : continue
00411                 try :
00412                     tran = example1.back_transcribe()
00413                 except ValueError, e:
00414                     if str(e) == "Proteins cannot be back transcribed!" : continue
00415                     if str(e) == "DNA cannot be back transcribed!" : continue
00416                     raise e
00417                 str1 = str(example1)
00418                 self.assertEqual(str1.replace("U","T").replace("u","t"), str(tran))
00419                 self.assertEqual(tran.alphabet, generic_dna) #based on limited examples             

Check obj.complement() method.

Definition at line 336 of file test_Seq_objs.py.

00336 
00337     def test_the_complement(self):
00338         """Check obj.complement() method."""
00339         mapping = ""
00340         for example1 in self._examples:
00341             if isinstance(example1, MutableSeq) : continue
00342             try :
00343                 comp = example1.complement()
00344             except ValueError, e:
00345                 self.assertEqual(str(e), "Proteins do not have complements!")
00346                 continue
00347             str1 = str(example1)
00348             #This only does the unambiguous cases
00349             if "U" in str1 or "u" in str1 \
00350             or example1.alphabet==generic_rna:
00351                 mapping = maketrans("ACGUacgu","UGCAugca")
00352             elif "T" in str1 or "t" in str1 \
00353             or example1.alphabet==generic_dna \
00354             or example1.alphabet==generic_nucleotide:
00355                 mapping = maketrans("ACGTacgt","TGCAtgca")
00356             elif "A" not in str1 and "a" not in str1:
00357                 mapping = maketrans("CGcg","GCgc")
00358             else :
00359                 #TODO - look at alphabet?
00360                 raise ValueError(example1)
00361             self.assertEqual(str1.translate(mapping), str(comp))
00362             self.assertEqual(comp.alphabet, example1.alphabet)
                
Check obj.reverse_complement() method.

Definition at line 363 of file test_Seq_objs.py.

00363 
00364     def test_the_reverse_complement(self):
00365         """Check obj.reverse_complement() method."""
00366         mapping = ""
00367         for example1 in self._examples:
00368             if isinstance(example1, MutableSeq) : continue
00369             try :
00370                 comp = example1.reverse_complement()
00371             except ValueError, e:
00372                 self.assertEqual(str(e), "Proteins do not have complements!")
00373                 continue
00374             str1 = str(example1)
00375             #This only does the unambiguous cases
00376             if "U" in str1 or "u" in str1 \
00377             or example1.alphabet==generic_rna:
00378                 mapping = maketrans("ACGUacgu","UGCAugca")
00379             elif "T" in str1 or "t" in str1 \
00380             or example1.alphabet==generic_dna \
00381             or example1.alphabet==generic_nucleotide:
00382                 mapping = maketrans("ACGTacgt","TGCAtgca")
00383             elif "A" not in str1 and "a" not in str1:
00384                 mapping = maketrans("CGcg","GCgc")
00385             else :
00386                 #TODO - look at alphabet?
00387                 continue
00388             self.assertEqual(str1.translate(mapping)[::-1], str(comp))
00389             self.assertEqual(comp.alphabet, example1.alphabet)

Check obj.transcribe() method.

Definition at line 390 of file test_Seq_objs.py.

00390 
00391     def test_the_transcription(self):
00392             """Check obj.transcribe() method."""
00393             mapping = ""
00394             for example1 in self._examples:
00395                 if isinstance(example1, MutableSeq) : continue
00396                 try :
00397                     tran = example1.transcribe()
00398                 except ValueError, e:
00399                     if str(e) == "Proteins cannot be transcribed!" : continue
00400                     if str(e) == "RNA cannot be transcribed!" : continue
00401                     raise e
00402                 str1 = str(example1)
00403                 self.assertEqual(str1.replace("T","U").replace("t","u"), str(tran))
00404                 self.assertEqual(tran.alphabet, generic_rna) #based on limited examples             

Check obj.translate() method.

Definition at line 420 of file test_Seq_objs.py.

00420 
00421     def test_the_translate(self):
00422             """Check obj.translate() method."""
00423             mapping = ""
00424             for example1 in self._examples:
00425                 if isinstance(example1, MutableSeq) : continue
00426                 try :
00427                     tran = example1.translate()
00428                 except ValueError, e:
00429                     if str(e) == "Proteins cannot be translated!" : continue
00430                     raise e
00431                 #This is based on the limited example not having stop codons:
00432                 if tran.alphabet not in [extended_protein, protein, generic_protein]:
00433                     print tran.alphabet
00434                     self.assertTrue(False)
00435                 #TODO - check the actual translation, and all the optional args

Check obj.translate() method with ambiguous codons.

Definition at line 492 of file test_Seq_objs.py.

00492 
00493     def test_the_translation_of_ambig_codons(self):
00494         """Check obj.translate() method with ambiguous codons."""
00495         for letters, ambig_values in [(ambiguous_dna.letters, ambiguous_dna_values),
00496                                       (ambiguous_rna.letters, ambiguous_rna_values)] :
00497             ambig = set(letters)
00498             for c1 in ambig:
00499                 for c2 in ambig:
00500                     for c3 in ambig:
00501                         values = set([str(Seq(a+b+c).translate()) \
00502                                       for a in ambig_values[c1] \
00503                                       for b in ambig_values[c2] \
00504                                       for c in ambig_values[c3]])
00505                         t = str(Seq(c1+c2+c3).translate())
00506                         if t=="*":
00507                             self.assertEqual(values, set("*"))
00508                         elif t=="X":
00509                             self.assertTrue(len(values) > 1, \
00510                                 "translate('%s') = '%s' not '%s'" \
00511                                 % (c1+c2+c3, t, ",".join(values)))
00512                         elif t=="Z":
00513                             self.assertEqual(values, set("EQ"))
00514                         elif t=="B":
00515                             self.assertEqual(values, set("DN"))
00516                         elif t=="J":
00517                             self.assertEqual(values, set("LI"))
00518                         else:
00519                             self.assertEqual(values, set(t))
00520                         #TODO - Use the Bio.Data.IUPACData module for the
00521                         #ambiguous protein mappings?

Here is the call graph for this function:

Check obj.translate() method with invalid codons.

Definition at line 479 of file test_Seq_objs.py.

00479 
00480     def test_the_translation_of_invalid_codons(self):
00481         """Check obj.translate() method with invalid codons."""
00482         for codon in ["TA?", "N-N", "AC_", "Ac_"]:
00483             for nuc in [Seq(codon),
00484                         Seq(codon, generic_nucleotide),
00485                         Seq(codon, generic_dna),
00486                         Seq(codon, unambiguous_dna)]:
00487                 try :
00488                     print nuc.translate()
00489                     self.assertTrue(False, "Transating %s should fail" % codon)
00490                 except TranslationError :
00491                     pass

Check obj.translate() method with stop codons.

Definition at line 436 of file test_Seq_objs.py.

00436 
00437     def test_the_translation_of_stops(self):
00438         """Check obj.translate() method with stop codons."""
00439         misc_stops = "TAATAGTGAAGAAGG"
00440         for nuc in [Seq(misc_stops),
00441                     Seq(misc_stops, generic_nucleotide),
00442                     Seq(misc_stops, generic_dna),
00443                     Seq(misc_stops, unambiguous_dna)]:
00444             self.assertEqual("***RR", str(nuc.translate()))
00445             self.assertEqual("***RR", str(nuc.translate(1)))
00446             self.assertEqual("***RR", str(nuc.translate("SGC0")))
00447             self.assertEqual("**W**", str(nuc.translate(table=2)))
00448             self.assertEqual("**WRR", str(nuc.translate(table='Yeast Mitochondrial')))
00449             self.assertEqual("**WSS", str(nuc.translate(table=5)))
00450             self.assertEqual("**WSS", str(nuc.translate(table=9)))
00451             self.assertEqual("**CRR", str(nuc.translate(table='Euplotid Nuclear')))
00452             self.assertEqual("***RR", str(nuc.translate(table=11)))
00453             self.assertEqual("***RR", str(nuc.translate(table='11')))
00454             self.assertEqual("***RR", str(nuc.translate(table='Bacterial')))
00455             self.assertEqual("", str(nuc.translate(to_stop=True)))
00456             self.assertEqual("O*ORR", str(nuc.translate(table=special_table)))
00457             self.assertEqual("*QWRR", str(nuc.translate(table=Chilodonella_uncinata_table)))
00458             #These test the Bio.Seq.translate() function - move these?:
00459             self.assertEqual("*QWRR", translate(str(nuc), table=Chilodonella_uncinata_table))
00460             self.assertEqual("O*ORR", translate(str(nuc), table=special_table))
00461             self.assertEqual("", translate(str(nuc), to_stop=True))
00462             self.assertEqual("***RR", translate(str(nuc), table='Bacterial'))
00463             self.assertEqual("***RR", translate(str(nuc), table='11'))
00464             self.assertEqual("***RR", translate(str(nuc), table=11))
00465             self.assertEqual("**W**", translate(str(nuc), table=2))
00466         self.assertEqual(str(Seq("TAT").translate()), "Y")
00467         self.assertEqual(str(Seq("TAR").translate()), "*")
00468         self.assertEqual(str(Seq("TAN").translate()), "X")
00469         self.assertEqual(str(Seq("NNN").translate()), "X")
00470         self.assertEqual(str(Seq("TAt").translate()), "Y")
00471         self.assertEqual(str(Seq("TaR").translate()), "*")
00472         self.assertEqual(str(Seq("TaN").translate()), "X")
00473         self.assertEqual(str(Seq("nnN").translate()), "X")
00474         self.assertEqual(str(Seq("tat").translate()), "Y")
00475         self.assertEqual(str(Seq("tar").translate()), "*")
00476         self.assertEqual(str(Seq("tan").translate()), "X")
00477         self.assertEqual(str(Seq("nnn").translate()), "X")
00478 

Here is the call graph for this function:

Check obj.tomutable() method.

Definition at line 315 of file test_Seq_objs.py.

00315 
00316     def test_tomutable(self):
00317         """Check obj.tomutable() method."""
00318         for example1 in self._examples:
00319             if isinstance(example1, MutableSeq) : continue
00320             mut = example1.tomutable()
00321             self.assertTrue(isinstance(mut, MutableSeq))
00322             self.assertEqual(str(mut), str(example1))
00323             self.assertEqual(mut.alphabet, example1.alphabet)

Check obj.toseq() method.

Definition at line 324 of file test_Seq_objs.py.

00324 
00325     def test_toseq(self):
00326         """Check obj.toseq() method."""
00327         for example1 in self._examples:
00328             try :
00329                 seq = example1.toseq()
00330             except AttributeError :
00331                 self.assertTrue(isinstance(example1, Seq))
00332                 continue
00333             self.assertTrue(isinstance(seq, Seq))
00334             self.assertEqual(str(seq), str(example1))
00335             self.assertEqual(seq.alphabet, example1.alphabet)

Check str(obj) and obj.tostring() match.

Definition at line 309 of file test_Seq_objs.py.

00309 
00310     def test_tostring(self):
00311         """Check str(obj) and obj.tostring() match."""
00312         for example1 in self._examples:
00313             str1 = str(example1)
00314             self.assertEqual(example1.tostring(), str1)


Member Data Documentation

Initial value:
[ \
        Seq("ACGTGGGGT", generic_protein),
        Seq("ACGTGGGGT", generic_nucleotide),
        Seq("ACGTGGGGT", generic_dna),
        Seq("ACGUGGGGU", generic_rna),
        Seq("GG", generic_protein),
        Seq("GG", generic_nucleotide),
        Seq("GG", generic_dna),
        Seq("GG", generic_rna),
        Seq("A", generic_protein),
        Seq("A", generic_nucleotide),
        Seq("A", generic_dna),
        Seq("A", generic_rna),
        UnknownSeq(1),
        UnknownSeq(1, character="n"),
        UnknownSeq(1, generic_rna),
        UnknownSeq(1, generic_rna, "n"),
        UnknownSeq(1, generic_rna, "N"),
        UnknownSeq(10, generic_rna, "N"),
        UnknownSeq(10, generic_dna, "N"),
        UnknownSeq(10, generic_nucleotide, "N"),
        UnknownSeq(10, generic_protein, "X"),
        UnknownSeq(10, character="X"),
        UnknownSeq(10),
        ]

Definition at line 65 of file test_Seq_objs.py.

list test_Seq_objs.StringMethodTests._start_end_values = [0, 1, 2, 1000, -1, -2, -999] [static, private]

Definition at line 93 of file test_Seq_objs.py.


The documentation for this class was generated from the following file: