본문 바로가기

Python53

python VCF file 정보 추출 (chromosome) 하고 plot 만들기 python VCF file 정보 추출 (chromosome) 하고 plot 만들기 import matplotlib.pyplot as plt def extract_chromosome(vcf_file): chromosomes = [] with open(vcf_file, 'r') as f: for line in f: # Skip header lines if line.startswith('#'): continue # Split the line into fields fields = line.strip().split('\t') # Extract chromosome information chromosome = fields[0] # Append the chromosome information to the list c.. 2024. 4. 16.
python VCF file 데이터 불러오기 VCF file 데이터 불러오기 1 vcf_list = [] colID_toParse = -1 header = True info_column_found = False #INFO column 확인 with open('sample_vcf.vcf', 'r') as fOpen: for i in fOpen: i = i.rstrip('\r\n') iSplit = i.split('\t') if header: header = False try: loc = iSplit.index('INFO') colID_toParse = int(loc) info_column_found = True except ValueError: print('Error, INFO column not present in header! Please che.. 2024. 4. 16.
기업 코딩 테스트 후기 (Bioinformatics 포지션) 문제 1 목표 : 주어진 문자열 리스트 strs 에서 가장 짧은 문자열을 선택하고, 이를 기준으로 공통된 접두사를 찾는다. ##1 def solution(strs): if not strs: return "" # 가장 짧은 문자열을 찾기. 공통 접두사의 길이는 이 가장 짧은 문자열의 길이 shortest_str = min(strs, key=len) # 가장 짧은 문자열의 각 문자를 반복하면서 다른 문자열들과 비교 for i, char in enumerate(shortest_str): for other in strs: # 만약 일치하지 않는 문자가 나타나면, 현재까지 일치했던 부분만 반환 if other[i] != char: return shortest_str[:i] # 모든 문자열이 가장 짧은 문자열을 접.. 2024. 4. 8.
VCF 파일 pandas dataframe 으로 전환하기 VCF -> Dataframe 1. VCF 파일 불러오기 2. metadata 제거 3. 나머지 데이터 pandas 전환 import io import os import pandas as pd def read_vcf(path): with open(path, 'r') as f: lines = [l for l in f if not l.startswith('##')] return pd.read_csv( io.StringIO(''.join(lines)), dtype={'#CHROM': str, 'POS': int, 'ID': str, 'REF': str, 'ALT': str, 'QUAL': str, 'FILTER': str, 'INFO': str}, sep='\t' ).rename(columns={'#CHROM.. 2024. 3. 15.
반응형