2012-04-26 85 views
-2

現在,我需要找到一種方法,其中Python可以找到上述代碼的密碼子位置編號5並提取該序列直到位置12(ATGG * CTTTACCTCGTC * TCACAGGAG)。所以輸出應該是這樣的:DNA提取蟒

>CCODE1112_5..11 
CTTTACCTCGTC 

我怎麼能告訴Python來獲得的第一個「_」和最終值後後開始值「..」,因此它可以自動做呢? ?謝謝!!!

+1

http://docs.python.org/library/stdtypes.html#str.find – tMC 2012-04-26 17:45:30

+0

也HTTP://docs.python .org/tutorial/inputoutput.html – 2012-04-26 17:51:42

+2

您應該明確說明您正在嘗試做什麼;沒有冒犯性,但描述有些模棱兩可,例如「密碼子位置編號5」,「提取那個」等。 – ninjagecko 2012-04-26 17:52:42

回答

0
def extractseq(queryseq , begin=5, end =12): 
    queryseq=queryseq.split('\n')#transform the string in a list of lines included in the string 

    return queryseq[1][begin-1:end-1] 

我覺得這個功能應該工作,謹防其在0開始在Python

後,寫在你的腳本,你只需要調用函數潛艇= extractseq(序列索引,5,12 )

OK對不起所以如果你想提取5並列入做伊斯利的一個方式12:

substring=queryseq.split('\n')[0].split('_')[1].split('...')#extraction of the substring 
begin=substring[0] 
end = substring[1] 
+0

中找到詳細的文檔,那麼您可以指定begin = 5,end = 12,但我怎麼能告訴Python獲得第一個「_」後的開始值和「..」後的結束值,以便它可以自動執行?不管怎麼說,多謝拉! :) – John 2012-04-26 17:55:39

0

我可能會(嘆氣)使用正則表達式從CCODE1112_5..12_ABC中提取5和12。

然後將提取的字符串轉換爲int的。

然後使用int作爲DNA數據上字符串切片的索引。

對於正則表達式:

正則表達式= re.compile(R '^ [^ ] *(\ d +)..(\ d +)_ * $'。 ) regex.match( 'CCODE1112_5..12_ABC') 匹配= regex.match( 'CCODE1112_5..12_ABC') match.group(1) '5' match.group(2) '12'

要轉換那些爲INT的,使用int(match.group(1)),例如。

然後你的索引是基於1的,而python是基於0的。另外,python的切片起點是你想要的值,而python的切片終點是你想要的值。因此,從組(1)中減去一個並單獨離開組(2)。

因此,像: 串= dna_data [left_point-1:right_point]