# -*- coding: utf-8 -*- """ Created on Sat Jun 20 19:36:34 2015 @author: chaofn """ import os """ 這個程序的目的是將linux下/ifs/home/fanchao/Manesh_pdb目錄中的所有文件(一共有215個文件) 批處理 將pdb文件生成dssp文件 """ #listdir返回文件名的列表 fileLine=os.listdir('/ifs/home/fanchao/Manesh_pdb') #遍歷整個列表 for i in range(len(fileLine)-1): #將字符串用變量表示 input_file='/ifs/home/fanchao/Manesh_pdb/'+fileLine[i] #先去掉文件名的后綴,然后形成后綴為dssp的文件名 out_file=fileLine[i].split('.')[0]+'.dssp' output_file='/ifs/home/fanchao/Manesh_dssp/'+out_file #注意:參數的傳遞(先是%s,然后是%變量名),多個變量的傳入要用元組表示,在元組前用% os.system('/ifs/share/lib/dssp/dssp2 -i %s -o %s' %(input_file,output_file))
批量處理數據:從dssp文件夾中遍歷提取符合要求的數據,並寫入另一個文件夾:
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Sun Jun 21 13:03:19 2015 4 5 @author: chaofan 6 """ 7 8 import os 9 import re 10 #列出dssp文件夾中的所有文件名,返回的是一個列表 11 files=os.listdir('G:/Manesh_dssp') 12 #遍歷整個文件夾 13 for filename in files: 14 #將文件的名稱和擴展名分離 15 portion=os.path.splitext(filename) 16 #將每個文件后綴.dssp轉化成.fasta。以便訪問.fasta的文件 17 fastaFile=portion[0]+'.fasta' 18 #打開參數指定的fasta文件 19 fp=open('G:/Manesh_fasta/%s' %fastaFile) 20 #讀取文件的第一行 21 strLine=fp.readlines()[0] 22 #用正則式提取該行的一個字母 23 letter=re.search(':([A-Z])\|',strLine).group(1) 24 #打開參數指定的dssp文件 25 fr=open('G:/Manesh_dssp/%s' %filename) 26 #生成后綴名為txt的文件 27 txtFile=portion[0]+'.txt' 28 fw=open('G:/Manesh_ACC/%s' %txtFile,'w' ) 29 #從每個dssp文件的第28行開始讀取 30 for line in fr.readlines()[28:]: 31 lineList=[] 32 #如果第11個字符等於參數字符,則寫入 33 if line[11]==letter: 34 lineList.extend([line[7:10],line[11],line[13],line[35:38],'\n']) 35 #將列表轉化成字符串並寫入文件 36 fw.write(' '.join(lineList)) 37 #關閉流 38 fw.close() 39 fr.close() 40 fw.close()