Ejemplo n.º 1
0
def getMapPeptide2Cds(peptide_sequence, cds_sequence, options):
    """get map between peptide sequence and cds sequence.

    The returned alignment is in nucleotides.

    """

    # remove whitespaces form protein sequence
    p = re.sub(" ", "", peptide_sequence)

    # remove gaps and whitespaces from cds
    c = re.sub("[ .-]", "", cds_sequence)

    w = Genomics.Protein2Wobble(p.upper())

    if options.loglevel >= 6:
        options.stdlog.write("# peptide original (%5i): %s\n" % (len(p), p))
        options.stdlog.write("# cds original     (%5i): %s\n" % (len(c), c))
        options.stdlog.write("# wobble sequence  (%5i): %s\n" % (len(w), w))
        options.stdlog.flush()

    seq_wobble = alignlib_lite.py_makeSequence(w)
    seq_cds = alignlib_lite.py_makeSequence(c.upper())
    seq_peptide = alignlib_lite.py_makeSequence(p)

    map_p2c = alignlib_lite.py_makeAlignmentVector()

    try:
        AlignCodonBased(seq_wobble,
                        seq_cds,
                        seq_peptide,
                        map_p2c,
                        options=options)
    except ValueError, msg:
        raise ValueError("mapping error for sequence: %s" % (msg))
Ejemplo n.º 2
0
def getMapPeptide2Cds( peptide_sequence, cds_sequence, options ):
    """get map between peptide sequence and cds sequence.
    
    The returned alignment is in nucleotides.

    """
    
    ## remove whitespaces form protein sequence
    p = re.sub(" ", "", peptide_sequence )

    ## remove gaps and whitespaces from cds
    c = re.sub("[ .-]", "", cds_sequence )

    w = Genomics.Protein2Wobble( p.upper() )

    if options.loglevel >= 6:
        options.stdlog.write( "# peptide original (%5i): %s\n" % (len(p), p) )
        options.stdlog.write( "# cds original     (%5i): %s\n" % (len(c), c) )
        options.stdlog.write( "# wobble sequence  (%5i): %s\n" % (len(w), w) )
        options.stdlog.flush()

    seq_wobble = alignlib_lite.py_makeSequence( w )
    seq_cds = alignlib_lite.py_makeSequence( string.upper(c) )
    seq_peptide = alignlib_lite.py_makeSequence( p )

    map_p2c = alignlib_lite.py_makeAlignmentVector()

    try:
        AlignCodonBased( seq_wobble, seq_cds, seq_peptide, map_p2c, options = options )
    except ValueError, msg:
        raise ValueError( "mapping error for sequence: %s" % (msg) )
Ejemplo n.º 3
0
def AlignPair(pair, anchor=0):
    """align a pair of introns."""

    map_intron_a2b = alignlib_lite.py_makeAlignmentVector()

    if param_loglevel >= 1:
        print "# aligning %s-%i with %s-%i: lengths %i and %i" % (
            pair.mToken1, pair.mIntronId1, pair.mToken2, pair.mIntronId2,
            len(pair.mAlignedSequence1), len(pair.mAlignedSequence2))
        sys.stdout.flush()

    s1 = "A" * anchor + pair.mAlignedSequence1 + "A" * anchor
    s2 = "A" * anchor + pair.mAlignedSequence2 + "A" * anchor

    if param_method == "dialigned":
        dialign.Align(s1, s2, map_intron_a2b)
    elif param_method == "dialignedlgs":
        dialignlgs.Align(s1, s2, map_intron_a2b)
    elif param_method == "dbaligned":
        dba.Align(s1, s2, map_intron_a2b)
    elif param_method == "clusaligned":
        raise NotImplementedError("clustalw wrapper not up-to-date")
        clustal.Align(s1, s2, map_intron_a2b)

    if anchor:
        map_intron_a2b.removeRowRegion(
            anchor + len(pair.mAlignedSequence1) + 1,
            map_intron_a2b.getRowTo())
        map_intron_a2b.removeRowRegion(1, anchor)
        map_intron_a2b.removeColRegion(
            anchor + len(pair.mAlignedSequence2) + 1,
            map_intron_a2b.getColTo())
        map_intron_a2b.removeColRegion(1, anchor)
        map_intron_a2b.moveAlignment(-anchor, -anchor)

    if map_intron_a2b.getLength() == 0:
        if param_loglevel >= 1:
            print "# Error: empty intron alignment"
        return False

    seq1 = alignlib_lite.py_makeSequence(pair.mAlignedSequence1)
    seq2 = alignlib_lite.py_makeSequence(pair.mAlignedSequence2)

    data = alignlib_lite.py_AlignmentFormatExplicit(map_intron_a2b, seq1, seq2)

    pair.mFrom1, pair.mAlignedSequence1, pair.mTo1 = data.mRowFrom, data.mRowAlignment, data.mRowTo
    pair.mFrom2, pair.mAlignedSequence2, pair.mTo2 = data.mColFrom, data.mColAlignment, data.mColTo
    pair.mMethod = param_method

    pair.mNumGaps, pair.mLength = map_intron_a2b.getNumGaps(
    ), map_intron_a2b.getLength()
    pair.mAligned = pair.mLength - pair.mNumGaps

    if param_loglevel >= 2:
        print "# alignment success", pair.mAlignedSequence1, pair.mAlignedSequence2

    return True
Ejemplo n.º 4
0
def AlignPair(pair, anchor=0):
    """align a pair of introns."""

    map_intron_a2b = alignlib_lite.py_makeAlignmentVector()

    if param_loglevel >= 1:
        print "# aligning %s-%i with %s-%i: lengths %i and %i" % (pair.mToken1, pair.mIntronId1,
                                                                  pair.mToken2, pair.mIntronId2,
                                                                  len(pair.mAlignedSequence1),
                                                                  len(pair.mAlignedSequence2))
        sys.stdout.flush()

    s1 = "A" * anchor + pair.mAlignedSequence1 + "A" * anchor
    s2 = "A" * anchor + pair.mAlignedSequence2 + "A" * anchor

    if param_method == "dialigned":
        dialign.Align(s1, s2, map_intron_a2b)
    elif param_method == "dialignedlgs":
        dialignlgs.Align(s1, s2, map_intron_a2b)
    elif param_method == "dbaligned":
        dba.Align(s1, s2, map_intron_a2b)
    elif param_method == "clusaligned":
        raise NotImplementedError("clustalw wrapper not up-to-date")
        clustal.Align(s1, s2, map_intron_a2b)

    if anchor:
        map_intron_a2b.removeRowRegion(
            anchor + len(pair.mAlignedSequence1) + 1, map_intron_a2b.getRowTo())
        map_intron_a2b.removeRowRegion(1, anchor)
        map_intron_a2b.removeColRegion(
            anchor + len(pair.mAlignedSequence2) + 1, map_intron_a2b.getColTo())
        map_intron_a2b.removeColRegion(1, anchor)
        map_intron_a2b.moveAlignment(-anchor, -anchor)

    if map_intron_a2b.getLength() == 0:
        if param_loglevel >= 1:
            print "# Error: empty intron alignment"
        return False

    seq1 = alignlib_lite.py_makeSequence(pair.mAlignedSequence1)
    seq2 = alignlib_lite.py_makeSequence(pair.mAlignedSequence2)

    data = alignlib_lite.py_AlignmentFormatExplicit(map_intron_a2b, seq1, seq2)

    pair.mFrom1, pair.mAlignedSequence1, pair.mTo1 = data.mRowFrom, data.mRowAlignment, data.mRowTo
    pair.mFrom2, pair.mAlignedSequence2, pair.mTo2 = data.mColFrom, data.mColAlignment, data.mColTo
    pair.mMethod = param_method

    pair.mNumGaps, pair.mLength = map_intron_a2b.getNumGaps(
    ), map_intron_a2b.getLength()
    pair.mAligned = pair.mLength - pair.mNumGaps

    if param_loglevel >= 2:
        print "# alignment success", pair.mAlignedSequence1, pair.mAlignedSequence2

    return True
Ejemplo n.º 5
0
def main(argv=None):
    """script main.

    parses command line options in sys.argv, unless *argv* is given.
    """

    if argv is None:
        argv = sys.argv

    parser = E.OptionParser(
        version="%prog version: $Id: codemls2tsv.py 2781 2009-09-10 11:33:14Z andreas $")

    parser.add_option("--methods", dest="methods", type="choice", action="append",
                      choices=("summary-numbers", "jalview",
                               "positive-site-table", "positive-site-list",
                               "count-positive-sites"),
                      help="methods for analysis.")

    parser.add_option("--selection-mode", dest="selection_mode", type="choice",
                      choices=("all", "consistent", "emes"),
                      help="how to select positive sites.")

    parser.add_option("--prefix", dest="prefix", type="string",
                      help="prefix for rows.")

    parser.add_option("--pattern-input-filenames", dest="pattern_input_filenames", type="string",
                      help="input pattern.")

    parser.add_option("--filter-probability", dest="filter_probability", type="float",
                      help="threshold for probability above which to include positive sites [default=%default].")

    parser.add_option("--filter-omega", dest="filter_omega", type="float",
                      help="threshold for omega above which to include positive sites [default=%default].")

    parser.add_option("--models", dest="models", type="string",
                      help="restrict output to set of site specific models.")

    parser.add_option("--analysis", dest="analysis", type="string",
                      help="restrict output to set of analysis [beb|neb].")

    parser.add_option("--significance-threshold", dest="significance_threshold", type="float",
                      help="significance threshold for log-likelihood test.")

    parser.add_option("--filter-mali", dest="filter_mali", type="choice",
                      choices=("none", "gaps"),
                      help="filter by mali to remove gapped positions.")

    parser.add_option("--filename-mali", dest="filename_mali", type="string",
                      help="filename with multiple alignment used for calculating sites - used for filtering")

    parser.add_option("--filename-map-mali", dest="filename_map_mali", type="string",
                      help="filename with multiple alignment to map sites onto.")

    parser.add_option("--jalview-titles", dest="jalview_titles", type="string",
                      help="comma separated list of jalview annotation titles.")

    parser.add_option("--jalview-symbol", dest="jalview_symbol", type="string",
                      help="symbol to use in jalview.")

    parser.set_defaults(
        methods=[],
        prefix=None,
        filter_probability=0,
        filter_omega=0,
        models="",
        analysis="",
        significance_threshold=0.05,
        selection_mode="consistent",
        filename_mali=None,
        filename_map_mali=None,
        jalview_symbol="*",
        jalview_titles="",
        filter_mali=None,
    )

    (options, args) = E.Start(parser)

    if options.jalview_titles:
        options.jalview_titles = options.jalview_titles.split(",")
    else:
        options.jalview_titles = args

    options.models = options.models.split(",")
    options.analysis = options.analysis.split(",")

    for a in options.analysis:
        if a not in ("beb", "neb"):
            raise "unknown analysis section: '%s', possible values are 'beb' and/or 'neb'" % a

    for a in options.models:
        if a not in ("8", "2", "3"):
            raise "unknown model: '%s', possible values are 2, 3, 8" % a

    codeml = WrapperCodeML.CodeMLSites()

    # filter and extract functions
    filter_f = lambda x: x.mProbability >= options.filter_probability and x.mOmega >= options.filter_omega
    extract_f = lambda x: x.mResidue

    # read multiple results
    results = []
    ninput, noutput, nskipped = 0, 0, 0

    headers = []
    for f in args:
        ninput += 1
        try:
            results.append(codeml.parseOutput(open(f, "r").readlines()))
        except WrapperCodeML.UsageError:
            if options.loglevel >= 1:
                options.stdlog.write("# no input from %s\n" % f)
            nskipped += 1
            continue
        noutput += 1
        headers.append(f)

    # map of nested model (key) to more general model
    map_nested_models = {'8': '7',
                         '2': '1',
                         '3': '0'}

    if options.filename_mali:
        mali = Mali.Mali()
        mali.readFromFile(open(options.filename_mali, "r"))
    else:
        mali = None

    ###############################################################
    ###############################################################
    ###############################################################
    # use multiple alignment to map residues to a reference mali
    # or a sequence.
    ###############################################################
    if options.filename_map_mali:

        if not mali:
            raise "please supply the input multiple alignment, if residues are to be mapped."

        # translate the alignments
        def translate(s):
            sequence = s.mString
            seq = []
            for codon in [sequence[x:x + 3] for x in range(0, len(sequence), 3)]:
                aa = Genomics.MapCodon2AA(codon)
                seq.append(aa)

            s.mString = "".join(seq)

        tmali = Mali.Mali()
        tmali.readFromFile(open(options.filename_mali, "r"))
        tmali.apply(translate)

        tmap_mali = Mali.Mali()
        tmap_mali.readFromFile(open(options.filename_map_mali, "r"))

        if tmap_mali.getAlphabet() == "na":
            tmap_mali.apply(translate)

        map_old2new = alignlib_lite.py_makeAlignmentVector()

        mali1 = alignlib_lite.py_makeProfileFromMali(convertMali2Mali(tmali))

        if tmap_mali.getLength() == 1:

            s = tmap_mali.values()[0].mString
            mali2 = alignlib_lite.py_makeSequence(s)
            # see if you can find an identical subsequence and then align to
            # thisD
            for x in tmali.values():
                if s in re.sub("[- .]+", "", x.mString):
                    mali1 = alignlib_lite.py_makeSequence(x.mString)
                    break
        else:
            mali2 = alignlib_lite.py_makeProfileFromMali(
                convertMali2Mali(tmap_mali))

        alignator = alignlib_lite.py_makeAlignatorDPFull(
            alignlib_lite.py_ALIGNMENT_LOCAL, -10.0, -2.0)
        alignator.align(map_old2new, mali1, mali2)

        consensus = tmap_mali.getConsensus()

        if options.loglevel >= 4:
            options.stdlog.write("# alphabet: %s\n" % tmap_mali.getAlphabet())
            options.stdlog.write("# orig  : %s\n" % tmali.getConsensus())
            options.stdlog.write("# mapped: %s\n" % consensus)
            options.stdlog.write("# alignment: %s\n" % map_old2new.Write())
    else:
        map_old2new = None

    for method in options.methods:

        if method == "summary-numbers":

            options.stdlog.write(
                """# Numbers of positive sites.
#
# The consistent row/column contains positive sites that are significant
# (above thresholds for probability and omega) for all models/analysis
# that have been selected (label: cons).
#
# The log-likelihood ratio test is performed for model pairs, depending
# on the output chosen.
# Significance threshold: %6.4f
# The pairs are 8 versus 7 and 2 versus 1 and 3 versus 0.
#
""" % options.significance_threshold )

            # write header
            if options.prefix:
                options.stdout.write("prefix\t")

            options.stdout.write("method\tnseq\t")
            h = []
            for model in options.models:
                for analysis in options.analysis:
                    h.append("%s%s" % (analysis, model))
                h.append("p%s" % (model))
                h.append("df%s" % (model))
                h.append("chi%s" % (model))
                h.append("lrt%s" % (model))

            options.stdout.write("\t".join(h))
            options.stdout.write("\tcons\tpassed\tfilename\n")

            nmethod = 0

            consistent_cols = [None for x in range(len(options.analysis))]
            passed_tests = {}
            for m in options.models:
                passed_tests[m] = 0

            for result in results:

                row_consistent = None

                if options.prefix:
                    options.stdout.write("%s" % (options.prefix))

                options.stdout.write("%i" % nmethod)
                options.stdout.write("\t%i" % (result.mNumSequences))

                npassed = 0

                for model in options.models:

                    sites = result.mSites[model]

                    # do significance test
                    full_model, null_model = model, map_nested_models[model]

                    lrt = Stats.doLogLikelihoodTest(
                        result.mSites[full_model].mLogLikelihood,
                        result.mSites[full_model].mNumParameters,
                        result.mSites[null_model].mLogLikelihood,
                        result.mSites[null_model].mNumParameters,
                        options.significance_threshold)

                    x = 0
                    for analysis in options.analysis:

                        if analysis == "neb":
                            s = set(
                                map(extract_f, filter(filter_f, sites.mNEB.mPositiveSites)))

                        elif analysis == "beb":
                            s = set(
                                map(extract_f, filter(filter_f, sites.mBEB.mPositiveSites)))

                        options.stdout.write("\t%i" % (len(s)))

                        if not lrt.mPassed:
                            s = set()

                        if row_consistent is None:
                            row_consistent = s
                        else:
                            row_consistent = row_consistent.intersection(s)

                        if consistent_cols[x] is None:
                            consistent_cols[x] = s
                        else:
                            consistent_cols[x] = consistent_cols[
                                x].intersection(s)

                        x += 1

                    if lrt.mPassed:
                        c = "passed"
                        passed_tests[model] += 1
                        npassed += 1
                    else:
                        c = "failed"

                    options.stdout.write("\t%5.2e\t%i\t%5.2f\t%s" %
                                         (lrt.mProbability,
                                          lrt.mDegreesFreedom,
                                          lrt.mChiSquaredValue,
                                          c))

                options.stdout.write(
                    "\t%i\t%i\t%s\n" % (len(row_consistent), npassed, headers[nmethod]))

                nmethod += 1

            if options.prefix:
                options.stdout.write("%s\t" % options.prefix)

            options.stdout.write("cons")

            row_consistent = None
            total_passed = 0
            for model in options.models:

                x = 0

                for analysis in options.analysis:

                    s = consistent_cols[x]
                    if s is None:
                        s = set()

                    options.stdout.write("\t%i" % (len(s)))

                    if row_consistent is None:
                        row_consistent = s
                    else:
                        row_consistent = row_consistent.intersection(s)

                    x += 1

                options.stdout.write("\tna\t%i" % passed_tests[model])
                total_passed += passed_tests[model]

            options.stdout.write(
                "\t%i\t%i\n" % (len(row_consistent), total_passed))

        elif method == "jalview":

            options.stdout.write("JALVIEW_ANNOTATION\n")
            options.stdout.write("# Created: %s\n\n" %
                                 (time.asctime(time.localtime(time.time()))))

            l = 1
            x = 0
            for result in results:

                sites, significance = selectPositiveSites(
                    [result], options.selection_mode, options, mali)

                codes = [""] * result.mLength

                if len(sites) == 0:
                    continue

                for site in sites:
                    codes[site - 1] = options.jalview_symbol

                options.stdout.write(
                    "NO_GRAPH\t%s\t%s\n" % (options.jalview_titles[x], "|".join(codes)))
                x += 1

        elif method == "count-positive-sites":

            sites, significance = selectPositiveSites(
                results, options.selection_mode, options, mali)

            options.stdout.write("%i\n" % (len(sites)))

        elif method in ("positive-site-table", ):

            sites, significance = selectPositiveSites(
                results, options.selection_mode, options, mali)

            headers = ["site", "P"]
            if map_old2new:
                headers.append("mapped")
                headers.append("Pm")

            options.stdout.write("\t".join(headers) + "\n")

            sites = list(sites)
            sites.sort()
            nmapped, nunmapped = 0, 0
            for site in sites:
                values = [site, "%6.4f" % significance[site]]

                if map_old2new:
                    r = map_old2new.mapRowToCol(site)
                    if r == 0:
                        values.append("na")
                        values.append("")
                        nunmapped += 1
                        if options.loglevel >= 2:
                            options.stdlog.write(
                                "# unmapped residue: %i\n" % site)
                    else:
                        values.append(r)
                        values.append(consensus[r - 1])
                        nmapped += 1

                options.stdout.write("\t".join(map(str, (values))) + "\n")

            if options.loglevel >= 1:
                options.stdlog.write("# sites: ninput=%i, noutput=%i, nskipped=%i\n" % (
                    len(sites), nmapped, nunmapped))

    E.info("ninput=%i, noutput=%i, nskipped=%i" % (ninput, noutput, nskipped))

    E.Stop()
Ejemplo n.º 6
0
def main(argv=None):
    """script main.

    parses command line options in sys.argv, unless *argv* is given.
    """

    if argv is None:
        argv = sys.argv

    parser = E.OptionParser(
        version="%prog version: $Id: sequences2mali.py 2782 2009-09-10 11:40:29Z andreas $", usage=globals()["__doc__"])

    parser.add_option("-i", "--input-format", dest="input_format", type="choice",
                      choices=(
                          "plain", "fasta", "clustal", "stockholm", "phylip"),
                      help="input format of multiple alignment")

    parser.add_option("-o", "--output-format", dest="output_format", type="choice",
                      choices=("plain", "fasta", "stockholm", "phylip"),
                      help="output format of multiple alignment")

    parser.add_option("-m", "--method", dest="method", type="choice",
                      choices=("add",),
                      help="""method to use to build multiple alignment.""")

    parser.add_option("-p", "--parameters", dest="parameters", type="string",
                      help="parameter stack for methods that require one.")

    parser.add_option("-a", "--alignment-method", dest="alignment_method", type="choice",
                      choices=("sw", "nw"),
                      help="alignment_method [%default].")

    parser.set_defaults(
        input_format="fasta",
        output_format="fasta",
        method=None,
        parameters="",
        gop=-10.0,
        gep=-1.0,
        alignment_method="sw",
    )

    (options, args) = E.Start(parser)

    options.parameters = options.parameters.split(",")

    iterator = FastaIterator.iterate(sys.stdin)

    if options.method == "add":

        mali = Mali.Mali()

        mali.readFromFile(
            open(options.parameters[0], "r"), format=options.input_format)
        del options.parameters[0]

        old_length = mali.getLength()

        new_mali = convertMali2Mali(mali)

        if options.alignment_method == "sw":
            alignator = alignlib_lite.py_makeAlignatorFullDP(
                options.gop, options.gep)
        else:
            alignator = alignlib_lite.py_makeAlignatorFullDPGlobal(
                options.gop, options.gep)

        while 1:
            cur_record = iterator.next()
            if cur_record is None:
                break

            map_mali2seq = alignlib_lite.py_makeAlignataVector()

            sequence = alignlib_lite.py_makeSequence(cur_record.sequence)
            profile = alignlib_lite.py_makeProfileFromMali(new_mali)

            if options.loglevel >= 4:
                options.stdlog.write(profile.Write())

            alignator.Align(profile, sequence, map_mali2seq)

            if options.loglevel >= 3:
                options.stdlog.write(map_mali2seq.Write())

            # add sequence to mali
            a = alignlib_lite.py_makeAlignatumFromString(cur_record.sequence)
            a.thisown = 0

            new_mali.addAlignatum(a, map_mali2seq, 1, 1, 1, 1, 1)

            id = cur_record.title
            mali.mIdentifiers.append(id)
            mali.mMali[id] = Mali.AlignedString(id, 0, len(
                cur_record.sequence), new_mali.getRow(new_mali.getWidth() - 1).getString())

        # substitute
        for x in range(old_length):
            mali.mMali[mali.mIdentifiers[x]].mString = new_mali.getRow(
                x).getString()

        mali.writeToFile(sys.stdout, format=options.output_format)

    E.Stop()
Ejemplo n.º 7
0
def getMapPeptide2Cds(peptide_sequence, cds_sequence, options):
    """get map between peptide sequence and cds sequence.

    The returned alignment is in nucleotides.

    """

    # remove whitespaces form protein sequence
    p = re.sub(" ", "", peptide_sequence)

    # remove gaps and whitespaces from cds
    c = re.sub("[ .-]", "", cds_sequence)

    w = Genomics.Protein2Wobble(p.upper())

    if options.loglevel >= 6:
        options.stdlog.write("# peptide original (%5i): %s\n" % (len(p), p))
        options.stdlog.write("# cds original     (%5i): %s\n" % (len(c), c))
        options.stdlog.write("# wobble sequence  (%5i): %s\n" % (len(w), w))
        options.stdlog.flush()

    seq_wobble = alignlib_lite.py_makeSequence(w)
    seq_cds = alignlib_lite.py_makeSequence(c.upper())
    seq_peptide = alignlib_lite.py_makeSequence(p)

    map_p2c = alignlib_lite.py_makeAlignmentVector()

    try:
        AlignCodonBased(
            seq_wobble, seq_cds, seq_peptide, map_p2c, options=options)
    except ValueError as msg:
        raise ValueError("mapping error for sequence: %s" % (msg))

    # if there are more than five frameshifts - do exhaustive alignment
    max_gaps = 5
    num_peptide_gaps = len(re.sub("[^-]", "", p))
    ngaps = map_p2c.getNumGaps() - \
        (num_peptide_gaps * 3) - abs(len(w) - len(c))

    if options.loglevel >= 6:
        options.stdlog.write(
            "# alignment between wobble and cds: ngaps=%i, npeptide_gaps=%i\n" % (ngaps, num_peptide_gaps))
        printPrettyAlignment(seq_wobble, seq_cds, p, map_p2c, options)

    if ngaps > max_gaps:
        if options.loglevel >= 2:
            options.stdlog.write(
                "# too many gaps (%i>%i), realigning exhaustively.\n" % (ngaps, max_gaps))
            options.stdlog.flush()
        full_map_p2c = alignlib_lite.py_makeAlignmentVector()

        AlignExhaustive(
            seq_wobble, seq_cds, seq_peptide, full_map_p2c, options)
        if options.loglevel >= 6:
            options.stdlog.write("# full alignment between wobble and cds:\n")
            options.stdlog.flush()
            printPrettyAlignment(seq_wobble, seq_cds, p, full_map_p2c, options)

        map_p2c = full_map_p2c

    # remove incomplete codons
    x = 0
    while x < len(p) * 3:
        if (map_p2c.mapRowToCol(x) < 0 or
                map_p2c.mapRowToCol(x + 1) < 0 or
                map_p2c.mapRowToCol(x + 2) < 0):
            map_p2c.removeRowRegion(x, x + 3)
        x += 3

    if map_p2c.getLength() == 0:
        if options.loglevel >= 1:
            options.stdlog.write("# WARNING: empty alignment\n")
            if options.loglevel >= 6:
                options.stdlog.write("# peptide original: %s\n" % p)
                options.stdlog.write("# cds original    : %s\n" % c)
                options.stdlog.write("# wobble sequence : %s\n" % w)

        raise ValueError("empty alignment")

    assert(map_p2c.getRowTo() <= seq_wobble.getLength())
    assert(map_p2c.getColTo() <= seq_cds.getLength())

    return map_p2c
Ejemplo n.º 8
0
            print globals()["__doc__"]
            sys.exit(0)

    alignator = alignlib_lite.py_makeAlignatorDPFull(
        alignlib_lite.py_ALIGNMENT_LOCAL, param_gop, param_gep)
    map_query2token = alignlib_lite.py_makeAlignmentVector()

    for line in sys.stdin:
        if line[0] == "#":
            continue

        query_token, sbjct_token, query_sequence, sbjct_sequence = string.split(
            line[:-1], "\t")

        map_query2token.clear()
        row = alignlib_lite.py_makeSequence(query_sequence)
        col = alignlib_lite.py_makeSequence(sbjct_sequence)
        alignator.align(map_query2token, row, col)

        pidentity = 100.0 * \
            alignlib_lite.py_calculatePercentIdentity(
                map_query2token, row, col)
        psimilarity = 100.0 * \
            alignlib_lite.py_calculatePercentSimilarity(map_query2token)
        print string.join(
            map(str,
                (query_token, sbjct_token, map_query2token.getScore(),
                 alignlib_lite.py_AlignmentFormatEmissions(map_query2token),
                 pidentity, psimilarity, map_query2token.getNumGaps())), "\t")

Ejemplo n.º 9
0
        input_filename_seq2 = None,
        options = "B=0 C=2")
    
    (options, args) = E.Start( parser ) 
    
    wrapper = BlastZ( options.options )

    import alignlib_lite
    seqs1 = Genomics.ReadPeptideSequences( open(options.input_filename_seq1, "r") )
    seqs2 = Genomics.ReadPeptideSequences( open(options.input_filename_seq2, "r") )
    seq1 = seqs1[seqs1.keys()[0]]
    seq2 = seqs2[seqs2.keys()[0]]    
    result = alignlib_lite.py_makeAlignmentVector()
    wrapper.Align( seq1, seq2, result) 

    print str( alignlib_lite.py_AlignmentFormatExplicit( result,
                                                 alignlib_lite.py_makeSequence( seq1 ),
                                                 alignlib_lite.py_makeSequence( seq2 ) ) )
    
    E.Stop()
        
            
        
                                 
        
        
        
        
    
        
Ejemplo n.º 10
0
def getMapPeptide2Cds(peptide_sequence, cds_sequence, options):
    """get map between peptide sequence and cds sequence.

    The returned alignment is in nucleotides.

    """

    # remove whitespaces form protein sequence
    p = re.sub(" ", "", peptide_sequence)

    # remove gaps and whitespaces from cds
    c = re.sub("[ .-]", "", cds_sequence)

    w = Genomics.Protein2Wobble(p.upper())

    if options.loglevel >= 6:
        options.stdlog.write("# peptide original (%5i): %s\n" % (len(p), p))
        options.stdlog.write("# cds original     (%5i): %s\n" % (len(c), c))
        options.stdlog.write("# wobble sequence  (%5i): %s\n" % (len(w), w))
        options.stdlog.flush()

    seq_wobble = alignlib_lite.py_makeSequence(w)
    seq_cds = alignlib_lite.py_makeSequence(c.upper())
    seq_peptide = alignlib_lite.py_makeSequence(p)

    map_p2c = alignlib_lite.py_makeAlignmentVector()

    try:
        AlignCodonBased(seq_wobble,
                        seq_cds,
                        seq_peptide,
                        map_p2c,
                        options=options)
    except ValueError as msg:
        raise ValueError("mapping error for sequence: %s" % (msg))

    # if there are more than five frameshifts - do exhaustive alignment
    max_gaps = 5
    num_peptide_gaps = len(re.sub("[^-]", "", p))
    ngaps = map_p2c.getNumGaps() - \
        (num_peptide_gaps * 3) - abs(len(w) - len(c))

    if options.loglevel >= 6:
        options.stdlog.write(
            "# alignment between wobble and cds: ngaps=%i, npeptide_gaps=%i\n"
            % (ngaps, num_peptide_gaps))
        printPrettyAlignment(seq_wobble, seq_cds, p, map_p2c, options)

    if ngaps > max_gaps:
        if options.loglevel >= 2:
            options.stdlog.write(
                "# too many gaps (%i>%i), realigning exhaustively.\n" %
                (ngaps, max_gaps))
            options.stdlog.flush()
        full_map_p2c = alignlib_lite.py_makeAlignmentVector()

        AlignExhaustive(seq_wobble, seq_cds, seq_peptide, full_map_p2c,
                        options)
        if options.loglevel >= 6:
            options.stdlog.write("# full alignment between wobble and cds:\n")
            options.stdlog.flush()
            printPrettyAlignment(seq_wobble, seq_cds, p, full_map_p2c, options)

        map_p2c = full_map_p2c

    # remove incomplete codons
    x = 0
    while x < len(p) * 3:
        if (map_p2c.mapRowToCol(x) < 0 or map_p2c.mapRowToCol(x + 1) < 0
                or map_p2c.mapRowToCol(x + 2) < 0):
            map_p2c.removeRowRegion(x, x + 3)
        x += 3

    if map_p2c.getLength() == 0:
        if options.loglevel >= 1:
            options.stdlog.write("# WARNING: empty alignment\n")
            if options.loglevel >= 6:
                options.stdlog.write("# peptide original: %s\n" % p)
                options.stdlog.write("# cds original    : %s\n" % c)
                options.stdlog.write("# wobble sequence : %s\n" % w)

        raise ValueError("empty alignment")

    assert (map_p2c.getRowTo() <= seq_wobble.getLength())
    assert (map_p2c.getColTo() <= seq_cds.getLength())

    return map_p2c
Ejemplo n.º 11
0
def main( argv = None ):
    """script main.

    parses command line options in sys.argv, unless *argv* is given.
    """

    if argv == None: argv = sys.argv

    parser = E.OptionParser( version = "%prog version: $Id: links2fasta.py 2446 2009-01-27 16:32:35Z andreas $", usage = globals()["__doc__"] )

    parser.add_option( "-s", "--sequences", dest="filename_sequences", type="string",
                       help="peptide sequence [Default=%default]" )

    parser.add_option( "-f", "--format", dest="format", type="string",
                       help="output format [Default=%default]" )

    parser.add_option( "-e", "--expand",  dest="expand", action="store_true",
                       help="expand positions from peptide to nucleotide alignment [Default=%default]")

    parser.add_option( "-m", "--map",  dest="filename_map", type="string",
                       help="map alignments [Default=%default]")
    
    parser.add_option( "-c", "--codons",  dest="require_codons", action="store_true",
                       help="require codons [Default=%default]")

    parser.add_option( "--one-based-coordinates",  dest="one_based_coordinates", action="store_true",
                       help="expect one-based coordinates. The default are zero based coordinates [Default=%default].")

    parser.add_option( "--no-identical",  dest="no_identical", action="store_true",
                       help="do not output identical pairs [Default=%default]" )

    parser.add_option( "-g", "--no-gaps",  dest="no_gaps", action="store_true",
                       help="remove all gaps from aligned sequences [Default=%default]")

    parser.add_option( "-x", "--exons",  dest="filename_exons", type="string",
                       help="filename with exon boundaries [Default=%default]")
    
    parser.add_option( "-o", "--outfile",  dest="filename_outfile", type="string",
                       help="filename to save links [Default=%default]")

    parser.add_option( "--min-length",  dest="min_length", type="int",
                       help="minimum length of alignment [Default=%default]")

    parser.add_option( "--filter",  dest="filename_filter", type="string",
                       help="given a set of previous alignments, only write new pairs [Default=%default].")

    parser.set_defaults(
        filename_sequences = None,
        filename_exons = None,
        filename_map = None,
        filename_outfile = None,
        no_gaps = False,
        format = "fasta",
        expand = False,
        require_codons = False,
        no_identical = False,
        min_length = 0,
        report_step = 100,
        one_based_coordinates = False,
        filename_filter = None)

    (options, args) = E.Start( parser, add_mysql_options = True )

    t0 = time.time()
    if options.filename_sequences:
        sequences = Genomics.ReadPeptideSequences( open(options.filename_sequences, "r") )
    else:
        sequences = {}

    if options.loglevel >= 1:
        options.stdlog.write( "# read %i sequences\n" % len(sequences) )
        sys.stdout.flush()

    if options.filename_exons:
        exons = Exons.ReadExonBoundaries( open(options.filename_exons, "r") )
    else:
        exons = {}

    if options.loglevel >= 1:
        options.stdlog.write( "# read %i exons\n" % len(exons) )
        sys.stdout.flush()

    if options.filename_map:
        map_old2new = {}
        for line in open(options.filename_map, "r"):
            if line[0] == "#": continue
            m = Map()
            m.read( line )
            map_old2new[m.mToken] = m
    else:
        map_old2new = {}

    if options.loglevel >= 1:
        options.stdlog.write( "# read %i maps\n" % len(map_old2new) )
        sys.stdout.flush()

    if options.filename_filter:
        if options.loglevel >= 1:        
            options.stdlog.write( "# reading filtering information.\n" )
            sys.stdout.flush()
            
        map_pair2hids = {}

        if os.path.exists( options.filename_filter ):
            
            infile = open(options.filename_filter, "r")

            iterator = FastaIterator.FastaIterator( infile )

            while 1:
                cur_record = iterator.next()
                if cur_record is None: break

                record1 = cur_record

                cur_record = iterator.next()
                if cur_record is None: break

                record2 = cur_record

                identifier1 = re.match("(\S+)", record1.title).groups()[0]
                identifier2 = re.match("(\S+)", record2.title).groups()[0]

                id = "%s-%s" % (identifier1, identifier2)
                s = Genomics.GetHID(record1.sequence + ";" + record2.sequence)

                if id not in map_pair2hids: map_pair2hids[id] = []

                map_pair2hids[id].append( s )

            infile.close()
            
        if options.loglevel >= 1:        
            options.stdlog.write( "# read filtering information for %i pairs.\n" % len(map_pair2hids) )
            sys.stdout.flush()
    else:
        map_pair2hids = None
        
    if options.loglevel >= 1:
        options.stdlog.write( "# finished input in %i seconds.\n" % (time.time() - t0))

    if options.filename_outfile:
        outfile = open(options.filename_outfile, "w")
    else:
        outfile = None
        
    map_row2col = alignlib_lite.py_makeAlignmentVector()
    tmp1_map_row2col = alignlib_lite.py_makeAlignmentVector()
    counts = {}

    iterations = 0

    t1 = time.time()
    ninput, nskipped, noutput = 0, 0, 0

    for link in BlastAlignments.iterator_links( sys.stdin ):

        iterations += 1
        ninput += 1

        if options.loglevel >= 1:
            if (iterations % options.report_step == 0):
                options.stdlog.write( "# iterations: %i in %i seconds.\n" % (iterations, time.time() - t1) )
                sys.stdout.flush()
                
        if link.mQueryToken not in sequences or \
           link.mSbjctToken not in sequences:
            nskipped += 1
            continue

        if options.loglevel >= 3:
            options.stdlog.write( "# read link %s\n" %  str(link) )
            
        row_seq = alignlib_lite.py_makeSequence( sequences[link.mQueryToken] )
        col_seq = alignlib_lite.py_makeSequence( sequences[link.mSbjctToken] )

        if options.one_based_coordinates:
            link.mQueryFrom -= 1
            link.mSbjctFrom -= 1

        if options.expand:
            link.mQueryFrom = link.mQueryFrom * 3 
            link.mSbjctFrom = link.mSbjctFrom * 3
            link.mQueryAli = ScaleAlignment( link.mQueryAli, 3 )
            link.mSbjctAli = ScaleAlignment( link.mSbjctAli, 3 )            
            
        map_row2col.clear()

        alignlib_lite.py_AlignmentFormatEmissions(
            link.mQueryFrom, link.mQueryAli,
            link.mSbjctFrom, link.mSbjctAli ).copy(  map_row2col )
        
        if link.mQueryToken in map_old2new:
            tmp1_map_row2col.clear()
            map_old2new[link.mQueryToken].expand()
            if options.loglevel >= 3:
                options.stdlog.write( "# combining in row with %s\n" %\
                                      str(alignlib_lite.py_AlignmentFormatEmissions(map_old2new[link.mQueryToken].mMapOld2New ) ))

            alignlib_lite.py_combineAlignment( tmp1_map_row2col,
                                      map_old2new[link.mQueryToken].mMapOld2New,
                                      map_row2col,
                                      alignlib_lite.py_RR )
            map_old2new[link.mQueryToken].clear()
            alignlib_lite.py_copyAlignment( map_row2col, tmp1_map_row2col )

        if link.mSbjctToken in map_old2new:
            tmp1_map_row2col.clear()
            map_old2new[link.mSbjctToken].expand()            
            if options.loglevel >= 3:
                options.stdlog.write( "# combining in col with %s\n" %\
                                      str(alignlib_lite.py_AlignmentFormatEmissions(map_old2new[link.mSbjctToken].mMapOld2New ) ))

            alignlib_lite.py_combineAlignment( tmp1_map_row2col,
                                       map_row2col,
                                       map_old2new[link.mSbjctToken].mMapOld2New,
                                       alignlib_lite.py_CR )
            map_old2new[link.mSbjctToken].clear()
            alignlib_lite.py_copyAlignment( map_row2col, tmp1_map_row2col )

        dr = row_seq.getLength() - map_row2col.getRowTo() 
        dc = col_seq.getLength() - map_row2col.getColTo() 
        if dr < 0 or dc < 0:
            raise ValueError("out of bounds alignment: %s-%s: alignment out of bounds. row=%i col=%i ali=%s" %\
                                          (link.mQueryToken,
                                           link.mSbjctToken,
                                           row_seq.getLength(),
                                           col_seq.getLength(),
                                           str(alignlib_lite.py_AlignmentFormatEmissions(map_row2col))))
            

        if options.loglevel >= 2:
            options.stdlog.write( str( alignlib_lite.py_AlignmentFormatExplicit( map_row2col, 
                                                                         row_seq, 
                                                                         col_seq )) + "\n" )
        ## check for incomplete codons
        if options.require_codons:

            naligned = map_row2col.getNumAligned()
            
            # turned off, while fixing alignlib_lite
            if naligned % 3 != 0:
                options.stdlog.write( "# %s\n" % str(map_row2col) )
                options.stdlog.write( "# %s\n" % str(link) )
                options.stdlog.write( "# %s\n" % str(map_old2new[link.mQueryToken]) )
                options.stdlog.write( "# %s\n" % str(map_old2new[link.mSbjctToken]) )
                options.stdlog.write( "#\n%s\n" % alignlib_lite.py_AlignmentFormatExplicit( map_row2col, 
                                                                                    row_seq,
                                                                                    col_seq ) )

                raise ValueError("incomplete codons %i in pair %s - %s" % (naligned, link.mQueryToken, link.mSbjctToken))

        ## if so desired, write on a per exon level:
        if exons:
            if link.mQueryToken not in exons:
                raise IndexError("%s not found in exons" % (link.mQueryToken))
            if link.mSbjctToken not in exons:
                raise IndexError("%s not found in exons" % (link.mSbjctToken))
            exons1 = exons[link.mQueryToken]
            exons2 = exons[link.mSbjctToken]

            ## Get overlapping segments
            segments = Exons.MatchExons( map_row2col, exons1, exons2 )
            
            for a,b in segments:
                tmp1_map_row2col.clear()

                # make sure you got codon boundaries. Note that frameshifts
                # in previous exons will cause the codons to start at positions
                # different from mod 3. The problem is that I don't know where
                # the frameshifts occur exactly. The exon boundaries are given
                # with respect to the cds, which include the frame shifts.
                # Unfortunately, phase information seems to be incomplete in the input files.

                from1, to1 = GetAdjustedBoundaries( a, exons1 )
                from2, to2 = GetAdjustedBoundaries( b, exons2 )

                alignlib_lite.py_copyAlignment( tmp1_map_row2col, map_row2col,
                                       from1+1, to1, from2+1, to2 )
                
                mode = Write( tmp1_map_row2col, row_seq, col_seq, link,
                              no_gaps = options.no_gaps,
                              no_identical = options.no_identical,
                              min_length = options.min_length,
                              suffix1="_%s" % str(a),
                              suffix2="_%s" % str(b),
                              outfile = outfile,
                              pair_filter = map_pair2hid,
                              format = options.format )

                if mode not in counts: counts[mode] = 0
                counts[mode] += 1

        else:
            mode = Write( map_row2col, row_seq, col_seq, link,
                          min_length = options.min_length,                          
                          no_gaps = options.no_gaps,
                          no_identical = options.no_identical,
                          outfile = outfile,
                          pair_filter = map_pair2hids,
                          format = options.format )
            
            if mode not in counts: counts[mode] = 0
            counts[mode] += 1

        noutput += 1
        
    if outfile: outfile.close()
    
    if options.loglevel >= 1:
        options.stdlog.write("# %s\n" % ", ".join( map( lambda x,y: "%s=%i" % (x,y), counts.keys(), counts.values() ) ))
        options.stdlog.write("# ninput=%i, noutput=%i, nskipped=%i\n" % (ninput, noutput, nskipped) )

    E.Stop()
