본문 바로가기

파이썬3

파이썬 list안의 list를 풀어주는 기능 (unlist) def unlist(lst): ''' lst : list 오브젝트 ''' result=[] for i in list: if type(i) is list: result+=unlist(i) else: result+=i return result 더보기
[pysam] reference fasta파일로부터 원하는 위치의 DNA서열 불러오기 import pysam ref = pysam.FastaFile('/path/to/reference.fa') seq = ref.fetch('chr7', 10042, 10252) print(seq) 더보기
전체 서열 내에서 특정 부분 서열이 어디 있는지 찾는 스크립트. def find_aligning_region(fullseq,subseq,extending=False): ''' This function locates sub-sequences (subseq) within the given full-sequence (fullseq). Next, it returns list containing start and end location of the sub-sequence. Sub-sequence should be fully included in the full-sequence. # Input fullseq : Full-sequence (string) subseq : Sub-sequence (string). Its length should be shorter than that .. 더보기
무작위 펩타이드 서열 생성 파이썬 기능 (random peptide sequence generator in python) 무작위 펩타이드 서열 생성기 코딩 자체는 간단한데 매번 만들기 귀찮기 때문에 블로그에 업로드를 한다. def random_peptide_generator(n): ''' n : 펩타이드가 몇 개의 아미노산으로 구성되어야하는지를 지정함. 해당 기능은 아미노산에 대해 중복을 허용하면서 서열이 생성되게 만듬. ''' import random amino_acids = [ 'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y' ] peptide=''.join(random.choices(amino_acids,k=n)) return peptide random_peptide_generator(n=10.. 더보기
PRIME 1.0 결과물을 파이썬으로 불러들이는 스크립트 def reading_prime_version1(path): import pandas as pd ''' This function loads prediction result of PRIME(version1.0) path : /path/to/the/prime/1.0/result.txt ''' with open(path,'r') as f: f1=f.readlines() f.close() # Removing unnecessary part f1=[line.replace('\n','') for line in f1 if '#' not in line] # tab-delimination f1=[line.split('\t') for line in f1] # DATAFRAME df=pd.DataFrame(f1[1:],col.. 더보기
리스트 나누기 (chunking, sub-list) 리스트 (list)를 내가 원하는 숫자만큼으로 나눠주는 기능임. 다만, 여기서 n은 n개의 원소로 이루어진 서브리스트를 생성해줌. def chunking_list(l,n): ''' l : list n : how many components should be included in a sub-list? ''' result=[] for idx in range(0,len(l),n): result.append(l[idx:idx+n]) return result 위의 기능은 빠지는 것 없이 모두 채워지도록 만들었음. 예를들어 101개의 원소로 이루어진 list이고 n=5일 때는 마지막 1개가 빠져야 맞지만 위의 기능에서는 맨 마지막 sub-list에 6개가 들어가게 만듬. 아래의 기능이 좀 더 나은 것 같다. num.. 더보기
netmhcpan 4.1 실행 및 결과 불러들이기 def reading_netmhcpan_result(path): ''' this function reads netmhcpan-result.tsv file and then makes it into dataframe. path : /path/to/netmhcpan-result-file.txt ''' with open(path,'r') as f: f1=f.readlines() f.close() # Remove qutes content=[i for i in f1 if '#' not in i and len(i)>3] # Get header header=[i for i in content if 'Pos' in i and 'MHC' in i][0] content=[i for i in content if header .. 더보기
언더바 (_)를 포함하는 위치를 슬라이싱하는 파이썬 스크립트 def generating_sliced_sequences_underbar(seq, n_mer=[8, 12]): ''' underbar (_)가 있는 지점을 포함하도록 slicing하는 기능 반드시 포함하고 싶은 위치는 _XXX_와 같이 2개의 언더바가 필요함. ''' result = [] # Locate underbar u_idx=[i for i in range(len(seq)) if seq[i]=='_'] total_n=u_idx[1]-u_idx[0] substrings=seq[u_idx[0]:(u_idx[1]+1)] # generate peptides seq2=seq.replace('_','') peptides=[] for n in range(n_mer[0],n_mer[1]+1): start=max(.. 더보기