2017-02-03 19 views
1

pythonを使用してディレクトリ内のファイルの名前を変更しようとしています。ファイルには現在、プール番号、AR番号、S番号(例:Pool1_AR001_S13__fw_paired.fastq.gz)のラベルが付けられています。各ファイルには特定のプラントシーケンス名が含まれています。これらのファイルの名前を変更するには、 'Pool_AR_S'を削除してシーケンス名に置き換えます。 'Lbienne_dor5_GS1'は接尾辞(例:fw_paired.fastq.gz、rv_unpaired.fastq.gz)を残していますが、ファイルを辞書に読み込もうとしていますが、次に何をすべきかについては固執しています。私は、次の形式で必要な情報を含む.txtファイルを持っている:pythonを使用してディレクトリ内のファイル名の一部を置換する

Pool1_AR010_S17 - Lbienne_lla10_GS2 
Pool1_AR011_S18 - Lbienne_lla10_GS3 
Pool1_AR020_S19 - Lcampanulatum_borau4_T_GS1 

私がこれまで持っているコードは次のとおりです。

from optparse import OptionParser 
import csv 
import os 

parser = OptionParser() 
parser.add_option("-w", "--wanted", dest="w") 
parser.add_option("-t","--trimmed", dest="t") 
parser.add_option("-d", "--directory", dest="working_dir", default="./") 
(options, args) = parser.parse_args() 

wanted_file = options.w 
trimmomatic_output = options.t 

#Read the wanted file and create a dictionary of index vs species identity 

with open(wanted_file, 'rb') as species_sequence: 
    species_list = list(csv.DictReader(species_sequence, delimiter='-')) 
    print species_list 


#Rename the Trimmomatic Output files according to the dictionary 


for trimmed_sequence in os.listdir(trimmomatic_output): 
os.rename(os.path.join(trimmomatic_output, trimmed_sequence), 
      os.path.join(trimmomatic_output, trimmed_sequence.replace(species_list[0], species_list[1])) 

あなたはの半分を置き換えるために私を助けることができるしてください。私は非常にPythonとオーバーフローをスタックするので、この質問が以前に尋ねられた場合、または私は間違った場所でこれを尋ねた場合はごめんなさい申し訳ありません。

答えて

1

最初の仕事は、これらすべてのモジュールを取り除くことです。彼らはいいかもしれませんが、あなたのような仕事のために、彼らは物事をより簡単にすることはほとんどありません。

.gzファイルが存在するディレクトリに.pyファイルを作成します。

import os 
files = os.listdir() #files is of list type 
#'txt_file' is the path of your .txt file containing those conversions 
dic=parse_txt(txt_file) #omitted the body of parse_txt() func.Should return a dictionary by parsing that .txt file 
for f in files: 
    pre,suf=f.split('__') #"Pool1_AR001_S13__(1)fw_paired.fastq.gz" 
          #(1)=assuming prefix and suffix are divided by double underscore 
    pre = dic[pre] 
    os.rename(f,pre+'__'+suf) 

parse_txt()関数のヘルプが必要な場合は、お知らせください。

0

ここに私がPython 2でテストした解決策があります。get_mappings関数の代わりに独自のロジックを使用すれば問題ありません。説明のためにコード内のコメントを参照してください。



    import os 

    def get_mappings(): 
     mappings_dict = {} 
     with(open('wanted_file.txt', 'r')) as f: 
      for line in f: 
       # if you have Pool1_AR010_S17 - Lbienne_lla10_GS2 
       # it becomes a list i.e ['Pool1_AR010_S17 ', ' Lbienne_lla10_GS2'] 
       #note that there may be spaces before/after the names as shown above 
       text = line.split('-') 
       #trim is used to remove spaces in the names 
       mappings_dict[text[0].strip()] = text[1].strip() 

     return mappings_dict 

    #PROGRAM EXECUTION STARTS FROM HERE 
    #assuming all files are in the current directory 
    # if not replace the dot(.) with the path of the directory where you have the files 
    files = os.listdir('.') 
    wanted_names_dict = get_mappings() 
    for filename in files: 
     try: 
      #prefix='Pool1_AR010_S17', suffix='fw_paired.fastq.gz' 
      prefix, suffix = filename.split('__') 
      new_filename = wanted_names_dict[prefix] + '__' + suffix 
      os.rename(filename, new_filename) 
      print 'renamed', filename, 'to', new_filename 
     except: 
      print 'No new name defined for file:' + filename 

関連する問題