你怎么做" - "在python?

时间:2014-09-16 01:56:41

标签: python

所以这个python代码是给我的。暗示它有效,但显然我遇到了问题。我只是想知道是否有人能告诉我什么是错的?

当我尝试运行

时,错误仍在继续
 intron1_length = ((my_dna.find(‘gtcata’)) –
 (my_dna.find(‘atcgat’)))

错误是" - "。只是不确定应该去哪里。谢谢!

 # -*- coding: utf-8 -*-

 # We’ll start with “data-aware” way to do this first.
 # Since we have the sequence, we can see that exon 1 ends with
 # ‘GACTA’. We can use that information, and that method, for
 # finding the positions of the elements of this gDNA. This is NOT
 # an elegant solution, and would not work if we didn’t know the
 # DNA sequence ahead of time.

 my_dna = "ATCGATCGATGGTCGAATGACTAgtcatagctatgcatgtagctactc
 gatcgtattttattcgatcgatcgatCGATCGATCATGCTATCATCGATCGATATCGATGCATC
 GACTACTATgtcatggctatgcatcgatcgtattttattcgatcgttcgatGATCGATCGATCGACTGACTTTGAA"   
 # here we introduce another useful operator for strings: len
 gene_length = len(my_dna)
 # we’ll use the starting position of useful substrings in the
 # sequence to find the positions of the exon-intron boundaries.
 # We’ll then use those to find the length of each segment.
 exon1_length = (my_dna.find('Agtcata'))
 intron1_length =((my_dna.find('gtcata')) - (my_dna.find('atcgat')))
 exon2_length = ((my_dna.find('CGATCG'))- (my_dna.find('Tgtcatg')))
 intron2_length = ((my_dna.find('gtcatg'))- (my_dna.find('tGATCGA')))
 exon3_length = (gene_length(my_dna.find('GATCGA'))
 print ("Gene length:" + str(gene_length))
 print ("Exon1 length:" + str(exon1_length))
 print ("Intron1 length:" + str(intron1_length))
 print ("Exon2 length:" + str(exon2_length))
 print ("Intron2 length:" + str(intron2_length))
 print ("Exon2 length:" + str(exon3_length)) 

3 个答案:

答案 0 :(得分:5)

因为!= -

您使用什么来编辑代码?一个wordprocessor ??

这些引号看起来也很可疑。使用不会在代码中添加这些内容的编辑器。

答案 1 :(得分:1)

以下代码运行时没有错误:

my_dna = """ATCGATCGATGGTCGAATGACTAgtcatagctatgcatgtagctactc
gatcgtattttattcgatcgatcgatCGATCGATCATGCTATCATCGATCGATATCGATGCATC
GACTACTATgtcatggctatgcatcgatcgtattttattcgatcgttcgatGATCGATCGATCGACTGACTTTGAA"""
# here we introduce another useful operator for strings: len
gene_length = len(my_dna)
# we’ll use the starting position of useful substrings in the
# sequence to find the positions of the exon-intron boundaries.
# We’ll then use those to find the length of each segment.
exon1_length = my_dna.find('Agtcata')
intron1_length = my_dna.find('gtcata') - my_dna.find('atcgat')
exon2_length = my_dna.find('CGATCG') - my_dna.find('Tgtcatg')
intron2_length = my_dna.find('gtcatg') - my_dna.find('tGATCGA')
exon3_length = gene_length - my_dna.find('GATCGA')
print ("Gene length:" + str(gene_length))
print ("Exon1 length:" + str(exon1_length))
print ("Intron1 length:" + str(intron1_length))
print ("Exon2 length:" + str(exon2_length))
print ("Intron2 length:" + str(intron2_length))
print ("Exon2 length:" + str(exon3_length))

假设您使用的是Python3,我将括号保留在print语句中。您可能需要更改exon3_length行,因为我不清楚您的目的是什么。

要使代码运行,只需要更改两个命令:

  • my_dna的定义是一个多行字符串,因此它需要三引号。另一方面,如果您打算将它作为单行字符串,则将其全部放在一行上。

  • exon3_length行有两个问题:不平衡的parens和尝试调用整数。

修复这些问题后,代码就会运行。

代码中使用的引号和减号,与摘录相反,都很好。不需要更改任何代码来运行代码。

答案 2 :(得分:0)

这是代码出错的部分:

exon3_length = (gene_length(my_dna.find('GATCGA'))

通过在dna字符串上调用gene_length

len被定义为更早的整数。此外,括号不正确,缺少一个关闭外部括号(这是多余的)。