如何在excel w/biopython中将一列3个字母的氨基酸转换为1个字母的氨基酸?

2024-09-30 14:18:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将excel中一列由三个字母组成的氨基酸转换为一个字母,并将一个字母的氨基酸打印到excel文件中相应的每一行。我知道我可以用biopython来做这个

我所尝试的:

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

但我希望大家理解,我不能为python转换字符串。我需要在excel中阅读一整列,然后按转换后的1个字母顺序打印一个新列。供参考的图片:

example


Tags: 文件字符串fromimport顺序字母图片excel
1条回答
网友
1楼 · 发布于 2024-09-30 14:18:27

对于旧的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

相关问题 更多 >