【问题标题】:Python store output as a variable after running an executable command运行可执行命令后,Python 将输出存储为变量
【发布时间】:2021-06-16 02:25:49
【问题描述】:

“TMalign...”是我用来获取数据的可执行文件。如何将输出存储到变量中,以便从输出中提取目标值。可执行文件是从一个长的 .cpp 编译而来的,所以我认为我不能从那里调用变量名。

import sys,os
os.system("./TMalign 3w4u.pdb 6bb5.pdb -u 139") #some command I have

输出是这样的,我需要提取TM-score值

*********************************************************************
 * TM-align (Version 20190822): protein structure alignment          *
 * References: Y Zhang, J Skolnick. Nucl Acids Res 33, 2302-9 (2005) *
 * Please email comments and suggestions to yangzhanglab@umich.edu   *
 *********************************************************************

Name of Chain_1: 3w4u.pdb (to be superimposed onto Chain_2)
Name of Chain_2: 6bb5.pdb
Length of Chain_1: 141 residues
Length of Chain_2: 139 residues

Aligned length= 139, RMSD=   1.07, Seq_ID=n_identical/n_aligned= 0.590
TM-score= 0.94726 (if normalized by length of Chain_1, i.e., LN=141, d0=4.42)
TM-score= 0.96044 (if normalized by length of Chain_2, i.e., LN=139, d0=4.38)
TM-score= 0.96044 (if normalized by user-specified LN=139.00 and d0=4.38)
(You should use TM-score normalized by length of the reference structure)

(":" denotes residue pairs of d <  5.0 Angstrom, "." denotes other aligned residues)
SLTKTERTIIVSMWAKISTQADTIGTETLERLFLSHPQTKTYFPHFDLHPGSAQLRAHGSKVVAAVGDAVKSIDDIGGALSKLSELHAYILRVDPVNFKLLSHCLLVTLAARFPADFTAEAHAAWDKFLSVVSSVLTEKYR
 :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::. .
-LSPADKTNVKAAWGKVGAHAGEYGAEALERMFLSFPTTKTYFPHFDLSHGSAQVKGHGKKVADALTNAVAHVDDMPNALSALSDLHAHKLRVDPVNFKLLSHCLLVTLAAHLPAEFTPAVHASLDKFLASVSTVLTSK-Y

Total CPU time is  0.03 seconds

感谢您的帮助!

【问题讨论】:

  • 欢迎来到 SO!您是否尝试过以下解决方案?
  • 是的,它工作得很好,除了 for 循环线。它应该是括号中的'\n'。但还是非常感谢!

标签: python output executable


【解决方案1】:

您应该考虑以下方法:

import re
from subprocess import check_output

ret = check_output(['./TMalign', '3w4u.pdb', '6bb5.pdb', '-u', '139'])

tm_scores = []

for line in str(ret).split('\\n'):
    if re.match(r'^TM-score=', line):
        score = line.split()[1:2] # Extract the value
        tm_scores.extend(score) # Saving only values

 # tm_scores now contains: ['0.94726', '0.96044', '0.96044']

虽然它有些复杂,但它是一种灵活且可调整的解决方案。注意,如果要在其他代码中使用,最好将其包装到一个函数中。

【讨论】:

    【解决方案2】:

    我的函数不是那么聪明,我会让输出写入文件以执行后续操作

    import os
    cmd = './TMalign 3w4u.pdb 6bb5.pdb -u 139'
    os.system(cmd + ">> 1.txt")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 2015-09-11
      • 2021-10-18
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      相关资源
      最近更新 更多