2017-05-12 1 views
0

두 개의 파일에 대해 간격 범위 연산을 수행하려고합니다. chrom이 동일한 지 확인한 다음 co0rdinatefile의 시작과 끝이 같거나 작은 지 확인합니다. gene_annotation 파일의 시작과 끝 (스트랜드가 "+"인 경우 시작과 끝은 예를 들어 10-20, "20-10"이 될 것입니다.), 일치하는 경우 시작 좌표와 끝 좌표 gene_id, geneannotation 파일의 gene_name입니다. 조정 된 파일의 행 ~ 50000 수 ~ 200,000팬더 또는 열거 형을 사용하여 두 데이터 집합 사이의 겹침/범위를 찾습니다

gene_annotationfile

chrom  start  end    gene_id gene_name strand 
17 71223692 71274336 ENSMUSG00000085299  Gm16627  - 
17 18186448 18211184 ENSMUSG00000067978 Vmn2r-ps113  + 
11 84645863 84684319 ENSMUSG00000020530  Ggnbp2  - 
7 51097639 51106551 ENSMUSG00000074155   Klk5  + 
13 31711037 31712238 ENSMUSG00000087276  Gm11378  + 

chrom start end strand 
    1 4247322 4247912 - 
    1 4427449 4432604 + 
    1 4763414 4764404 - 
    1 4764597 4767606 - 
    1 4764597 4766491 - 
    1 4766882 4767606 - 
    1 4767729 4772649 - 
    1 4767729 4768829 - 
    1 4767729 4775654 - 
    1 4772382 4772649 - 
    1 4772814 4774032 - 
    1 4772814 4774159 - 
    1 4772814 4775654 - 
    1 4772814 4774032 + 
    1 4774186 4775654 - 
    1 4774186 4775654 
    1 4774186 4775699 - 
coordinates_file 주석 파일의 행

수 (표현의 목적을 위해 나는 머리 annoataion 파일이)

원하는 출력

내가 지금까지

chrom, start, end,strand, gene_id, gene_name 
1  4427432 4432686 + ENSMUSG0001,ENSMUSG0002 abcd,efgh 

내 코드를 작성하려는 경우에 gene_id하기 위해 매핑 할 수 일치가있는 경우

chrom, start, end,strand, gene_id, gene_name 
1  4427432 4432686 + ENSMUSG0001 abcd 

또 다른 문제는 어떤 경우에 있습니다

import csv 

with open('coordinates.txt', 'r') as source: 
     coordinates = list(csv.reader(source, delimiter="\t")) 

with open('/gene_annotations.txt', 'rU') as source: 
     #if i do not use 'rU' i get this error Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode? 
     annotations = list(csv.reader(source, delimiter="\t")) 

for index,line in enumerate(coordinates): 

    for index2, line2 in enumerate(annotations): 


     if coordinates[line][0] == annotations[line2][0] and coordinates[line][1] <= annotations[line2][1] and annotations[line2][2] >= coordinates[line][2] : 
     print "%s\t%s\t%s\t%s\t%s" % (coordinates[line][0],coordinates[line][1],coordinates[line][2], annotations[line2][3], annotations[line2][4]) 
     break 

오류가 나는

---> 15   if coordinates[line][0] == annotations[line2][0] and coordinates[line][1] <= annotations[line2][1] and annotations[line2][2] >= coordinates[line][2] : 
16    print "%s\t%s\t%s\t%s\t%s" % (coordinates[line][0],coordinates[line][1],coordinates[line][2], annotations[line2][3], annotations[line2][4]) 
17    break 

TypeError: list indices must be integers, not list 

팬더가 이것에 대한 좋은 접근 방법이 될까요?

답변

1

좌표는 [[1,2], [3,4]]와 같은 목록 목록이라고 가정합니다. 라인 줄로 좌표의 각 행 인덱스와 인덱스를 반환 좌표 위에

for index,line in enumerate(coordinates): 

반복.

if coordinates[line][0] == annotations[line2][0] and coordinates[line][1] <= annotations[line2][1] and annotations[line2][2] >= coordinates[line][2] : 

오류 메시지는 여기에서 색인에 대한 목록 (줄)을 사용하고 있음을 의미합니다. 당신은 가능성이 라인 대신 인덱스를 사용하고 싶었 :

if coordinates[index][0] == annotations[index2][0] and coordinates[index][1] <= annotations[index2][1] and annotations[index2][2] >= coordinates[index][2] : 

더 좋은 단지 줄을 사용하는 것입니다 :

if line[0] == line2[0] and line[1] <= line2[1] and line2[2] >= line[2] : 

https://docs.python.org/2.7/reference/compound_stmts.html?highlight=for_stmt#grammar-token-for_stmt

참조
관련 문제