簡體   English   中英

如何在帶有 biopython 的 excel 中將一列 3 字母氨基酸轉換為 1 字母氨基酸?

[英]How do I convert a column of 3-letter amino acids to 1- letter amino acids in excel w/ biopython?

我想將 excel 中的一列三個字母的氨基酸轉換為一個字母,並將一個字母的氨基酸打印到 excel 文件中的每個相應行。 我知道我可以為此使用biopython

我試過的:

import Bio
from Bio.SeqUtils import seq1
seq1("MetAlaIleValMetGlyArgTrpLysGlyAlaArgTer")
'MAIVMGRWKGAR*'

但我希望人們理解,我不能為 python 放置一個字符串來進行轉換。 我需要閱讀 excel 中的一整列,並使用轉換后的 1 字母序列打印一個新列。 供參考的圖片:

例子

對於舊的 xls 文件,我在下面管理代碼,與您的示例一起使用新列保存文件的副本。 我了解 pandas 庫可以更好地管理 xlx 和更新版本。 讓我知道它對你有用

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Created on Sat May  1 15:24:42 2021

@author: Pietro


https://stackoverflow.com/questions/67271713/how-do-i-convert-a-column-of-3-letter-amino-acids-to-1-letter-amino-acids-in-ex#comment119033760_67271713

"""

#import Bio #not needed

from Bio.SeqUtils import seq1


# Reading an excel file using Python

import xlrd   #read xls library

from xlutils.copy import copy  #copies xls library

## from xlwt import Workbook # writes xls library ##not necessary
 


loc = ("inputz.xls") # input xlx same directory of python script
 


wb = xlrd.open_workbook(loc) #open xls file

wb_copy = copy(wb) #copy of your xls file

sheet = wb.sheet_by_index(0) #select sheet of input file

sheet_copy = wb_copy.get_sheet(0)  #select sheet of copy file



lenght_rows = sheet.nrows #number of rows in input file 

print(sheet.cell_value(0, 0)) # prints sheet 0 cell 0,0 of input file 

print(lenght_rows) #prints number of rows of input file

# loops over row (0,1 to 0, number of rows) of input file
# 
# and use biopython to convert 3 letter into 1 letter 
# writes 3 and 1 letters to copy xls file

for i in range(1,lenght_rows):   
    print(sheet.cell_value(i, 0), '   :  ' , seq1(sheet.cell_value(i, 0)))
    sheet_copy.write(i,0,sheet.cell_value(i, 0))
    sheet_copy.write(i,1,seq1(sheet.cell_value(i, 0)))
    
wb_copy.save('output.xls') #saves xls output file

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM