生信算法9 - 正则表达式匹配氨基酸序列、核型和字符串

news/2024/7/7 19:02:39 标签: 正则表达式, mysql, 数据库

1. 使用正则表达式匹配指定的氨基酸序列

import re

# 氨基酸序列
seq = 'VSVLTMFRYAGWLDRLYMLVGTQLAAIIHGVALPLMMLI'

# 正则表达式匹配
match = re.search(r'[A|G]W', seq)

# 打印match及匹配到开始位置和结束位置
print(match)
# <re.Match object; span=(10, 12), match='GW'>
print(match.start())
print(match.end())

if match:
    # 打印匹配到氨基酸
    print(match.group())
    # GW
else:
    print("no match!")

2. 使用正则表达式查找全部的氨基酸序列

import re

seq = 'RQSAMGSNKSKPKDASQRRRSLEPAENVHGAGGGAFPASQRPSKP'

# 匹配R开头、第二个氨基酸为任意、第三个氨基酸为S或T、第四个氨基酸不为P的连续4个氨基酸徐磊
matches = re.findall(r'R.[ST][^P]', seq)
print(matches)
# ['RQSA', 'RRSL', 'RPSK']

# finditer 匹配对象迭代器
match_iter = re.finditer(r'R.[ST][^P]', seq)

# 遍历
for match in match_iter:
    # 打印group和span
    print(match.group(), match.span())
    print(match.start(), match.end())

		# RQSA (0, 4)
		# 0 4

		# RRSL (18, 22)
		# 18 22

		# RPSK (40, 44)
		# 40 44

3. 使用正则表达式匹配多个特殊字符,分割字符串

import re

# 匹配特殊字符|和;,并分割字符串
annotation = 'ATOM:CA|RES:ALA|CHAIN:B;NUMRES:166'
split_string = re.split(r'[|;]', annotation)

print(split_string)
# ['ATOM:CA', 'RES:ALA', 'CHAIN:B', 'NUMRES:166']

4. 正则表达式获取核型染色体数量,区带和CNV大小

karyotype1 = '46,XY; -11{p11.2-p13, 48.32Mb}'
karyotype2 = '47,XXX; +X{+3};-11{p11.2-p13.2, 48.32Mb}'

#### 匹配染色体数量 ####
match = re.search(r'(\d+,\w+);', karyotype1)
print(match)
# <re.Match object; span=(0, 6), match='46,XY;'>

chr = match.group(1)
print(chr)
# 46,XY

#### 匹配染色体开始和结束区带和CNV大小 ####
match2 = re.search(r'([p|q|pter]\d+.?\d+)-([p|q|qter]\d+.?\d+), (\d+.?\d+)Mb', karyotype2)
print(match2)

cyto_start = match2.group(1)
cyto_end = match2.group(2)
size = match2.group(3)

print(cyto_start)
# p11.2
print(cyto_end)
# p13.2
print(size)
# 48.32

5. 正则表达式获取指定格式的字符串内容

# 结果变异VCF文件描述信息
string = """
    ##ALT=<ID=DEL,Description="Deletion">
    ##ALT=<ID=DUP,Description="Duplication">
    ##ALT=<ID=INV,Description="Inversion">
    ##ALT=<ID=INVDUP,Description="InvertedDUP with unknown boundaries">
    ##ALT=<ID=TRA,Description="Translocation">
    ##ALT=<ID=INS,Description="Insertion">
    ##FILTER=<ID=UNRESOLVED,Description="An insertion that is longer than the read and thus we cannot predict the full size.">
    ##INFO=<ID=CHR2,Number=1,Type=String,Description="Chromosome for END coordinate in case of a translocation">
    ##INFO=<ID=END,Number=1,Type=Integer,Description="End position of the structural variant">
    ##INFO=<ID=MAPQ,Number=1,Type=Integer,Description="Median mapping quality of paired-ends">
    ##INFO=<ID=RE,Number=1,Type=Integer,Description="read support">
    ##INFO=<ID=IMPRECISE,Number=0,Type=Flag,Description="Imprecise structural variation">
    ##INFO=<ID=PRECISE,Number=0,Type=Flag,Description="Precise structural variation">
    ##INFO=<ID=SVLEN,Number=1,Type=Integer,Description="Length of the SV">
    ##INFO=<ID=SVMETHOD,Number=1,Type=String,Description="Type of approach used to detect SV">
    ##INFO=<ID=SVTYPE,Number=1,Type=String,Description="Type of structural variant">
    ##INFO=<ID=SEQ,Number=1,Type=String,Description="Extracted sequence from the best representative read.">
    ##INFO=<ID=STRANDS2,Number=4,Type=Integer,Description="alt reads first + ,alt reads first -,alt reads second + ,alt reads second -.">
    ##INFO=<ID=REF_strand,Number=.,Type=Integer,Description="plus strand ref, minus strand ref.">
    ##INFO=<ID=Strandbias_pval,Number=A,Type=Float,Description="P-value for fisher exact test for strand bias.">
    ##INFO=<ID=STD_quant_start,Number=A,Type=Float,Description="STD of the start breakpoints across the reads.">
    ##INFO=<ID=STD_quant_stop,Number=A,Type=Float,Description="STD of the stop breakpoints across the reads.">
    ##INFO=<ID=Kurtosis_quant_start,Number=A,Type=Float,Description="Kurtosis value of the start breakpoints across the reads.">
    ##INFO=<ID=Kurtosis_quant_stop,Number=A,Type=Float,Description="Kurtosis value of the stop breakpoints across the reads.">
    ##INFO=<ID=SUPTYPE,Number=.,Type=String,Description="Type by which the variant is supported.(SR,AL,NR)">
    ##INFO=<ID=STRANDS,Number=A,Type=String,Description="Strand orientation of the adjacency in BEDPE format (DEL:+-, DUP:-+, INV:++/--)">
    ##INFO=<ID=AF,Number=A,Type=Float,Description="Allele Frequency.">
    ##INFO=<ID=ZMW,Number=A,Type=Integer,Description="Number of ZMWs (Pacbio) supporting SV.">
    ##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
    ##FORMAT=<ID=DR,Number=1,Type=Integer,Description="# high-quality reference reads">
    ##FORMAT=<ID=DV,Number=1,Type=Integer,Description="# high-quality variant reads">
    """

import re

# 创建空dataframe
df_output = pd.DataFrame()

list_type = []
list_id = []
list_description = []

# 遍历字符串内容,内容拷贝至结构变异VCF文件
for str in string.split('\n'):
    # 去除末尾\n和字符串内空格
    str = str.strip().replace(' ', '')
    
    # 内容为空或字符串为空则跳过
    if not str and str == '':
        continue
    
    # 正则表达式匹配##后的英文字符
    match = re.search(r'##(\w+)', str)
    type = match.group(1) if match else 'ERORR'

    # 匹配ID内容
    match = re.search(r'ID=(\w+)', str)
    id = match.group(1) if match else 'ERORR'

    # 匹配Description内容
    match = re.search(r'Description=\"(.*?)\"', str)
    description = match.group(1) if match else 'ERORR'

    # 加入列表
    list_type.append(type)
    list_id.append(id)
    list_description.append(description)
    
print(list_description)
# 加入dataframe
df_output['Type'] = list_type
df_output['ID'] = list_id
df_output['Description'] = list_description

# 保存至excel
df_output.to_excel('结构变异描述信息说明.xlsx', index=False)

生信算法文章推荐

生信算法1 - DNA测序算法实践之序列操作

生信算法2 - DNA测序算法实践之序列统计

生信算法3 - 基于k-mer算法获取序列比对索引

生信算法4 - 获取overlap序列索引和序列的算法

生信算法5 - 序列比对之全局比对算法

生信算法6 - 比对reads碱基数量统计及百分比统计

生信算法7 - 核酸序列Fasta和蛋白PDB文件读写与检索

生信算法8 - HGVS转换与氨基酸字母表


http://www.niftyadmin.cn/n/5534845.html

相关文章

视频监控平台web客户端的免密查看视频页:在PC浏览器上如何调试手机上的前端网页(PC上的手机浏览器的开发者工具)

目录 一、手机上做前端页面开发调试 1、背景 2、视频监控平台AS-V1000的视频分享页 3、调试手机前端页面代码的条件 二、手机端的准备工作 1、手机准备 2、手机的开发者模式 3、PC和手机的连接 &#xff08;1&#xff09;进入调试模式 &#xff08;2&#xff09;选择…

大模型日报 2024-07-04

大模型日报 2024-07-04 一、大模型资讯 大厂高管转战 AI 创业盘点&#xff1a;超 25 人&#xff0c;覆盖全产业链&#xff0c;AI 应用最热门 涉及多家互联网大厂高管加入生成式 AI 创业&#xff0c;涵盖多个领域及融资情况。 腾讯云发布自研大数据高性能计算引擎 Meson 软硬一体…

STM32第十二课:ADC检测烟雾浓度(MQ2)

文章目录 需求一、MQ-2 气体传感器特点应用电路及引脚 二、实现流程1.开时钟&#xff0c;分频&#xff0c;配IO2.配置ADC的工作模式3.配置通道4.复位&#xff0c;AD校准5.数值的获取 需求实现总结 需求 使用ADC将MQ2模块检测到的烟雾浓度模拟量转化为数字量。 最后&#xff0c…

腾讯云函数node.js返回自动带反斜杠

云函数返回自动带反斜杠 这里建立了如下一个云函数,目的是当APP过来请求的时候响应支持的版本号: use strict; function main_ret(status,code){let ret {status: status,error: code};return JSON.stringify(ret); } exports.main_handler async (event, context) > {/…

TCP、UDP详解

目录 1.区别 1.1 概括 1.2 详解 2.TCP 2.1 内容 2.2 可靠传输 2.2.1 确认应答 2.2.2 超时重传 2.2.3 连接管理 三次握手 四次挥手 2.2.4 滑动窗口 2.2.5 流量控制 2.2.6 拥塞控制 2.2.7 延时应答 2.2.8 捎带应答 2.2.9 面向字节流 2.2.10 异常情况的处理 1.…

机械设备制造企业MES系统解决方案介绍

机械设备制造行业涵盖了各类工业设备、工程机械、农业机械等多个领域&#xff0c;对生产精度、质量控制和效率提出了较高要求。为了提升生产效率、保证产品质量并满足客户需求&#xff0c;越来越多的机械设备制造企业引入了MES系统。本文将详细介绍MES系统在机械设备制造行业的…

C++内存管理(候捷)第一讲 笔记

内存分配的每一层面 applications可以调用STL&#xff0c;里面会有allocator进行内存分配&#xff1b;也可以使用C 基本工具primitives&#xff0c;比如new, new[], new(), ::operator new()&#xff1b;还可以使用更底层的malloc和free分配和释放内存。最底层的是系统调用&…

Node.js下载及安装详细教程

目录 Node.js安装详细教程 下载安装环境变量配置文件结构配置npm在安装全局模块时的路径和缓存cache的路径测试常见命令 Node.js安装详细教程 &#x1f441;官网下载地址:Download | Node.js (nodejs.org) 下载速度慢的话 可以使用网盘下载&#xff1a; https://pan.quark.…