美文网首页
python 简单脚本练习2

python 简单脚本练习2

作者: 生信小白2018 | 来源:发表于2019-04-24 11:20 被阅读0次

简单python脚本

更改名字(xxx.fa)

Gene.1::Chr01:378401-378884::g.1::m.1 type:5prime_partial len:102 gc:universal Chr01:378401-378884:484-179(-)
这一串,只留>Chr01:378401-378884

#!/usr/bin/evn python2
# -*- coding: utf-8 -*-
__author__='xxx'
import sys, os
def rename_forzwp3(infile, outfile):
        ini = open(infile, 'r')
        out = open(outfile, 'w')
        for i in ini:
                i = i.strip()
                if i.startswith('>'):
                        print >>out, ">"+i.split('::')[1] 
                else:
                        print >>out, i
if __name__=='__main__':
        infile=sys.argv[1]
        outfile=sys.argv[2]
        rename_forzwp3(infile,outfile)

更改名字:重命名seq0--seqN


#!/usr/bin/evn python3
# -*- coding: utf-8 -*-
__author__='xxx'

import sys, os

def rename_for_zwp(infile, outfile):
    ini = open(infile, 'r')
    out = open(outfile, 'w')
    j = 0
    for i in ini:
        i = i.strip()
        if i.startswith('>'):
            print('>seq'+str(j), file=out)
            j += 1
        else:
            print(i, file=out)
    

if __name__ == '__main__':
    infile =sys.argv[1]
    outfile = sys.argv[2]
    rename_for_zwp(infile, outfile)

split name

##splitname.py for python3
#!/usr/lib/python
#-*- coding:utf-8 --*-

filename="1.fa"
for line in open(filename,errors='ignore'):
    if line[0]=='>':
        lineL=line.split(':')
        print (lineL[0])
    else:
        print (line.strip())

oneline to multiline

#!/usr/bin/evn python3
# -*- coding: utf-8 -*-
__author__='xxx'

import sys, os
length = 80
def one_line_to_multiline(infile, outfile):
    ini = open(infile, 'r')
    out = open(outfile, 'w')

    for i in ini:
        if i[0]=='>':
#       if i.startswith('>'):
            print (i, file=out)
        else:
            len_seq=len(i)
            for j in range(0, len_seq, length): 
                print (i[j:j+length], file=out)

if __name__=='__main__':
    infile = sys.argv[1]
    outfile = sys.argv[2]
    one_line_to_multiline(infile, outfile)

multi_line_to_one_line

#!/usr/bin/evn python3
# -*- coding: utf-8 -*-
__author__='xxx'

import sys, os
aDict = {}
def multi_line_to_one_line(infile, outfile):
    ini = open(infile, 'r')
    out = open(outfile, 'w')
    for line in ini:
        if line.startswith('>'):
            key = line.strip()
            aDict[key] = []
        else:
            aDict[key].append(line.strip())
    for key, valueL in aDict.items():
        print (key, file=out)               ##for python3
        print (''.join(valueL), file=out)   ##for python3
#       print >> out, key                   ##for python2
#       print >> out, ''.join(valueL)       ##for python2

if __name__ == '__main__':
    infile =sys.argv[1]
    outfile = sys.argv[2]
    multi_line_to_one_line(infile, outfile)

format transform

#!/usr/bin/evn python3
# -*- coding: utf-8 -*-
__author__='xxx'

# reformt
#ma03Gene00005  
#ma03Gene00006  GO:0005575,GO:0005622,GO:0005623,GO:0005737,GO:0005768
#to
#ma03Gene00005  
#ma03Gene00006  GO:0005575
#ma03Gene00006  GO:0005622
#ma03Gene00006  GO:0005623
#ma03Gene00006  GO:0005737
#ma03Gene00006  GO:0005768


import sys,os

def go(infile,outfile):
    ini = open(infile, 'r')
    out = open(outfile, 'w')

    for i in ini:
        i=i.strip()
        jj = i.split('\t')
        if len(jj)<2:
            print(i,file=out)
        else:
            j=jj[1].split(',')
            for ii in j:
                print(jj[0]+ '\t' +ii, file=out)


if __name__=='__main__':
    infile = sys.argv[1]
    outfile = sys.argv[2]
    go(infile, outfile)

相关文章

网友评论

      本文标题:python 简单脚本练习2

      本文链接:https://www.haomeiwen.com/subject/stiwgqtx.html