【问题标题】:How do I convert a column of 3-letter amino acids to 1- letter amino acids in excel w/ biopython?如何在带有 biopython 的 excel 中将一列 3 个字母的氨基酸转换为 1 个字母的氨基酸?
【发布时间】:2021-05-01 23:07:22
【问题描述】:

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

我的尝试:

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

但我希望大家理解,我不能放置一个字符串供 python 转换。我需要在 excel 中读取一整列并使用转换后的 1 字母序列打印一个新列。供参考的图片:

【问题讨论】:

  • 到目前为止,您尝试了哪些方法来打开数据?您的示例不会尝试读取任何输入
  • 我不知道如何打开和读取数据
  • 逐行读取xcel文件,将数据转换成字符串,然后在字符串上使用biopython。否则,您需要创建自己的字典,将 3 字母代码与一个字母代码匹配并使用相同的方法

标签: python biopython


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 2016-07-07
    • 1970-01-01
    • 2014-03-28
    相关资源
    最近更新 更多