2012-08-22 4 views
1

XML 형식의 Blastx 출력 파일에서 키워드 (사용자가 지정)를 성공적으로 검색하는 스크립트를 만들었습니다. 이제, 정렬 제목의 키워드를 포함하는 레코드 (쿼리, 히트, 스코어, evalue 등)를 새 파일에 작성해야합니다.blastx 출력 파일에서 특정 항목을 추출하고 새 파일에 쓰기

각 쿼리 제목, 히트 타이틀, e- 값 및 정렬 길이에 대해 별도의 목록을 만들었지 만 새 파일에 쓸 수는 없습니다.

  • 문제 1 : 파이썬 오류 및 목록 중 하나에 값이없는 경우 ...? 그런 다음 다른 모든 목록은 쿼리에 대한 잘못된 정보를 제공합니다 ("선 미끄러짐", 경우 ...).

  • 문제 2 : 파이썬에서 오류가 발생하지 않고 모든 목록의 길이가 같은 경우에도 어떻게 파일에 쓸 수있어 각 목록의 첫 번째 항목이 서로 연결됩니다 (따라서 , 각 목록의 항목 # 10도 연관되어 있습니까?) 대신 사전을 만들어야합니까?

  • 문제 # 3 : 사전은 키에 대해 단 하나의 값을 가지고, 어떤 내 쿼리가 여러 안타가있는 경우? 덮어 쓰거나 건너 뛸 것인지 또는 오류 일지 확실하지 않습니다. 어떤 제안? 나의 현재 스크립트 :

    from Bio.Blast import NCBIWWW 
    from Bio.Blast import NCBIXML 
    import re 
    
    #obtain full path to blast output file (*.xml) 
    outfile = input("Full path to Blast output file (XML format only): ") 
    
    #obtain string to search for 
    search_string = input("String to search for: ") 
    
    #open the output file 
    result_handle = open(outfile) 
    
    #parse the blast record 
    blast_records = NCBIXML.parse(result_handle) 
    
    #initialize lists 
    query_list=[] 
    hit_list=[] 
    expect_list=[] 
    length_list=[] 
    
    #create 'for loop' that loops through each HIGH SCORING PAIR in each ALIGNMENT from each RECORD 
    for record in blast_records: 
         for alignment in record.alignments:  #for description in record.descriptions??? 
           for hsp in alignment.hsps:  #for title in description.title??? 
    
             #search for designated string 
             search = re.search(search_string, alignment.title) 
    
             #if search comes up with nothing, end 
             if search is None: 
               print ("Search string not found.") 
               break 
    
             #if search comes up with something, add it to a list of entries that match search string 
             else: 
    
               #option to include an 'exception' (if it finds keyword then DOES NOT add that entry to list) 
               if search is "trichomonas" or "entamoeba" or "arabidopsis": 
                 print ("found exception.") 
                 break 
               else: 
    
                 query_list.append(record.query) 
                 hit_list.append(alignment.title) 
                 expect_list.append(expect_val) 
                 length_list.append(length) 
    
                 #explicitly convert 'variables' ['int' object or 'float'] to strings 
                 length = str(alignment.length) 
                 expect_val = str(hsp.expect) 
    
                 #print ("\nquery name: " + record.query) 
                 #print ("alignment title: " + alignment.title) 
                 #print ("alignment length: " + length) 
                 #print ("expect value: " + expect_val) 
                 #print ("\n***Alignment***\n") 
                 #print (hsp.query) 
                 #print (hsp.match) 
                 #print (hsp.sbjct + "\n\n") 
    
    
                 if query_len is not hit_len is not expect_len is not length_len: 
                   print ("list lengths don't match!") 
                   break 
                 else: 
    
                   qrylen = len(query_list) 
                   query_len = str(qrylen) 
                   hitlen = len(hit_list) 
                   hit_len = str(hitlen) 
                   expectlen = len(expect_list) 
                   expect_len = str(expectlen) 
                   lengthlen = len(length_list) 
                   length_len = str(lengthlen) 
                   outpath = str(outfile) 
    
                   #create new file 
                   outfile = open("__Blast_Parse_Search.txt", "w") 
                   outfile.write("File contains entries from [" + outpath + "] that contain [" + search_string + "]") 
                   outfile.close 
    
                   #write list to file 
                   i = 0 
                   list_len = int(query_len) 
                   for i in range(0, list_len): 
    
                     #append new file 
                     outfile = open("__Blast_Parse_Search.txt", "a") 
                     outfile.writelines(query_list + hit_list + expect_list + length_list) 
                     i = i + 1 
    
                   #write to disk, close file 
                   outfile.flush() 
                   outfile.close 
    
    print ("query list length " + query_len) 
    print ("hit list length " + hit_len) 
    print ("expect list length " + expect_len) 
    print ("length list length " + length_len + "\n\n") 
    print ("first record: " + query_list[0] + " " + hit_list[0] + " " + expect_list[0] + " " + length_list[0]) 
    print ("last record: " + query_list[-1] + " " + hit_list[-1] + " " + expect_list[-1] + " " + length_list[-1]) 
    print ("\nFinished.\n") 
    

답변

0

내가 제대로 문제를 이해한다면 당신은 같은 라인 슬립 일에 대한 기본 값을 사용할 수 있습니다

try: 
    x(list) 
except exception: 
    append_default_value(list) 

http://docs.python.org/tutorial/errors.html#handling-exceptions

또는 사전에 대한 튜플을 사용 키는 (0,1,1)과 같으며 기본값으로 get 메소드를 사용하십시오.

http://docs.python.org/py3k/library/stdtypes.html#mapping-types-dict

당신이 당신의 출력 파일에 데이터 구조를 유지해야하는 경우가 보류 사용하여 시도 할 수 있습니다 :

또는 각 촬영 한 후, 참조의 몇 가지 유형을 추가하고 각 레코드를 예를 들어 고유 ID를 줄 수 '#32{somekey:value}#21#22#44#'

다시 말해서 튜플을 사용하여 여러 개의 키를 가질 수 있습니다.

그게 도움이되는지 모르겠지만 코드의 어떤 부분에 문제가 있는지 정확히 알 수 있습니다. x()처럼 나는 y을 출력하지만, 나는 z을 기대한다.

관련 문제