1# Copyright 2003 by Bartek Wilczynski.  All rights reserved.
2#
3# This file is part of the Biopython distribution and governed by your
4# choice of the "Biopython License Agreement" or the "BSD 3-Clause License".
5# Please see the LICENSE file that should have been included as part of this
6# package.
7
8"""Parsing AlignACE output files."""
9
10from Bio.motifs import Motif, Instances
11from Bio.Seq import Seq
12
13
14class Record(list):
15    """AlignACE record (subclass of Python list)."""
16
17    def __init__(self):
18        """Initialize the class."""
19        self.parameters = None
20
21
22def read(handle):
23    """Parse an AlignACE format handle as a Record object."""
24    record = Record()
25    line = next(handle)
26    record.version = line.strip()
27    line = next(handle)
28    record.command = line.strip()
29    mask = None
30    number = None
31    for line in handle:
32        line = line.strip()
33        if line == "":
34            pass
35        elif line[:4] == "Para":
36            record.parameters = {}
37        elif line[0] == "#":
38            seq_name = line.split("\t")[1]
39            record.sequences.append(seq_name)
40        elif "=" in line:
41            par_name, par_value = line.split("=")
42            par_name = par_name.strip()
43            par_value = par_value.strip()
44            record.parameters[par_name] = par_value
45        elif line[:5] == "Input":
46            record.sequences = []
47        elif line[:5] == "Motif":
48            words = line.split()
49            assert words[0] == "Motif"
50            number = int(words[1])
51            instances = []
52        elif line[:3] == "MAP":
53            alphabet = "ACGT"
54            instances = Instances(instances, alphabet)
55            motif = Motif(alphabet, instances)
56            motif.score = float(line.split()[-1])
57            motif.number = number
58            motif.mask = mask
59            record.append(motif)
60        elif len(line.split("\t")) == 4:
61            seq = Seq(line.split("\t")[0])
62            instances.append(seq)
63        elif "*" in line:
64            mask = line.strip("\r\n")
65        else:
66            raise ValueError(line)
67    return record
68