본문 바로가기

Bioinformatics(생정보학)

MixMHCpred 실행 및 결과 불러오기

728x90
반응형
def running_mixMHCpred(x,path,mixmhcpred):
    '''
    # parameters
    x : dataframe ['peptide','mhc','other columns'] # input dataframe
    path : /path/to/workdir # 작업경로
    mixmhcpred : /path/to/mixmhcpred/MixMHCpred # 실행 파일경로
    # return
    dataframe with mixmhcpred result
    # saved file
    /path/to/workdir/mixMHCpred_peptide_input.txt
    /path/to/mixMHCpred-result.txt
    '''
    import subprocess as sbp
    import pandas as pd
    # Exporting the peptide file
    peptides='\n'.join(x['peptide'].unique())
    with open(path+'/mixMHCpred_peptide_input.txt','w') as f:
        f.write(peptides)
    # Get MHCs
    mhcs=x['mhc'].unique()
    mhcs=[i.split('-')[1].replace('*','').replace(':','') for i in mhcs]
    mhcs=','.join(mhcs)
    # Make command line and run it.
    cmd=f'{mixmhcpred} -i {path}/mixMHCpred_peptide_input.txt -o {path}/mixMHCpred-result.txt -a {mhcs}'
    sbp.call(cmd,shell=True)
    # Loading the result
    mm=pd.read_csv(f'{path}/mixMHCpred-result.txt',sep='\t',comment='#')
    # Attach the prediction result to original data
    mm=mm.drop(['Score_bestAllele','BestAllele','%Rank_bestAllele'],axis=1)
    x1=x.copy()
    x1['mixMHCkey']=x1['mhc'].apply(lambda x: x.split('-')[1].replace('*','').replace(':',''))
    mm1=mm.melt('Peptide')
    mm1[['category','mixMHCkey']]=mm1['variable'].str.split('_').tolist()
    mm2=mm1.pivot(index=['Peptide','mixMHCkey'],columns='category').reset_index().loc[:,['Peptide','mixMHCkey','value']]
    mm2.columns=['peptide','mixMHCkey','%Rank','Score']
    x2=pd.merge(x1,mm2,on=['peptide','mixMHCkey']).drop('mixMHCkey',axis=1)
    gc.collect()
    return x2
728x90
반응형