Python&Perl处理fasta文件对比

前言:

生物信息国内乃至世界范围内使用的脚本语言毫无疑问是Perl,以BGI为代表,Perl确实简单易用,几行代码或命令行便能很轻松解决工作需要;但,有市场就有竞争。

概述

Python 虽然和Perl差不多时间出现,但Python真正用于生信分析还是比Perl 稍稍晚那么一点的。有差异就会有对比,初学者会问生信perl还是python ?  这样的问题前一两年有很多人问,请看我在某度上的搜索:

自我感觉:

这个问题或多或少困扰着生信入门者,当然也包括我了,由于种种原因我刚开始接触的是Perl ,但后来发现Python实在太火了,无所不能啊,所以又自学了Python;

后来发现对于序列的处理(不用第三方包或模块)Perl速度确实比Python快,但有些特性上比如数据的处理,可视化这些Python又几乎可以干R语言的事情,Python确实由于Perl ,二者很难说哪个更好,我觉得是相互补充吧?

如果入门根据自己自身和语言特点随便选一个入门即可,后续有时间可以两个都学学,不影响的。

下面展示一个常见的例子:

题目:从一个fasta文件中抽取几条序列?

Python方法一:

#!/usr/bin/python
import sys
def Fasta(inputfile) :
    f = open(inputfile,"r")
    fastadic = {}
    while True :
        line = f.readline().rstrip()
        if len(line) == 0 :
            break
        else :
            pass
        if line.startswith(">") :
            name = line.split()[0]
            fastadic[name] = ""
        else :
            fastadic[name]+=line
    f.close()
    return fastadic


def usage():
    print ("1,fasta\n2,list")

if len(sys.argv) != 3 :
    usage()
    sys.exit()

#print (sys.argv[0])
fastafile = sys.argv[1]
listfile = sys.argv[2]

dic = Fasta(fastafile)
ff=open(listfile,"r")
while 1:
    line1 = ff.readline().rstrip()
    if len(line1) == 0 :
        break
    else :
        pass
    array = line1.split("\t")
    if ">"+array[0] in dic.keys() :
        print (">"+array[0]+"\n"+dic[">"+array[0]])
    else :
        print ("Error:"+line1)
ff.close()

Python方法二:

#!/usr/bin/python
import sys
def Fasta(inputfile) :
    f = open(inputfile,"r")
    fastadic = {}
    while True :
        line = f.readline().rstrip()
        if len(line) == 0 :
            break
        else :
            pass
        if line.startswith(">") :
            name = line.split()[0]
            fastadic[name] = []
        else :
            fastadic[name].append(line)
    f.close()
    return fastadic


def usage():
    print ("1,fasta\n2,list")

if len(sys.argv) != 3 :
    usage()
    sys.exit()

#print (sys.argv[0])
fastafile = sys.argv[1]
listfile = sys.argv[2]

dic = Fasta(fastafile)
ff=open(listfile,"r")
while 1:
    line1 = ff.readline().rstrip()
    if len(line1) == 0 :
        break
    else :
        pass
    array = line1.split("\t")
    if ">"+array[0] in dic.keys() :
        print (">"+array[0]+"\n"+"".join(dic[">"+array[0]]))
    else :
        print ("Error:"+line1)
ff.close()

Perl 方法三:

#!/usr/bin/perl
die "perl $0 <inputfastafile> <list> \n" if @ARGV != 2 ;
use strict;
use warnings;
my %fasta ;
my @name;
open I,$ARGV[0] ;
while (<I>) {
       chomp;
       if (/^>/)  {
            @name = split/\s+/,$_;
           $fasta{$name[0]} = "" ;
            }
           #if ($_ !~/^>/ ) {
           else {
            $fasta{$name[0]}.= $_ ;
           # }
   }
}
close I;



open T,$ARGV[1] ; 
while (<T>) {
    chomp; 
    my @tax = split/\t/ ,$_ ;
    if (exists $fasta{">".$tax[0]}) {
         print ">".$tax[0],"\n",$fasta{">".$tax[0]}, "\n" ;
      }
}

close T;

速度上是方法一 < 方法二 < 方法三 

Perl 是最快的,个中原理有兴趣的可以去网上搜答案

#######################################################################################

若喜欢,请赞赏或点赞分享,赠人玫瑰手有余香!

 

 

posted @ 2018-09-13 09:50  凤舞琦天  阅读(919)  评论(1)    收藏  举报
https://files.cnblogs.com/files/liuguoqi/weinxin.bmp