본문 바로가기
Python

Python programming for Bioinformatics - 연습문제 5

by 코딩하는 미토콘드리아 bioinformatics 2023. 7. 24.
반응형

파이썬으로 실무에 적용 가능할 법한 bioinformatics 연습문제를 만들어서 풀어보겠습니다.

input 데이터를 사용해서 output 형태로 만들면 성공입니다.

 

 

INPUT : corNerve.txt

corNerve.txt
0.03MB

1.파일 설명 :
1)이 파일은 값을 중심으로 N*N matrix 이다.
2)첫 번째 행 : gene name list (순서를 나타냄.)
3)두 번째 행부터의 첫 번째 열:  gene name
4)두 번째 행부터의 두 번째 열부터 끝 열까지 : 해당 gene과 다른 gene과의 correlation value 값이다.
 

OUTPUT

 

1.문제 설명 :

1) Correlation 값이 0.9 이상이면 gene name을 파일에 써준다. 

2.  결과 파일:

1번째 필드 : 해당 행의 gene name , 2번째 필드 : 조건을 만족하는 gene 개수, 3-끝번째 필드 : 해당 genecorrelation value 0.9 이상인 gene name.
각 필드는 tab으로 구분한다.
조건을 만족하는 gene 개수가 1개 이상일 경우만 출력한다.
=> 결과는 header 없이 40 라인이 나옵니다.

 

코드 설명

f = open("./corNerve.txt")
f = f.readlines()

glist = f[0].replace("\n","").replace("\"","")
glist = glist.split(" ")

out_dict = {}

for i in f[1:]:
    i=i.replace("\"","").split(" ")
    for j in range(1,len(i)):
        if float(i[j]) >= 0.9 and i[0] != glist[j-1]:
	# higher than 0.9 and remove glist itself, glist-corr 
            
           if out_dict.get(i[0]):
                    out_dict[i[0]].append(glist[j-1])
           else:
                    out_dict[i[0]] = [glist[j-1]]


# out_put = open("./outputEX5.txt", mode = 'w')
for i,j in out_dict.items():
    # more than 1 gene
    if len(j) >= 1: 
        print(f'{i}\t {len(j)} \t {" ".join(j)}') 

print(j)

 

반응형