用python做生信_1 python生信入门
1. 解决的实际问题,计算人类参考基因组hg38每条染色体碱基数据ATCG的比例
2. 计算人类参考基因组hg38的外显子区域碱基数量
3. 目的是了解基础python语法
4. 参考书目:python编程从入门到实践,参考视频资料见最底部
打开python3
vim 1.txt
:wq ##Esc+ :wq保存退出
python3 # 打开python3
f = open('./1.txt','r')
for i in f:
print (i)
with open('./1.txt') as file_object:
for line in file_object:
print (line)
图1
f = open('./2.txt','w')
with open('./1.txt','r') as imput_f:
for line in imput_f:
line = line.strip()
result= int(line)+int(line)
f.write(str(result)+'\n')
f.close()
图2
output_file = open('./2.txt','a') ## 附加内容
with open('./1.txt','r') as imput_f:
for line in imput_f:
line = line.strip()
result= int(line)+int(line)
output_file.write(str(result)+'\n')
output_file.close()
图3
完成探索人类基因组GC含量与gene密度的关系;
提示:
1.将染色体成分1Mb大小的bin统计GC含量;
2.将染色体分为1Mb大小的bin统计gene的密度;
3.使用R将这两者用散点图进行绘图;
4.使用R的线性回归函数lm()进行回归;
5.R回归部分,参考资料
blog.fens.me/r-linear-regression/
def my_abs(imput):
if imput >= 0:
return imput
else:
return -imput
my_abs(-10)
作业
人类的线粒体长度大约为16.7Kb,请读取人类线粒体参考基因组序列信息(fasta格式),统计各种碱基的数量以及人类线粒体参考基因组的总长度。
编程作业
# _*_ coding:UTF-8 _*_
### 保证中文不报错pycharm
选取参考基因组hg38作为测试数据1000-1300行 n 和 p是同时出现的
cat ~/reference/UCSC/hg38/hg38.fa| sed -n '1000,1300p' >> ~/project/python/t.txt
cd - # 回到上一个目录
python3
图4
chrM_genome = ''
with open('./t.txt','r') as imput_f:
for line in imput_f:
line = line.strip().upper() ## 去除前后空格,大写
chrM_genome = chrM_genome + line
print (len(chrM_genome))
print (chrM_genome.count('A'))
print ('the char A count is : {0}'.format(chrM_genome.count('A'))) # format 将其格式化
Fastq 格式 四行
图5
>>> with open(r'./fa_q.txt','r') as imput:
... for index,line in enumerate(imput):
... if index % 4==0:
... print ('>'+line.strip()[1:])
... elif index % 4 ==1:
... print (line.strip())
... elif index % 4==2:
... continue
... elif index %4==3:
... continue
...
>SRR1039512.325 HWI-ST177:314:C12K6ACXX:1:1101:5657:2438 length=63
TCAGAAGAAAAGCATATGTCTGCCATGGGACTATTGCAGTGCGTCTCCATCAGTGTTAACACA
>SRR1039512.326 HWI-ST177:314:C12K6ACXX:1:1101:5550:2446 length=63
TAAGCAGTGCTTGAATTATTTGGTTTCGGTTGTTTTCTATTAGACTATGGTGAGCTCAGGTGA
>SRR1039512.327 HWI-ST177:314:C12K6ACXX:1:1101:5908:2367 length=63
TGGGGAATAGCTTCTCAAGGGCAAGTGCTGTTTCCATACTAGTTCTTTTCCTTGCTCTCTTAT
>SRR1039512.328 HWI-ST177:314:C12K6ACXX:1:1101:5852:2396 length=63
GGAGGAAGCCCACGGTGGGATCTGGCACAAAGATGTAGAGCCGTTTCCGCTCATCGGTCTCCA
>SRR1039512.329 HWI-ST177:314:C12K6ACXX:1:1101:5768:2409 length=63
ACCTGGCAGTGCTGCAACAGTATCTGCCTCTACAAGTAACATCATACCCCCAAGACACCAGAA
>SRR1039512.330 HWI-ST177:314:C12K6ACXX:1:1101:5840:2413 length=63
GCAGAGGTAATTGCCTGAATCCTTCTGTTGTAGACTACGTAGCAGAAGGCCTTGATCTGTCCT
>SRR1039512.331 HWI-ST177:314:C12K6ACXX:1:1101:5826:2463 length=63
GAAAGGCAAGGAGGAAGCTTATCTATGAAAAAGCAAAGCACTATCACAAGGAATATAGGC
>SRR1039512.332 HWI-ST177:314:C12K6ACXX:1:1101:5888:2477 length=63
GTCTGTCTCTTGCTTCAACAGTGTTTGGACGGAACAGATCCGGGGACTCTCTTCCAGCCTCCG
>SRR1039512.333 HWI-ST177:314:C12K6ACXX:1:1101:6208:2288 length=63
GTCATAACCACCACCGCTTACACCTGGAGGTCCAGGAGGGCCAGGGGGACCAGGGGGGCCAGC
##每隔40一行
with open(r'C:\Users\kexin\Desktop\python\fa_q.txt','r') as imput:
for index,line in enumerate(imput):
if index % 4==0:
print ('>'+line.strip()[1:])
elif index % 4 ==1:
for i in range(0,len(line.strip()),40):
print (line.strip()[i:i+40])
elif index % 4==2:
continue
elif index %4==3:
continue
output_file = open (r'C:\Users\kexin\Desktop\python\result2.fastq','w')
with open(r'C:\Users\kexin\Desktop\python\fa_q.txt','r') as imput:
for index,line in enumerate(imput):
if index % 4==0:
output_file.write(('>'+line.strip()[1:])+'\n')
elif index % 4 ==1:
for i in range(0,len(line.strip()),40):
output_file.write(line.strip()[i:i+40]+'\n')
elif index % 4==2:
print (index)
elif index %4==3:
continue
output_file.close()
读取fasta文件
with open (r'C:\Users\kexin\Desktop\python\result2.fastq','r') as input_file:
seq=''
header= input_file.readline().strip()[1:] ## 读取第一行
for line in input_file:
if line[0]!='>':
seq =seq.strip()+line ### 这里是要确保刚刚被40个一行分隔的seq数据重新叠加
elif line[0]=='>':
print (header)
print (seq)
header = line.strip()[1:]
seq = '' ### 一个循环结束将seq清空
print (header)
print (seq)
一个能读取fasta格式的函数(收藏版)
def read_fasta(file_path=''):
line = ''
try:
fasta_handle=open(file_path,'r')
except:
raise IOError('you input FAST file is not right')
while True:
line = fasta_handle.readline()
if line == '':
return
if line[0] =='>':
break
## while the file is not empty, load FASTA file
while True:
if line[0] !='>':
raise ValueError("Records in Fasta files should start with '>'")
title = line[1:].rstrip()
lines =[]
line = fasta_handle.readline()
while True:
if not line:
break
if line[0] == '>':
break
lines.append(line.rstrip())
line = fasta_handle.readline()
yield title, ''.join(lines).replace('','').replace('\r','')
if not line:
return
fasta_handle.close()
assert False, 'your input fasta file have format problem'
read_fasta(file_path=r'C:\Users\kexin\Desktop\python\result2.fastq') ## 出现一个报错,在windows里面,要加上r,不然\代表转义:参考https://blog.csdn.net/caibaoH/article/details/78335094
for header,seq in read_fasta(file_path=r'C:\Users\kexin\Desktop\python\result2.fastq'):
print (header)
print (seq)
计算人类参考基因组的序列长度
分析步骤:
利用读取fasta的代码读取基因组序列信息
统计每条序列的长度,并输出
统计人类参考基因组的总长度,并输出
统计每条染色体N的长度,并输出
提取基因组特定区域的序列,并输出(支持+/-链)
hg38_genome={}
for chr_name, chr_seq in read_fasta(file_path=r'/four/yqchen/reference/UCSC/hg38/hg38.fa'):
hg38_genome[chr_name]=chr_seq
这里要用绝对路径不然报错
图6
hg38_genome.keys()
for chr_name in hg38_genome.keys():
print (chr_name,len(hg38_genome.get(chr_name)))
## 这里有点不对,但是无所谓了
图7
hg38_total_len=0
for index in range (1,26):
if index<= 22:
chr_name = 'chr{0}'.format(index)
elif index == 23:
chr_name = 'chrX'
elif index == 24:
chr_name = 'chrY'
elif index == 25:
chr_name = 'chrM'
print (chr_name,len(hg38_genome.get(chr_name)))
这里结果是正确的,这是为什么?这里没搞明白的。
图8
hg38_total_len = 0
for index in range (1,26):
if index<= 22:
chr_name = 'chr{0}'.format(index)
elif index == 23:
chr_name = 'chrX'
elif index == 24:
chr_name = 'chrY'
elif index == 25:
chr_name = 'chrM'
hg38_total_len= hg38_total_len + int(len(hg38_genome.get(chr_name)))
print (hg38_total_len) ### 和上面那一句分开执行
图9
图10
len(hg38_genome['chr21'])
hg38_genome['chr21'].count('N')
hg38_genome['chr22'].count('N')
## 正负链
chr_name='chr1'
chr_start=10000
chr_end=10300
hg38_genome[chr_name][chr_start-1:chr_end-1].upper()
#### 反向互补
import string
translate_table=string.maketrans('ATGC','TACG')
a=hg38_genome[chr_name][chr_start-1:chr_end-1].upper()
a.translate(translate_table)[::-1]
##方向互补定义函数
import string
def com_rev(input):
translate_table=string.maketrans('ATGC','TACG')
return input.upper().translate(translate_table)[::-1]
编码基因长度,从参考基因组注释信息找
with open(r'C:/Users/kexin/Desktop/python/hg38_RefSeq_genetable') as input_f:
header = input_f.readline()
for line in input_f:
line=line.strip().split('\t')
print (line)
break
with open(r'C:/Users/kexin/Desktop/python/hg38_RefSeq_genetable') as input_f:
header = input_f.readline() #### 读一行 不要用readlines
for line in input_f:
line=line.strip().split('\t')
exon_count = int(line [8])
exon_start =line[9].strip(',') ### 去掉最后的逗号
print (exon_start)
break
with open(r'C:/Users/kexin/Desktop/python/hg38_RefSeq_genetable') as input_f:
header = input_f.readline() #### 读一行 不要用readlines
for line in input_f:
line=line.strip().split('\t')
# print (line)
exon_count = int(line [8])
exon_start =line[9].strip(',')
exon_end =line[10].strip(',')### 去掉最后的逗号
print (exon_start)
print (exon_end)
break
with open(r'C:/Users/kexin/Desktop/python/hg38_RefSeq_genetable') as input_f:
header = input_f.readline() #### 读一行 不要用readlines
for line in input_f:
line=line.strip().split('\t')
# print (line)
exon_count = int(line [8])
exon_start = line[9].strip(',').split(',') ### map函数int用于后面的列表
exon_end = line[10].strip(',').split(',')
print (exon_start)
print (exon_end)
break
with open(r'C:/Users/kexin/Desktop/python/hg38_RefSeq_genetable') as input_f:
header = input_f.readline() #### 读一行 不要用readlines
for line in input_f:
line=line.strip().split('\t')
# print (line)
exon_count = int(line [8])
exon_start = map(int,line[9].strip(',').split(',')) ### map函数int用于后面的列表
exon_end = map(int,line[10].strip(',').split(','))
print (exon_start)
print (exon_end)
break
with open(r'C:/Users/kexin/Desktop/python/hg38_RefSeq_genetable') as input_f:
header = input_f.readline() #### 读一行 不要用readlines
for line in input_f:
line=line.strip().split('\t')
# print (line)
exon_total_len=0
exon_count = int(line [8])
exon_start = map(int,line[9].strip(',').split(',')) ### map函数int用于后面的列表
exon_end = map(int,line[10].strip(',').split(','))
for index in range(exon_count):
exon_total_len= exon_total_len+exon_end[index]-exon_start[index]
print (exon_total_len)
print (exon_start)
print (exon_end)
break
这里python3 会报错
1555423217495.png
1555423298540.png
with open(r'C:/Users/kexin/Desktop/python/hg38_RefSeq_genetable') as input_f:
header = input_f.readline() #### 读一行 不要用readlines
for line in input_f:
line=line.strip().split('\t')
# print (line)
exon_total_len=0
exon_count = int(line [8])
exon_start = list(map(int, line[9].strip(',').split(','))) ### map函数int用于后面的列表
exon_end = list(map(int, line[10].strip(',').split(',')))
for index in range(exon_count):
exon_total_len= exon_total_len+exon_end[index]-exon_start[index]
print (exon_total_len)
print (exon_start)
print (exon_end)
break
完整计算版:
gene_exon_len_dict={}
with open(r'C:/Users/kexin/Desktop/python/hg38_RefSeq_genetable') as input_f:
header = input_f.readline() #### 读一行 不要用readlines
for line in input_f:
line=line.strip().split('\t')
# print (line)
exon_total_len=0
exon_count = int(line [8])
exon_start = list(map(int, line[9].strip(',').split(','))) ### map函数int用于后面的列表
exon_end = list(map(int, line[10].strip(',').split(',')))
for index in range(exon_count):
exon_total_len= exon_total_len+exon_end[index]-exon_start[index]
gene_id = line[12]
if gene_exon_len_dict.get(gene_id) is None: #如果gene_exon_len_dict字典里没有这个基因,加入这个基因的长度
gene_exon_len_dict[gene_id] = exon_total_len
else:
if gene_exon_len_dict.get(gene_id) < exon_total_len:
gene_exon_len_dict[gene_id] = exon_total_len
len(gene_exon_len_dict)
exon_total =0
for gene_id in gene_exon_len_dict.keys():
exon_total = exon_total+gene_exon_len_dict[gene_id]
# exon_total = exon_total+gene_exon_len_dict.values(gene_id) 这是错误写法
print (exon_total)
exon_rate = 111336157 / 3088286401 *100
exon_rate
推荐视频
用python做生信_1 python生信入门相关推荐
- python 做啥用-使用 Python 可以做什么?
翻译自 <Python学习手册(第5版)> Systems Programming Python 对操作系统服务的内置接口使其非常适合编写可移植.可维护的系统管理工具和实用程序 utili ...
- 学python有前途吗-我们能用Python做什么?学Python有前途吗?
我们能用Python做什么? 目前流行的大数据分析.数据科学.机器学习等行业,Python长期稳固第一阵营,甚至就是第一语言.不管从哪个方面来考虑,都应该选择 Python. 在安全渗透行业,大量的攻 ...
- python做统计分析_用Python做数据分析,Numpy,Pandas,matp
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 用Python做数据分析,Numpy,Pandas,matplotlib是怎么也绕不开的三个包,我最开始学习pandas是看的<利用Python进行 ...
- python做购物车代码大全-python 字典实现简单购物车
# -*- coding: utf-8 -*- #总金额 asset_all=0 i1=input('请输入总资产:') asset_all=int(i1) #商品列表 goods=[ {'name' ...
- python做些什么-学会Python后都能做什么?网友们的回答简直不要太厉害
如今,越来越多的人加入到学习Python的队伍当中. 有的学习者是设计师,学习Python可以帮助他们查找更多的海报案例:有的学习者是大学生,学习Python可以帮助他们更好地查阅论文资料:还有的学习 ...
- 用python做数据分析pdf_利用python进行数据分析pdf
利用python进行数据分析pdf微盘下载!<利用python进行数据分析>利用Python实现数据密集型应用由浅入深帮助读者解决数据分析问题~适合刚刚接触Python的分析人员以及刚刚接 ...
- python做excel自动化视频教程-从零基础入门到精通用Python处理Excel数据视频教程...
从零基础入门到精通用Python处理Excel数据视频教程 1.从零基础开始用python处理Excel数据 1-1 什么是python.mp4 1-2 为什么要学习用Python处理Excel表格. ...
- python做excel自动化-用python进行办公自动化都需要学习什么知识呢?
自动化办公无非是excel.ppt.word.邮件.文件处理.数据分析处理.爬虫这些.我来一一介绍如何学习,找资料! 最近做了个Python办公自动化的Live讲座,不要脸的推一波~ python基础 ...
- 用python做炒股软件-同花顺有python接口_基于python的炒股软件
股票详细数据 怎么获得股市数据针对股票等金融数据的获取,python提供了一个非常实用的模块-tushare,自动完成了数据从采集.清洗到存储的全过程,可以极大减轻金融分析人员的工作量,下面我简单介绍 ...
- python做Linux进程运行,Python实现在Linux系统下更改当前进程运行用户
在上一篇文章中,我们讲了如何在linux上用python写一个守护进程.主要原理是利用linux的fork函数来创建一个进程,然后退出父进程运行,生成的子进程就会成为一个守护进程.细心观察的可能会发现 ...
最新文章
- R语言使用ggplot2包使用geom_violin函数绘制分组小提琴图(自定义边界调色板、brewer调色板、比例灰度)实战
- modelsim与debussy联调环境的搭建
- zip、gz压缩文件查看命令zless、less
- 【C++】 C++虚函数表详细分析(上)
- [APIO2013]机器人(DP+SPFA最短路)
- 力扣—— 79/212. 单词搜索
- iphonex适配游戏_Galaxy Fold应用适配大测试,这些软件超有远见!
- postgres 连接数查看与设置
- Android Handler.post(Runnable r)再一次梳理Android的消息机制(以及handler的内存泄露)
- 用什么工具可以制作gif?分享一款在线制作gif动画工具
- 电池充电器UL1310、启动电源UL2743、电脑风扇507测试报告怎么办理?
- 浏览器的使用方法,如何添加书签|常用网站|扩展程序?
- 产品经理听完《等你下课》心态崩了?选择汇新云重振旗鼓
- 配置 Android 的 SDK, DNK, JDK, ANT 环境
- error: #79: expected a type specifier
- 一些五笔不好打出来的字(转)-留作记念
- PG中XLOG日志结构
- 20190401每周精品之读书
- 【Python】批量修改照片日期
- Leetcode1407. 排名靠前的旅行者
热门文章
- 格林公式、高斯公式及斯托克斯公式的理解及相互关系
- python opencv批量修改图片分辨率
- 2021全国大学生信息安全竞赛初赛部分WP
- HTML+CSS大作业 (水果之家10个网页)
- android输入法剪切板历史记录,干货分享 讯飞输入法剪切板使用技巧知多少
- 编写单片机中断程序的注意事项 成都电气开发
- 信创办公--基于WPS的Word最佳实践系列(应用导航窗格:轻松掌握文章结构)
- php元万亿单位转换,万与亿的换算(万元换成亿元换算器)
- 树的深度优先和广度优先
- VMWARE虚拟机使用的是此版本 VMware Workstation 不支持的硬件版本。 模块“Upgrade”启动失败。 未能启动虚拟机。