Ejemplo n.º 12
0
def main(argv=None):
    """script main.

    parses command line options in sys.argv, unless *argv* is given.
    """

    if argv == None: argv = sys.argv

    parser = E.OptionParser(
        version=
        "%prog version: $Id: sequences2mali.py 2782 2009-09-10 11:40:29Z andreas $",
        usage=globals()["__doc__"])

    parser.add_option("-i",
                      "--input-format",
                      dest="input_format",
                      type="choice",
                      choices=("plain", "fasta", "clustal", "stockholm",
                               "phylip"),
                      help="input format of multiple alignment")

    parser.add_option("-o",
                      "--output-format",
                      dest="output_format",
                      type="choice",
                      choices=("plain", "fasta", "stockholm", "phylip"),
                      help="output format of multiple alignment")

    parser.add_option("-m",
                      "--method",
                      dest="method",
                      type="choice",
                      choices=("add", ),
                      help="""method to use to build multiple alignment.""")

    parser.add_option("-p",
                      "--parameters",
                      dest="parameters",
                      type="string",
                      help="parameter stack for methods that require one.")

    parser.add_option("-a",
                      "--alignment-method",
                      dest="alignment_method",
                      type="choice",
                      choices=("sw", "nw"),
                      help="alignment_method [%default].")

    parser.set_defaults(
        input_format="fasta",
        output_format="fasta",
        method=None,
        parameters="",
        gop=-10.0,
        gep=-1.0,
        alignment_method="sw",
    )

    (options, args) = E.Start(parser)

    options.parameters = options.parameters.split(",")

    iterator = FastaIterator.iterate(sys.stdin)

    if options.method == "add":

        mali = Mali.Mali()

        mali.readFromFile(open(options.parameters[0], "r"),
                          format=options.input_format)
        del options.parameters[0]

        old_length = mali.getLength()

        new_mali = convertMali2Mali(mali)

        if options.alignment_method == "sw":
            alignator = alignlib_lite.py_makeAlignatorFullDP(
                options.gop, options.gep)
        else:
            alignator = alignlib_lite.py_makeAlignatorFullDPGlobal(
                options.gop, options.gep)

        while 1:
            cur_record = iterator.next()
            if cur_record is None: break

            map_mali2seq = alignlib_lite.py_makeAlignataVector()

            sequence = alignlib_lite.py_makeSequence(cur_record.sequence)
            profile = alignlib_lite.py_makeProfileFromMali(new_mali)

            if options.loglevel >= 4:
                options.stdlog.write(profile.Write())

            alignator.Align(profile, sequence, map_mali2seq)

            if options.loglevel >= 3:
                options.stdlog.write(map_mali2seq.Write())

            ## add sequence to mali
            a = alignlib_lite.py_makeAlignatumFromString(cur_record.sequence)
            a.thisown = 0

            new_mali.addAlignatum(a, map_mali2seq, 1, 1, 1, 1, 1)

            id = cur_record.title
            mali.mIdentifiers.append(id)
            mali.mMali[id] = Mali.AlignedString(
                id, 0, len(cur_record.sequence),
                new_mali.getRow(new_mali.getWidth() - 1).getString())

        # substitute
        for x in range(old_length):
            mali.mMali[mali.mIdentifiers[x]].mString = new_mali.getRow(
                x).getString()

        mali.writeToFile(sys.stdout, format=options.output_format)

    E.Stop()
Ejemplo n.º 13
0
def main(argv=None):

    if argv is None:
        argv = sys.argv

    parser = E.OptionParser(
        version=
        "%prog version: $Id: align_all_vs_all.py 2782 2009-09-10 11:40:29Z andreas $",
        usage=globals()["__doc__"])

    parser.add_option("-s",
                      "--sequences",
                      dest="filename_sequences",
                      type="string",
                      help="input file with sequences")

    parser.set_defaults(
        filename_sequences=None,
        gop=-10.0,
        gep=-1.0,
    )

    (options, args) = E.Start(parser, add_pipe_options=True)

    if options.filename_sequences:
        infile = open(options.filename_sequences, "r")
    else:
        infile = sys.stdin

    parser = FastaIterator.FastaIterator(infile)

    sequences = []
    while 1:
        cur_record = iterator.next()

        if cur_record is None:
            break
        sequences.append(
            (cur_record.title,
             alignlib_lite.py_makeSequence(re.sub(" ", "",
                                                  cur_record.sequence))))

    if options.filename_sequences:
        infile.close()

    alignator = alignlib_lite.py_makeAlignatorFullDP(options.gop, options.gep)
    map_a2b = alignlib_lite.py_makeAlignataVector()
    nsequences = len(sequences)

    for x in range(0, nsequences - 1):
        for y in range(x + 1, nsequences):
            alignator.Align(sequences[x][1], sequences[y][1], map_a2b)

            row_ali, col_ali = alignlib_lite.py_writeAlignataCompressed(
                map_a2b)

            options.stdout.write(
                "%s\t%s\t%i\t%i\t%i\t%s\t%i\t%i\t%s\t%i\t%i\t%i\t%i\n" %
                (sequences[x][0], sequences[y][0], map_a2b.getScore(),
                 map_a2b.getRowFrom(), map_a2b.getRowTo(), row_ali,
                 map_a2b.getColFrom(), map_a2b.getColTo(), col_ali,
                 map_a2b.getScore(),
                 100 * alignlib_lite.py_calculatePercentIdentity(
                     map_a2b, sequences[x][1], sequences[y][1]),
                 sequences[x][1].getLength(), sequences[y][1].getLength()))

    E.Stop()
Ejemplo n.º 14
0
def main(argv=None):
    """script main.

    parses command line options in sys.argv, unless *argv* is given.
    """

    if argv == None: argv = sys.argv

    parser = E.OptionParser(
        version=
        "%prog version: $Id: blast2fasta.py 2782 2009-09-10 11:40:29Z andreas $",
        usage=globals()["__doc__"])

    parser.add_option("-s",
                      "--sequences",
                      dest="filename_sequences",
                      type="string",
                      help="filename with sequences.")
    parser.add_option("-f",
                      "--format",
                      dest="format",
                      type="string",
                      help="output format.")

    parser.set_defaults(
        filename_sequences=None,
        format="fasta",
    )

    (options, args) = E.Start(parser)

    if not options.filename_sequences:
        raise "please supply filename with sequences."

    sequences = Genomics.ReadPeptideSequences(
        open(options.filename_sequences, "r"))

    if options.loglevel >= 1:
        print "# read %i sequences" % len(sequences)

    for k in sequences.keys():
        sequences[k] = alignlib_lite.py_makeSequence(sequences[k])

    if options.loglevel >= 2:
        print "# converted %i sequences" % len(sequences)

    ninput, noutput, nskipped, nfailed = 0, 0, 0, 0
    link = BlastAlignments.Link()

    ali = alignlib_lite.py_makeAlignataVector()

    for line in sys.stdin:

        if line[0] == "#": continue

        link.Read(line)
        ninput += 1

        if link.mQueryToken not in sequences or link.mSbjctToken not in sequences:
            nskipped += 1
            continue

        ali.Clear()
        alignlib_lite.py_fillAlignataCompressed(ali, link.mQueryFrom,
                                                link.mQueryAli,
                                                link.mSbjctFrom,
                                                link.mSbjctAli)

        result = alignlib_lite.py_writePairAlignment(
            sequences[link.mQueryToken], sequences[link.mSbjctToken],
            ali).split("\n")

        if len(result) != 3:
            nfailed += 1

        if options.format == "fasta":
            print ">%s %i-%i\n%s\n>%s %i-%i\n%s\n" %\
                  (link.mQueryToken, link.mQueryFrom, link.mQueryTo, result[0].split("\t")[1],
                   link.mSbjctToken, link.mSbjctFrom, link.mSbjctTo, result[1].split("\t")[1] )

        noutput += 1

    E.info("ninput=%i, noutput=%i, nskipped=%i, nfailed=%i" %
           (ninput, noutput, nskipped, nfailed))
    E.Stop()
