Skip to content

Commit

Permalink
No code changes. Removing white space before ':' character in SeqIO a…
Browse files Browse the repository at this point in the history
…nd AlignIO modules to match PEP8 style guide.
  • Loading branch information
peterjc committed Nov 11, 2009
1 parent 168a79f commit 7fedbbd
Show file tree
Hide file tree
Showing 21 changed files with 935 additions and 935 deletions.
88 changes: 44 additions & 44 deletions Bio/AlignIO/ClustalIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@
from Bio.Align.Generic import Alignment
from Interfaces import AlignmentIterator, SequentialAlignmentWriter

class ClustalWriter(SequentialAlignmentWriter) :
class ClustalWriter(SequentialAlignmentWriter):
"""Clustalw alignment writer."""
def write_alignment(self, alignment) :
def write_alignment(self, alignment):
"""Use this to write (another) single alignment to an open file."""

if len(alignment.get_all_seqs()) == 0 :
if len(alignment.get_all_seqs()) == 0:
raise ValueError("Must have at least one sequence")

#Old versions of the parser in Bio.Clustalw used a ._version property,
try :
try:
version = str(alignment._version)
except AttributeError :
except AttributeError:
version = ""
if not version :
if not version:
version = '1.81'
if version.startswith("2.") :
if version.startswith("2."):
#e.g. 2.0.x
output = "CLUSTAL %s multiple sequence alignment\n\n\n" % version
else :
else:
#e.g. 1.81 or 1.83
output = "CLUSTAL X (%s) multiple sequence alignment\n\n\n" % version

cur_char = 0
max_length = len(alignment._records[0].seq)

if max_length <= 0 :
if max_length <= 0:
raise ValueError("Non-empty sequences are required")

# keep displaying sequences until we reach the end
Expand Down Expand Up @@ -73,12 +73,12 @@ def write_alignment(self, alignment) :
# Want a trailing blank new line in case the output is concatenated
self.handle.write(output + "\n")

class ClustalIterator(AlignmentIterator) :
class ClustalIterator(AlignmentIterator):
"""Clustalw alignment iterator."""

def next(self) :
def next(self):
handle = self.handle
try :
try:
#Header we saved from when we were parsing
#the previous alignment.
line = self._header
Expand All @@ -97,7 +97,7 @@ def next(self) :

# find the clustal version in the header line
version = None
for word in line.split() :
for word in line.split():
if word[0]=='(' and word[-1]==')':
word = word[1:-1]
if word[0] in '0123456789':
Expand All @@ -106,7 +106,7 @@ def next(self) :

#There should be two blank lines after the header line
line = handle.readline()
while line.strip() == "" :
while line.strip() == "":
line = handle.readline()

#If the alignment contains entries with the same sequence
Expand All @@ -118,8 +118,8 @@ def next(self) :
seq_cols = None #: Used to extract the consensus

#Use the first block to get the sequence identifiers
while True :
if line[0] != " " and line.strip() != "" :
while True:
if line[0] != " " and line.strip() != "":
#Sequences identifier...
fields = line.rstrip().split()

Expand All @@ -132,22 +132,22 @@ def next(self) :
seqs.append(fields[1])

#Record the sequence position to get the consensus
if seq_cols is None :
if seq_cols is None:
start = len(fields[0]) + line[len(fields[0]):].find(fields[1])
end = start + len(fields[1])
seq_cols = slice(start, end)
del start, end
assert fields[1] == line[seq_cols]

if len(fields) == 3 :
if len(fields) == 3:
#This MAY be an old style file with a letter count...
try :
try:
letters = int(fields[2])
except ValueError :
except ValueError:
raise ValueError("Could not parse line, bad sequence number:\n%s" % line)
if len(fields[1].replace("-","")) != letters :
if len(fields[1].replace("-","")) != letters:
raise ValueError("Could not parse line, invalid sequence number:\n%s" % line)
elif line[0] == " " :
elif line[0] == " ":
#Sequence consensus line...
assert len(ids) == len(seqs)
assert len(ids) > 0
Expand All @@ -159,7 +159,7 @@ def next(self) :
line = handle.readline()
assert line.strip() == ""
break
else :
else:
#No consensus
break
line = handle.readline()
Expand All @@ -169,18 +169,18 @@ def next(self) :
assert seq_cols is not None

#Confirm all same length
for s in seqs :
for s in seqs:
assert len(s) == len(seqs[0])
if consensus :
if consensus:
assert len(consensus) == len(seqs[0])

