how can i shorten this program??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CCGG26
    New Member
    • Sep 2010
    • 13

    how can i shorten this program??

    Code:
    map = {}
    dna = [ "A", "C", "G", "T"]
    
    with open("matrix.txt", "r") as myfile:
        line = myfile.readline()
        mylist = line.split()
        map{"AA"} = mylist[0]
        map{"AC"} = mylist[1]
        map{"AG"} = mylist[2]
        map{"AT"} = mylist[3]
        line = myfile.readline()
        mylist = line.split()
        map{"CA"} = mylist[0]
        map{"CC"} = mylist[1]
        map{"CG"} = mylist[2]
        map{"CT"} = mylist[3]
        line = myfile.readline()
        mylist = line.split()
        map{"GA"} = mylist[0]
        map{"GC"} = mylist[1]
        map{"GG"} = mylist[2]
        map{"GT"} = mylist[3]
        line = myfile.readline()
        mylist = line.split()
        map{"TA"} = mylist[0]
        map{"TC"} = mylist[1]
        map{"TG"} = mylist[2]
        map{"TT"} = mylist[3]
    gap = -2
    map{"A-"} = gap
    map{"C-"} = gap
    map{"T-"} = gap
    map{"G-"} = gap
    map{"-A"} = gap
    map{"-C"} = gap
    map{"-T"} = gap
    map{"-G"} = gap
    with open("dna.fasta.txt","r") as myfile:
        data = myfile.readlines()
        myfile.close()
    
     
    for i in range(len(data)):
        data[i] = data[i].rstrip("\n")
             
    seq1 = data[1]
    seq2 = data[3]
    score = 0
    
          
    for i in range(0,len(seq1),1):
             
            key = seq1[i] + seq2[i]
            score = score + int(map[key])
          
    print(score)
    Last edited by bvdet; Nov 10 '10, 02:41 AM. Reason: Add code tags [code]....code goes here....[/code]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Here's a way to shorten some of it.
    Code:
    myfile = '''0 1 2 3
    4 5 6 7
    8 9 10 11
    12 13 14 15'''
    
    myfileList = myfile.split("\n")
    dna = [ "A", "C", "G", "T"]
    
    dnaDict = {}
    for j, a in enumerate(dna):
        itemList = myfileList[j].split()
        for i, b in enumerate(dna):
            dnaDict["%s%s" % (a, b)] = itemList[i]
    The example approximates reading the data from a file.

    Comment

    Working...