Python_(3/4)T6600の生データを解析してPTM結果をレポート | バイオとDX

バイオとDX

バイオ医薬品とDX、データサイエンス

# =============================================================================
# Modificationのフィルタリング
# =============================================================================
import os
import datetime
import pandas as pd
import re

from openpyxl import load_workbook
from openpyxl.styles import Font
from openpyxl.styles.alignment import Alignment

# 元になるエクセルファイルを読み込み
#xls1 = input('Excel file to be analysed ,as ###.xlsx:')
#xls2 = str(xls1) + '.xlsx'
xls1 = 'bpv'
xls2 = str(xls1) + '.xlsx'

# 解析内容(1脱アミ、2酸化、3ほか)を入力する
analysis1 = ('deami_oxid', 'deamidation', 'oxidation', 'others')
an = input('filtered for "0:deamidated and oxidation, 1:deamidatied; 2:oxidation; 3:others":')
analysis = analysis1[int(an)]
  
# エクセルの所定のシートを指定して、データフレームに読み込む
df = pd.read_excel(xls2, sheet_name='Matched')

# =============================================================================
# 条件に合致するものだけを選び出す
# =============================================================================

# ■ 基準1/9 ■ Auto-Validatedが、「TRUE」のもの
df1 = df[df['Auto-Validated'] == True]

# ■ 基準2/9 ■ Useが、「Certain」のもの
df2 = df1[df1['Use'] == 'Use']

# ■ 基準3/9 ■ Errorが、10ppm以下のもの
df4 = df2.loc[df2['Error (ppm)'] < 10]

# ■ 基準4/9 ■ Modificationsが空欄のものは除外 ←DeamidatedとOxidation検出するので不要になった
#df4 = df3[pd.notnull(df3['Modifications']) == True]
 
# ■ 基準5/9 ■ 
#Modificationsで、表記が1つだけのものを抽出
#ただし、('Carboxymethyl'はカウントに入れない)
#いずれかの意、「or」としたいなら、「Ca|a2|m5|g0|g1|g2」 を使うこと

#Targetを規定
De = 'Deamidated'
Ox = 'Oxidation'
Ca = 'Carboxymethyl'
a2 = 'A2G1'
m5 = 'M5'
g0 = 'G0'
g1 = 'G1'
g2 = 'G2'

#方法a)項目にて、DeamidatedとOxidationの合計が1つだけのもののみ抽出
#なお、糖鎖のものを検出するなら改変必要
df5 = df4[df4['Modifications'].str.count(De) + df4['Modifications'].str.count(Ox)  == 1]

#方法b)項目数総数からCarboxymethylを除いた総数が1つのみ抽出
#df5 = df4[df4['Modifications'].str.count(',') + 1 - df4['Modifications'].str.count('Carboxymethyl') == 1]

# =============================================================================
# Modifictionを1つにする
# =============================================================================
# Deamiか、あるいは、Oxidationを含むものだけにして、あとはすべて除去する

df2 = df5['Modifications']

li_modi2 = []
li_modi5 = []

for i, modi in enumerate(df2):
    li_modi2 = re.split(',', modi)
    for j, modi3 in enumerate(li_modi2):
        print('modi3: ' + modi3)
        if (De in modi3) or (Ox in modi3):
            #print(modi3 +'は、DeamidatedあるいはOxidationのいずれかを一つ含む')            
            modi3 = modi3.strip()  #空白文字を除く            
            li_modi5.append(modi3)  #合致したものをリストに追加                 
        else:
            None
            #print(modi3 +'は、DeamidatedあるいはOxidationのいずれかを一つ含まない')

#print('-----')
#print('li_modi5')
#print(li_modi5)
#print('-----')
#print('df5')
#print(df5)

df5['Modifications1'] = li_modi5

# =============================================================================
# 日付を得て、ディレクトリにフォルダを作成して、結果を保存
# =============================================================================

# 日付取得/ファイル名のため
d_today = str(datetime.date.today())

# ディレクトリにフォルダ作成
dirname = d_today+'_'+xls1+'_'+analysis+'/'
if not os.path.exists(dirname):
    os.mkdir(dirname)   
    
# エクセルとして保存
filename1 = dirname + xls1 +'_' +analysis +'.xlsx'
df5.to_excel(filename1)

# =============================================================================
# エクセルの書式設定
# =============================================================================
wb = load_workbook(filename1)
ws = wb.active

# set font  #【エクセル】フォント設定
font = Font(name='Calibri', size=9)
for row in ws:
    for cell in row:
        ws[cell.coordinate].font = font

# alignment  #【エクセル】タイトル行だけ左寄せ
for row in ws['A1:AZ1']:
    for cell in row:
        cell.alignment = Alignment(horizontal='left')

filename1 = dirname + xls1 +'_' +analysis +'.xlsx'
wb.save(filename1)    #【エクセル】書式整えたものを保存


# コードの終わり