如何在excel w/biopython中将一列3个字母的氨基酸转换为1个字母的氨基酸?
我想将excel中一列由三个字母组成的氨基酸转换为一个字母,并将一个字母的氨基酸打印到excel文件中相应的每一行。我知道我可以使用如何在excel w/biopython中将一列3个字母的氨基酸转换为1个字母的氨基酸?,python,biopython,Python,Biopython,我想将excel中一列由三个字母组成的氨基酸转换为一个字母,并将一个字母的氨基酸打印到excel文件中相应的每一行。我知道我可以使用biopython来实现这一点 我所尝试的: import Bio from Bio.SeqUtils import seq1 seq1("MetAlaIleValMetGlyArgTrpLysGlyAlaArgTer") 'MAIVMGRWKGAR*' 但我希望大家理解,我不能为python转换字符串。我需要在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
到目前为止,您试图打开哪些数据?您的示例不尝试读取任何输入。我不知道如何打开和读取数据。请逐行读取xcel文件,将数据转换为字符串,然后在字符串上使用biopython。否则,您需要创建自己的字典,将3个字母代码与一个字母代码相匹配,并从此处使用相同的方法: