2011年12月30日星期五
ID To Namelist
ID To Namelist
import os
import time, sys
from Bio import SeqIO
filename=raw_input("Pls enter the name of the output txt file of Blastall.exe: ")
Approach=raw_input("Pls enter the the approach of your Datebase: ")
# Timing start
start=time.time()
wrerr = sys.stderr.write
# Create a file named 'Results' in current dir
if os.path.exists('Results'):
for root, dirs, files in os.walk('Results'):
for name in files:
os.remove(os.path.join(root,name))
else:
os.mkdir('Results')
f=open(filename,'r')
f1=open('Results//ID_datebase.txt','w')
a={}
b=[]
# Search every dirs and files at F:\python missions\YY\Datebase
for root,dirs,files in os.walk(Approach):
for file in files:
for record in SeqIO.parse(os.path.join(root, file), 'fasta'):
b.append(record.id)
a[os.path.join(root, file).split('\\')[-2]+'-'+os.path.join(root, file).split('\\')[-1]]=b
b=[]
# Output ID_datebase
f1.write('Namelist'+'\t'+'Corresponding ID'+'\n')
for key in sorted(a.keys()):
f1.write(str(key)+'\t'+str(a[key])+'\n')
# Name the IDs
num=0
for line in f:
num+=1
for key in a.keys():
if line.split('\t')[1].strip() in str(a[key]):
f2=open('Results\\'+key+'.txt','a')
f2.write(str(line).strip()+'\t'+key+'\t'+'\n')
if num%100000==0:
print str(num)+" sequences have been processed!"
end=time.time()
wrerr("OK, All Work Finished in %3.2f secs\n" % (end-start))
raw_input("Press <Enter> to close this window: ")
2011年11月10日星期四
用Python的split输出excel文件
Here’s an example of some data where the dates not formatted well for easy import into Excel:
The first column has the day and month separated by a space. The second column is year-day, which we’ll ignore. The third column has the time. The data we’re interested in is in the 9th column (temperature). The goal is to have a simple Excel file where the first column is date, and the second column is temperature.
|
利用list(set())和sorted处理列表或字典
1.利用list(set(a))来删除列表a或者字典中重复的元素。
2.利用sorted 来排序字典a。排序原则:将列表b中元素按照其在字典a中对应值的大小来排序。Reference: http://wiki.python.org/moin/HowTo/Sorting/
>>> a={'a': '3', 'c': '2', 'e': '1', 'd': '0'}
>>> b=['a','e']
>>> sorted(b,key=a.__getitem__)
['e', 'a']
特殊地:
对字典a的 健值列表(a.keys()) 按其对应值的大小进行排序,结果输出到列表中
>>> a={'a':'3','c':'2','d':'0','e':'1'}
>>> sorted(a.keys(),key=a.__getitem__)
['d', 'e', 'c', 'a']
>>> a=['2','2','4','3','4','0','1','1']
>>> list(set(a))
['1', '0', '3', '2', '4']
2.利用sorted 来排序字典a。排序原则:将列表b中元素按照其在字典a中对应值的大小来排序。Reference: http://wiki.python.org/moin/HowTo/Sorting/
>>> a={'a': '3', 'c': '2', 'e': '1', 'd': '0'}
>>> b=['a','e']
>>> sorted(b,key=a.__getitem__)
['e', 'a']
特殊地:
对字典a的 健值列表(a.keys()) 按其对应值的大小进行排序,结果输出到列表中
>>> a={'a':'3','c':'2','d':'0','e':'1'}
>>> sorted(a.keys(),key=a.__getitem__)
['d', 'e', 'c', 'a']
订阅:
博文 (Atom)