Ejemplo n.º 15
0
    parser.add_option("-o",
                      "--options",
                      dest="options",
                      type="string",
                      help="BlastZ options.")

    parser.set_defaults(input_filename_seq1=None,
                        input_filename_seq2=None,
                        options="B=0 C=2")

    (options, args) = E.Start(parser)

    wrapper = BlastZ(options.options)

    import alignlib_lite
    seqs1 = Genomics.ReadPeptideSequences(
        open(options.input_filename_seq1, "r"))
    seqs2 = Genomics.ReadPeptideSequences(
        open(options.input_filename_seq2, "r"))
    seq1 = seqs1[seqs1.keys()[0]]
    seq2 = seqs2[seqs2.keys()[0]]
    result = alignlib_lite.py_makeAlignmentVector()
    wrapper.Align(seq1, seq2, result)

    print str(
        alignlib_lite.py_AlignmentFormatExplicit(
            result, alignlib_lite.py_makeSequence(seq1),
            alignlib_lite.py_makeSequence(seq2)))

    E.Stop()
Ejemplo n.º 16
0
def main(argv=None):
    """script main.

    parses command line options in sys.argv, unless *argv* is given.
    """

    if argv == None: argv = sys.argv

    parser = E.OptionParser(
        version=
        "%prog version: $Id: codemls2tsv.py 2781 2009-09-10 11:33:14Z andreas $"
    )

    parser.add_option("--methods",
                      dest="methods",
                      type="choice",
                      action="append",
                      choices=("summary-numbers", "jalview",
                               "positive-site-table", "positive-site-list",
                               "count-positive-sites"),
                      help="methods for analysis.")

    parser.add_option("--selection-mode",
                      dest="selection_mode",
                      type="choice",
                      choices=("all", "consistent", "emes"),
                      help="how to select positive sites.")

    parser.add_option("--prefix",
                      dest="prefix",
                      type="string",
                      help="prefix for rows.")

    parser.add_option("--pattern-input-filenames",
                      dest="pattern_input_filenames",
                      type="string",
                      help="input pattern.")

    parser.add_option(
        "--filter-probability",
        dest="filter_probability",
        type="float",
        help=
        "threshold for probability above which to include positive sites [default=%default]."
    )

    parser.add_option(
        "--filter-omega",
        dest="filter_omega",
        type="float",
        help=
        "threshold for omega above which to include positive sites [default=%default]."
    )

    parser.add_option("--models",
                      dest="models",
                      type="string",
                      help="restrict output to set of site specific models.")

    parser.add_option("--analysis",
                      dest="analysis",
                      type="string",
                      help="restrict output to set of analysis [beb|neb].")

    parser.add_option("--significance-threshold",
                      dest="significance_threshold",
                      type="float",
                      help="significance threshold for log-likelihood test.")

    parser.add_option("--filter-mali",
                      dest="filter_mali",
                      type="choice",
                      choices=("none", "gaps"),
                      help="filter by mali to remove gapped positions.")

    parser.add_option(
        "--filename-mali",
        dest="filename_mali",
        type="string",
        help=
        "filename with multiple alignment used for calculating sites - used for filtering"
    )

    parser.add_option(
        "--filename-map-mali",
        dest="filename_map_mali",
        type="string",
        help="filename with multiple alignment to map sites onto.")

    parser.add_option(
        "--jalview-titles",
        dest="jalview_titles",
        type="string",
        help="comma separated list of jalview annotation titles.")

    parser.add_option("--jalview-symbol",
                      dest="jalview_symbol",
                      type="string",
                      help="symbol to use in jalview.")

    parser.set_defaults(
        methods=[],
        prefix=None,
        filter_probability=0,
        filter_omega=0,
        models="",
        analysis="",
        significance_threshold=0.05,
        selection_mode="consistent",
        filename_mali=None,
        filename_map_mali=None,
        jalview_symbol="*",
        jalview_titles="",
        filter_mali=None,
    )

    (options, args) = E.Start(parser)

    if options.jalview_titles:
        options.jalview_titles = options.jalview_titles.split(",")
    else:
        options.jalview_titles = args

    options.models = options.models.split(",")
    options.analysis = options.analysis.split(",")

    for a in options.analysis:
        if a not in ("beb", "neb"):
            raise "unknown analysis section: '%s', possible values are 'beb' and/or 'neb'" % a

    for a in options.models:
        if a not in ("8", "2", "3"):
            raise "unknown model: '%s', possible values are 2, 3, 8" % a

    codeml = WrapperCodeML.CodeMLSites()

    ## filter and extract functions
    filter_f = lambda x: x.mProbability >= options.filter_probability and x.mOmega >= options.filter_omega
    extract_f = lambda x: x.mResidue

    ## read multiple results
    results = []
    ninput, noutput, nskipped = 0, 0, 0

    headers = []
    for f in args:
        ninput += 1
        try:
            results.append(codeml.parseOutput(open(f, "r").readlines()))
        except WrapperCodeML.UsageError:
            if options.loglevel >= 1:
                options.stdlog.write("# no input from %s\n" % f)
            nskipped += 1
            continue
        noutput += 1
        headers.append(f)

    ## map of nested model (key) to more general model
    map_nested_models = {'8': '7', '2': '1', '3': '0'}

    if options.filename_mali:
        mali = Mali.Mali()
        mali.readFromFile(open(options.filename_mali, "r"))
    else:
        mali = None

    ###############################################################
    ###############################################################
    ###############################################################
    ## use multiple alignment to map residues to a reference mali
    ## or a sequence.
    ###############################################################
    if options.filename_map_mali:

        if not mali:
            raise "please supply the input multiple alignment, if residues are to be mapped."

        ## translate the alignments
        def translate(s):
            sequence = s.mString
            seq = []
            for codon in [
                    sequence[x:x + 3] for x in range(0, len(sequence), 3)
            ]:
                aa = Genomics.MapCodon2AA(codon)
                seq.append(aa)

            s.mString = "".join(seq)

        tmali = Mali.Mali()
        tmali.readFromFile(open(options.filename_mali, "r"))
        tmali.apply(translate)

        tmap_mali = Mali.Mali()
        tmap_mali.readFromFile(open(options.filename_map_mali, "r"))

        if tmap_mali.getAlphabet() == "na":
            tmap_mali.apply(translate)

        map_old2new = alignlib_lite.py_makeAlignmentVector()

        mali1 = alignlib_lite.py_makeProfileFromMali(convertMali2Mali(tmali))

        if tmap_mali.getLength() == 1:

            s = tmap_mali.values()[0].mString
            mali2 = alignlib_lite.py_makeSequence(s)
            ## see if you can find an identical subsequence and then align to thisD
            for x in tmali.values():
                if s in re.sub("[- .]+", "", x.mString):
                    mali1 = alignlib_lite.py_makeSequence(x.mString)
                    break
        else:
            mali2 = alignlib_lite.py_makeProfileFromMali(
                convertMali2Mali(tmap_mali))

        alignator = alignlib_lite.py_makeAlignatorDPFull(
            alignlib_lite.py_ALIGNMENT_LOCAL, -10.0, -2.0)
        alignator.align(map_old2new, mali1, mali2)

        consensus = tmap_mali.getConsensus()

        if options.loglevel >= 4:
            options.stdlog.write("# alphabet: %s\n" % tmap_mali.getAlphabet())
            options.stdlog.write("# orig  : %s\n" % tmali.getConsensus())
            options.stdlog.write("# mapped: %s\n" % consensus)
            options.stdlog.write("# alignment: %s\n" % map_old2new.Write())
    else:
        map_old2new = None

    for method in options.methods:

        if method == "summary-numbers":

            options.stdlog.write( \
"""# Numbers of positive sites.
#
# The consistent row/column contains positive sites that are significant
# (above thresholds for probability and omega) for all models/analysis
# that have been selected (label: cons).
#
# The log-likelihood ratio test is performed for model pairs, depending
# on the output chosen.
# Significance threshold: %6.4f
# The pairs are 8 versus 7 and 2 versus 1 and 3 versus 0.
#
""" % options.significance_threshold )

            ## write header
            if options.prefix: options.stdout.write("prefix\t")

            options.stdout.write("method\tnseq\t")
            h = []
            for model in options.models:
                for analysis in options.analysis:
                    h.append("%s%s" % (analysis, model))
                h.append("p%s" % (model))
                h.append("df%s" % (model))
                h.append("chi%s" % (model))
                h.append("lrt%s" % (model))

            options.stdout.write("\t".join(h))
            options.stdout.write("\tcons\tpassed\tfilename\n")

            nmethod = 0

            consistent_cols = [None for x in range(len(options.analysis))]
            passed_tests = {}
            for m in options.models:
                passed_tests[m] = 0

            for result in results:

                row_consistent = None

                if options.prefix:
                    options.stdout.write("%s" % (options.prefix))

                options.stdout.write("%i" % nmethod)
                options.stdout.write("\t%i" % (result.mNumSequences))

                npassed = 0

                for model in options.models:

                    sites = result.mSites[model]

                    ## do significance test
                    full_model, null_model = model, map_nested_models[model]

                    lrt = Stats.doLogLikelihoodTest(
                        result.mSites[full_model].mLogLikelihood,
                        result.mSites[full_model].mNumParameters,
                        result.mSites[null_model].mLogLikelihood,
                        result.mSites[null_model].mNumParameters,
                        options.significance_threshold)

                    x = 0
                    for analysis in options.analysis:

                        if analysis == "neb":
                            s = set(
                                map(
                                    extract_f,
                                    filter(filter_f,
                                           sites.mNEB.mPositiveSites)))

                        elif analysis == "beb":
                            s = set(
                                map(
                                    extract_f,
                                    filter(filter_f,
                                           sites.mBEB.mPositiveSites)))

                        options.stdout.write("\t%i" % (len(s)))

                        if not lrt.mPassed:
                            s = set()

                        if row_consistent == None:
                            row_consistent = s
                        else:
                            row_consistent = row_consistent.intersection(s)

                        if consistent_cols[x] == None:
                            consistent_cols[x] = s
                        else:
                            consistent_cols[x] = consistent_cols[
                                x].intersection(s)

                        x += 1

                    if lrt.mPassed:
                        c = "passed"
                        passed_tests[model] += 1
                        npassed += 1
                    else:
                        c = "failed"

                    options.stdout.write("\t%5.2e\t%i\t%5.2f\t%s" %\
                                         (lrt.mProbability,
                                          lrt.mDegreesFreedom,
                                          lrt.mChiSquaredValue,
                                          c))

                options.stdout.write(
                    "\t%i\t%i\t%s\n" %
                    (len(row_consistent), npassed, headers[nmethod]))

                nmethod += 1

            if options.prefix:
                options.stdout.write("%s\t" % options.prefix)

            options.stdout.write("cons")

            row_consistent = None
            total_passed = 0
            for model in options.models:

                x = 0

                for analysis in options.analysis:

                    s = consistent_cols[x]
                    if s == None:
                        s = set()

                    options.stdout.write("\t%i" % (len(s)))

                    if row_consistent == None:
                        row_consistent = s
                    else:
                        row_consistent = row_consistent.intersection(s)

                    x += 1

                options.stdout.write("\tna\t%i" % passed_tests[model])
                total_passed += passed_tests[model]

            options.stdout.write("\t%i\t%i\n" %
                                 (len(row_consistent), total_passed))

        elif method == "jalview":

            options.stdout.write("JALVIEW_ANNOTATION\n")
            options.stdout.write("# Created: %s\n\n" %
                                 (time.asctime(time.localtime(time.time()))))

            l = 1
            x = 0
            for result in results:

                sites, significance = selectPositiveSites(
                    [result], options.selection_mode, options, mali)

                codes = [""] * result.mLength

                if len(sites) == 0: continue

                for site in sites:
                    codes[site - 1] = options.jalview_symbol

                options.stdout.write(
                    "NO_GRAPH\t%s\t%s\n" %
                    (options.jalview_titles[x], "|".join(codes)))
                x += 1

        elif method == "count-positive-sites":

            sites, significance = selectPositiveSites(results,
                                                      options.selection_mode,
                                                      options, mali)

            options.stdout.write("%i\n" % (len(sites)))

        elif method in ("positive-site-table", ):

            sites, significance = selectPositiveSites(results,
                                                      options.selection_mode,
                                                      options, mali)

            headers = ["site", "P"]
            if map_old2new:
                headers.append("mapped")
                headers.append("Pm")

            options.stdout.write("\t".join(headers) + "\n")

            sites = list(sites)
            sites.sort()
            nmapped, nunmapped = 0, 0
            for site in sites:
                values = [site, "%6.4f" % significance[site]]

                if map_old2new:
                    r = map_old2new.mapRowToCol(site)
                    if r == 0:
                        values.append("na")
                        values.append("")
                        nunmapped += 1
                        if options.loglevel >= 2:
                            options.stdlog.write("# unmapped residue: %i\n" %
                                                 site)
                    else:
                        values.append(r)
                        values.append(consensus[r - 1])
                        nmapped += 1

                options.stdout.write("\t".join(map(str, (values))) + "\n")

            if options.loglevel >= 1:
                options.stdlog.write(
                    "# sites: ninput=%i, noutput=%i, nskipped=%i\n" %
                    (len(sites), nmapped, nunmapped))

    E.info("ninput=%i, noutput=%i, nskipped=%i" % (ninput, noutput, nskipped))

    E.Stop()
Ejemplo n.º 17
0
def getAlignmentFull(m, q, t, options):
    """print alignment with gaps in both query and target."""
    a = alignlib_lite.py_AlignmentFormatExplicit(
        m, alignlib_lite.py_makeSequence(q), alignlib_lite.py_makeSequence(t))
    return a.mRowAlignment, a.mColAlignment
Ejemplo n.º 18
0
                if unaligned_pair and \
                        unaligned_pair.mToken1 == pair.mToken1 and \
                        unaligned_pair.mToken2 == pair.mToken2 and \
                        unaligned_pair.mIntronId1 == pair.mIntronId1:

                    map_a2b = alignlib_lite.py_makeAlignmentVector()
                    f = AlignmentFormatEmissions(
                        pair.mFrom1,
                        pair.mAlignedSequence1,
                        pair.mFrom2,
                        pair.mAlignedSequence2).copy(map_a2b)
                    map_a2b.moveAlignment(-unaligned_pair.mFrom1 +
                                          1, -unaligned_pair.mFrom2 + 1)

                    data = alignlib_lite.py_AlignmentFormatExplicit(map_a2b,
                                                                    alignlib_lite.py_makeSequence(
                                                                        unaligned_pair.mAlignedSequence1),
                                                                    alignlib_lite.py_makeSequence(unaligned_pair.mAlignedSequence2))

                    from1, ali1, to1 = data.mRowFrom, data.mRowAlignment, data.mRowTo
                    from2, ali2, to2 = data.mColFrom, data.mColAlignment, data.mColTo

                    pair.mAlignedSequence1 = ali1
                    pair.mAlignedSequence2 = ali2

                else:
                    raise "sequence not found for pair %s" % str(pair)

            if param_do_gblocks:
                if param_loglevel >= 4:
                    print "# length before: %i %i" % (len(pair.mAlignedSequence1), pair.mAligned)
                pair.mAlignedSequence1, pair.mAlignedSequence2 = gblocks.GetBlocks(
Ejemplo n.º 19
0
def AlignCodonBased(seq_wobble,
                    seq_cds,
                    seq_peptide,
                    map_p2c,
                    options,
                    diag_width=2,
                    max_advance=2):
    """advance in codons in seq_wobble and match to nucleotides in seq_cds.

    Due to alinglib this is all in one-based coordinates.
    Takes care of frameshifts.
    """

    map_p2c.clear()

    gop, gep = -1.0, -1.0
    matrix = alignlib_lite.py_makeSubstitutionMatrixBackTranslation(
        1, -10, 1, alignlib_lite.py_getDefaultEncoder())

    pep_seq = seq_peptide.asString()
    cds_seq = seq_cds.asString()
    wobble_seq = seq_wobble.asString()

    lcds = seq_cds.getLength()
    lwobble = seq_wobble.getLength()
    y = 0
    x = 0

    last_start = None

    while x < lwobble and y < lcds:

        xr = seq_wobble.asResidue(x)
        # skip over masked chars in wobble - these are gaps
        if seq_wobble.asChar(x) == "X":
            x += 1
            continue

        # skip over masked chars in wobble - these are from
        # masked chars in the peptide sequence
        # Note to self: do not see all implications of this change
        # check later.
        if seq_wobble.asChar(x) == "N":
            x += 1
            continue

        # skip over gaps in wobble
        if seq_wobble.asChar(x) == "-":
            x += 1
            continue

        s = matrix.getValue(xr, seq_cds.asResidue(y))

        if options.loglevel >= 6:
            if (x % 3 == 0):
                c = seq_cds.asChar(y) + seq_cds.asChar(y +
                                                       1) + seq_cds.asChar(y +
                                                                           2)
                options.stdlog.write(
                    "# c=%s, x=%i, y=%i, aa=%s target=%s\n" %
                    (c, x, y, Genomics.MapCodon2AA(c), pep_seq[int(x / 3)]))

            options.stdlog.write(
                "# x=%i\twob=%s\ty=%i\tcds=%s\txr=%s\tcds=%i\tscore=%s\n" %
                (x, seq_wobble.asChar(x), y, seq_cds.asChar(y), xr,
                 seq_cds.asResidue(y), str(s)))

        # deal with mismatches
        if s <= 0:

            tmp_map_p2c = alignlib_lite.py_makeAlignmentVector()

            # backtrack to previous three codons and align
            # three codons for double frameshifts that span two codons and
            # produce two X's and six WWWWWW.

            # number of nucleotides to extend (should be multiple of 3)
            # less than 12 caused failure for some peptides.
            d = 15

            # extend by amound dx
            dx = (x % 3) + d

            x_start = max(0, x - dx)
            # map to ensure that no ambiguous residue mappings
            # exist after re-alignment
            y_start = max(0,
                          map_p2c.mapRowToCol(x_start, alignlib_lite.py_RIGHT))

            if (x_start, y_start) == last_start:
                raise ValueError("infinite loop detected")

            last_start = (x_start, y_start)

            x_end = min(x_start + 2 * d, len(wobble_seq))
            y_end = min(y_start + 2 * d, len(cds_seq))

            wobble_fragment = alignlib_lite.py_makeSequence(
                wobble_seq[x_start:x_end])
            cds_fragment = alignlib_lite.py_makeSequence(
                cds_seq[y_start:y_end])

            AlignExhaustive(wobble_fragment, cds_fragment, "", tmp_map_p2c,
                            options)

            if options.loglevel >= 10:
                options.stdlog.write(
                    "# fragmented alignment from %i-%i, %i-%i:\n%s\n" %
                    (x_start, x_end, y_start, y_end,
                     str(
                         alignlib_lite.py_AlignmentFormatExplicit(
                             tmp_map_p2c, wobble_fragment, cds_fragment))))

                options.stdlog.flush()

            # clear alignment
            map_p2c.removeRowRegion(x_start, x_end)
            ngap = 0
            last_x, last_y = None, None
            for xxx in range(tmp_map_p2c.getRowFrom(), tmp_map_p2c.getRowTo()):
                yyy = tmp_map_p2c.mapRowToCol(xxx)

                if yyy >= 0:
                    x = xxx + x_start
                    y = yyy + y_start
                    xr = seq_wobble.asResidue(x)
                    s = matrix.getValue(seq_wobble.asResidue(x),
                                        seq_cds.asResidue(y))
                    if s < 0:
                        raise ValueError(
                            "mismatched residue wobble: %i (%s), cds: %i (%s)"
                            % (x, seq_wobble.asChar(x), y, seq_cds.asChar(y)))

                    map_p2c.addPair(x, y, s)
                    last_x, last_y = x, y
                    if options.loglevel >= 6:
                        options.stdlog.write(
                            "# reset: x=%i\twob=%s\ty=%i\tcds=%s\txr=%s\tcds=%i\tscore=%i\n"
                            % (x, seq_wobble.asChar(x), y, seq_cds.asChar(y),
                               xr, seq_cds.asResidue(y), s))
                        options.stdlog.flush()
                    ngap = 0
                else:
                    ngap += 1

                # treat special case of double frameshifts. They might cause a petide/wobble residue
                # to be eliminated and thus the translated sequences will differ.
                # simply delete the last residue between x and y and move to
                # next codon.
                if ngap == 3:
                    map_p2c.removeRowRegion(last_x, last_x + 1)

                    last_x += 1
                    map_p2c.addPair(last_x, last_y)
                    if options.loglevel >= 6:
                        options.stdlog.write(
                            "# double: x=%i\twob=%s\ty=%i\tcds=%s\txr=%s\tcds=%i\tscore=%i\n"
                            % (last_x, seq_wobble.asChar(last_x), last_y,
                               seq_cds.asChar(last_y), xr,
                               seq_cds.asResidue(last_y), s))
                        options.stdlog.flush()
                    ngap = 0

            # exit condition if alignment is shorter than problematic residue
            # need to catch this to avoid infinite loop.
            if tmp_map_p2c.getRowTo() < d:
                if lwobble - x <= 4:
                    # only last codon is missing, so ok
                    break
                else:
                    raise ValueError("failure to align in designated window.")

            s = 0

        s = matrix.getValue(xr, seq_cds.asResidue(y))

        if s < 0:
            raise ValueError("mis-matching residues.")

        map_p2c.addPair(x, y, float(s))

        # advance to next residues
        x += 1
        y += 1

    # sanity checks
    assert (map_p2c.getRowTo() <= seq_wobble.getLength())
    assert (map_p2c.getColTo() <= seq_cds.getLength())
Ejemplo n.º 20
0
def main(argv=None):
    """script main.

    parses command line options in sys.argv, unless *argv* is given.
    """

    if argv is None:
        argv = sys.argv

    parser = E.OptionParser(
        version=
        "%prog version: $Id: links2fasta.py 2446 2009-01-27 16:32:35Z andreas $",
        usage=globals()["__doc__"])

    parser.add_option("-s",
                      "--sequences",
                      dest="filename_sequences",
                      type="string",
                      help="peptide sequence [Default=%default]")

    parser.add_option("-f",
                      "--format",
                      dest="format",
                      type="string",
                      help="output format [Default=%default]")

    parser.add_option(
        "-e",
        "--expand",
        dest="expand",
        action="store_true",
        help=
        "expand positions from peptide to nucleotide alignment [Default=%default]"
    )

    parser.add_option("-m",
                      "--map",
                      dest="filename_map",
                      type="string",
                      help="map alignments [Default=%default]")

    parser.add_option("-c",
                      "--codons",
                      dest="require_codons",
                      action="store_true",
                      help="require codons [Default=%default]")

    parser.add_option(
        "--one-based-coordinates",
        dest="one_based_coordinates",
        action="store_true",
        help=
        "expect one-based coordinates. The default are zero based coordinates [Default=%default]."
    )

    parser.add_option("--no-identical",
                      dest="no_identical",
                      action="store_true",
                      help="do not output identical pairs [Default=%default]")

    parser.add_option(
        "-g",
        "--no-gaps",
        dest="no_gaps",
        action="store_true",
        help="remove all gaps from aligned sequences [Default=%default]")

    parser.add_option("-x",
                      "--exons",
                      dest="filename_exons",
                      type="string",
                      help="filename with exon boundaries [Default=%default]")

    parser.add_option("-o",
                      "--outfile",
                      dest="filename_outfile",
                      type="string",
                      help="filename to save links [Default=%default]")

    parser.add_option("--min-length",
                      dest="min_length",
                      type="int",
                      help="minimum length of alignment [Default=%default]")

    parser.add_option(
        "--filter",
        dest="filename_filter",
        type="string",
        help=
        "given a set of previous alignments, only write new pairs [Default=%default]."
    )

    parser.set_defaults(filename_sequences=None,
                        filename_exons=None,
                        filename_map=None,
                        filename_outfile=None,
                        no_gaps=False,
                        format="fasta",
                        expand=False,
                        require_codons=False,
                        no_identical=False,
                        min_length=0,
                        report_step=100,
                        one_based_coordinates=False,
                        filename_filter=None)

    (options, args) = E.Start(parser, add_mysql_options=True)

    t0 = time.time()
    if options.filename_sequences:
        sequences = Genomics.ReadPeptideSequences(
            open(options.filename_sequences, "r"))
    else:
        sequences = {}

    if options.loglevel >= 1:
        options.stdlog.write("# read %i sequences\n" % len(sequences))
        sys.stdout.flush()

    if options.filename_exons:
        exons = Exons.ReadExonBoundaries(open(options.filename_exons, "r"))
    else:
        exons = {}

    if options.loglevel >= 1:
        options.stdlog.write("# read %i exons\n" % len(exons))
        sys.stdout.flush()

    if options.filename_map:
        map_old2new = {}
        for line in open(options.filename_map, "r"):
            if line[0] == "#":
                continue
            m = Map()
            m.read(line)
            map_old2new[m.mToken] = m
    else:
        map_old2new = {}

    if options.loglevel >= 1:
        options.stdlog.write("# read %i maps\n" % len(map_old2new))
        sys.stdout.flush()

    if options.filename_filter:
        if options.loglevel >= 1:
            options.stdlog.write("# reading filtering information.\n")
            sys.stdout.flush()

        map_pair2hids = {}

        if os.path.exists(options.filename_filter):

            infile = open(options.filename_filter, "r")

            iterator = FastaIterator.FastaIterator(infile)

            while 1:
                cur_record = iterator.next()
                if cur_record is None:
                    break

                record1 = cur_record

                cur_record = iterator.next()
                if cur_record is None:
                    break

                record2 = cur_record

                identifier1 = re.match("(\S+)", record1.title).groups()[0]
                identifier2 = re.match("(\S+)", record2.title).groups()[0]

                id = "%s-%s" % (identifier1, identifier2)
                s = Genomics.GetHID(record1.sequence + ";" + record2.sequence)

                if id not in map_pair2hids:
                    map_pair2hids[id] = []

                map_pair2hids[id].append(s)

            infile.close()

        if options.loglevel >= 1:
            options.stdlog.write(
                "# read filtering information for %i pairs.\n" %
                len(map_pair2hids))
            sys.stdout.flush()
    else:
        map_pair2hids = None

    if options.loglevel >= 1:
        options.stdlog.write("# finished input in %i seconds.\n" %
                             (time.time() - t0))

    if options.filename_outfile:
        outfile = open(options.filename_outfile, "w")
    else:
        outfile = None

    map_row2col = alignlib_lite.py_makeAlignmentVector()
    tmp1_map_row2col = alignlib_lite.py_makeAlignmentVector()
    counts = {}

    iterations = 0

    t1 = time.time()
    ninput, nskipped, noutput = 0, 0, 0

    for link in BlastAlignments.iterator_links(sys.stdin):

        iterations += 1
        ninput += 1

        if options.loglevel >= 1:
            if (iterations % options.report_step == 0):
                options.stdlog.write("# iterations: %i in %i seconds.\n" %
                                     (iterations, time.time() - t1))
                sys.stdout.flush()

        if link.mQueryToken not in sequences or \
           link.mSbjctToken not in sequences:
            nskipped += 1
            continue

        if options.loglevel >= 3:
            options.stdlog.write("# read link %s\n" % str(link))

        row_seq = alignlib_lite.py_makeSequence(sequences[link.mQueryToken])
        col_seq = alignlib_lite.py_makeSequence(sequences[link.mSbjctToken])

        if options.one_based_coordinates:
            link.mQueryFrom -= 1
            link.mSbjctFrom -= 1

        if options.expand:
            link.mQueryFrom = link.mQueryFrom * 3
            link.mSbjctFrom = link.mSbjctFrom * 3
            link.mQueryAli = ScaleAlignment(link.mQueryAli, 3)
            link.mSbjctAli = ScaleAlignment(link.mSbjctAli, 3)

        map_row2col.clear()

        alignlib_lite.py_AlignmentFormatEmissions(
            link.mQueryFrom, link.mQueryAli, link.mSbjctFrom,
            link.mSbjctAli).copy(map_row2col)

        if link.mQueryToken in map_old2new:
            tmp1_map_row2col.clear()
            map_old2new[link.mQueryToken].expand()
            if options.loglevel >= 3:
                options.stdlog.write("# combining in row with %s\n" % str(
                    alignlib_lite.py_AlignmentFormatEmissions(
                        map_old2new[link.mQueryToken].mMapOld2New)))

            alignlib_lite.py_combineAlignment(
                tmp1_map_row2col, map_old2new[link.mQueryToken].mMapOld2New,
                map_row2col, alignlib_lite.py_RR)
            map_old2new[link.mQueryToken].clear()
            alignlib_lite.py_copyAlignment(map_row2col, tmp1_map_row2col)

        if link.mSbjctToken in map_old2new:
            tmp1_map_row2col.clear()
            map_old2new[link.mSbjctToken].expand()
            if options.loglevel >= 3:
                options.stdlog.write("# combining in col with %s\n" % str(
                    alignlib_lite.py_AlignmentFormatEmissions(
                        map_old2new[link.mSbjctToken].mMapOld2New)))

            alignlib_lite.py_combineAlignment(
                tmp1_map_row2col, map_row2col,
                map_old2new[link.mSbjctToken].mMapOld2New, alignlib_lite.py_CR)
            map_old2new[link.mSbjctToken].clear()
            alignlib_lite.py_copyAlignment(map_row2col, tmp1_map_row2col)

        dr = row_seq.getLength() - map_row2col.getRowTo()
        dc = col_seq.getLength() - map_row2col.getColTo()
        if dr < 0 or dc < 0:
            raise ValueError(
                "out of bounds alignment: %s-%s: alignment out of bounds. row=%i col=%i ali=%s"
                %
                (link.mQueryToken, link.mSbjctToken, row_seq.getLength(),
                 col_seq.getLength(),
                 str(alignlib_lite.py_AlignmentFormatEmissions(map_row2col))))

        if options.loglevel >= 2:
            options.stdlog.write(
                str(
                    alignlib_lite.py_AlignmentFormatExplicit(
                        map_row2col, row_seq, col_seq)) + "\n")
        # check for incomplete codons
        if options.require_codons:

            naligned = map_row2col.getNumAligned()

            # turned off, while fixing alignlib_lite
            if naligned % 3 != 0:
                options.stdlog.write("# %s\n" % str(map_row2col))
                options.stdlog.write("# %s\n" % str(link))
                options.stdlog.write("# %s\n" %
                                     str(map_old2new[link.mQueryToken]))
                options.stdlog.write("# %s\n" %
                                     str(map_old2new[link.mSbjctToken]))
                options.stdlog.write("#\n%s\n" %
                                     alignlib_lite.py_AlignmentFormatExplicit(
                                         map_row2col, row_seq, col_seq))

                raise ValueError(
                    "incomplete codons %i in pair %s - %s" %
                    (naligned, link.mQueryToken, link.mSbjctToken))

        # if so desired, write on a per exon level:
        if exons:
            if link.mQueryToken not in exons:
                raise IndexError("%s not found in exons" % (link.mQueryToken))
            if link.mSbjctToken not in exons:
                raise IndexError("%s not found in exons" % (link.mSbjctToken))
            exons1 = exons[link.mQueryToken]
            exons2 = exons[link.mSbjctToken]

            # Get overlapping segments
            segments = Exons.MatchExons(map_row2col, exons1, exons2)

            for a, b in segments:
                tmp1_map_row2col.clear()

                # make sure you got codon boundaries. Note that frameshifts
                # in previous exons will cause the codons to start at positions
                # different from mod 3. The problem is that I don't know where
                # the frameshifts occur exactly. The exon boundaries are given
                # with respect to the cds, which include the frame shifts.
                # Unfortunately, phase information seems to be incomplete in
                # the input files.

                from1, to1 = GetAdjustedBoundaries(a, exons1)
                from2, to2 = GetAdjustedBoundaries(b, exons2)

                alignlib_lite.py_copyAlignment(tmp1_map_row2col, map_row2col,
                                               from1 + 1, to1, from2 + 1, to2)

                mode = Write(tmp1_map_row2col,
                             row_seq,
                             col_seq,
                             link,
                             no_gaps=options.no_gaps,
                             no_identical=options.no_identical,
                             min_length=options.min_length,
                             suffix1="_%s" % str(a),
                             suffix2="_%s" % str(b),
                             outfile=outfile,
                             pair_filter=map_pair2hid,
                             format=options.format)

                if mode not in counts:
                    counts[mode] = 0
                counts[mode] += 1

        else:
            mode = Write(map_row2col,
                         row_seq,
                         col_seq,
                         link,
                         min_length=options.min_length,
                         no_gaps=options.no_gaps,
                         no_identical=options.no_identical,
                         outfile=outfile,
                         pair_filter=map_pair2hids,
                         format=options.format)

            if mode not in counts:
                counts[mode] = 0
            counts[mode] += 1

        noutput += 1

    if outfile:
        outfile.close()

    if options.loglevel >= 1:
        options.stdlog.write("# %s\n" % ", ".join(
            map(lambda x, y: "%s=%i" %
                (x, y), counts.keys(), counts.values())))
        options.stdlog.write("# ninput=%i, noutput=%i, nskipped=%i\n" %
                             (ninput, noutput, nskipped))

    E.Stop()
Ejemplo n.º 21
0
def AlignCodonBased( seq_wobble, seq_cds, seq_peptide, map_p2c, options,
                     diag_width = 2, max_advance = 2 ):
    """advance in codons in seq_wobble and match to nucleotides in seq_cds.

    Due to alinglib this is all in one-based coordinates.
    Takes care of frameshifts.
    """
    
    map_p2c.clear()

    gop, gep = -1.0, -1.0
    matrix = alignlib_lite.py_makeSubstitutionMatrixBackTranslation( 1, -10, 1, alignlib_lite.py_getDefaultEncoder() )

    pep_seq = seq_peptide.asString()
    cds_seq = seq_cds.asString()
    wobble_seq = seq_wobble.asString()
    
    lcds = seq_cds.getLength()
    lwobble = seq_wobble.getLength()
    y = 0
    x = 0

    last_start = None

    while x < lwobble and y < lcds:

        xr = seq_wobble.asResidue( x )
        # skip over masked chars in wobble - these are gaps
        if seq_wobble.asChar(x) == "X": 
            x += 1
            continue

        # skip over masked chars in wobble - these are from
        # masked chars in the peptide sequence
        # Note to self: do not see all implications of this change
        # check later.
        if seq_wobble.asChar(x) == "N": 
            x += 1
            continue

        # skip over gaps in wobble 
        if seq_wobble.asChar(x) == "-": 
            x += 1
            continue

        s = matrix.getValue( xr, seq_cds.asResidue(y) )

        if options.loglevel >= 6:
            if (x % 3 == 0):
                c = seq_cds.asChar(y) + seq_cds.asChar(y+1) + seq_cds.asChar(y+2)
                options.stdlog.write( "# c=%s, x=%i, y=%i, aa=%s target=%s\n" % (c, x, y,
                                                                                 Genomics.MapCodon2AA( c ),
                                                                                 pep_seq[int(x/3)]) )
                                      
            options.stdlog.write( "# x=%i\twob=%s\ty=%i\tcds=%s\txr=%s\tcds=%i\tscore=%s\n" % \
                                      (x, seq_wobble.asChar(x), y, seq_cds.asChar(y), xr, seq_cds.asResidue(y), str(s) ))
            
        # deal with mismatches
        if s <= 0:

            tmp_map_p2c = alignlib_lite.py_makeAlignmentVector()

            ## backtrack to previous three codons and align
            ## three codons for double frameshifts that span two codons and
            ## produce two X's and six WWWWWW.

            ## number of nucleotides to extend (should be multiple of 3)
            ## less than 12 caused failure for some peptides.
            d = 15
            
            # extend by amound dx
            dx = (x % 3) + d
            
            x_start = max(0, x - dx )
            # map to ensure that no ambiguous residue mappings
            # exist after re-alignment
            y_start = max(0, map_p2c.mapRowToCol( x_start, alignlib_lite.py_RIGHT ))

            if (x_start, y_start) == last_start:
                raise ValueError( "infinite loop detected" )

            last_start = (x_start, y_start)

            x_end = min(x_start + 2 * d, len(wobble_seq) )
            y_end = min(y_start + 2 * d, len(cds_seq) )

            wobble_fragment = alignlib_lite.py_makeSequence(wobble_seq[x_start:x_end])
            cds_fragment = alignlib_lite.py_makeSequence(cds_seq[y_start:y_end])
            
            AlignExhaustive( wobble_fragment, cds_fragment, "", tmp_map_p2c, options )

            if options.loglevel >= 10:
                 options.stdlog.write("# fragmented alignment from %i-%i, %i-%i:\n%s\n" % (x_start, x_end,
                                                                                           y_start, y_end,
                                                                                           str(alignlib_lite.py_AlignmentFormatExplicit( tmp_map_p2c,
                                                                                                                                 wobble_fragment, 
                                                                                                                                 cds_fragment ))))
                 
                 options.stdlog.flush()

            ## clear alignment
            map_p2c.removeRowRegion( x_start, x_end )
            ngap = 0
            last_x, last_y = None, None
            for xxx in range( tmp_map_p2c.getRowFrom(), tmp_map_p2c.getRowTo() ):
                yyy = tmp_map_p2c.mapRowToCol(xxx)

                if yyy >= 0:
                    x = xxx + x_start
                    y = yyy + y_start
                    xr = seq_wobble.asResidue(x)
                    s = matrix.getValue( seq_wobble.asResidue(x), seq_cds.asResidue(y) )
                    if s < 0:
                        raise ValueError("mismatched residue wobble: %i (%s), cds: %i (%s)" % (x, seq_wobble.asChar(x), y, seq_cds.asChar(y)))
                    
                    map_p2c.addPair( x, y, s)
                    last_x, last_y = x, y
                    if options.loglevel >= 6:
                        options.stdlog.write( "# reset: x=%i\twob=%s\ty=%i\tcds=%s\txr=%s\tcds=%i\tscore=%i\n" % \
                                              (x, seq_wobble.asChar(x), y, seq_cds.asChar(y), xr, seq_cds.asResidue(y), s ))
                        options.stdlog.flush()
                    ngap = 0
                else:
                    ngap += 1

                # treat special case of double frameshifts. They might cause a petide/wobble residue
                # to be eliminated and thus the translated sequences will differ.
                # simply delete the last residue between x and y and move to next codon.
                if ngap == 3:
                    map_p2c.removeRowRegion( last_x, last_x + 1 )

                    last_x += 1
                    map_p2c.addPair( last_x, last_y )
                    if options.loglevel >= 6:
                        options.stdlog.write( "# double: x=%i\twob=%s\ty=%i\tcds=%s\txr=%s\tcds=%i\tscore=%i\n" % \
                                              (last_x, seq_wobble.asChar(last_x), last_y, seq_cds.asChar(last_y), xr, seq_cds.asResidue(last_y), s ))
                        options.stdlog.flush()                    
                    ngap = 0
                    
            ## exit condition if alignment is shorter than problematic residue
            ## need to catch this to avoid infinite loop.
            if tmp_map_p2c.getRowTo() < d:
                if lwobble - x <= 4:
                    ## only last codon is missing, so ok
                    break
                else:
                    raise ValueError("failure to align in designated window.")
                    
            s = 0
            
        s = matrix.getValue( xr, seq_cds.asResidue(y) )

        if s < 0:
            raise ValueError("mis-matching residues.")
        
        map_p2c.addPair( x, y, float(s) )
        
        # advance to next residues
        x += 1
        y += 1

    # sanity checks
    assert( map_p2c.getRowTo() <= seq_wobble.getLength() )
    assert( map_p2c.getColTo() <= seq_cds.getLength() )
Ejemplo n.º 22
0
def main( argv = None ):
    """script main.

    parses command line options in sys.argv, unless *argv* is given.
    """

    if argv == None: argv = sys.argv

    parser = E.OptionParser( version = "%prog version: $Id: blast2fasta.py 2782 2009-09-10 11:40:29Z andreas $",
                             usage = globals()["__doc__"] )

    parser.add_option("-s", "--sequences", dest="filename_sequences", type="string",
                      help="filename with sequences."  )
    parser.add_option("-f", "--format", dest="format", type="string",
                      help="output format."  )
    
    parser.set_defaults(
        filename_sequences = None,
        format = "fasta",
        )

    (options, args) = E.Start( parser )

    if not options.filename_sequences:
        raise "please supply filename with sequences."

    sequences = Genomics.ReadPeptideSequences( open(options.filename_sequences, "r") )

    if options.loglevel >= 1:
        print "# read %i sequences" % len(sequences)
        
    for k in sequences.keys():
        sequences[k] = alignlib_lite.py_makeSequence( sequences[k] )

    if options.loglevel >= 2:
        print "# converted %i sequences" % len(sequences)
    
    ninput, noutput, nskipped, nfailed = 0, 0, 0, 0
    link = BlastAlignments.Link()

    ali = alignlib_lite.py_makeAlignataVector()
    
    for line in sys.stdin:
        
        if line[0] == "#": continue

        link.Read( line )
        ninput += 1

        if link.mQueryToken not in sequences or link.mSbjctToken not in sequences:
            nskipped += 1
            continue
        
        ali.Clear()
        alignlib_lite.py_fillAlignataCompressed( ali, link.mQueryFrom, link.mQueryAli, link.mSbjctFrom, link.mSbjctAli )


        result = alignlib_lite.py_writePairAlignment( sequences[link.mQueryToken], sequences[link.mSbjctToken], ali ).split("\n")

        if len(result) != 3:
            nfailed += 1

        if options.format == "fasta":
            print ">%s %i-%i\n%s\n>%s %i-%i\n%s\n" %\
                  (link.mQueryToken, link.mQueryFrom, link.mQueryTo, result[0].split("\t")[1],
                   link.mSbjctToken, link.mSbjctFrom, link.mSbjctTo, result[1].split("\t")[1] )
            
        noutput += 1
     
    E.info( "ninput=%i, noutput=%i, nskipped=%i, nfailed=%i" % (ninput, noutput, nskipped, nfailed) )
    E.Stop()
Ejemplo n.º 23
0
def getAlignmentFull(m, q, t, options):
    """print alignment with gaps in both query and target."""
    a = alignlib_lite.py_AlignmentFormatExplicit(
        m, alignlib_lite.py_makeSequence(q), alignlib_lite.py_makeSequence(t))
    return a.mRowAlignment, a.mColAlignment
Ejemplo n.º 24
0
    def Align( self, method, anchor = 0, loglevel = 1 ):
        """align a pair of sequences.
        get rid of this and use a method class instead in the future
        """
        
        map_a2b = alignlib_lite.py_makeAlignmentVector()
        s1 = "A" * anchor + self.mSequence1 + "A" * anchor
        s2 = "A" * anchor + self.mSequence2 + "A" * anchor    

        self.strand = "+"

        if method == "dialign":
            dialign = WrapperDialign.Dialign( self.mOptionsDialign )
            dialign.Align( s1, s2, map_a2b )
        elif method == "blastz":
            blastz = WrapperBlastZ.BlastZ( self.mOptionsBlastZ )
            blastz.Align( s1, s2, map_a2b )
            if blastz.isReverseComplement():
                self.strand = "-"
                self.mSequence2 = Genomics.complement( self.mSequence2 )

        elif method == "dialignlgs":
            dialignlgs = WrapperDialign.Dialign( self.mOptionsDialignLGS )
            dialignlgs.Align( s1, s2, map_a2b ) 
        elif method == "dba":
            dba = WrapperDBA.DBA()
            dba.Align( s1, s2, map_a2b )
        elif method == "clustal":
            raise NotImplementedError( "clustal wrapper needs to be updated")
            clustal = WrapperClustal.Clustal()
            clustal.Align( s1, s2, map_a2b )
        elif method == "nw":
            seq1 = alignlib_lite.py_makeSequence( s1 )
            seq2 = alignlib_lite.py_makeSequence( s2 )
            alignator = alignlib_lite.py_makeAlignatorDPFull( alignlib_lite.py_ALIGNMENT_GLOBAL,
                                                      gop=-12.0,
                                                      gep=-2.0 )
            alignator.align( map_a2b, seq1, seq2 )
        elif method == "sw":                        
            seq1 = alignlib_lite.py_makeSequence( s1 )
            seq2 = alignlib_lite.py_makeSequence( s2 )
            alignlib_lite.py_performIterativeAlignment( map_a2b, seq1, seq2, alignator_sw, min_score_sw )
        else:
            ## use callback function
            method(s1, s2, map_a2b)

        if map_a2b.getLength() == 0:
            raise AlignmentError("empty alignment")

        if anchor:
            map_a2b.removeRowRegion( anchor + len(self.mSequence1) + 1, map_a2b.getRowTo() )
            map_a2b.removeRowRegion( 1, anchor)        
            map_a2b.removeColRegion( anchor + len(self.mSequence2) + 1, map_a2b.getColTo() )        
            map_a2b.removeColRegion( 1, anchor)
            map_a2b.moveAlignment( -anchor, -anchor )

        f = alignlib_lite.py_AlignmentFormatExplicit( map_a2b, 
                                              alignlib_lite.py_makeSequence( self.mSequence1),
                                              alignlib_lite.py_makeSequence( self.mSequence2) )

        self.mMethod = method
        self.mAlignment = map_a2b
        self.mAlignedSequence1, self.mAlignedSequence2 = f.mRowAlignment, f.mColAlignment
        f = alignlib_lite.py_AlignmentFormatEmissions( map_a2b )
        self.mAlignment1, self.mAlignment2 = f.mRowAlignment, f.mColAlignment
        self.mAlignmentFrom1 = map_a2b.getRowFrom()
        self.mAlignmentTo1 = map_a2b.getRowTo()        
        self.mAlignmentFrom2 = map_a2b.getColFrom()
        self.mAlignmentTo2 = map_a2b.getColTo()        
        self.mNumGaps, self.mLength = map_a2b.getNumGaps(), map_a2b.getLength()
        self.mAligned = self.mLength - self.mNumGaps

        self.SetPercentIdentity()
        self.SetBlockSizes()
Ejemplo n.º 25
0
            sys.exit(0)
        elif o in ("-h", "--help"):
            print globals()["__doc__"]
            sys.exit(0)

    alignator = alignlib_lite.py_makeAlignatorDPFull(alignlib_lite.py_ALIGNMENT_LOCAL, param_gop, param_gep)
    map_query2token = alignlib_lite.py_makeAlignmentVector()

    for line in sys.stdin:
        if line[0] == "#":
            continue

        query_token, sbjct_token, query_sequence, sbjct_sequence = string.split(line[:-1], "\t")

        map_query2token.clear()
        row = alignlib_lite.py_makeSequence(query_sequence)
        col = alignlib_lite.py_makeSequence(sbjct_sequence)
        alignator.align(map_query2token, row, col)

        pidentity = 100.0 * alignlib_lite.py_calculatePercentIdentity(map_query2token, row, col)
        psimilarity = 100.0 * alignlib_lite.py_calculatePercentSimilarity(map_query2token)
        print string.join(
            map(
                str,
                (
                    query_token,
                    sbjct_token,
                    map_query2token.getScore(),
                    alignlib_lite.py_AlignmentFormatEmissions(map_query2token),
                    pidentity,
                    psimilarity,
Ejemplo n.º 26
0
            if param_is_compressed:
                if unaligned_pair and \
                        unaligned_pair.mToken1 == pair.mToken1 and \
                        unaligned_pair.mToken2 == pair.mToken2 and \
                        unaligned_pair.mIntronId1 == pair.mIntronId1:

                    map_a2b = alignlib_lite.py_makeAlignmentVector()
                    f = AlignmentFormatEmissions(
                        pair.mFrom1, pair.mAlignedSequence1, pair.mFrom2,
                        pair.mAlignedSequence2).copy(map_a2b)
                    map_a2b.moveAlignment(-unaligned_pair.mFrom1 + 1,
                                          -unaligned_pair.mFrom2 + 1)

                    data = alignlib_lite.py_AlignmentFormatExplicit(
                        map_a2b,
                        alignlib_lite.py_makeSequence(
                            unaligned_pair.mAlignedSequence1),
                        alignlib_lite.py_makeSequence(
                            unaligned_pair.mAlignedSequence2))

                    from1, ali1, to1 = data.mRowFrom, data.mRowAlignment, data.mRowTo
                    from2, ali2, to2 = data.mColFrom, data.mColAlignment, data.mColTo

                    pair.mAlignedSequence1 = ali1
                    pair.mAlignedSequence2 = ali2

                else:
                    raise "sequence not found for pair %s" % str(pair)

            if param_do_gblocks:
                if param_loglevel >= 4:
                    print "# length before: %i %i" % (len(
Ejemplo n.º 27
0
def main( argv = None ):
    
    if argv == None: argv = sys.argv

    parser = E.OptionParser( version = "%prog version: $Id: align_all_vs_all.py 2782 2009-09-10 11:40:29Z andreas $",
                             usage = globals()["__doc__"] )

    parser.add_option("-s", "--sequences", dest="filename_sequences", type="string",
                      help="input file with sequences"  )

    parser.set_defaults(
        filename_sequences = None,
        gop = -10.0,
        gep = -1.0,
        )

    (options, args) = E.Start( parser, add_pipe_options = True )

    if options.filename_sequences:
        infile = open(options.filename_sequences, "r")
    else:
        infile = sys.stdin

    parser = FastaIterator.FastaIterator( infile )

    sequences = []
    while 1:
        cur_record = iterator.next()
        
        if cur_record is None: break
        sequences.append( (cur_record.title, alignlib_lite.py_makeSequence(re.sub( " ", "", cur_record.sequence)) ) )
    
    if options.filename_sequences:
        infile.close()

    alignator = alignlib_lite.py_makeAlignatorFullDP( options.gop, options.gep )
    map_a2b = alignlib_lite.py_makeAlignataVector()
    nsequences = len(sequences)
    
    for x in range(0,nsequences-1):
        for y in range(x+1, nsequences):
            alignator.Align( sequences[x][1], sequences[y][1], map_a2b)

            row_ali, col_ali = alignlib_lite.py_writeAlignataCompressed( map_a2b )
            
            options.stdout.write( "%s\t%s\t%i\t%i\t%i\t%s\t%i\t%i\t%s\t%i\t%i\t%i\t%i\n" % (\
                sequences[x][0], sequences[y][0],
                map_a2b.getScore(),
                map_a2b.getRowFrom(),
                map_a2b.getRowTo(),
                row_ali,
                map_a2b.getColFrom(),
                map_a2b.getColTo(),
                col_ali,
                map_a2b.getScore(),
                100 * alignlib_lite.py_calculatePercentIdentity( map_a2b, sequences[x][1], sequences[y][1]),
                sequences[x][1].getLength(),
                sequences[y][1].getLength() ))
            

    E.Stop()