본문 바로가기

Python

원하는 위치를 포함하는 K-mer 펩타이드 서열 생성하기 def generate_peptides(seq, pos, length): """ Generate peptide sequences from a given peptide sequence. Parameters: - seq: The input peptide sequence (string). - pos: The starting position for generating peptides (integer). - length: The length of the generated peptides (integer). Returns: - A list of generated peptide sequences. """ # Generate peptide sequences start=max([0,pos-length+1]) end=po.. 더보기
[python] tqdm 사용법 1. 설명 tqdm은 파이썬에서 진행상황을 알려주는 프로그레스바 (progress bar)를 생성할 때 쓰이는 라이브러리이다. 2. 사용법 tqdm은 리스트와 유사한 인스턴스를 감싼 후 for문과 함께 쓰인다. from tqdm import tqdm for i in tqdm(range(10)): i for i in [1,2,3]: i for i in set(['a','b','c']): i 3. 커스터마이징 tqdm의 장점으로는 각 과정 (tick) 때마다 프로그레스바에 사용자가 원하는 값을 출력할 수 있다는 것이다. 이것은 딥러닝을 할 때 loss값을 표기할 때 유용하다. from tqdm import tqdm import time progress_bar=tqdm(range(100)) for i in p.. 더보기
[python] sequence logo 그리는 법 import logomaker # 예제용 무작위 서열 정보 생성 import random def generate_random_9mer_sequence(n,length=9): ''' this function generate random 9mer peptide sequence ''' aas=list('ACDEFGHIKLMNPQRSTVWY') # Generate n random 9-mer peptide sequences peptides = set() # Use a set to store unique peptides while len(peptides) < n: peptide = ''.join(random.choice(aas) for _ in range(length)) peptides.add(peptide) re.. 더보기
binomial distribution 신뢰구간 구하는 법 1. 개요 binomial distribution에서 신뢰구간을 구하는 방법은 2가지 종류가 있다. Normal approximation method 이 방식은 샘플의 수가 충분히 크다면 binomial distribution은 normal distribution을 따를거라고 가정한다. Wilson Score Interval method 이 방식은 샘플의 수가 작을 때 정확하다고 알려져 있다. 2. Normal approximation method 샘플의 확률/비율 (p)을 결정한다. p = x/n x = 성공한 수 n = 전체 시행 횟수 표준오차 (standard error)를 계산한다. SE = root(p*(1-p)/n) 신뢰 수준을 결정한다. 95%신뢰구간에 상응하는 Z값은 1.96 신뢰구간은 아.. 더보기
symbol to phred score 변환 파이썬 기능 https://support.illumina.com/help/BaseSpace_OLH_009008/Content/Source/Informatics/BS/QualityScoreEncoding_swBS.htm 시퀀싱 데이터의 fastq나 bam파일에는 각 염기서열 별로 얼만큼 확신하는지를 나타내는 phred score값이 있다. 이것의 문제는 값이 2자리 수를 넘어가게 되면 1-letter인 염기서열과 매칭이 되지 않으므로 각 숫자별로 상징 (symbol)을 매칭한다. 이를 통해 염기서열 길이와 phred score값의 길이는 일치하게 된다. 문제는 상징에서 점수로 변환하는 것은 외우고 다니기 어렵다. 이를 해결하고자 일루미나 홈페이지에서 있는 표를 참고하여 파이썬 기능을 만들었다. score_mapping.. 더보기
2개의 회귀선의 기울기를 통계적으로 비교하는 법 H0 : beta1과 beta2는 같다. H1 : beta1과 beta2는 다르다. def compare_slope_test(x1,x2,y): from statsmodels.formula.api import ols import numpy as np from scipy.stats import t ''' x1,x2,y : np.array or list conatining numeric values. They should have same size. ''' df=pd.DataFrame({'x1':x1,'x2':x2,'y':y}) x1_model=ols('y~x1',data=df).fit() x2_model=ols('y~x2',data=df).fit() slope_difference=x1_model.params.. 더보기
f-string 사용법 내가 만든 프로그램이 작동하는지 확인하기 위해 종종 중간에 메세지를 내보내면 할 때가 있다. 또는 메세지 중간에 많은 변수들이 들어가야할 때가 있다. 이럴 때 쉽게 사용할 수 있는 것이 파이썬의 f-string이다. var1='Kim' print(f'{var1} : hi') # Kim : hi names=['kim','oh','lee'] for name in names: print(f'{name} : hi') # kim : hi # oh : hi # lee : hi 위와 같이 f를 쓴 후에 넣고 싶은 문장을 뒤에 붙이면 된다. 이 때 {} 안에 원하는 변수를 넣으면 된다. 소수점을 포맷팅할 수도 있다. # 예제 1 pi=3.1415926 # 소수점 미만 2자리까지만 출력 print(f'{pi:.2f}').. 더보기
유전자 정보 불러오기 (mygene) In [1]: import mygene In [2]: mg = mygene.MyGeneInfo() In [3]: mg.getgene(1017) Out[3]: {'_id': '1017', 'entrezgene': 1017, 'name': 'cyclin-dependent kinase 2', 'symbol': 'CDK2', 'taxid': 9606, ... } # use "fields" parameter to return a subset of fields In [4]: mg.getgene(1017, fields='name,symbol,refseq') Out[4]: {'_id': '1017', 'name': 'cyclin-dependent kinase 2', 'refseq': {'genomic': ['AC_00.. 더보기