hashing a sequence

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kdt
    New Member
    • Mar 2007
    • 50

    #1

    hashing a sequence

    Hi
    i have a sequence of characters that I need to perform analysis on. I want to be able to generate a table with unique substrings of length n and their positions. A hash table was used in perl, not sure of the python equivelent .

    sequence = 'gtccaaagtt'

    trying to get the output into a table with substring in one column and position(s). for the above sequence this could be something like;
    aa 4,5

    i have the following code:

    Code:
    def split_len(seq, length):
          return [seq[i:i+length] for i in range(0, len(seq), length)]
    but it doesnt store locations or take into consideration repeats.

    been searching google for two days now Sad please help
    thanks
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by kdt
    Hi
    i have a sequence of characters that I need to perform analysis on. I want to be able to generate a table with unique substrings of length n and their positions. A hash table was used in perl, not sure of the python equivelent .

    sequence = 'gtccaaagtt'

    trying to get the output into a table with substring in one column and position(s). for the above sequence this could be something like;
    aa 4,5

    i have the following code:

    Code:
    def split_len(seq, length):
          return [seq[i:i+length] for i in range(0, len(seq), length)]
    but it doesnt store locations or take into consideration repeats.

    been searching google for two days now Sad please help
    thanks
    dictionaries are the equivalent of hashes in Perl. your piece of code actually splits the seq according to length given
    eg
    Code:
    >>> length=3
    >>> [seq[i:i+length] for i in range(0, len(seq), length)]
    ['gtc', 'caa', 'agt', 't']
    can you describe the output you want again?

    Comment

    • kdt
      New Member
      • Mar 2007
      • 50

      #3
      Originally posted by ghostdog74
      dictionaries are the equivalent of hashes in Perl. your piece of code actually splits the seq according to length given
      eg
      Code:
      >>> length=3
      >>> [seq[i:i+length] for i in range(0, len(seq), length)]
      ['gtc', 'caa', 'agt', 't']
      can you describe the output you want again?
      thanks for the quick reply. i was wanting the sequence to be split, but i think im going about it the wrong way. i want to take a sequence, break it up substrings of length n and record the position occurrence of the substrings in the query string. So for example for the sequence "attgccatgc ", specifying n=2 i would like a dictionary with:
      string position
      at 1,7
      tt 2
      tg 3,8
      gc 4,9
      etc...

      hope this helps

      thanks

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by kdt
        thanks for the quick reply. i was wanting the sequence to be split, but i think im going about it the wrong way. i want to take a sequence, break it up substrings of length n and record the position occurrence of the substrings in the query string. So for example for the sequence "attgccatgc ", specifying n=2 i would like a dictionary with:
        string position
        at 1,7
        tt 2
        tg 3,8
        gc 4,9
        etc...

        hope this helps

        thanks
        Like this?
        Code:
        import re
        
        def indexList(s, item, i=0):
            i_list = []
            while True:
                try:
                    i = s.index(item, i)
                    i_list.append(i)
                    i += 1
                except:
                    break
            return i_list
        
        s = 'Having the base plate subassembly mark at each column will do two things - reference the large scale base plate details and provide the checker with a check of the column bases in the model without having to check each column in the model.'
        
        sub_length = 4
        
        subList = [s[i:i+sub_length] for i in range(len(s)) if re.match(r'[a-z]{%s}' % sub_length, s[i:i+sub_length])]
        
        dd = {}
        for subi in subList:
            if not dd.has_key(subi):
                dd[subi] = indexList(s, subi)
        
        for key in dd:
            print '%s = %s' % (key, dd[key])
        Code:
        >>> ence = [80]
        renc = [79]
        fere = [77]
        scal = [95]
        thin = [66]
        colu = [47, 164, 219]
        ving = [2, 200]
        mode = [184, 233]
        ovid = [126]
        ..........................

        Comment

        • kdt
          New Member
          • Mar 2007
          • 50

          #5
          thanks btdev, works a treat :)

          Comment

          Working...