how to access the individual elements of a matrix in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aboxylica
    New Member
    • Jul 2007
    • 111

    #16
    headerList = linelist.pop(0)[1:]
    WHAT DOES THIS LINE DO?


    Originally posted by bvdet
    Check variable 'lineList'. It should look like this:[code=Python]>>> for line in lineList:
    ... print line
    ...
    ['01', '0.00', '3.67', '0.00', '0.00']
    ['02', '0.00', '0.00', '3.67', '0.00']
    ['03', '0.00', '0.00', '0.00', '3.67']
    ['04', '0.00', '3.67', '0.00', '0.00']
    ['05', '3.67', '0.00', '0.00', '0.00']
    ['06', '3.46', '0.00', '0.22', '0.00']
    ['07', '0.00', '0.00', '3.67', '0.00']
    ['08', '0.00', '0.00', '0.00', '3.67']
    ['09', '0.00', '0.00', '0.00', '3.67']
    ['10', '0.00', '3.67', '0.00', '0.00']
    ['11', '3.67', '0.00', '0.00', '0.00']
    ['12', '3.67', '0.00', '0.00', '0.00']
    ['13', '0.00', '0.00', '3.67', '0.00']
    ['14', '0.00', '0.00', '0.00', '3.67']
    ['15', '0.00', '0.00', '3.67', '0.00']
    ['16', '0.00', '3.67', '0.00', '0.00']
    >>> [/code]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #17
      Originally posted by aboxylica
      <snip>what does the strip( ) do?
      strip() removes whitespace (or any characters you tell it to) from BOTH ends of a string. Like this:[CODE=python]
      >>> s = " had_whitespace "
      >>> t = s.strip()
      >>> print repr(t) # repr() prints the string representation of a varialbe
      'had_whitespace '
      >>> [/CODE]

      Comment

      • aboxylica
        New Member
        • Jul 2007
        • 111

        #18
        headerList = linelist.pop(0)[1:]
        WHAT DOES THIS LINE DO?



        Originally posted by bartonc
        strip() removes whitespace (or any characters you tell it to) from BOTH ends of a string. Like this:[CODE=python]
        >>> s = " had_whitespace "
        >>> t = s.strip()
        >>> print repr(t) # repr() prints the string representation of a varialbe
        'had_whitespace '
        >>> [/CODE]

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #19
          Originally posted by aboxylica
          headerList = linelist.pop(0)[1:]
          WHAT DOES THIS LINE DO?
          Since I didn't write that, I'll just have to do my best to describe it:
          Pop(0) takes the zeroth element from linelist (which looks like another list). The result is stored in an unseen, temporary variable. [1:] takes all but the zeroth element from that temporary variable and that (shortened list) is stored in headerList. You can check it out by doing something like
          [CODE=python]temp = linelist.pop(0)
          print temp
          headerList = temp[1:] # this is called a "slice" from the temp list.
          print headerList[/CODE]
          Hope that helps.

          Comment

          • aboxylica
            New Member
            • Jul 2007
            • 111

            #20
            thanks for that . but could you give ur mail id..because I am not able to paste my exact file here and am stuck with this problem.plz help!
            Originally posted by bartonc
            Since I didn't write that, I'll just have to do my best to describe it:
            Pop(0) takes the zeroth element from linelist (which looks like another list). The result is stored in an unseen, temporary variable. [1:] takes all but the zeroth element from that temporary variable and that (shortened list) is stored in headerList. You can check it out by doing something like
            [CODE=python]temp = linelist.pop(0)
            print temp
            headerList = temp[1:] # this is called a "slice" from the temp list.
            print headerList[/CODE]
            Hope that helps.

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #21
              Originally posted by aboxylica
              thanks for that . but could you give ur mail id..because I am not able to paste my exact file here and am stuck with this problem.plz help!
              Paste what you can here and I'll format it for you.

              Comment

              • aboxylica
                New Member
                • Jul 2007
                • 111

                #22
                Originally posted by bartonc
                Paste what you can here and I'll format it for you.
                NA bap
                PO A C G T
                01 0.00 3.67 0.00 0.00
                02 0.00 0.00 3.67 0.00
                03 0.00 0.00 0.00 3.67
                04 0.00 3.67 0.00 0.00
                05 3.67 0.00 0.00 0.00
                06 3.46 0.00 0.22 0.00
                07 0.00 0.00 3.67 0.00
                08 0.00 0.00 0.00 3.67
                09 0.00 0.00 0.00 3.67
                10 0.00 3.67 0.00 0.00
                11 3.67 0.00 0.00 0.00
                12 3.67 0.00 0.00 0.00
                13 0.00 0.00 3.67 0.00
                14 0.00 0.00 0.00 3.67
                15 0.00 0.00 3.67 0.00
                16 0.00 3.67 0.00 0.00
                //
                //
                NA bcd
                PO A C G T
                01 42.55 8.75 145.86 8.14
                02 0.14 0.53 204.64 0.00
                03 126.83 78.02 0.11 0.34
                04 0.21 0.17 0.00 204.92
                05 0.00 12.38 0.43 192.50
                06 174.48 0.95 1.32 28.56
                07 79.53 4.70 100.44 20.64
                //
                //
                NA bin
                PO A C G T
                01 0.45 8.27 0.00 11.39
                02 0.00 0.00 10.02 10.09
                03 5.80 1.39 0.00 12.93
                04 12.33 5.18 2.60 0.00
                05 12.43 0.00 0.00 7.68
                06 18.55 0.00 1.57 0.00
                07 0.05 0.58 0.00 19.48
                08 20.11 0.00 0.00 0.00
                09 20.06 0.05 0.00 0.00
                10 20.11 0.00 0.00 0.00
                11 0.00 15.33 0.00 4.78
                12 20.06 0.05 0.00 0.00
                13 14.99 0.35 4.78 0.00
                14 13.64 2.42 3.37 0.68
                15 5.03 0.00 15.08 0.00
                16 7.23 0.45 10.94 1.49
                //
                //
                I want to add one to each and every element of the file(of course except the first column giving the position)
                and want to access the elements like A[01]=,c[02]=,etc..

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #23
                  Originally posted by aboxylica
                  NA bap
                  PO A C G T
                  01 0.00 3.67 0.00 0.00
                  02 0.00 0.00 3.67 0.00
                  03 0.00 0.00 0.00 3.67
                  04 0.00 3.67 0.00 0.00
                  05 3.67 0.00 0.00 0.00
                  06 3.46 0.00 0.22 0.00
                  07 0.00 0.00 3.67 0.00
                  08 0.00 0.00 0.00 3.67
                  09 0.00 0.00 0.00 3.67
                  10 0.00 3.67 0.00 0.00
                  11 3.67 0.00 0.00 0.00
                  12 3.67 0.00 0.00 0.00
                  13 0.00 0.00 3.67 0.00
                  14 0.00 0.00 0.00 3.67
                  15 0.00 0.00 3.67 0.00
                  16 0.00 3.67 0.00 0.00
                  //
                  //
                  NA bcd
                  PO A C G T
                  01 42.55 8.75 145.86 8.14
                  02 0.14 0.53 204.64 0.00
                  03 126.83 78.02 0.11 0.34
                  04 0.21 0.17 0.00 204.92
                  05 0.00 12.38 0.43 192.50
                  06 174.48 0.95 1.32 28.56
                  07 79.53 4.70 100.44 20.64
                  //
                  //
                  NA bin
                  PO A C G T
                  01 0.45 8.27 0.00 11.39
                  02 0.00 0.00 10.02 10.09
                  03 5.80 1.39 0.00 12.93
                  04 12.33 5.18 2.60 0.00
                  05 12.43 0.00 0.00 7.68
                  06 18.55 0.00 1.57 0.00
                  07 0.05 0.58 0.00 19.48
                  08 20.11 0.00 0.00 0.00
                  09 20.06 0.05 0.00 0.00
                  10 20.11 0.00 0.00 0.00
                  11 0.00 15.33 0.00 4.78
                  12 20.06 0.05 0.00 0.00
                  13 14.99 0.35 4.78 0.00
                  14 13.64 2.42 3.37 0.68
                  15 5.03 0.00 15.08 0.00
                  16 7.23 0.45 10.94 1.49
                  //
                  //
                  I want to add one to each and every element of the file(of course except the first column giving the position)
                  and want to access the elements like A[01]=,c[02]=,etc..
                  Lacking your working program pasted here, I'm afraid that the best I can tell you is it should be as simple as:[CODE=python]temp = A[01]
                  print temp
                  A[01] = temp + 1
                  print A[01][/CODE]or even [CODE=python]print A[01]
                  A[01] += 1
                  print A[01][/CODE]in a loop through your data structure.

                  Comment

                  • aboxylica
                    New Member
                    • Jul 2007
                    • 111

                    #24
                    [CODE=python]
                    file_=open("hal f1.txt","r")
                    file_content=fi le_.readlines()
                    linelist=[line.strip().sp lit() for line in file_content if line != '\n']

                    headerlist=line list.pop(0)[1:]

                    keys=[i[0] for i in linelist]


                    values=[[float(s) for s in item] for item in [j[1:] for j in linelist]]

                    linedict =dict(zip(keys, values))

                    datadict={}
                    for i,item in enumerate(heade rlist):
                    datadict[item]={}
                    for key in linedict:
                    print key
                    datadict[item][key]=linedict[key][i]
                    print datadict['A']['01']
                    [/CODE]

                    when i execute i get an error that says
                    keyerror: '01'

                    <removed quote=bvdet to make this post appear.>

                    'headerList':[code=Python]>>> headerList
                    ['A', 'C', 'G', 'T']
                    >>> [/code]The values in 'headerList' will be 'keys' in 'dataDict'. 'dataDict' will be the main dictionary, and the values will be subdictionaries . A dictionary key is associated with a value - in this case the value will be another dictionary. Variable 'keys' contain the subdictionary keys:[code=Python]>>> keys
                    ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16']
                    >>> [/code]'lineDict' is a temporary dictionary created to make it easier to compile the data in the form you wanted:[code=Python]>>> lineDict
                    {'02': [0.0, 0.0, 3.6699999999999 999, 0.0], '03': [0.0, 0.0, 0.0, 3.6699999999999 999], '13': [0.0, 0.0, 3.6699999999999 999, 0.0], '01': [0.0, 3.6699999999999 999, 0.0, 0.0], '06': [3.46, 0.0, 0.22, 0.0], '07': [0.0, 0.0, 3.6699999999999 999, 0.0], '04': [0.0, 3.6699999999999 999, 0.0, 0.0], '05': [3.6699999999999 999, 0.0, 0.0, 0.0], '08': [0.0, 0.0, 0.0, 3.6699999999999 999], '09': [0.0, 0.0, 0.0, 3.6699999999999 999], '16': [0.0, 3.6699999999999 999, 0.0, 0.0], '12': [3.6699999999999 999, 0.0, 0.0, 0.0], '14': [0.0, 0.0, 0.0, 3.6699999999999 999], '11': [3.6699999999999 999, 0.0, 0.0, 0.0], '15': [0.0, 0.0, 3.6699999999999 999, 0.0], '10': [0.0, 3.6699999999999 999, 0.0, 0.0]}
                    >>> [/code]Using 'enumerate' on 'headerList', Python gives me these values:[code=Python]>>> for i, item in enumerate(heade rList):
                    ... print i, item
                    ...
                    0 A
                    1 C
                    2 G
                    3 T
                    >>> [/code]Here's an interactive example showing what is happening inside the nested for loop:[code=Python]>>> dataDict
                    {}
                    >>> key
                    '10'
                    >>> item
                    'T'
                    >>> lineDict
                    {'02': [0.0, 0.0, 3.6699999999999 999, 0.0], '03': [0.0, 0.0, 0.0, 3.6699999999999 999], '13': [0.0, 0.0, 3.6699999999999 999, 0.0], '01': [0.0, 3.6699999999999 999, 0.0, 0.0], '06': [3.46, 0.0, 0.22, 0.0], '07': [0.0, 0.0, 3.6699999999999 999, 0.0], '04': [0.0, 3.6699999999999 999, 0.0, 0.0], '05': [3.6699999999999 999, 0.0, 0.0, 0.0], '08': [0.0, 0.0, 0.0, 3.6699999999999 999], '09': [0.0, 0.0, 0.0, 3.6699999999999 999], '16': [0.0, 3.6699999999999 999, 0.0, 0.0], '12': [3.6699999999999 999, 0.0, 0.0, 0.0], '14': [0.0, 0.0, 0.0, 3.6699999999999 999], '11': [3.6699999999999 999, 0.0, 0.0, 0.0], '15': [0.0, 0.0, 3.6699999999999 999, 0.0], '10': [0.0, 3.6699999999999 999, 0.0, 0.0]}
                    >>> lineDict[key][3]
                    0.0
                    >>> dataDict[item] = {}
                    >>> dataDict[item][key] = lineDict[key][2]
                    >>> dataDict
                    {'T': {'10': 0.0}}
                    >>> [/code]I hope this helps you understand what is happening.

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #25
                      Originally posted by aboxylica
                      [CODE=python]
                      file_=open("hal f1.txt","r")
                      file_content=fi le_.readlines()
                      linelist=[line.strip().sp lit() for line in file_content if line != '\n']

                      headerlist=line list.pop(0)[1:]

                      keys=[i[0] for i in linelist]


                      values=[[float(s) for s in item] for item in [j[1:] for j in linelist]]

                      linedict =dict(zip(keys, values))

                      datadict={}
                      for i,item in enumerate(heade rlist):
                      datadict[item]={}
                      for key in linedict:
                      print key
                      datadict[item][key]=linedict[key][i]
                      print datadict['A']['01']
                      [/CODE]

                      when i execute i get an error that says
                      keyerror: '01'
                      [CODE=python]
                      file_=open("hal f1.txt","r")
                      file_content=fi le_.readlines()
                      linelist=[line.strip().sp lit() for line in file_content if line != '\n']

                      headerlist=line list.pop(0)[1:]

                      keys=[i[0] for i in linelist]


                      values=[[float(s) for s in item] for item in [j[1:] for j in linelist]]

                      linedict =dict(zip(keys, values))


                      # Form the data dictionary first:
                      datadict={}
                      for i,item in enumerate(heade rlist):
                      datadict[item]={}
                      for key in linedict:
                      datadict[item][key]=linedict[key][i]

                      # Then loop through the data structure. But since you have turned
                      # the data sideways, I can not see the structure at this moment.
                      [/CODE]

                      Comment

                      • aboxylica
                        New Member
                        • Jul 2007
                        • 111

                        #26
                        [CODE=python]file_=open("hal f1.txt","r")
                        file_content=fi le_.readlines()
                        linelist=[line.strip().sp lit() for line in file_content if line != '\n']

                        headerlist=line list.pop(0)[1:]
                        print headerlist

                        keys=[i[0] for i in linelist]


                        values=[[float(s) for s in item] for item in [j[1:] for j in linelist]]

                        linedict =dict(zip(keys, values))

                        datadict={}
                        for i,item in enumerate(heade rlist):
                        datadict[item]={}
                        for key in linedict:
                        print key
                        datadict[item][key]=linedict[key][i]
                        print datadict['A']['01'][/CODE]
                        this is my program. i am getting an error that says
                        key error:'01' and i do want to add one to all the elements
                        (i mean the float values)
                        can you see the error?

                        Comment

                        • bartonc
                          Recognized Expert Expert
                          • Sep 2006
                          • 6478

                          #27
                          Originally posted by aboxylica
                          [CODE=python]file_=open("hal f1.txt","r")
                          file_content=fi le_.readlines()
                          linelist=[line.strip().sp lit() for line in file_content if line != '\n']

                          headerlist=line list.pop(0)[1:]
                          print headerlist

                          keys=[i[0] for i in linelist]


                          values=[[float(s) for s in item] for item in [j[1:] for j in linelist]]

                          linedict =dict(zip(keys, values))

                          datadict={}
                          for i,item in enumerate(heade rlist):
                          datadict[item]={}
                          for key in linedict:
                          print key
                          datadict[item][key]=linedict[key][i]
                          print datadict['A']['01'][/CODE]
                          this is my program. i am getting an error that says
                          key error:'01' and i do want to add one to all the elements
                          (i mean the float values)
                          can you see the error?
                          I've answered above.

                          You must learn to use code tags. Instructions are on the right hand side of the page when you are making your post (or reply). There is also very much helpful information in the How to ask a question section of our Posting Guidelines. Thanks
                          Last edited by bartonc; Jul 8 '07, 10:37 AM. Reason: Added [CODE=python][CODE] tags.

                          Comment

                          • bvdet
                            Recognized Expert Specialist
                            • Oct 2006
                            • 2851

                            #28
                            Originally posted by bartonc
                            Lacking your working program pasted here, I'm afraid that the best I can tell you is it should be as simple as:[CODE=python]temp = A[01]
                            print temp
                            A[01] = temp + 1
                            print A[01][/CODE]or even [CODE=python]print A[01]
                            A[01] += 1
                            print A[01][/CODE]in a loop through your data structure.
                            It looks like you have three data sets in the file. Which one do you want to work on?

                            Notice that I simplified my example code by eliminating some of the lines in the data file. It appears that you have not adjusted the code to account for that.

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #29
                              I have modified the first part of my example code to read the first data set in the OP file:[code=Python]fn = r'H:\TEMP\temsy s\data9.txt'
                              f = open(fn)

                              line = f.next()
                              while not line.startswith ('PO'):
                              line = f.next()

                              headerList = line.strip().sp lit()[1:]
                              lineList = []

                              line = f.next().strip( )
                              while not line.startswith ('/'):
                              if line != '':
                              lineList.append (line.strip().s plit())
                              line = f.next().strip( )

                              f.close()[/code]This will add a set amount to every element in the data:[code=Python]# Add 1.0 to every element in dataDict subdictionaries
                              for keyMain in dataDict:
                              for keySub in dataDict[keyMain]:
                              dataDict[keyMain][keySub] += 1.0[/code][code=Python]>>> dataDict
                              {'A': {'02': 1.0, '03': 1.0, '13': 1.0, '01': 1.0, '06': 4.46, '07': 1.0, '04': 1.0, '05': 4.6699999999999 999, '08': 1.0, '09': 1.0, '10': 1.0, '16': 1.0, '11': 4.6699999999999 999, '15': 1.0, '12': 4.6699999999999 999, '14': 1.0}, 'C': {'02': 1.0, '03': 1.0, '13': 1.0, '01': 4.6699999999999 999, '06': 1.0, '07': 1.0, '04': 4.6699999999999 999, '05': 1.0, '08': 1.0, '09': 1.0, '10': 4.6699999999999 999, '16': 4.6699999999999 999, '11': 1.0, '15': 1.0, '12': 1.0, '14': 1.0}, 'T': {'02': 1.0, '03': 4.6699999999999 999, '13': 1.0, '01': 1.0, '06': 1.0, '07': 1.0, '04': 1.0, '05': 1.0, '08': 4.6699999999999 999, '09': 4.6699999999999 999, '10': 1.0, '16': 1.0, '11': 1.0, '15': 1.0, '12': 1.0, '14': 4.6699999999999 999}, 'G': {'02': 4.6699999999999 999, '03': 1.0, '13': 4.6699999999999 999, '01': 1.0, '06': 1.22, '07': 4.6699999999999 999, '04': 1.0, '05': 1.0, '08': 1.0, '09': 1.0, '10': 1.0, '16': 1.0, '11': 1.0, '15': 4.6699999999999 999, '12': 1.0, '14': 1.0}}
                              >>> [/code]

                              Comment

                              • aboxylica
                                New Member
                                • Jul 2007
                                • 111

                                #30
                                Code:
                                f=open("weight_matrix.transfac.txt","r")
                                line=f.next()
                                while not line.startswith('PO'):
                                    line=f.next()
                                
                                headerlist=line.strip().split()[1:]
                                linelist=[]
                                
                                
                                line=f.next().strip()
                                while not line.startswith('/'):
                                    if line != '':
                                        linelist.append(line.strip().split())
                                    line=f.next().strip()
                                    
                                keys=[i[0] for i in linelist]
                                values=[[float(s) for s in item] for item in [j[1:] for j in linelist]]
                                
                                linedict=dict(zip(keys,values))
                                datadict={}
                                for i,item in enumerate(headerlist):
                                    datadict[item]={}
                                    for key in linedict:
                                        datadict[item][key]=linedict[key][i]
                                        for keymain in datadict:
                                            for keysub in datadict[keymain]:
                                                datadict[keymain][keysub]+=1.0
                                                print datadict
                                so here is the code that you suggested for creating dictionaries for a file(matrix)
                                now i have gotta do something like..
                                supposing the sequence entered is something like "ATATTA".. so A is in the first position so A[01]*T[02]*A[03]*T[04]*T[05]*A[06]=??
                                the sequence is going to be entered by the user everytime(so it will keep changing)
                                how do i do this?? what changes should i do??hope I am clear!!
                                THIS is the file containing the matrix
                                NA bap
                                PO A C G T
                                01 0.00 3.67 0.00 0.00
                                02 0.00 0.00 3.67 0.00
                                03 0.00 0.00 0.00 3.67
                                04 0.00 3.67 0.00 0.00
                                05 3.67 0.00 0.00 0.00
                                06 3.46 0.00 0.22 0.00
                                07 0.00 0.00 3.67 0.00
                                08 0.00 0.00 0.00 3.67
                                09 0.00 0.00 0.00 3.67
                                10 0.00 3.67 0.00 0.00
                                11 3.67 0.00 0.00 0.00
                                12 3.67 0.00 0.00 0.00
                                13 0.00 0.00 3.67 0.00
                                14 0.00 0.00 0.00 3.67
                                15 0.00 0.00 3.67 0.00
                                16 0.00 3.67 0.00 0.00
                                //
                                //
                                NA bcd
                                PO A C G T
                                01 42.55 8.75 145.86 8.14
                                02 0.14 0.53 204.64 0.00
                                03 126.83 78.02 0.11 0.34
                                04 0.21 0.17 0.00 204.92
                                05 0.00 12.38 0.43 192.50
                                06 174.48 0.95 1.32 28.56
                                07 79.53 4.70 100.44 20.64
                                //
                                //
                                NA bin
                                PO A C G T
                                01 0.45 8.27 0.00 11.39
                                02 0.00 0.00 10.02 10.09
                                03 5.80 1.39 0.00 12.93
                                04 12.33 5.18 2.60 0.00
                                05 12.43 0.00 0.00 7.68
                                06 18.55 0.00 1.57 0.00
                                07 0.05 0.58 0.00 19.48
                                08 20.11 0.00 0.00 0.00
                                09 20.06 0.05 0.00 0.00
                                10 20.11 0.00 0.00 0.00
                                11 0.00 15.33 0.00 4.78
                                12 20.06 0.05 0.00 0.00
                                13 14.99 0.35 4.78 0.00
                                14 13.64 2.42 3.37 0.68
                                15 5.03 0.00 15.08 0.00
                                16 7.23 0.45 10.94 1.49
                                //
                                //

                                Comment

                                Working...