1、面向计算生物学的Python教程,马红武 中科院天津工生所,概览,Python基础知识 Python实例:文件处理和信息提取 Python实例:网络信息提取 Biopython: 从数据库获得信息 Biopython:序列处理及相似性分析 NetworkX: 生物网络分析 NumPython & Matplotlib:Python上的Matlab及作图 其它,Python基础知识,为什么用Python Python安装 Python基本数据结构,Why Use Python?,Python is object-oriented Its free (open source) Its portab
2、le (across platform) Its powerful (“battery included”, various packages for doing almost anything) Its mixable (Glue language, link with C etc) Its easy to use (interpreted language but fast performance) Its easy to learn (“pythonic” “code that is as understandable as plain English”, learn from exam
3、ples),安装python,http:/www.python.org 建议仍使用python 2.7 Windows下Activepython Linux(Ubuntu)自带python,可安装IDE软件进行编辑调试(Eric python IDE, SPE等) Debug功能一定要掌握好!,Pythonwin编辑器,代码编写区,交互窗口,调试工具栏,交互窗口使用,Examples: print Hello world Hello world # Relevant output is displayed on subsequent lines without the symbol x = 0
4、,1,2 # Quantities stored in memory are not displayed by default x #可用于查询正在调试的程序中的变量 # If a quantity is stored in memory, typing its name will display it 0,1,2 y=2,3 x+y 0,1,2,2,3 # 不确定操作符功能时可在交互窗口中试用 7/2 3 # 整数返回整数,试试7.0/2, 编程时需注意! import script # DO NOT add the .py suffix. Script is a module here #
5、 可用来查看一个模块是否安装正常,Operations on Numbers,Basic algebraic operations Four arithmetic operations: a+b, a-b, a*b, a/b Exponentiation: a*b Other elementary functions are not part of standard Python, but included in packages like NumPy and SciPy Comparison operators Greater than, less than, etc.: a b, a =
6、b Identity tests: a = b, a != b Bitwise operators Bitwise or: a | b Bitwise exclusive or: a b # Dont confuse this with exponentiation Bitwise and: a & b Shift a left or right by b bits: a b,数据结构,Lists (字符串可以当list 处理) a=1, s, 2,3, 5.5 a0 # start from 0 1 a-1 # from the last one 5.5 a1:3 s,2,3 a:2 a.a
7、ppend(3) 1, s, 2,3, 5.5, 3 a.sort() a.reverse() len(a)Turples t1 = (0,1,2,3)Sets Dictionary,语法结构: If For while,if x2 and x5: # if condition1 is true, execute action1action1 elif s in str: # if condition1 is not true, but condition2 is, execute action2 # action2 else: # if neither condition1 nor cond
8、ition2 is true, execute action3 # action3 完全通过缩进来确定执行那些操作,不用end if。for i in range(1,7): For x in l: while x 4:,定义函数,Usually, function definitions have the following basic structure: def func(args):return values Regardless of the arguments, (including the case of no arguments) a function call must en
9、d with parentheses. 一个函数可以只执行某些操作而不返回任何值,fname=bsu_mini_11_1.xmldef readsbml(fname):print time.clock() import libsbml as ls if _name_=“_main_“:readsbml(fname),好的程序代码编写习惯,模块调用,Modules may well contain submodules. Say we have a file named module.py which, in its definition, imports a submodule named s
10、ubmodule, which in turn contains some quantity named x. import module module.submodule.x We can also import the submodule without importing other quantities defined in module.py: from module import submodule submodule.x We would also call x this way if we had read in module.py with execfile(),安装外部模块
11、,直接从exe文件安装 从源代码:python setup.py install 利用安装工具如setuptools,文本文件信息提取,Myfolder=e:/computer/ fwm=open(myfolder+metnet.mod,w) mymodel=open(myfolder+model.txt) for s in mymodel.readlines(): r=s.strip(n).split(t) fwm.write(var +r0+;+n),输入文件 tab delimited ACALDt acalde acaldc -1000 1000 0 ACKr c : ac + atp
12、 actp + adp -1000 1000 0 ACONTa c : cit acon-C + h2o -1000 1000 0 ACONTb c : acon-C + h2o icit -1000 1000 0 ACt2r ace + he acc + hc -1000 1000 0 ADK1 c : amp + atp (2) adp -1000 1000 0,s = csv.reader(open(myfolder+“krea.txt“), delimiter=“t“) ls= ls.extend(s) #内容付给一list,亦可用csv工具包,网页信息提取,import urllib f = urllib.urlopen(http:/www.genome.jp/dbget-bin/www_bget?rn:r00001) data=f.read() data=f.readlines() #同本地文件处理,Html格式文件有专门的parser可以处理进行信息提取Htmllib, BeautifulSoup等,