#Loop over any remaining blocks...
done = False
while not done :
while not done:
#There should be a blank line between each block.
#Also want to ignore any consensus line from the
#previous block.
while (not line) or line.strip() == "" :
while (not line) or line.strip() == "":
line = handle.readline()
if not line : break # end of file
if not line : break # end of file
Expand All @@ -191,7 +191,7 @@ def next(self) :
self._header = line
break

for i in range(len(ids)) :
for i in range(len(ids)):
assert line[0] != " ", "Unexpected line:\n%s" % repr(line)
fields = line.rstrip().split()

Expand All @@ -200,11 +200,11 @@ def next(self) :
if len(fields) < 2 or len(fields) > 3:
raise ValueError("Could not parse line:\n%s" % repr(line))

if fields[0] != ids[i] :
if fields[0] != ids[i]:
raise ValueError("Identifiers out of order? Got '%s' but expected '%s'" \
% (fields[0], ids[i]))

if fields[1] != line[seq_cols] :
if fields[1] != line[seq_cols]:
start = len(fields[0]) + line[len(fields[0]):].find(fields[1])
assert start == seq_cols.start, 'Old location %s -> %i:XX' % (seq_cols, start)
end = start + len(fields[1])
Expand All @@ -215,19 +215,19 @@ def next(self) :
seqs[i] += fields[1]
assert len(seqs[i]) == len(seqs[0])

if len(fields) == 3 :
if len(fields) == 3:
#This MAY be an old style file with a letter count...
try :
try:
letters = int(fields[2])
except ValueError :
except ValueError:
raise ValueError("Could not parse line, bad sequence number:\n%s" % line)
if len(seqs[i].replace("-","")) != letters :
if len(seqs[i].replace("-","")) != letters:
raise ValueError("Could not parse line, invalid sequence number:\n%s" % line)

#Read in the next line
line = handle.readline()
#There should now be a consensus line
if consensus :
if consensus:
assert line[0] == " "
assert seq_cols is not None
consensus += line[seq_cols]
Expand All @@ -239,32 +239,32 @@ def next(self) :


assert len(ids) == len(seqs)
if len(seqs) == 0 or len(seqs[0]) == 0 :
if len(seqs) == 0 or len(seqs[0]) == 0:
return None

if self.records_per_alignment is not None \
and self.records_per_alignment != len(ids) :
and self.records_per_alignment != len(ids):
raise ValueError("Found %i records in this alignment, told to expect %i" \
% (len(ids), self.records_per_alignment))

alignment = Alignment(self.alphabet)
alignment_length = len(seqs[0])
for i in range(len(ids)) :
for i in range(len(ids)):
if len(seqs[i]) != alignment_length:
raise ValueError("Error parsing alignment - sequences of different length?")
alignment.add_sequence(ids[i], seqs[i])
#TODO - Handle alignment annotation better, for now
#mimic the old parser in Bio.Clustalw
if version :
if version:
alignment._version = version
if consensus :
if consensus:
assert len(consensus) == alignment_length, \
"Alignment length is %i, consensus length is %i, '%s'" \
% (alignment_length, len(consensus), consensus)
alignment._star_info = consensus
return alignment

if __name__ == "__main__" :
if __name__ == "__main__":
print "Running a quick self-test"

#This is a truncated version of the example in Tests/cw02.aln
Expand Down Expand Up @@ -364,7 +364,7 @@ def next(self) :
"TDPTYAPFESKNS-QGELVGFDIDLAKELCKRINTQCTFVENPLDALIPS" + \
"LKAKKIDAIMSSLSITEKRQQEIAFTDKLYAADSRLV"

for alignment in ClustalIterator(StringIO(aln_example2 + aln_example1)) :
for alignment in ClustalIterator(StringIO(aln_example2 + aln_example1)):
print "Alignment with %i records of length %i" \
% (len(alignment.get_all_seqs()),
alignment.get_alignment_length())
Expand All @@ -378,7 +378,7 @@ def next(self) :
handle = StringIO()
ClustalWriter(handle).write_file(alignments)
handle.seek(0)
for i,a in enumerate(ClustalIterator(handle)) :
for i,a in enumerate(ClustalIterator(handle)):
assert a.get_alignment_length() == alignments[i].get_alignment_length()
handle.seek(0)

Expand All @@ -387,7 +387,7 @@ def next(self) :
handle = StringIO()
ClustalWriter(handle).write_file([alignment])
handle.seek(0)
for i,a in enumerate(ClustalIterator(handle)) :
for i,a in enumerate(ClustalIterator(handle)):
assert a.get_alignment_length() == alignment.get_alignment_length()
assert len(a.get_all_seqs()) == 1

Expand Down
Loading

0 comments on commit 7fedbbd

Please sign in to comment.