多序列比对sto格式转a3m

多序列比对(Multiple Sequence Alignment,MSA)是对多个生物序列进行对齐的过程,以揭示它们之间的共同模式和结构。在生物信息学中,有多种文件格式用于存储多序列比对的结果,其中包括Stockholm (.sto) 和 A3M (.a3m) 格式。

注释信息: Stockholm文件通常包含更多的注释信息,提供关于序列和比对的额外信息。相比之下,A3M文件主要关注于序列本身。

应用领域: Stockholm文件广泛用于多种生物信息学工具,如HMMER。A3M文件通常用于蛋白质结构预测和深度学习模型的训练,如Alphafold。

复制代码
from typing import Sequence, Iterable, Optional

def _convert_sto_seq_to_a3m(
      query_non_gaps: Sequence[bool], sto_seq: str) -> Iterable[str]:
    for is_query_res_non_gap, sequence_res in zip(query_non_gaps, sto_seq):
        if is_query_res_non_gap:
            yield sequence_res
        elif sequence_res != '-':
            yield sequence_res.lower()
    
def convert_stockholm_to_a3m(stockholm_format: str,
                             max_sequences: Optional[int] = None,
                             remove_first_row_gaps: bool = True) -> str:
    """Converts MSA in Stockholm format to the A3M format."""
    descriptions = {}
    sequences = {}
    reached_max_sequences = False

    for line in stockholm_format.splitlines():
        reached_max_sequences = max_sequences and len(sequences) >= max_sequences
        if line.strip() and not line.startswith(('#', '//')):
            # Ignore blank lines, markup and end symbols - remainder are alignment
            # sequence parts.
            seqname, aligned_seq = line.split(maxsplit=1)
            if seqname not in sequences:
                if reached_max_sequences:
                    continue
                sequences[seqname] = ''
            sequences[seqname] += aligned_seq

    for line in stockholm_format.splitlines():
        if line[:4] == '#=GS':
            # Description row - example format is:
            # #=GS UniRef90_Q9H5Z4/4-78            DE [subseq from] cDNA: FLJ22755 ...
            columns = line.split(maxsplit=3)
            seqname, feature = columns[1:3]
            
            value = columns[3] if len(columns) == 4 else ''
            if feature != 'DE':
                continue
            if reached_max_sequences and seqname not in sequences:
                continue
            descriptions[seqname] = value
        if len(descriptions) == len(sequences):
            break

    # Convert sto format to a3m line by line
    a3m_sequences = {}
    if remove_first_row_gaps:
        # query_sequence is assumed to be the first sequence
        query_sequence = next(iter(sequences.values()))
        query_non_gaps = [res != '-' for res in query_sequence]
    for seqname, sto_sequence in sequences.items():
        # Dots are optional in a3m format and are commonly removed.
        out_sequence = sto_sequence.replace('.', '')
        if remove_first_row_gaps:
            out_sequence = ''.join(
                _convert_sto_seq_to_a3m(query_non_gaps, out_sequence))
            a3m_sequences[seqname] = out_sequence

    fasta_chunks = (f">{k} {descriptions.get(k, '')}\n{a3m_sequences[k]}"
                    for k in a3m_sequences)
    return '\n'.join(fasta_chunks) + '\n'  # Include terminating newline

with open("test_aln.sto") as f:
    sto_str = f.read()

print(sto_str)
print("====")
a3m_str = convert_stockholm_to_a3m(sto_str)
print(a3m_str)
相关推荐
Biomamba生信基地2 天前
基于SpliceAI的TCGA-LIHC体细胞突变剪接效应预测
生物信息学·tcga·spliceai
Bioinfo Guy5 天前
【期刊投稿指南】可接纯生信!npj Genomic Medicine的选题与专题征稿
生物信息学
Bioinfo Guy6 天前
Cell Discov. | 1型糖尿病如何扰乱雌性生殖内分泌轴?
生物信息学
Bioinfo Guy7 天前
npj Digit. Med. | 从牙周炎到脑内炎症:单核转录组与机器学习揭示黄连解毒汤的cGAS–STING调节作用
生物信息学
Bioinfo Guy10 天前
J. Nanobiotechnol. | 三七NotoEV跨界递送植物miRNA,重塑缺血后神经元应激反应
生物信息学
Bioinfo Guy10 天前
【期刊投稿指南】Frontiers in Genetics:肿瘤多组学从计算发现走向验证
生物信息学
Bioinfo Guy11 天前
Adv. Sci. | 2602例验证:H&E病理AI能否给胃肠癌前病变分层?
生物信息学
Bioinfo Guy14 天前
Nat. Commun. | TDP-43病理并非随机扩散:ALS/ALS-FTD运动皮层中最受影响的是特定兴奋性神经元
生物信息学
Bioinfo Guy14 天前
Mol. Cancer | 胃癌化疗耐药新轴:YAP1/TEAD4-MCM10把DNA复制、干性和Wnt信号串起来
生物信息学
Biomamba生信基地17 天前
《Nature methods》:基于单细胞多组学检测DNA甲基化和组蛋白修饰重构表观基因组维持的动态过程
生物信息学·单细胞多组学·甲基化