#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Question Help on list append!! Have been working on this the whole day!


    I have a problem on adding elements to the end of a list.
    Actually a_list.append works on some of my elements, but fails in others. They are in the same for loop. I really appreciate it if someone can help me find out why. I have been working on this all day today!

    Here it is:

    Code:
    for i in range(len(outlist)):
        right_nt = []
        for m in range(k):
            dic = {}
            for j in range(max_freq):
                nt = outlist[i][j][m]
                if nt in dic:
                    dic[nt] += 1
                else:
                    dic[nt] = 1
            print dic
            max_value = max(dic.values())
            print max_value
            if dic[nt] == max_value:
                right_nt.append(nt)
        kmer = ''.join(right_nt)
        print 'kmer is', kmer
    And here is the output:
    {'A': 3}
    3
    {'T': 2, 'C': 1}
    2
    {'G': 3}
    3
    {'T': 2, 'A': 1}
    2
    {'T': 2, 'C': 1}
    2

    {'G': 3}
    3
    {'C': 3}
    3
    kmer is ATGTGC

    It supposed to be ATGTTGC. Why is one of the T missing?
    Please help!
    Thank you!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    please show, in addition to anything else I overlooked,
    Code:
    print('outlist[0]')
    print(outlist[0])
    
    print('k')
    print(k)
    
    print('max_freq')
    print(max_freq)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by b49P23TIvg
    please show, in addition to anything else I overlooked,
    Code:
    print('outlist[0]')
    print(outlist[0])
    
    print('k')
    print(k)
    
    print('max_freq')
    print(max_freq)
    Thanks very much for your reply.

    Here it is

    outlist[0]
    ['ACGTTGC', 'ATGTCGC', 'ATGATGC']
    k
    7
    max_freq
    3


    I appreciate your time!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    Code:
    # the necessary input
    
    outlist = (['ACGTTGC', 'ATGTCGC', 'ATGATGC'],)
    
    
    
    # in turn, group is each item of outlist.
    # group therefor is a list of sequences
    
    for group in outlist:
    
    
    
        # counts is a list of dictionaries.
        # Each dictionary has an initial acid count of 0.
        # The length of counts is the length of the first sequence in group.
    
        counts = [dict(zip('ACGT',(0,)*4)) for sequence in group[0]]
    
    
    
        # count the number of each acid by position in sequence.
    
        for sequence in group:
    
            for (position, acid,) in enumerate(sequence):
    
                counts[position][acid] += 1
    
    
    
        # prepare the output list
    
        right_nt = []
    
    
    
        # acid_counts accumulates the summary by acid dictionary for each position in turn
    
        for acid_counts in counts:
    
            max_counts = max(acid_counts.values())
    
    
    
            # find the acid name associated with the most frequent value
    
            for (key, value,) in acid_counts.items():
                if value == max_counts:
                    break
    
    
            # and append it to the output list
    
            right_nt.append(key)
    
    
        # and the rest of the program looks useful
        kmer = ''.join(right_nt)
        print('kmer is {}'.format(kmer))
    
    
    
    # note that you'd need to insert additional code to verify
    # the sequences in a group are all the same length, and if
    # not then use an algorithm to determine "best alignment".
    Last edited by b49P23TIvg; November 16th, 2013 at 01:41 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo