def scanMotifReport(seqs, motif, threshold=0, jaspar = 'JASPAR_matrices.txt'):
    """ Produce a plot for a scan of the specified motif.
        The plot has as its x-axis position of sequence, and
        the y-axis the cumulative, non-negative PWM score over all sequences. """
    # check that all sequences are the same length and set sequence length
    seq_len = len(seqs[0])
    for seq in seqs:
        if len(seq) != seq_len:
            usage(sys.argv[0], "All sequences must have same length")
            return

    # create the motif and its reverse complemennt
    bg = prb.Distrib(sym.DNA_Alphabet, sequence.getCount(seqs))
    d = prb.readMultiCounts(jaspar)
    try:
        fg1 = d[motif]
        fg2 = getReverse(d[motif])
    except KeyError:
        usage(sys.argv[0], "Unknown motif %s" % motif)
        return
    print("Motif %s:" % motif)
    pwm1 = sequence.PWM(fg1, bg)
    pwm1.display(format='JASPAR')
    print("Motif %s (reverse complement):" % motif)
    pwm2 = sequence.PWM(fg2, bg)
    pwm2.display(format='JASPAR')

    # initialize things to zero
    avg_motif_score = np.zeros(seq_len)

    # compute average score at each position (on both strands) in sequences
    i_seq = 0
    motif_width = pwm1.length
    for seq in seqs:
        i_seq += 1
        # print >> sys.stderr, "Scoring seq: %4d\r" % (i_seq),

        # positive strand
        hits = pwm1.search(seq, threshold)
        pos_scores = seq_len * [0]
        for hit in hits:
            # mark hit at *center* of site (hence motif_width/2)
            pos_scores[hit[0]+(motif_width/2)] = hit[2]

        # negative strand
        hits = pwm2.search(seq, threshold)
        neg_scores = seq_len * [0]
        for hit in hits:
            neg_scores[hit[0]+(motif_width/2)] = hit[2]

        # use maximum score on two strands
        for i in range(seq_len):
            score = max(pos_scores[i], neg_scores[i])
            if (score > threshold):
                avg_motif_score[i] += score

    # compute average score
    for i in range(seq_len):
        avg_motif_score[i] /= len(seqs)

    # hw = 5 # window width is 2*hw + 1
    # smoothed_avg_motif_score = np.zeros(seq_len)
    # for i in range(hw, seq_len-motif_width+1-hw):
    #    smoothed_avg_motif_score[i]=sum(avg_motif_score[i-hw:i+hw+1])/(2*hw+1)

    # plot the average score curve
    # print >> sys.stderr, ""
    x = list(range(-(seq_len/2), (seq_len/2)))    # call center of sequence X=0
    lbl = "%s" % (motif)
    plt.plot(x, avg_motif_score, label=lbl)
    #plt.plot(x, smoothed_avg_motif_score, label=lbl)
    plt.axhline(color='black', linestyle='dotted')
    plt.legend(loc='lower center')
    plt.xlabel('position')
    plt.ylabel('average motif score')
    plt.title(motif)
    plt.show()
Exemple #2
0
def scanMotifReport(seqs, motif, threshold=0, jaspar="JASPAR_matrices.txt"):
    """ Produce a plot for a scan of the specified motif.
        The plot has as its x-axis position of sequence, and
        the y-axis the cumulative, non-negative PWM score over all sequences. """
    # check that all sequences are the same length and set sequence length
    seq_len = len(seqs[0])
    for seq in seqs:
        if len(seq) != seq_len:
            usage(sys.argv[0], "All sequences must have same length")
            return

    # create the motif and its reverse complemennt
    bg = prb.Distrib(sym.DNA_Alphabet, sequence.getCount(seqs))
    d = prb.readMultiCounts(jaspar)
    try:
        fg1 = d[motif]
        fg2 = getReverse(d[motif])
    except KeyError:
        usage(sys.argv[0], "Unknown motif %s" % motif)
        return
    print "Motif %s:" % motif
    pwm1 = sequence.PWM(fg1, bg)
    pwm1.display(format="JASPAR")
    print "Motif %s (reverse complement):" % motif
    pwm2 = sequence.PWM(fg2, bg)
    pwm2.display(format="JASPAR")

    # initialize things to zero
    avg_motif_score = np.zeros(seq_len)

    # compute average score at each position (on both strands) in sequences
    i_seq = 0
    motif_width = pwm1.length
    for seq in seqs:
        i_seq += 1
        # print >> sys.stderr, "Scoring seq: %4d\r" % (i_seq),

        # positive strand
        hits = pwm1.search(seq, threshold)
        pos_scores = seq_len * [0]
        for hit in hits:
            # mark hit at *center* of site (hence motif_width/2)
            pos_scores[hit[0] + (motif_width / 2)] = hit[2]

        # negative strand
        hits = pwm2.search(seq, threshold)
        neg_scores = seq_len * [0]
        for hit in hits:
            neg_scores[hit[0] + (motif_width / 2)] = hit[2]

        # use maximum score on two strands
        for i in range(seq_len):
            score = max(pos_scores[i], neg_scores[i])
            if score > threshold:
                avg_motif_score[i] += score

    # compute average score
    for i in range(seq_len):
        avg_motif_score[i] /= len(seqs)

    # hw = 5 # window width is 2*hw + 1
    # smoothed_avg_motif_score = np.zeros(seq_len)
    # for i in range(hw, seq_len-motif_width+1-hw):
    #    smoothed_avg_motif_score[i]=sum(avg_motif_score[i-hw:i+hw+1])/(2*hw+1)

    # plot the average score curve
    # print >> sys.stderr, ""
    x = range(-(seq_len / 2), (seq_len / 2))  # call center of sequence X=0
    lbl = "%s" % (motif)
    plt.plot(x, avg_motif_score, label=lbl)
    # plt.plot(x, smoothed_avg_motif_score, label=lbl)
    plt.axhline(color="black", linestyle="dotted")
    plt.legend(loc="lower center")
    plt.xlabel("position")
    plt.ylabel("average motif score")
    plt.title(motif)
    plt.show()
def scanMotifReport_new(seqs, motif, threshold=3.4567, jaspar = 'JASPAR_matrices.txt', seed=0):
    """ Produce a plot for a scan of the specified motif.
        The plot has as its x-axis position of sequence, and
        the y-axis the number of sequences with a best hit at position x.
        Sequences with no hit above 'threshold' are ignored.
        Ties for best hit are broken randomly.
        The p-value of the central region that is most "centrally enriched"
        and the width of the best central region is printed in the label
        of the plot.
    """

    # set the random seed for repeatability
    random.seed(seed)

    # Copy the code from your "improved" version of scanMotifReport()
    # to here, and follow the instructions in the Prac to develop this
    # new function.

    # check that all sequences are the same length and set sequence length
    seq_len = len(seqs[0])
    for seq in seqs:
        if len(seq) != seq_len:
            usage(sys.argv[0], "All sequences must have same length")
            return

    # create the motif and its reverse complemennt
    bg = prb.Distrib(sym.DNA_Alphabet, sequence.getCount(seqs))
    d = prb.readMultiCounts(jaspar)
    try:
        fg1 = d[motif]
        fg2 = getReverse(d[motif])
    except KeyError:
        usage(sys.argv[0], "Unknown motif %s" % motif)
        return
    print("Motif %s:" % motif)
    pwm1 = sequence.PWM(fg1, bg)
    pwm1.display(format='JASPAR')
    print("Motif %s (reverse complement):" % motif)
    pwm2 = sequence.PWM(fg2, bg)
    pwm2.display(format='JASPAR')

    # initialize things to zero
    hit_count = np.zeros(seq_len)
    n_seqs_with_hits = 0.0

    # Scan each sequence for all hits on both strands and record
    # the number of "best hits" at each sequence position.
    #
    motif_width = pwm1.length
    i_seq = 0
    for seq in seqs:
        i_seq += 1
        # print >> sys.stderr, "Scoring seq: %4d\r" % (i_seq),
        # scan with both motifs
        hits = pwm1.search(seq, threshold) + pwm2.search(seq, threshold)
        # Record position of best hit
        if (hits):
                n_seqs_with_hits += 1
                # find best hit score
                best_score = max(hits, key=operator.itemgetter(1))[2]
                # find ties
                best_hits = [ hit for hit in hits if hit[2] == best_score ]
                # break ties at random
                best_hit = random.choice(best_hits)
                # mark hit at *center* of site (hence pwm1.length/2)
                hit_count[best_hit[0] + pwm1.length/2] += 1
    # divide number of sequences with hit by total number of hits
    site_probability = [ (cnt/n_seqs_with_hits) for cnt in hit_count ]

    print("Number of sequences with hit (score >= %f): %d" % (threshold, n_seqs_with_hits), file=sys.stderr)

    # STATISTICS
    # Get the cumulative hit counts in concentric windows
    # and perform the Binomial Test.  Report best region and its p-value.
    #
    best_r = 0
    best_log_pvalue = 1
    center = seq_len/2                  # center of sequence
    cum_hit_count = np.zeros(seq_len)   # total hits in window of width i
    for i in range(1, (seq_len - pwm1.length/2 + 1)/2):
        cum_hit_count[i] = cum_hit_count[i-1] + hit_count[center-i] + hit_count[center+i]
        # Compute probability of observed or more best hits in central window
        # assuming uniform probability distribution in each sequence.
    #   successes = cum_hit_count[i]
    #   trials = n_seqs_with_hits
    #    p_success = ?
    #    log_pvalue = ?
    #    if (log_pvalue < best_log_pvalue):
    #        best_log_pvalue = log_pvalue
    #        best_r = 2*i
    # End STATISTICS

    hw = 5
    smoothed_site_probability = np.zeros(seq_len)
    for i in range(hw, seq_len-motif_width+1-hw):
        smoothed_site_probability[i]=sum(site_probability[i-hw:i+hw+1])/(2*hw+1)

    x = list(range(-(seq_len/2), (seq_len/2)))        # call center of sequence X=0
    lbl = "%s, t=%.2f" % (motif, threshold)
    #lbl = "%s, t=%.2f, w=%d, p=%.2e" % (motif, threshold, best_r, math.exp(best_log_pvalue))
    plt.plot(x, smoothed_site_probability, label=lbl)
    plt.axhline(color='black', linestyle='dotted')
    plt.legend(loc='lower center')
    plt.xlabel('Position of best site')
    plt.ylabel('Smoothed probability')
    plt.title(motif)
    plt.show()
