简体   繁体   中英

Why am I getting results with the same script?

I have the following script (see below), however I am getting two different results with from two txt files. The first txt file contains five sequences and the second txt file contains a single sequence that is present in the first file.

Sequences in the first file:

KLSEQESLL

RVRFFFPSL

QVHPQKVT

SLDDYNHLV

HLFGYSWYK

Sequences in the first file:

KLSEQESLL

Note: The sequence KLSEQESLL is common in both files as mentioned above

Code:

with open('file.txt', 'r') as f:

    for my_kmer in f:

        KARP850103 = {'A': 0.892, 'L': 0.921, 'R': 0.901, 'K': 1.057, 'N': 0.930,
                      'M': 0.804, 'D': 0.932, 'F': 0.914, 'C': 0.925, 'P': 0.932,
                      'Q': 0.885, 'S': 0.923, 'E': 0.933, 'T': 0.934, 'G': 0.923,
                      'W': 0.803, 'H': 0.894, 'Y': 0.837, 'I': 0.872, 'V': 0.913}
        BUNA790102 = {'A': 4.349, 'L': 4.385, 'R': 4.396, 'K': 4.358, 'N': 4.755,
                      'M': 4.513, 'D': 4.765, 'F': 4.663, 'C': 4.686, 'P': 4.471,
                      'Q': 4.373, 'S': 4.498, 'E': 4.295, 'T': 4.346, 'G': 3.972,
                      'W': 4.702, 'H': 4.630, 'Y': 4.604, 'I': 4.224, 'V': 4.184}
        VINM940101 = {'A': 0.984, 'L': 0.935, 'R': 1.008, 'K': 1.102, 'N': 1.048,
                      'M': 0.952, 'D': 1.068, 'F': 0.915, 'C': 0.906, 'P': 1.049,
                      'Q': 1.037, 'S': 1.046, 'E': 1.094, 'T': 0.997, 'G': 1.031,
                      'W': 0.904, 'H': 0.950, 'Y': 0.929, 'I': 0.927, 'V': 0.931}
        KARP850102 = {'A': 0.946, 'L': 0.961, 'R': 1.028, 'K': 1.082, 'N': 1.006,
                      'M': 0.862, 'D': 1.089, 'F': 0.912, 'C': 0.878, 'P': 1.085,
                      'Q': 1.025, 'S': 1.048, 'E': 1.036, 'T': 1.051, 'G': 1.042,
                      'W': 0.917, 'H': 0.952, 'Y': 0.930, 'I': 0.892, 'V': 0.927}
        KARP850101 = {'A': 1.041, 'L': 0.967, 'R': 1.038, 'K': 1.093, 'N': 1.117,
                      'M': 0.947, 'D': 1.033, 'F': 0.930, 'C': 0.960, 'P': 1.055,
                      'Q': 1.165, 'S': 1.169, 'E': 1.094, 'T': 1.073, 'G': 1.142,
                      'W': 0.925, 'H': 0.982, 'Y': 0.961, 'I': 1.002, 'V': 0.982}
        ANDN920101 = {'A': 4.35, 'L': 4.17, 'R': 4.38, 'K': 4.36, 'N': 4.75,
                      'M': 4.52, 'D': 4.76, 'F': 4.66, 'C': 4.65, 'P': 4.44,
                      'Q': 4.37, 'S': 4.50, 'E': 4.29, 'T': 4.35, 'G': 3.97,
                      'W': 4.70, 'H': 4.63, 'Y': 4.60, 'I': 3.95, 'V': 3.95}
        FASG760104 = {'A': 9.69, 'L': 9.60, 'R': 8.99, 'K': 9.18, 'N': 8.80,
                      'M': 9.21, 'D': 9.60, 'F': 9.18, 'C': 8.35, 'P': 10.64,
                      'Q': 9.13, 'S': 9.21, 'E': 9.67, 'T': 9.10, 'G': 9.78,
                      'W': 9.44, 'H': 9.17, 'Y': 9.11, 'I': 9.68, 'V': 9.62}

        aaindex_values = []
        aaindex_listT = [VINM940103, VINM940104, VINM940102, KARP850103, BUNA790102, VINM940101, 
                        KARP850102, KARP850101, ANDN920101, FASG760104]
        for i in aaindex_listT:
            a_a = (my_kmer.count("A") * i["A"])
            c_c = (my_kmer.count("C") * i["C"])
            d_d = (my_kmer.count("D") * i["D"])
            e_e = (my_kmer.count("E") * i["E"])
            f_f = (my_kmer.count("F") * i["F"])
            g_g = (my_kmer.count("G") * i["G"])
            h_h = (my_kmer.count("H") * i["H"])
            i_i = (my_kmer.count("I") * i["I"])
            k_k = (my_kmer.count("K") * i["K"])
            l_l = (my_kmer.count("L") * i["L"])
            m_m = (my_kmer.count("M") * i["M"])
            n_n = (my_kmer.count("N") * i["N"])
            p_p = (my_kmer.count("P") * i["P"])
            q_q = (my_kmer.count("Q") * i["Q"])
            r_r = (my_kmer.count("R") * i["R"])
            s_s = (my_kmer.count("S") * i["S"])
            t_t = (my_kmer.count("T") * i["T"])
            v_v = (my_kmer.count("V") * i["V"])
            w_w = (my_kmer.count("W") * i["W"])
            y_y = (my_kmer.count("Y") * i["Y"])

            aaindex_comp = round(((a_a + c_c + d_d + e_e + f_f + g_g + h_h + i_i + k_k + l_l + m_m + n_n 
                                   + p_p + q_q + r_r + s_s + t_t + v_v + w_w + y_y)/len(my_kmer)), 3)

            aaindex_values.append(aaindex_comp)

        print(aaindex_values)

Result for the KLSEQESLL sequence are different in both cases

Doubt: Why are the results different?

You're not taking into account the newline characters ( \n and possibly \r ) which occur at the end of lines. If you iterate over the lines of a file, you'll get those characters. For example, suppose I have the following file (file.txt):

foo
bar
blah

If I run

with open('file.txt','r') as f:
    lines=[line for line in f]

the elements of lines will be 'foo\n' , 'bar\n' , and 'blah' . Notice that the last one won't have a newline character because it is the last line.

When you read in KLSEQESLL from the first file, it isn't the last line and so it'll have a '\n' at the end. That matters when you compute len(my_kmer) .

In the second file, the KLSEQESLL is the last line and thus there isn't a newline character at the end making len(my_kmer) different.

What you should do is this:

with open('file.txt', 'r') as f:
    for my_kmer in f:
        my_kmer = my_kmer.rstrip()
        ...

That will strip off, among other things, any newline characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM