Going to a certain position in a string [closed]

懵懂的女人 提交于 2019-12-13 07:15:59

问题


I want to get to a certain point on a string that is opposite (from the negative side) to that of what I am given.

AAAAAAAAAACCCCCCCCCCTTTTTTTTTTGGGGGGGGGG TTTTTTTTTTGGGGGGGGGGAAAAAAAAAACCCCCCCCCC

So you need to convert coordinates. On the bottom strand, base 0 (the right-most C) is opposed to base 39 on the top strand. Base 1 is against base 38. Base 2 is against case 37. (Important point: notice what happens when you add these two numbers up — every time.) So base 10 is against base 29, and base 19 is against base 20.

So: if I want to find base 10-20 on the bottom strand, I can look at base 20-29 on the top (and then reverse-complement it).

I have written the following:

fp = open(infile, 'r')
for line in fp:
   tokens = line.split()
   exonstarts = tokens[8][:-1].split(',')
   exonends = tokens[9][:-1].split(',')
   zipped = list(zip(exonstarts, exonends))
   chrom_len = len(chr_string)
   s = ''.join(bc[base.upper()] for base in chr_string[-starts-1:-ends-1] for starts, ends in zipped)+'\n'

Yet, every time I do this I get:

Error: global name 'starts' is not defined

How do I fix this??


回答1:


Try adding parentheses around the last term:

s = ''.join(bc[base.upper()] for base in (chr_string[-starts-1:-ends-1]\
                                         ^
            for starts, ends in zipped)) +'\n'
                                      ^

Your defining two different generators here. This is equivalent to:

strands = (chr_string[-starts-1:-ends-1] for starts, ends in zipped)
complementary_strands = (bc[base.upper()] for base in stage_1)
joined_exons = ''.join(stage_2) + '\n'



回答2:


It looks like you are trying to do too much in your generator expression.

The two fors are the wrong way around. You mean:

s = ''.join(bc[base.upper()] for starts,ends in zipped for base in chr_string[-starts-1:-ends-1])+'\n'

Then starts and ends are defined for the second for.

Given the questions you've asked today, I recommend reading a good book, such as Dive Into Python 3 so that you can solve these issues yourself.




回答3:


You're defining exonstarts and then referring to starts, which is not defined.



来源:https://stackoverflow.com/questions/10359993/going-to-a-certain-position-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!