Back to index

python-biopython  1.60
Functions | Variables
test_AlignIO Namespace Reference

Functions

def str_summary
def alignment_summary
def check_simple_write_read
def simple_alignment_comparison
def check_phylip_reject_duplicate

Variables

tuple test_write_read_alignment_formats = sorted(AlignIO._FormatToWriter)
 test_write_read_align_with_seq_count = test_write_read_alignment_formats\
list test_files
tuple handle = StringIO()
tuple alignments = list(AlignIO.parse(handle, t_format))
tuple list_of_records = list(AlignIO.read(open("Clustalw/opuntia.aln"),"clustal"))
list alignments2 = []
list alignments3 = []
tuple seq_iterator = AlignIO.parse(handle=open(t_filename,"r"), format=t_format)
tuple record = seq_iterator.next()
list alignments4 = [record]
list alignments5 = [record]
tuple alignment = AlignIO.read(handle=open(t_filename), format=t_format)
tuple summary = AlignInfo.SummaryInfo(alignment)
tuple dumb_consensus = summary.dumb_consensus()
tuple pssm = summary.pos_specific_score_matrix()
tuple rep_dict = summary.replacement_dictionary()
tuple info_content = summary.information_content()
tuple data = open(t_filename,"r")

Function Documentation

def test_AlignIO.alignment_summary (   alignment,
  index = "  ",
  vertical_threshold = 5 
)
Returns a concise summary of an Alignment object as a string.

Definition at line 77 of file test_AlignIO.py.

00077 
00078 def alignment_summary(alignment, index="  ", vertical_threshold=5):
00079     """Returns a concise summary of an Alignment object as a string."""
00080     answer = []
00081     alignment_len = alignment.get_alignment_length()
00082     rec_count = len(alignment)
00083     if rec_count < vertical_threshold:
00084         #Show each sequence row horizontally
00085         for record in alignment:
00086             answer.append("%s%s %s" \
00087             % (index,str_summary(record.seq.tostring()),record.id))
00088     else:
00089         #Show each sequence row vertically
00090         for i in range(min(5,alignment_len)):
00091             answer.append(index + str_summary(alignment[:,i]) \
00092                                 + " alignment column %i" % i)
00093         if alignment_len > 5:
00094             i = alignment_len - 1
00095             answer.append(index + str_summary("|" * rec_count) \
00096                                 + " ...")
00097             answer.append(index + str_summary(alignment[:,i]) \
00098                                 + " alignment column %i" % i)
00099     return "\n".join(answer)
00100 

Here is the call graph for this function:

Ensure that attempting to write sequences with duplicate IDs after
truncation fails for Phylip format.

Definition at line 199 of file test_AlignIO.py.

00199 
00200 def check_phylip_reject_duplicate():
00201     """
00202     Ensure that attempting to write sequences with duplicate IDs after
00203     truncation fails for Phylip format.
00204     """
00205     handle = StringIO()
00206     sequences = [SeqRecord(Seq('AAAA'), id='longsequencename1'),
00207                  SeqRecord(Seq('AAAA'), id='longsequencename2'),
00208                  SeqRecord(Seq('AAAA'), id='other_sequence'),]
00209     alignment = MultipleSeqAlignment(sequences)
00210     try:
00211         # This should raise a ValueError
00212         AlignIO.write(alignment, handle, 'phylip')
00213         assert False, "Duplicate IDs after truncation are not allowed."
00214     except ValueError, e:
00215         # Expected - check the error
00216         assert "Repeated name 'longsequen'" in str(e)
00217 
00218 check_phylip_reject_duplicate()
00219 
00220 
#Check parsers can cope with an empty file
def test_AlignIO.check_simple_write_read (   alignments,
  indent = " " 
)

Definition at line 101 of file test_AlignIO.py.

00101 
00102 def check_simple_write_read(alignments, indent=" "):
00103     #print indent+"Checking we can write and then read back these alignments"
00104     for format in test_write_read_align_with_seq_count:
00105         records_per_alignment = len(alignments[0])
00106         for a in alignments:
00107             if records_per_alignment != len(a):
00108                 records_per_alignment = None
00109         #Can we expect this format to work?
00110         if not records_per_alignment \
00111         and format not in test_write_read_alignment_formats:
00112             continue
00113 
00114         print indent+"Checking can write/read as '%s' format" % format
00115 
00116         #Going to write to a handle...
00117         handle = StringIO()
00118 
00119         try:
00120             c = AlignIO.write(alignments, handle=handle, format=format)
00121             assert c == len(alignments)
00122         except ValueError, e:
00123             #This is often expected to happen, for example when we try and
00124             #write sequences of different lengths to an alignment file.
00125             print indent+"Failed: %s" % str(e)
00126             #Carry on to the next format:
00127             continue
00128 
00129         #First, try with the seq_count
00130         if records_per_alignment:
00131             handle.flush()
00132             handle.seek(0)
00133             try:
00134                 alignments2 = list(AlignIO.parse(handle=handle, format=format, \
00135                                                  seq_count=records_per_alignment))
00136             except ValueError, e:
00137                 #This is BAD.  We can't read our own output.
00138                 #I want to see the output when called from the test harness,
00139                 #run_tests.py (which can be funny about new lines on Windows)
00140                 handle.seek(0)
00141                 raise ValueError("%s\n\n%s\n\n%s" \
00142                                   % (str(e), repr(handle.read()), repr(alignments2)))
00143             simple_alignment_comparison(alignments, alignments2, format)
00144 
00145         if format in test_write_read_alignment_formats:
00146             #Don't need the seq_count
00147             handle.flush()
00148             handle.seek(0)
00149             try:
00150                 alignments2 = list(AlignIO.parse(handle=handle, format=format))
00151             except ValueError, e:
00152                 #This is BAD.  We can't read our own output.
00153                 #I want to see the output when called from the test harness,
00154                 #run_tests.py (which can be funny about new lines on Windows)
00155                 handle.seek(0)
00156                 raise ValueError("%s\n\n%s\n\n%s" \
00157                                   % (str(e), repr(handle.read()), repr(alignments2)))
00158             simple_alignment_comparison(alignments, alignments2, format)
00159 
00160         if len(alignments)>1:
00161             #Try writing just one Alignment (not a list)
00162             handle = StringIO()
00163             SeqIO.write(alignments[0], handle, format)
00164             assert handle.getvalue() == alignments[0].format(format)

