2012-10-12 50 views
1

我想寫一個Python腳本,它需要一個特殊類型的文件作爲輸入。
此文件包含有關多個基因的信息,並且關於一個基因的信息被寫在多行上,其中行數對於每個基因都不相同。一個例子是:從一個文件到Python中的多個詞典

gene   join(373616..374161,1..174) 
       /locus_tag="AM1_A0001" 
       /db_xref="GeneID:5685236" 
CDS    join(373616..374161,1..174) 
       /locus_tag="AM1_A0001" 
       /codon_start=1 
       /transl_table=11 
       /product="glutathione S-transferase, putative" 
       /protein_id="YP_001520660.1" 
       /db_xref="GI:158339653" 
       /db_xref="GeneID:5685236" 
       /translation="MKIVSFKICPFVQRVTALLEAKGIDYDIEYIDLSHKPQWFLDLS 
       PNAQVPILITDDDDVLFESDAIVEFLDEVVGTPLSSDNAVKKAQDRAWSYLATKHYLV 
       QCSAQRSPDAKTLEERSKKLSKAFGKIKVQLGESRYINGDDLSMVDIAWLPLLHRAAI 
       IEQYSGYDFLEEFPKVKQWQQHLLSTGIAEKSVPEDFEERFTAFYLAESTCLGQLAKS 
       KNGEACCGTAECTVDDLGCCA" 
gene   241..381 
       /locus_tag="AM1_A0002" 
       /db_xref="GeneID:5685411" 
CDS    241..381 
       /locus_tag="AM1_A0002" 
       /codon_start=1 
       /transl_table=11 
       /product="hypothetical protein" 
       /protein_id="YP_001520661.1" 
       /db_xref="GI:158339654" 
       /db_xref="GeneID:5685411" 
       /translation="MLINPEDKQVEIYRPGQDVELLQSPSTISGADVLPEFSLNLEWI 
       WR" 
gene   388..525 
       /locus_tag="AM1_A0003" 
       /db_xref="GeneID:5685412" 
CDS    388..525 
       /locus_tag="AM1_A0003" 
       /codon_start=1 
       /transl_table=11 
       /product="hypothetical protein" 
       /protein_id="YP_001520662.1" 
       /db_xref="GI:158339655" 
       /db_xref="GeneID:5685412" 
       /translation="MKEAGFSENSRSREGQPKLAKDAAIAKPYLVAMTAELQIMATET 
       L" 

我想要什麼,現在,是創建字典,其中每個字典包含一個基因的信息,像這樣的列表:我絕對

gene_1 = {"locus": /locus_tag, "product": /product, ...} 
gene_2 = {"locus": /locus_tag, "product": /product, ...} 

沒有想法當一個基因/字典完成後,我應該如何讓Python知道,下一個應該開始。
有人可以幫我嗎?有沒有辦法做到這一點?

澄清:我知道如何提取我想要的信息,將它保存在變量中並將其存入字典中。我只是不知道如何告訴Python爲每個基因創建一個字典。

+0

你把*任何*代碼在一起作爲一個企圖解決這個問題?如果是這樣,請張貼它。 –

+0

冒號可以作爲值的一部分出現嗎?如果沒有,你可以簡單地通過檢查該行是否包含冒號來找到新實體的開始... – l4mpi

+0

@PaulC我試着簡單地循環遍歷文件,但那不會。我一直在尋找新的想法幾天,但我似乎是一個初學者太多,想出點什麼。提取數據和填充字典不是問題,只是簡單的循環不會。 –

回答

0

如果有人有興趣的初學者的解決方案,我發現在我收到的意見的幫助下,這裏是:

import sys, re 

annot = file("example.embl", "r") 
embl = "" 
annotation = [] 

for line in annot: 
    embl += line 

embl_list = embl.split("FT gen") 

for item in embl_list: 
    if "e   " in item: 
     split_item = item.split("\n") 
     for l in split_item: 
      if "e   " in l: 
       if not "complement" in l: 
        coordinates = l[13:len(l)] 
        C = coordinates.split("..") 
        genestart = C[0] 
        geneend = C[1] 
        strand = "+" 
       if "complement" in l: 
        coordinates = l[24:len(l)-1] 
        C = coordinates.split("..") 
        genestart = C[0] 
        geneend = C[1] 
        strand = "-" 

      if "/locus_tag" in l: 
       L = l.split('"') 
       locus = L[1] 

      if "/product" in l: 
       P = l.split('"') 
       product = P[1] 

     annotation.append({ 
      "locus": locus, 
      "genestart": genestart, 
      "geneend": geneend, 
      "product": product, 
     }) 
    else: 
     print "Finished!" 
1

我已經把一個也許不太好,但是功能的解析器爲此,純Python,也許它可以用來至少作爲一個基本理念:

import re 
import pprint 
printer = pprint.PrettyPrinter(indent=4) 

with open("entities.txt", "r") as file_obj: 
    entities = list() 

    for line in file_obj.readlines(): 
     line = line.replace('\n', '') 

     if re.match(r'\s*(gene|CDS)\s+[\w(\.,)]+', line): 
      parts = line.split() 
      entity = {parts[0]: parts[1]} 
      entities.append(entity) 
     else: 
      try: 
       (attr_name,) = re.findall(r'/\w+=', line) 
       attr_name = attr_name.strip('/=') 
      except ValueError: 
       addition = line.strip() 
       entity[last_key] = ''.join([entity[last_key], addition]) 
      else: 
       try: 
        (attr_value,) = re.findall(r'="\w+$', line) 
        last_key = attr_name 
       except ValueError: 
        try: 
         (attr_value,) = re.findall(r'="[\w\s\.:,-]+"', line) 
        except ValueError: 
         (attr_value,) = re.findall(r'=\d+$', line) 

        attr_value = attr_value.strip('"=') 

       if attr_name in entity: 
        entity[attr_name] = [entity[attr_name], attr_value] 
       else: 
        entity[attr_name] = attr_value 

printer.pprint(entities) 
+0

謝謝,andrean! 現在我已經有了一個可行的解決方案,這要歸功於Paul C的提示,但是您的外觀更加乾淨。 –

相關問題