반응형
파이썬으로 실무에 적용 가능할 법한 bioinformatics 연습문제를 만들어서 풀어보겠습니다.
input 데이터를 사용해서 output 형태로 만들면 성공입니다.
INPUT 1 : MCP_hight1000_CVC100.txt
MCP_hight1000_CVC100.txt
0.07MB
INPUT 2 : a.txt
1.문제 설명 :
1) INPUT1 파일에서 첫 번째 필드 값(‘_’ 를 기준으로 앞부분만을 취함)이 INPUT2 파일의 두 번째 필드 값에 있는지를 체크한다. 그리고 해당 값의 gene_symbol(7번째) 이 존재하면, 2번째 (rs_num), 7번째(gene_symbol), 8번째 (func) 필드 값을 OUTPUT 파일에 써준다.
OUTPUT
2. 결과 파일:
• 맨 앞 첫 번째 열은 해당 행의 number 이다.
=> 결과는 header 없이 96 라인이 만들어진다.
코드 설명
import os
#data load
f = open("./MCP_hight1000_CVC100.txt")
f = f.readlines()
for i in range(0,len(f)):
f[i] = f[i].replace("\n","")
f[i] = f[i].split('\t')
f2 = open("./a.txt")
f2 = f2.readlines()
for i in range(0,len(f2)):
f2[i] = f2[i].replace("\n","")
f2[i] = f2[i].split('\t')
idx = {}
for i in f2:
field_id, field_rs = line.split('\t')
idx[field_id] = field name
idx = dict(map(str.split,f2))
for file in os.listdir("/mydir"):
if file.endswith(".txt"):
print(os.path.join("/mydir", file))
def all_indices(value, qlist):
indices = []
idx = -1
idx = f[1][0].find("_")
f[1][0][:idx]
#make a list for output
flist = []
for i in range(0,len(f2)):
for j in range(0,len(f)):
idx = f[j][0].find("_")
# check field value with _
if f[j][0][:idx] == f2[i][1] and f2[i][6] != '':
#compared value_ and a.txt, 7th field in a.txt
flist.append(f"{f2[i][1]}\t{f2[i][6]}\t{f2[i][7]}")
output = open("./outputEX3.txt", mode = 'w')
#string to output file on a new line every time
for i in flist:
output.write(i+"\n")
output.close()
#import time
#start = time.time()
#print("time: " , time.time() - start)
반응형
'Python' 카테고리의 다른 글
Python programming for Bioinformatics - 연습문제 5 (0) | 2023.07.24 |
---|---|
Python programming for Bioinformatics - 연습문제 4 (0) | 2023.07.24 |
Python programming for Bioinformatics - 연습문제 2 (0) | 2023.07.24 |
Python programming for Bioinformatics - 연습문제 1 (0) | 2023.07.24 |
Pandas DataFrame 첫번째 행을 헤더로 지정 (0) | 2022.03.11 |