问题
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 for
s 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