Exemple #4
0
def scanMotifReport_new(seqs, motif, threshold=3.4567, jaspar="JASPAR_matrices.txt", seed=0):
    """ Produce a plot for a scan of the specified motif.
        The plot has as its x-axis position of sequence, and
        the y-axis the number of sequences with a best hit at position x.
        Sequences with no hit above 'threshold' are ignored.
        Ties for best hit are broken randomly.
        The p-value of the central region that is most "centrally enriched"
        and the width of the best central region is printed in the label
        of the plot.
    """

    # set the random seed for repeatability
    random.seed(seed)

    # Copy the code from your "improved" version of scanMotifReport()
    # to here, and follow the instructions in the Prac to develop this
    # new function.

    # check that all sequences are the same length and set sequence length
    seq_len = len(seqs[0])
    for seq in seqs:
        if len(seq) != seq_len:
            usage(sys.argv[0], "All sequences must have same length")
            return

    # create the motif and its reverse complemennt
    bg = prb.Distrib(sym.DNA_Alphabet, sequence.getCount(seqs))
    d = prb.readMultiCounts(jaspar)
    try:
        fg1 = d[motif]
        fg2 = getReverse(d[motif])
    except KeyError:
        usage(sys.argv[0], "Unknown motif %s" % motif)
        return
    print "Motif %s:" % motif
    pwm1 = sequence.PWM(fg1, bg)
    pwm1.display(format="JASPAR")
    print "Motif %s (reverse complement):" % motif
    pwm2 = sequence.PWM(fg2, bg)
    pwm2.display(format="JASPAR")

    # initialize things to zero
    hit_count = np.zeros(seq_len)
    n_seqs_with_hits = 0.0

    # Scan each sequence for all hits on both strands and record
    # the number of "best hits" at each sequence position.
    #
    motif_width = pwm1.length
    i_seq = 0
    for seq in seqs:
        i_seq += 1
        # print >> sys.stderr, "Scoring seq: %4d\r" % (i_seq),
        # scan with both motifs
        hits = pwm1.search(seq, threshold) + pwm2.search(seq, threshold)
        # Record position of best hit
        if hits:
            n_seqs_with_hits += 1
            # find best hit score
            best_score = max(hits, key=operator.itemgetter(1))[2]
            # find ties
            best_hits = [hit for hit in hits if hit[2] == best_score]
            # break ties at random
            best_hit = random.choice(best_hits)
            # mark hit at *center* of site (hence pwm1.length/2)
            hit_count[best_hit[0] + pwm1.length / 2] += 1
    # divide number of sequences with hit by total number of hits
    site_probability = [(cnt / n_seqs_with_hits) for cnt in hit_count]

    print >> sys.stderr, "Number of sequences with hit (score >= %f): %d" % (threshold, n_seqs_with_hits)

    # STATISTICS
    # Get the cumulative hit counts in concentric windows
    # and perform the Binomial Test.  Report best region and its p-value.
    #
    best_r = 0
    best_log_pvalue = 1
    center = seq_len / 2  # center of sequence
    cum_hit_count = np.zeros(seq_len)  # total hits in window of width i
    for i in range(1, (seq_len - pwm1.length / 2 + 1) / 2):
        cum_hit_count[i] = cum_hit_count[i - 1] + hit_count[center - i] + hit_count[center + i]
        # Compute probability of observed or more best hits in central window
        # assuming uniform probability distribution in each sequence.
    #   successes = cum_hit_count[i]
    #   trials = n_seqs_with_hits
    #    p_success = ?
    #    log_pvalue = ?
    #    if (log_pvalue < best_log_pvalue):
    #        best_log_pvalue = log_pvalue
    #        best_r = 2*i
    # End STATISTICS

    hw = 5
    smoothed_site_probability = np.zeros(seq_len)
    for i in range(hw, seq_len - motif_width + 1 - hw):
        smoothed_site_probability[i] = sum(site_probability[i - hw : i + hw + 1]) / (2 * hw + 1)

    x = range(-(seq_len / 2), (seq_len / 2))  # call center of sequence X=0
    lbl = "%s, t=%.2f" % (motif, threshold)
    # lbl = "%s, t=%.2f, w=%d, p=%.2e" % (motif, threshold, best_r, math.exp(best_log_pvalue))
    plt.plot(x, smoothed_site_probability, label=lbl)
    plt.axhline(color="black", linestyle="dotted")
    plt.legend(loc="lower center")
    plt.xlabel("Position of best site")
    plt.ylabel("Smoothed probability")
    plt.title(motif)
    plt.show()