Here is the call graph for this function:

def test_AlignIO.simple_alignment_comparison (   alignments,
  alignments2,
  format 
)

Definition at line 165 of file test_AlignIO.py.

00165 
00166 def simple_alignment_comparison(alignments, alignments2, format):
00167     assert len(alignments) == len(alignments2)
00168     for a1, a2 in zip(alignments, alignments2):
00169         assert a1.get_alignment_length() == a2.get_alignment_length()
00170         assert len(a1) == len(a2)
00171         for r1, r2 in zip(a1,a2):
00172             #Check the bare minimum (ID and sequence) as
00173             #many formats can't store more than that.
00174 
00175             #Check the sequence
00176             assert r1.seq.tostring() == r2.seq.tostring()
00177 
00178             #Beware of different quirks and limitations in the
00179             #valid character sets and the identifier lengths!
00180             if format in ["phylip", "phylip-sequential"]:
00181                 assert r1.id.replace("[","").replace("]","")[:10] == r2.id, \
00182                        "'%s' vs '%s'" % (r1.id, r2.id)
00183             elif format=="phylip-relaxed":
00184                 assert r1.id.replace(" ", "").replace(':', '|') == r2.id, \
00185                         "'%s' vs '%s'" % (r1.id, r2.id)
00186             elif format=="clustal":
00187                 assert r1.id.replace(" ","_")[:30] == r2.id, \
00188                        "'%s' vs '%s'" % (r1.id, r2.id)
00189             elif format=="stockholm":
00190                 assert r1.id.replace(" ","_") == r2.id, \
00191                        "'%s' vs '%s'" % (r1.id, r2.id)
00192             elif format=="fasta":
00193                 assert r1.id.split()[0] == r2.id
00194             else:
00195                 assert r1.id == r2.id, \
00196                        "'%s' vs '%s'" % (r1.id, r2.id)
00197     return True
00198 
#Check Phylip files reject duplicate identifiers.

Here is the caller graph for this function:

def test_AlignIO.str_summary (   text,
  max_len = 40 
)

Definition at line 71 of file test_AlignIO.py.

00071 
00072 def str_summary(text, max_len=40):
00073     if len(text) <= max_len:
00074         return text
00075     else:
00076         return text[:max_len-4] + "..." + text[-3:]

Here is the caller graph for this function:


Variable Documentation

tuple test_AlignIO.alignment = AlignIO.read(handle=open(t_filename), format=t_format)

Definition at line 307 of file test_AlignIO.py.

tuple test_AlignIO.alignments = list(AlignIO.parse(handle, t_format))

Definition at line 223 of file test_AlignIO.py.

Definition at line 262 of file test_AlignIO.py.

Definition at line 268 of file test_AlignIO.py.

Definition at line 285 of file test_AlignIO.py.

Definition at line 298 of file test_AlignIO.py.

tuple test_AlignIO.data = open(t_filename,"r")

Definition at line 345 of file test_AlignIO.py.

tuple test_AlignIO.dumb_consensus = summary.dumb_consensus()

Definition at line 330 of file test_AlignIO.py.

tuple test_AlignIO.handle = StringIO()

Definition at line 222 of file test_AlignIO.py.

tuple test_AlignIO.info_content = summary.information_content()

Definition at line 337 of file test_AlignIO.py.

tuple test_AlignIO.list_of_records = list(AlignIO.read(open("Clustalw/opuntia.aln"),"clustal"))

Definition at line 234 of file test_AlignIO.py.

tuple test_AlignIO.pssm = summary.pos_specific_score_matrix()

Definition at line 334 of file test_AlignIO.py.

tuple test_AlignIO.record = seq_iterator.next()

Definition at line 272 of file test_AlignIO.py.

tuple test_AlignIO.rep_dict = summary.replacement_dictionary()

Definition at line 335 of file test_AlignIO.py.

tuple test_AlignIO.seq_iterator = AlignIO.parse(handle=open(t_filename,"r"), format=t_format)

Definition at line 269 of file test_AlignIO.py.

tuple test_AlignIO.summary = AlignInfo.SummaryInfo(alignment)

Definition at line 329 of file test_AlignIO.py.

Definition at line 27 of file test_AlignIO.py.

test_AlignIO.test_write_read_align_with_seq_count = test_write_read_alignment_formats\

Definition at line 16 of file test_AlignIO.py.

tuple test_AlignIO.test_write_read_alignment_formats = sorted(AlignIO._FormatToWriter)

Definition at line 15 of file test_AlignIO.py.