본문 바로가기
Python

Python programming for Bioinformatics - 연습문제 4

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

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

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

 

INPUT : SSGI_GO_TC_process.data

SSGI_GO_TC_process.data
0.23MB

1.파일 설명 :
1)1번째 필드 : GO, 2번째 필드 : TC list, 3번째 필드 : TC 개수
2. 문제 설명 :

1) GO TC 리스트를 TC에 대한 GO 리스트로 바꿔서 결과 파일을 만든다.

 

OUTPUT

2. 결과 파일:

  1번째 필드 : TC, 2번째 필드 : GO list, 3번째 필드 : GO 개수
  각 필드는 tab으로 구분한다.
  2번째 GO 리스트는 중간에 콤마로 연결시켜준다.

    (INPUT 파일과는 조금 다름. 처음에는 콤마 안 들어감.)

=> 결과는 header 없이 5886 라인이 나옵니다.

 

코드 설명

#data load
f = open("SSGI_GO_TC_process.data")
f = f.readlines()

#for loop for i
#remove new line

for i in f:
	print(i)


for i in range(0,len(f)):
    f[i] = f[i].replace("\n","")
#remove comma after tab
    f[i] = f[i].replace("\t,","\t")
#split by tab
    f[i] = f[i].split('\t')

#make a dict for GO key to TC value
adict = {}

for i in range(0,len(f)):
    adict[f[i][0]]= f[i][1].split(",")

#print(adict)

#make another dict TC key to GO value
bdict = {}

#TC key for loop, TC key match and GO value append
for i,j in adict.items():
	for k in range(0,len(j)):
		if bdict.get(j[k]):		
			bdict[j[k]].append(i)
	else:
		bdict[j[k]] = [i]

#print(bdict)

output=open("./outputEX4.txt",mode='w')
for i,j in bdict.items():
    output.write(f"{i}\t{','.join(j)}\t{len(j)}\n")
output.close()

with open(r"./SSGI_GO_TC_process.data", 'r') as fp:
	for count, line in enumerate(fp):
		pass
print('Total lines', count +1)

 

반응형