Class: MakeBlastDb

Inherits:
Object
  • Object
show all
Defined in:
lib/seqtrimnext/classes/make_blast_db.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ MakeBlastDb

Returns a new instance of MakeBlastDb.



4
5
6
7
8
9
10
11
# File 'lib/seqtrimnext/classes/make_blast_db.rb', line 4

def initialize(dir)

  @db_folder = dir
  @status_folder = File.join(@db_folder,'status_info')
  @formatted_folder = File.join(@db_folder,'formatted')
  
  update_dbs
end

Class Method Details

.format_db(path_db, db_name, formatted_folder) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/seqtrimnext/classes/make_blast_db.rb', line 42

def self.format_db(path_db, db_name, formatted_folder)

    #hay que hacer el cat solo cuando cambian los ficheros que hay en subfolder1
    formatted_file = File.join(formatted_folder, db_name+'.fasta')
		cmd = "makeblastdb -in #{formatted_file} -parse_seqids -dbtype nucl >> logs/formatdb.log"
		system(cmd)
		$LOG.info(cmd)

end

Instance Method Details

#catFasta(path_start, path_end) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/seqtrimnext/classes/make_blast_db.rb', line 13

def catFasta(path_start,path_end)
  $LOG.debug("Cat of #{path_start}")
  
  # system("cat #{path_start} > #{path_end}")
  system("cat /dev/null > #{path_end}")
       
  system("for i in `find #{path_start} -type f ! -name '.*'`; do echo cat of $i; cat $i >> #{path_end}; echo \"\n\" >> #{path_end}; done")
  
end

#dirEmpty?(path_db) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
# File 'lib/seqtrimnext/classes/make_blast_db.rb', line 23

def dirEmpty?(path_db)

  folder2=Dir.open("#{path_db}")
  
  ignore = ['.','..','.DS_Store']
  
  res = folder2.entries - ignore
  
  return res.empty?  
end

#merge_db_files(path_db, db_name, formatted_folder) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/seqtrimnext/classes/make_blast_db.rb', line 34

def merge_db_files(path_db, db_name, formatted_folder)
if !dirEmpty?(path_db)
  #hay que hacer el cat solo cuando cambian los ficheros que hay en subfolder1
  formatted_file = File.join(formatted_folder, db_name+'.fasta')
  catFasta(File.join(path_db),formatted_file)
end
end

#update_dbsObject


Check if files for DataBase have been updated, and only when that has happened, makeblastdb will run

Consideres the next directories structure:

@dir is the main directory                               
@dir/folder0  is the directoy where will be storaged the DB created/updated
@dir/folder0/subfolder1 is where are storaged all the fasta files of the type subfolder1 
@dir/update is where register the log for each subfolder1, to check if DB has been updated



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/seqtrimnext/classes/make_blast_db.rb', line 61

def update_dbs

  FileUtils.mkdir_p(@status_folder)
  FileUtils.mkdir_p(@formatted_folder)
     
  ignore_folders=['.','..','status_info','formatted']

	$LOG.info("Checking Blast databases at #{@db_folder} for updates")
 
  dbs_folder=Dir.open(@db_folder)
  
  #if all file_update.entries is in folder1.entries then cat db/* > DB , make blast, guardar ls nuevo 
  dbs_folder.entries.each do |db_name|
  	
    db_folder=File.join(@db_folder,db_name)
		if (!ignore_folders.include?(db_name) and File.directory?(db_folder))
 			
 			#puts "Checking #{db_name} in #{db_folder}"
 	
     #path_db = File.join(@dir,db_folder)
			
			# set status files    
    	new_status_file = File.join(@status_folder,'new_'+db_name+'.txt')
    	old_status_file = File.join(@status_folder,'old_'+db_name+'.txt')
    
      cmd = "ls -lR #{db_folder} > #{new_status_file}"
      $LOG.debug(cmd)
      # list new status tu new_status_file
      # system("ls -lR #{File.join(db_folder,'*')} > #{new_status_file}")
      system(cmd)
      
      # if new and old statuses files changed, then reformat
      if (!(File.exists?(old_status_file)) || !system("diff -q #{new_status_file} #{old_status_file} > /dev/null ") || !File.exists?(File.join(@formatted_folder,db_name+'.fasta')))
  					
						$LOG.info("Database #{db_name} modified. Merging and formatting")
						
  					merge_db_files(db_folder,db_name,@formatted_folder)
  										    
            MakeBlastDb.format_db(db_folder,db_name,@formatted_folder)
            
            # rename new_status_file to replace the old one
            system("mv #{new_status_file} #{old_status_file}")
       end
    	
    end
    
  end #end folder1.entries
 
end