본문 바로가기
Python

Pandas를 활용한 VCF 파일 분석 방법 (심화)

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

Pandas를 활용한 VCF 파일 분석 방법

 

 

1. 데이터 타입 변환 및 필터링

데이터 분석을 위해서 필요한 열의 데이터 타입을 변환합니다.

예를 들어, POS는 정수형, QUAL은 실수형으로 변환할 수 있습니다.

# 데이터 타입 변환
df['POS'] = df['POS'].astype(int)
df['QUAL'] = df['QUAL'].astype(float)
df['AF'] = df['AF'].apply(lambda x: float(x.split(',')[0]))

# 고품질 변이만 필터링
high_quality_variants = df[df['QUAL'] >= 30]
print(high_quality_variants)

 

2. 고품질 변이 분석

고품질 변이의 빈도, 위치, 유전자 등을 분석합니다.

# 각 변이 위치별 빈도수 계산
position_counts = high_quality_variants['POS'].value_counts()
print(position_counts)

# ALT 빈도 계산
alt_freq = high_quality_variants['ALT'].value_counts()
print(alt_freq)

 

3. 시각화

Matplotlib이나 Seaborn을 사용하여 변이 데이터를 시각화합니다.

import matplotlib.pyplot as plt
import seaborn as sns

# 변이 품질 분포 시각화
plt.figure(figsize=(10, 6))
sns.histplot(high_quality_variants['QUAL'], bins=30, kde=True)
plt.title('Distribution of Variant Quality Scores')
plt.xlabel('Quality Score')
plt.ylabel('Frequency')
plt.show()

# 변이 위치 분포 시각화
plt.figure(figsize=(10, 6))
sns.scatterplot(x='POS', y='QUAL', data=high_quality_variants)
plt.title('Variant Positions and Quality')
plt.xlabel('Position')
plt.ylabel('Quality Score')
plt.show()

 

4. 한 걸음 더: 특정 유전자 내 변이 분석

특정 유전자에 대한 변이를 분석하고 시각화합니다.

# 예를 들어, 특정 유전자 영역의 변이를 필터링
gene_start = 10000
gene_end = 50000
gene_variants = high_quality_variants[(high_quality_variants['POS'] >= gene_start) & (high_quality_variants['POS'] <= gene_end)]

# 유전자 영역 내 변이 시각화
plt.figure(figsize=(10, 6))
sns.scatterplot(x='POS', y='QUAL', data=gene_variants)
plt.title('Variants in Specific Gene Region')
plt.xlabel('Position')
plt.ylabel('Quality Score')
plt.show()

 

https://pypi.org/project/vcf2pandas/

 

vcf2pandas

Package to convert a vcf into a pandas dataframe.

pypi.org

 

반응형