python合并RepeatMasker预测结果中染色体的overlap区域
作者:生信工具箱 时间:2021-12-28 21:29:35
前言
RepeatMasker是一个通过已有数据库预测重复序列的软件,可以筛选DNA序列中的散在重复序列和低复杂序列,是重复序列注释的重要软件。
问题
我们想对RepeatMasker预测的结果文件进行重复序列的合并,也就是去除染色体之间的overlap区域同时将基因间距小于50个bp的也同样视为overlap,我们应该如何用python处理并生成新的预测结果?
思路
首先需要对文件进行预处理提取出需要处理的列,'//'可以忽略
对相同染色体序列按照升序进行归并排序
分别取相应染色体按照滑动窗口的思路进行双指针比对,注意gap=50
1. 预处理
我们这里只需要结果文件的前三列,可以使用awk命令获取
awk '{for(i = 1; i <= 3; i++)
printf("%s ", $i);
printf("\n")}' result.txt > pretreatment.txt
#result.txt为结果文件,pretreatment.txt为预处理结果文件
2. 将pretreatment.txt作为输入文件,
with open ('pretreatment.txt','r')as f:
for i in f.readlines():
if i.strip() == '//':
continue
c = i.strip().split('\t')
b.append(c[0])
a.append((c[0],int(c[1]),int(c[2])))
print ("全部染色体数量: "+str(len(a)))
3.去重+归并排序
c = [i for i in b_set if b.count(i) == 1]
for i in a:
if i[0] not in c:
continue
a.remove(i)
result.append((i[0],int(i[1]),int(i[2])))
print ("去重后染色体数量: "+str(len(a)))
a.sort(key = lambda x : (x[0], x[1], x[2]))
#按照第一列,第二列,第三列分别排降升序
4.开始比对,gap=50
q = ''
start = 0
end = 0
tem1 = []
tem2 = []
gap = 50
for i in a:
if i[0] != q:
if tem1:
if tem1 not in tem2:
tem2.append(tem1)
tem1 = []
q = I[0]
start = int(i[1])
end = int(i[2])
continue
if int(i[1]) < end or int(i[1]) - end < gap:
if int(i[2]) > end:
end = int(i[2])
continue
else:
continue
tem1.append([q,start,end])
start = int(i[1])
end = int(i[2])
5.将new_result.txt作为输出文件,生成结果
with open ('new_result.txt','w')as f:
for i in tem2:
for o in I:
print (o[0],o[1],o[2],file=f)
for i in result:
print (i[0],i[1],i[2],file=f)
6. 完整代码
a = []
b = []
with open ('pretreatment.txt','r')as f:
for i in f.readlines():
if i.strip() == '//':
continue
c = i.strip().split('\t')
b.append(c[0])
a.append((c[0],int(c[1]),int(c[2])))
print ("全部染色体数量: "+str(len(a)))
b_set = set(b)
result = []
c = [i for i in b_set if b.count(i) == 1]
for i in a:
if i[0] not in c:
continue
a.remove(i)
result.append((i[0],int(i[1]),int(i[2])))
print ("去重后染色体数量: "+str(len(a)))
a.sort(key = lambda x : (x[0], x[1], x[2]))
q = ''
start = 0
end = 0
tem1 = []
tem2 = []
gap = 50
for i in a:
if i[0] != q:
if tem1:
if tem1 not in tem2:
tem2.append(tem1)
tem1 = []
q = I[0]
start = int(i[1])
end = int(i[2])
continue
if int(i[1]) < end or int(i[1]) - end < gap:
if int(i[2]) > end:
end = int(i[2])
continue
else:
continue
tem1.append([q,start,end])
start = int(i[1])
end = int(i[2])
with open ('new_result.txt','w')as f:
for i in tem2:
for o in I:
print (o[0],o[1],o[2],file=f)
for i in result:
print (i[0],i[1],i[2],file=f)
来源:https://www.jianshu.com/p/42faa3a0228e
标签:python,RepeatMasker,overlap,染色体,区域预测
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
标签水平右对齐更适合中文网站
2009-05-01 11:54:00
![](https://img.aspxhome.com/file/UploadPic/20095/1/01-51s.gif)
利用Python实现端口扫描器的全过程
2021-07-08 01:25:32
![](https://img.aspxhome.com/file/2023/7/90517_0s.gif)
用python生成mysql数据库结构文档
2021-05-15 12:04:33
![](https://img.aspxhome.com/file/2023/5/67945_0s.png)
python lambda 表达式形式分析
2021-05-06 06:05:58
![](https://img.aspxhome.com/file/2023/0/71320_0s.png)
php strftime函数的详细用法
2023-06-07 19:09:37
oracle 存储过程和函数例子
2009-08-08 22:27:00
Python yield的使用详解
2021-07-17 22:23:29
Python利用arcpy模块实现栅格的创建与拼接
2021-10-07 22:39:37
![](https://img.aspxhome.com/file/2023/8/81798_0s.png)
实例讲解Python爬取网页数据
2023-01-10 03:55:05
python3.7 使用pymssql往sqlserver插入数据的方法
2021-10-13 00:23:34
Python详解复杂CSV文件处理方法
2021-04-05 11:12:02
![](https://img.aspxhome.com/file/2023/6/67936_0s.png)
基于pygame实现童年掌机打砖块游戏
2023-09-18 20:41:28
![](https://img.aspxhome.com/file/2023/4/87514_0s.jpg)
在VS2017中用C#调用python脚本的实现
2021-09-19 00:59:06
Python面向对象编程基础解析(二)
2023-11-16 01:53:23
Python流程控制 while循环实现解析
2023-02-07 04:16:33
![](https://img.aspxhome.com/file/2023/9/86179_0s.jpg)
详解使用Nginx和uWSGI配置Python的web项目的方法
2021-06-28 00:24:47
![](https://img.aspxhome.com/file/2023/6/112496_0s.jpg)
Oracle数据表分区的策略
2010-07-28 12:59:00
Python用模块pytz来转换时区
2021-05-27 20:09:32
Linux下python3.7.0安装教程
2021-07-30 05:25:58
![](https://img.aspxhome.com/file/2023/7/90437_0s.jpg)
专家教你安装 MySQL的与MySQL GUI Tools
2012-01-29 17:59:05