2017-10-13 14 views
0

누군가이 특정 줄 구문을 설명 할 수 있습니까? (길지는 않습니다. 줄이 3 줄입니다.) 사전에 대한 정의를 만들고 파일에서 포함합니다. fastang 시퀀스 nameHandle를 사용하여 :
줄을 내가 이해하지 못하는 것은 #유전학 및 패스트라 시퀀스 구문에 대한 Python 사전

def getfasta(file):      #creating the definition 

    nameHandle=open('fastas.txt,'r')  #(this is for opening the file that we're gonna use) 
    fastas={}        #I know it means my dictionnary name 
    for line in nameHandle:    #I know what it means 
     if line [0]=='>':    #(it's beacause each first line in a fasta seq starts with >) 
      header=line[1:]    #(Starting this line I can't understand a thing) 
      fastas[header]='' 
     else: 
      fastas[header]+=line[:-1] 
    nameHandle.close()     #closing the package 
    return(fastas)      #gives us the dictionary with the keys and all of content 
+0

코드에서 들여 쓰기가 조금 엉망입니다 당신은 더 쉽게 읽을 수 있도록 그것을 수정해야합니다. – DarksteelPenguin

+0

이 모든 작업이 fasta 시퀀스의 이름을 추출하는 것처럼 보입니다. 그러나 나는 그것이 정확하다고 완전히 확신하지 못합니다. 'line [: - 1]'은 마지막 문자가없는 파일의 줄입니다. 어딘가에 'split'이 있어야 할 것처럼 느껴집니다. –

+1

코드를 조금 더 분명하게 만들려고 시도했습니다. –

답변

0
def getfasta(file): 
    nameHandle = open('fastas.txt,'r') 
    fastas={} 
    for line in nameHandle: 
     if line [0]=='>': 
      header=line[1:] # this takes the whole line except the first character and stores it into a string 
      fastas[header]='' # this creates a new entry in your dictionary, with key=header and value='' 
     else: 
      fastas[header]+=line[:-1] # this line (except the last character, '\n') is added to the value associated to the previous header 
    nameHandle.close() 
    return(fastas) 

없이 하나 그래서 무슨 일이 있습니다 : 파일이 라인으로 라인을 읽습니다. 첫 번째 줄은 ">"로 시작한다고 가정합니다. 나머지 줄 ("첫 번째 항목"이라고 부름)이 사전의 키로 사용됩니다. 다음 줄은 연관된 값에 추가됩니다. ">"로 시작하는 다른 행에 도달하면 새 키로 사용되며 다음 행은 새로운 값으로 추가됩니다. 다음의 예와

:

>entryOne 
AT 
CG 
>entryTwo 
CA 
GT 

결과 사전은 다음과 같습니다

{(key="entryOne", value="ATCG"), (key="entryTwo", value="CAGT")} 
+0

Waw에게 감사드립니다. Darksteel과 여러분 모두, 예를 들어 특히 더 좋습니다! –