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

    #31
    HERE IS MY code!!plz help how to proceed??

    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]=??
    how do i do this?? what changes should i do??
    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

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #32
      <Mod NOTE: merged duplicate threads - again>
      Originally posted by aboxylica
      HERE IS MY code!!plz help how to proceed??
      Thanks for using code tags.
      First thing to do is try it. It looks like it should work.

      Right after that, READ the rest of the POSTING GUIDELINES. You have committed several infractions and must consider yourself warned.

      Comment

      • aboxylica
        New Member
        • Jul 2007
        • 111

        #33
        I am sorry about that. I will follow the rules hence forth.but i dont know how to proceed with my problem further.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #34
          Originally posted by aboxylica
          I am sorry about that. I will follow the rules hence forth.but i dont know how to proceed with my problem further.
          Ok. All is forgiven. Here's the thing. I can't give you working code.
          I can give you examples of what I see going on here so that you can try things on your own. It looks to me like you could multiply the specified elements (after your matrix has been created) like this:[CODE=python]
          >>> seq = "ATATTA"
          >>> res = 1
          >>> for i, key in enumerate(seq):
          ... res *= dd[key]["%02d" %(i + 1)]
          >>> print res[/CODE]

          Comment

          • aboxylica
            New Member
            • Jul 2007
            • 111

            #35
            Thanks a lot for all the help! I still have a small doubt.what does this line do?
            Code:
            while not  line.startswith('PO'):
                
               
                line=f.next()
                print line
            I mean this is supposed to refer to the line which doesnt start with "PO' right? when i say print line. It is printing
            PO A C G T
            but this is not what it means right?

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #36
              Originally posted by aboxylica
              Thanks a lot for all the help! I still have a small doubt.what does this line do?
              Code:
              while not  line.startswith('PO'):
                  
                 
                  line=f.next()
                  print line
              I mean this is supposed to refer to the line which doesnt start with "PO' right? when i say print line. It is printing
              PO A C G T
              but this is not what it means right?
              f.next() merely advances one line in the file. The object is to advance to the first line that starts with "PO".

              I asked you a question in an earlier post about your data. It looks like there are three data sets in your data file. The example code I posted only parses the first data set.

              Comment

              • aboxylica
                New Member
                • Jul 2007
                • 111

                #37
                okay,then why do we say,
                while not?? it should be while..right??
                and yes there are actually a lot of data sets in my file around 15 to twenty first am trying to make it work for one.then i will do it for the rest(sorry for not answering I am really going crazy with the program)i owe u an apology
                And i have one more doubt:
                [CODE]
                datadict={}
                for i,item in enumerate(heade rlist):
                datadict[item]={}
                for key in linedict:
                print key
                # when i say a print key here, it is printing the keys twice and in an unorderly #fashion.why is this happening?
                datadict[item][key]=linedict[key][i]
                for keymain in datadict:
                for keysub in datadict[keymain]:
                datadict[keymain][keysub]+=1.0
                print datadict
                looking fwd for ur reply!
                cheers!

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #38
                  Originally posted by aboxylica
                  okay,then why do we say,
                  while not?? it should be while..right??
                  and yes there are actually a lot of data sets in my file around 15 to twenty first am trying to make it work for one.then i will do it for the rest(sorry for not answering I am really going crazy with the program)i owe u an apology
                  And i have one more doubt:
                  Code:
                  datadict={}
                  for i,item in enumerate(headerlist):
                      datadict[item]={}
                      for key in linedict:
                          print key
                  # when i say a print key here, it is printing the keys twice and in an unorderly #fashion.why is this happening?
                          datadict[item][key]=linedict[key][i]
                          for keymain in datadict:
                             for keysub in datadict[keymain]:
                                   datadict[keymain][keysub]+=1.0
                                  print datadict
                  looking fwd for ur reply!
                  cheers!
                  Code:
                  I should be 'while not'. We want to skip the lines until the line starts with 'PO'. You can use this same method to advance into later data sets.
                  
                  No problem about not answering. 
                  
                  Dictionaries are unordered collections of data. You can print in an orderly fashion like this:[code=Python]keys = lineDict.keys()
                  keys.sort()
                  for key in keys:
                      print '%s = %s' % (key, lineDict[key])

                  Comment

                  • aboxylica
                    New Member
                    • Jul 2007
                    • 111

                    #39
                    my file:
                    my file:
                    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
                    //
                    //



                    This is my code:
                    Code:
                    f=open("deeps1.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]]
                    array={}
                    linedict=dict(zip(keys,values))
                    keys = linedict.keys()
                    keys.sort()
                    for key in keys:
                        array=[key,linedict[key]]
                    
                    datadict={}
                    datadict1={}
                    for i,item in enumerate(headerlist):
                        datadict[item]={}
                        print datadict[item]#this one returns empty dictionary
                        for key_ in linedict:
                            print item# all items are getting printed
                            datadict[item][key_]=linedict[key_][i]
                            # but for the print statement below its saying key error:'C'
                           print datadict['C']['01']
                    I have written the problems in comment form in the code!

                    can you execute my code and see why this is happening? I have been stuck with this for a day now.
                    waiting for your reply
                    cheers!

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #40
                      Originally posted by aboxylica
                      my file:
                      my file:
                      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
                      //
                      //



                      This is my code:
                      Code:
                      f=open("deeps1.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]]
                      array={}
                      linedict=dict(zip(keys,values))
                      keys = linedict.keys()
                      keys.sort()
                      for key in keys:
                          array=[key,linedict[key]]
                      
                      datadict={}
                      datadict1={}
                      for i,item in enumerate(headerlist):
                          datadict[item]={}
                          print datadict[item]#this one returns empty dictionary
                          for key_ in linedict:
                              print item# all items are getting printed
                              datadict[item][key_]=linedict[key_][i]
                              # but for the print statement below its saying key error:'C'
                             print datadict['C']['01']
                      I have written the problems in comment form in the code!

                      can you execute my code and see why this is happening? I have been stuck with this for a day now.
                      waiting for your reply
                      cheers!
                      For that to work, you'll have to print every new addition to the datadict.
                      Code:
                              # print every item going into the datadict because 'C' has not been created yet
                             print linedict[key_][i]

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #41
                        Originally posted by aboxylica
                        my file:
                        my file:
                        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
                        //
                        //



                        This is my code:
                        Code:
                        f=open("deeps1.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]]
                        array={}
                        linedict=dict(zip(keys,values))
                        keys = linedict.keys()
                        keys.sort()
                        for key in keys:
                            array=[key,linedict[key]]
                        
                        datadict={}
                        datadict1={}
                        for i,item in enumerate(headerlist):
                            datadict[item]={}
                            print datadict[item]#this one returns empty dictionary
                            for key_ in linedict:
                                print item# all items are getting printed
                                datadict[item][key_]=linedict[key_][i]
                                # but for the print statement below its saying key error:'C'
                               print datadict['C']['01']
                        I have written the problems in comment form in the code!

                        can you execute my code and see why this is happening? I have been stuck with this for a day now.
                        waiting for your reply
                        cheers!
                        I made a few minor changes to your code:[code=Python]f=open("deeps1. txt","r")
                        line=f.next()
                        while not line.startswith ('PO'):
                        line=f.next()

                        headerlist=line .strip().split( )[1:]
                        linelist=[]

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

                        keys=[i[0] for i in linelist]
                        values=[[float(s) for s in item] for item in [j[1:] for j in linelist]]
                        array={}
                        linedict=dict(z ip(keys,values) )
                        keys = linedict.keys()
                        keys.sort()
                        # initialize list object 'array'
                        array = []
                        for key in keys:
                        # append each item to list object
                        array.append([key,linedict[key]])

                        # initialize dictionary
                        datadict={}

                        for i,item in enumerate(heade rlist):
                        datadict[item]={}
                        # print 'item' here, datadict[item] is empty at this point
                        print item
                        for key_ in linedict:
                        datadict[item][key_]=linedict[key_][i]

                        # Change indentation level
                        print datadict['C']['01'][/code]

                        Comment

                        • aboxylica
                          New Member
                          • Jul 2007
                          • 111

                          #42
                          sorry,
                          that doesnt seem to do anything.
                          [CODE=python]
                          f=open("deeps1. txt","r")
                          line=f.next()
                          while not line.startswith ('PO'):
                          line=f.next()

                          headerlist=line .strip().split( )[1:]
                          linelist=[]


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

                          keys=[i[0] for i in linelist]
                          values=[[float(s) for s in item] for item in [j[1:] for j in linelist]]
                          array={}
                          linedict=dict(z ip(keys,values) )
                          keys = linedict.keys()
                          keys.sort()
                          for key in keys:
                          array=[key,linedict[key]]

                          datadict={}
                          datadict1={}
                          for i,item in enumerate(heade rlist):
                          datadict[item]={}
                          print datadict[item]
                          for key_ in linedict:

                          datadict[item][key_]=linedict[key_][i]
                          print linedict[key][i]#this is what i added and it still gives the same answer
                          print datadict['C']['01'][/CODE]

                          :( or should i add it somewhere else?
                          waiting for ur reply
                          cheers!
                          Last edited by bartonc; Jul 9 '07, 07:07 PM. Reason: Added closing [/CODE] tags.

                          Comment

                          • bvdet
                            Recognized Expert Specialist
                            • Oct 2006
                            • 2851

                            #43
                            CODE TAGS! This code:[code=Python]for i,item in enumerate(heade rlist):
                            datadict[item]={}
                            print item
                            for key_ in linedict:
                            datadict[item][key_]=linedict[key_][i]
                            print linedict[key_][i]

                            print datadict['C']['01'][/code]give me this output:[code=Python]>>> A
                            0.0
                            0.0
                            0.0
                            0.0
                            3.46
                            0.0
                            0.0
                            3.67
                            0.0
                            0.0
                            0.0
                            3.67
                            0.0
                            3.67
                            0.0
                            0.0
                            C
                            0.0
                            0.0
                            0.0
                            3.67
                            0.0
                            0.0
                            3.67
                            0.0
                            0.0
                            0.0
                            3.67
                            0.0
                            0.0
                            0.0
                            0.0
                            3.67
                            G
                            3.67
                            0.0
                            3.67
                            0.0
                            0.22
                            3.67
                            0.0
                            0.0
                            0.0
                            0.0
                            0.0
                            0.0
                            0.0
                            0.0
                            3.67
                            0.0
                            T
                            0.0
                            3.67
                            0.0
                            0.0
                            0.0
                            0.0
                            0.0
                            0.0
                            3.67
                            3.67
                            0.0
                            0.0
                            3.67
                            0.0
                            0.0
                            0.0
                            3.67
                            >>> [/code]

                            Comment

                            • aboxylica
                              New Member
                              • Jul 2007
                              • 111

                              #44
                              thank you sir!that works for me too..but what exactly i want when i type
                              datadict['C']["01'] is 3.67(according to my file that is printed below).But it is printing the entire thing
                              this is what my file says:
                              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
                              //
                              //
                              so how do I change the code accordingly?
                              Code:
                              f=open("deeps1.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]]
                              array={}
                              linedict=dict(zip(keys,values))
                              keys = linedict.keys()
                              keys.sort()
                              for key in keys:
                                  array=[key,linedict[key]]
                              
                              datadict={}
                              datadict1={}
                              for i,item in enumerate(headerlist):
                                  datadict[item]={}
                                  print item
                                  for key_ in linedict:
                                      datadict[item][key_]=linedict[key_][i]
                                      print linedict[key_][i] 
                              print datadict['C']['01']
                              waiting,
                              cheers!

                              Comment

                              • bvdet
                                Recognized Expert Specialist
                                • Oct 2006
                                • 2851

                                #45
                                Originally posted by aboxylica
                                thank you sir!that works for me too..but what exactly i want when i type
                                datadict['C']["01'] is 3.67(according to my file that is printed below).But it is printing the entire thing
                                this is what my file says:
                                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
                                //
                                //
                                so how do I change the code accordingly?
                                Code:
                                f=open("deeps1.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]]
                                array={}
                                linedict=dict(zip(keys,values))
                                keys = linedict.keys()
                                keys.sort()
                                for key in keys:
                                    array=[key,linedict[key]]
                                
                                datadict={}
                                datadict1={}
                                for i,item in enumerate(headerlist):
                                    datadict[item]={}
                                    print item
                                    for key_ in linedict:
                                        datadict[item][key_]=linedict[key_][i]
                                        print linedict[key_][i] 
                                print datadict['C']['01']
                                waiting,
                                cheers!
                                I tested your code exactly as you posted it:[code=Python]>>> datadict['C']['01']
                                3.6699999999999 999
                                >>> [/code]There are other print statements in the code.

                                Comment

                                Working...