利用Python【Orange】结合DNA序列进行人种预测

http://blog.csdn.net/jj12345jj198999/article/details/8951120

coursera上 web intelligence and big data 终于布置了HW7,这一次的要求是对一系列DNA序列进行预测,具体说明如下:

Data Analytics Assignment (for HW7)

Predict the Ethnicity of Individuals from their Genes

============================================

It is now possible to get the DNA sequence of an
individual at a reasonable cost.  An individual‘s genetic make-up
determines a number

of charactersistics - eye colour, propensity for certain
diseases, response to treatment and so on.  In this problem, you are
given a subset of genetic information for several individuals. For some
of the individuals you are also told their ethinicity.
 Your task is to figure out the ethnicity of the other individuals.

The information provided is as follows:

1. For each individual the presence (1) or absence (0) of a
 genetic variation at a particular position on chromosome 6
is provided.  In some cases, information for an individual at a
particular position is not available and this represented as
? (missing).

2. Information is provided for approximately 204000 positions. These are   your features.

3. The training set has data for 139 individuals along with their ethnicity.

4. The test (prediction) set has data for 11 individuals.
 You have to predict the ethnicity for these individuals and enter your
answers via HW7.

Data Sets

-----------

The training set is available here: genestrain.tab.zip (6.2
Mb)

The test set is available here: genesblind.tab.zip (1.2
Mb)

File Format

-----------

(Note: Data sets are .tab files in the tab-separated format that can be read into Orange):

Both the training and test data files have a header line
which is a tab-separated line of column/feature names: For example
‘6_10000005‘ indicates that the column describes the presence or absence
of variations at position 10000005 on chromosome
#6.

Entries in the second header line indicate the type of column (in this case all features are ‘discrete‘).

Entries in the third header line indicate the nature of each column:

A ‘ ‘ for most columns that contain a feature, and ‘class‘ for the first
column as it contains the actual class labels (i.e., ethnicities of the
individuals in each row).

These header lines are followed by lines containing feature values (0, 1, or ?) for each genetic feature of an individual.

In the training set file the first column, which denotes the class
label, is a three-letter code with one of the following values:

o CEU is Northern and Western European

o GIH is Gujarati Indian from Houston

o JPT is Japanese in Tokyo

o ASW is Americans of African Ancestry

o YRI is Yoruba in Ibadan, Nigera

In the test file the ethnicity column also exists but is blank.

=========================

For the purposes of your HW answer alone, each three letter code is to be marked with a NUMERIC VALUE  as indicated in the table below:

o CEU is Northern and Western European - 0

o GIH is Gujarati Indian from Houston - 1

o JPT is Japanese in Tokyo - 2

o ASW is Americans of African Ancestry - 3

o YRI is Yoruba in Ibadan, Nigera - 4

YOU MUST USE THE ABOVE NUMERIC VALUES TO ENCODE YOUR ANSWER. Note: This
numeric value has no presence in the test or training data.

Task: For each of the individuals in
the test file, predict their ethnicity as CEU, GIH, JPT, ASW or YRI and
enter your answers in HW7 in exactly the order that the 11 individuals appear in the test file.
So, for example, if your prediction is CEU, GIH, JPT, ASW, YRI CEU, GIH, JPT, ASW, YRI, CEU, you should enter your answer as 0 1 2 3 4 0 1 2 3 4 0 (i.e. numbers separated
by a space - no commas, tabs or anything else, just as space between single digit numbers).

不过很多人在discussion form里面反映着印度老师在描述的时候没有把问题讲明白(主要是没告诉他们该怎么做),也没在video里面给个指导视频啥的。好在把数据下下来以后,发现其中有一个训练集,一个预测集,估计也只能是先训练,再预测而已。

训练集是一个tab文件,格式如下:

横坐标class代表人种(这里有139行,代表139个训练数据),纵坐标代表DNA片段(约有20万个,后面n列未列出)

预测集如下:

这里第一列加 问号 的就是要预测的,总共为11个人种信息。

了解完数据的情况后,下一步就是看如何来训练和预测了,discussion form中有人提出了用Orange这个库,基友Python,使用起来很方便,地址是 http://orange.biolab.si/doc/ofb/c_basics.htm ,更详细的可以看 http://orange.biolab.si/docs/latest/tutorial/rst/classification/

针对这个问题,贝叶斯分类器就能搞定了,代码很短如下:

[python] view plain copy print?

  1. # Description: Read data, build naive Bayesian classifier and classify first few instances
  2. # Category:    modelling
  3. # Uses:        genestrain.tab
  4. # Predict:     genesblind.tab
  5. # Referenced:  c_basics.htm
  6. import orange
  7. data = orange.ExampleTable("genestrain")
  8. data2= orange.ExampleTable("genesblind")
  9. classifier = orange.BayesLearner(data)
  10. i = 0
  11. for item in data2:
  12. c = classifier(item)
  13. print "%d: %s " % (i, c)
  14. i = i + 1

可以看到这里先用训练数据进行训练,得到分类器,然后用分类器对预测数据的每一行进行预测,输出结果,思想还是比较清晰的,不过唯一的缺点是在数据量稍大一点时,运行速度和消耗资源很大,针对这题要使用1G内存,运行10分钟:

最终输出结果如下:

这样就得到了有待预测的11个人种,填写答案搞定。

估计这是这门课最后一次编程作业了,还剩一个在线的final exam,赶紧结课吧。

时间: 2024-07-29 12:17:09

利用Python【Orange】结合DNA序列进行人种预测的相关文章

利用Needleman–Wunsch算法进行DNA序列全局比对

生物信息学原理作业第二弹:利用Needleman–Wunsch算法进行DNA序列全局比对. 具体原理:https://en.wikipedia.org/wiki/Needleman%E2%80%93Wunsch_algorithm. 贴上python代码: 1 # -*- coding: utf-8 -*- 2 """ 3 Created on Sat Nov 25 18:20:01 2017 4 5 @author: zxzhu 6 后需修改: 7 1.加命令行参数 8 2

python实现DNA序列字符串转换,互补链,反向链,反向互补链

在生物信息学分析中,经常对DNA序列进行一系列操作,包括子序列截取,互补序列获取,反向序列获取,反向互补序列获取.在python语言中,可编写如下函数完成这些简单功能. 子序列截取 python中对序列截取使用字符串切片功能就可以完成,例如: >>> seq="ATGATATAGtatatatgCAAGAGg" >>> subseq = seq[1:6] >>> subseq "TGATA" 注意,切片操作是“0

DNA序列局部比对(Smith–Waterman algorithm)

生物信息原理作业第三弹:DNA序列局部比对,利用Smith–Waterman算法,python3.6代码实现. 实例以及原理均来自https://en.wikipedia.org/wiki/Smith%E2%80%93Waterman_algorithm. 1 import numpy as np 2 import pandas as pd 3 sequence1 = 'TGTTACGG' 4 sequence2 = 'GGTTGACTA' 5 s1 = '' 6 s2 = '' 7 gap =

利用 Python yield 创建协程将异步编程同步化

在 Lua 和 Python 等脚本语言中,经常提到一个概念: 协程.也经常会有同学对协程的概念及其作用比较疑惑,本文今天就来探讨下协程的前世今生. 首先回答一个大家最关心的问题:协程的好处是什么? 通俗易懂的回答: 让原来要使用 异步 + 回调 方式写的非人类代码,可以用看似同步的方式写出来. 1.回顾同步与异步编程 同步编程即线性化编程,代码按照既定顺序执行,上一条语句执行完才会执行下一条,否则就一直等在那里. 但是许多实际操作都是CPU 密集型任务和 IO 密集型任务,比如网络请求,此时不

利用python实现二分法

利用python实现二分法:我的实现思路如下 1.判断要查找的值是否大于最大值,如果大于则直接返回False 2.判断要查找的值是否小于最小值,如果小于则直接返回False 3.如果要查找的值在最大值和最小值之间,则进入循环 a.首先序列的长度要大于1,然后获取序列中间一个值的大小 b.然后和要查找的值做比较,如果相等,则直接返回True,如果不相等,则判断如果中间的值大于要查找的值,则说明要查找的值在该中间值的左边,如果中间的值小于要查找的值,则说明要查找的值在中间的值的右边 c.最后如果序列

环状DNA序列

大意: 一个DNA序列是环状的,这意味着有N个碱基的序列有N种表示方法(假设无重复).而这N个序列有一种最小的表示,这个最小表示的意思是这个序列的字典序最小(字典序的意思是在字典中的大小 比如ABC<ACB,B<BCD,EF<G) 方法:在一个序列中从任意两个位置开始,产生的序列的大小是可以比较的.然后利用这种比较方法找出最小值 #include <iostream> using namespace std; #define MAX 105 int lessthan(char

利用 Python 进行数据分析(四)NumPy 基础:ndarray 简单介绍

一.NumPy 是什么 NumPy 是 Python 科学计算的基础包,它专为进行严格的数字处理而产生.在之前的随笔里已有更加详细的介绍,这里不再赘述. 利用 Python 进行数据分析(一)简单介绍 二.ndarray 是什么 ndarray 是一个多维的数组对象,具有矢量算术运算能力和复杂的广播能力,并具有执行速度快和节省空间的特点. ndarray 的一个特点是同构:即其中所有元素的类型必须相同. 三.ndarray 的创建 array() 函数 最简单的方法, 使用 NumPy 提供的

利用python爬下段子网站的搞笑段子

众所周知,python是写爬虫的利器,今天作者用python写一个小爬虫爬下一个段子网站的众多段子. 目标段子网站为“http://ishuo.cn/”,我们先分析其下段子的所在子页的url特点,可以轻易发现发现为“http://ishuo.cn/subject/”+数字, 经过测试发现,该网站的反扒机制薄弱,可以轻易地爬遍其所有站点. 现在利用python的re及urllib库将其所有段子扒下 import sys import re import urllib #返回html格式 def g

Python 数据分析(二 本实验将学习利用 Python 数据聚合与分组运算,时间序列,金融与经济数据应用等相关知识

Python 数据分析(二) 本实验将学习利用 Python 数据聚合与分组运算,时间序列,金融与经济数据应用等相关知识 第1节 groupby 技术 第2节 数据聚合 第3节 分组级运算和转换 第4节 透视表和交叉表 第5节 时间序列 第6节 日期的规范.频率以及移动 第7节 时区处理 第8节 时期及算术运算 第9节 重采样及频率转换 第10节 时间序列绘图 groupby 技术 一.实验简介 Python 数据分析(二)需要同学们先行学完 Python 数据分析(一)的课程. 对数据集进行分