/usr/lib/python2.7/dist-packages/cogent/format/fasta.py is in python-cogent 1.9-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | #!/usr/bin/env python
"""Writer for FASTA sequence format
"""
__author__ = "Jeremy Widmann"
__copyright__ = "Copyright 2007-2016, The Cogent Project"
__credits__ = ["Jeremy Widmann", "Rob Knight", "Gavin Huttley"]
__license__ = "GPL"
__version__ = "1.9"
__maintainer__ = "Jeremy Widmann"
__email__ = "jeremy.widmann@colorado.edu"
__status__ = "Production"
class _fake_seq(str):
"""a holder for string sequences that allows provision of a seq.Label
attribute, required by fasta formatting funcs."""
def __new__(cls, Label, Seq):
new = str.__new__(cls, Seq)
new.Label = Label
return new
def __getslice__(self, *args, **kwargs):
new_seq = str.__getslice__(self, *args, **kwargs)
return self.__new__(self.__class__,self.Label, new_seq)
def fasta_from_sequences(seqs, make_seqlabel = None, line_wrap = None):
"""Returns a FASTA string given a list of sequences. A sequence.Label
attribute takes precedence over sequence.Name.
- seqs can be a list of sequence objects or strings.
- make_seqlabel: callback function that takes the seq object and returns
a label str
- line_wrap: a integer for maximum line width
"""
fasta_list = []
for i,seq in enumerate(seqs):
# Check if it has a label, or one is to be created
label = str(i)
if make_seqlabel is not None:
label = make_seqlabel(seq)
elif hasattr(seq, 'Label') and seq.Label:
label = seq.Label
elif hasattr(seq, 'Name') and seq.Name:
label = seq.Name
# wrap sequence lines
seq_str = str(seq)
if line_wrap is not None:
numlines,remainder = divmod(len(seq_str),line_wrap)
if remainder:
numlines += 1
body = ["%s" % seq_str[j*line_wrap:(j+1)*line_wrap] \
for j in range(numlines)]
else:
body = ["%s" % seq_str]
fasta_list.append('>'+label)
fasta_list += body
return '\n'.join(fasta_list)
def fasta_from_alignment(aln, make_seqlabel=None, line_wrap=None, sorted=True):
"""Returns a FASTA string given an alignment.
- aln can be an Alignment object or dict.
- make_seqlabel: callback function that takes the seq object and returns
a label str
- line_wrap: a integer for maximum line width
"""
# get seq output order
try:
order = aln.Names[:]
except AttributeError:
order = aln.keys()
if sorted:
order.sort()
try:
seq_dict = aln.NamedSeqs
except AttributeError:
seq_dict = aln
ordered_seqs = []
for label in order:
seq = seq_dict[label]
if isinstance(seq, str):
seq = _fake_seq(label, seq)
ordered_seqs.append(seq)
return fasta_from_sequences(ordered_seqs, make_seqlabel=make_seqlabel,
line_wrap=line_wrap)
|