def writeToFile(self, outfile, write_ranges=True, format="plain",
                    options=None):
        if format == "plain-fasta":
            format = "fasta"
            write_ranges = False

        write_ranges = write_ranges and self.mWriteRanges

        if format == "plain":
            for identifier in self.mIdentifiers:
                m = self.mMali[identifier]
                outfile.write("%i\t%s\t%i\t%s\n" % (
                    m.mFrom + 1, m.mString, m.mTo, m.getIdentifier()))

        elif format == "fasta":
            for identifier in self.mIdentifiers:
                m = self.mMali[identifier]
                if write_ranges:
                    outfile.write(
                        ">%s/%i-%i\n%s\n" % (m.getIdentifier(), m.mFrom + 1, m.mTo, m.mString))
                else:
                    outfile.write(">%s\n%s\n" % (identifier, m.mString))

        elif format == "stockholm":
            outfile.write("# STOCKHOLM 1.0\n")

            if options:
                for o in options:
                    outfile.write("%s\n" % o)

            # calculate offset:
            max_l = 0
            for identifier in self.mIdentifiers:
                m = self.mMali[identifier]
                id = m.getIdentifier()
                # tab does not work as separator
                if m.mTo and write_ranges:
                    x = "%s/%i-%i" % (id, m.mFrom + 1, m.mTo)
                else:
                    x = "%s" % (id)
                max_l = max(max_l, len(x))
            for identifier in self.mAnnotations.keys():
                x = "#=GC %s" % identifier
                max_l = max(max_l, len(x))

            format = "%-" + str(max_l) + "s  %s\n"
            for identifier in self.mIdentifiers:
                m = self.mMali[identifier]
                id = m.getIdentifier()
                # tab does not work as separator
                if m.mTo and write_ranges:
                    x = "%s/%i-%i" % (id, m.mFrom + 1, m.mTo)
                else:
                    x = "%s" % (id)

                outfile.write(format % (x, m.mString))

            for identifier, value in self.mAnnotations.items():
                x = "#=GC %s" % identifier
                outfile.write(format % (x, value))

            outfile.write("//\n")

        elif format == "phylip":

            outfile.write("%i %i\n" % (self.getLength(), self.getWidth()))

            for identifier in self.mIdentifiers:
                outfile.write("%s   %s\n" %
                              (identifier, self.mMali[identifier].mString))

        elif format.lower() == "profile":
            if self.mName:
                name = self.mName
            else:
                name = ",".join(self.mIdentifiers)

            outfile.write(">profile=%s length=%i width=%i\n" %
                          (name, self.getWidth(), self.getLength()))

            for identifier in self.mIdentifiers:
                outfile.write("%s\n" % (self.mMali[identifier].mString))

        elif format == "nexus":
            # nexus formatted output - MrBayes conformant.
            outfile.write("#NEXUS\n")
            outfile.write("begin data;\n")
            outfile.write("  dimensions ntax=%i nchar=%i;\n" %
                          (self.getLength(), self.getWidth()))
            outfile.write(
                "  format datatype=dna interleave=no gap=%s;\n" % (self.mGapChar))
            outfile.write("  matrix\n")
            max_len = max(map(lambda x: len(x), self.mIdentifiers))
            format = "  %-" + str(max_len) + "s %s\n"
            for identifier in self.mIdentifiers:
                outfile.write(
                    format % (identifier, self.mMali[identifier].mString))
            outfile.write("  ;\n")
            outfile.write("end;\n")

        else:
            raise "unknown alignment format %s" % format
write alignment to file.
dataset/ETHPy150Open CGATOxford/cgat/CGAT/Mali.py/Mali.writeToFile