Python遗传学和fastas序列语法词典

2024-04-19 18:53:57 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能给我解释一些具体的行语法吗(不长,只有3行)对于这个,它是用来为字典创建一个定义,并使用namehold从包含fastas序列的文件中写入的:
我听不懂的台词是没有#

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

Tags: theinfor字典withline语法it
1条回答
网友
1楼 · 发布于 2024-04-19 18:53:57
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")}

相关问题 更多 >