2 # = lib/evo/apps/multi_sequence_extractor.rb - MultiSequenceExtractor class
4 # Copyright:: Copyright (C) 2006-2008 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # $Id: multi_sequence_extractor.rb,v 1.10 2010/12/13 19:00:11 cmzmasek Exp $
10 require 'lib/evo/util/constants'
11 require 'lib/evo/util/util'
12 require 'lib/evo/msa/msa'
13 require 'lib/evo/msa/msa_factory'
14 require 'lib/evo/io/msa_io'
15 require 'lib/evo/io/parser/fasta_parser'
16 require 'lib/evo/io/writer/fasta_writer'
17 require 'lib/evo/util/command_line_arguments'
23 class MultiSequenceExtractor
27 PRG_DESC = "extraction of sequences by name from multiple multi-sequence ('fasta') files"
28 PRG_DATE = "2008.08.13"
29 COPYRIGHT = "2008-2009 Christian M Zmasek"
30 CONTACT = "phylosoft@gmail.com"
31 WWW = "www.phylosoft.org"
32 HELP_OPTION_1 = 'help'
35 LOG_SUFFIX = ".mse_log"
36 FASTA_SUFFIX = ".fasta"
37 FASTA_WITH_NORMALIZED_IDS_SUFFIX = ".ni.fasta"
38 NORMALIZED_IDS_MAP_SUFFIX = ".nim"
39 PROTEINS_LIST_FILE_SEPARATOR = "\t"
43 Util.print_program_information( PRG_NAME,
52 ld = Constants::LINE_DELIMITER
55 cla = CommandLineArguments.new( ARGV )
56 rescue ArgumentError => e
57 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
60 if ( cla.is_option_set?( HELP_OPTION_1 ) ||
61 cla.is_option_set?( HELP_OPTION_2 ) )
66 if ( cla.get_number_of_files != 4 && cla.get_number_of_files != 5 )
71 allowed_opts = Array.new
73 disallowed = cla.validate_allowed_options_as_str( allowed_opts )
74 if ( disallowed.length > 0 )
75 Util.fatal_error( PRG_NAME,
76 "unknown option(s): " + disallowed,
80 seq_names_files_suffix = cla.get_file_name( 0 )
81 input_dir = cla.get_file_name( 1 )
82 out_dir = cla.get_file_name( 2 )
83 out_dir_doms = cla.get_file_name( 3 )
86 if ( cla.get_number_of_files == 5 )
87 mapping_file = cla.get_file_name( 4 )
89 Util.check_file_for_readability( mapping_file )
90 rescue ArgumentError => e
91 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
95 if !File.exist?( input_dir )
96 Util.fatal_error( PRG_NAME, "error: input directory [#{input_dir}] does not exist" )
98 if !File.exist?( out_dir )
99 Util.fatal_error( PRG_NAME, "error: output directory [#{out_dir}] does not exist" )
101 if !File.exist?( out_dir_doms )
102 Util.fatal_error( PRG_NAME, "error: output directory [#{out_dir_doms}] does not exist" )
104 if !File.directory?( input_dir )
105 Util.fatal_error( PRG_NAME, "error: [#{input_dir}] is not a directory" )
107 if !File.directory?( out_dir )
108 Util.fatal_error( PRG_NAME, "error: [#{out_dir}] is not a directory" )
110 if !File.directory?( out_dir_doms )
111 Util.fatal_error( PRG_NAME, "error: [#{out_dir_doms}] is not a directory" )
117 log << "Program : " + PRG_NAME + ld
118 log << "Version : " + PRG_VERSION + ld
119 log << "Program date : " + PRG_DATE + ld
122 puts( "Sequence names files suffix: " + seq_names_files_suffix )
123 log << "Sequence names files suffix: " + seq_names_files_suffix + ld
124 puts( "Input dir : " + input_dir )
125 log << "Input dir : " + input_dir + ld
126 puts( "Output dir : " + out_dir )
127 log << "Output dir : " + out_dir + ld
128 puts( "Output dir domains : " + out_dir_doms )
129 log << "Output dir domains : " + out_dir_doms + ld
130 if ( mapping_file != nil )
131 puts( "Mapping file : " + mapping_file )
132 log << "Mapping file : " + mapping_file + ld
134 log << "Date : " + Time.now.to_s + ld
137 if ( mapping_file != nil )
138 species_codes_to_paths = extract_mappings( mapping_file )
141 input_files = obtain_inputfiles( input_dir, seq_names_files_suffix )
145 input_files.each { |input_file|
149 puts counter.to_s + "/" + input_files.size.to_s
150 read_seq_family_file( input_file,
151 seq_names_files_suffix,
153 species_codes_to_paths,
160 Util.print_message( PRG_NAME, "OK" )
166 def read_seq_family_file( input_file,
167 seq_names_files_suffix,
169 species_codes_to_paths,
176 Util.check_file_for_readability( input_file )
177 rescue ArgumentError => e
178 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
180 basename = File.basename( input_file, seq_names_files_suffix )
181 out_file_path_fasta_file = out_dir + Constants::FILE_SEPARATOR + basename + FASTA_SUFFIX
182 out_file_path_normalized_ids_fasta_file = out_dir + Constants::FILE_SEPARATOR + basename + FASTA_WITH_NORMALIZED_IDS_SUFFIX
183 out_file_path_ids_map = out_dir + Constants::FILE_SEPARATOR + basename + NORMALIZED_IDS_MAP_SUFFIX
184 doms_out_file_path_fasta_file = out_dir_doms + Constants::FILE_SEPARATOR + basename + "_domains" + FASTA_SUFFIX
186 Util.check_file_for_writability( out_file_path_fasta_file )
187 Util.check_file_for_writability( out_file_path_normalized_ids_fasta_file )
188 Util.check_file_for_writability( out_file_path_ids_map )
189 Util.check_file_for_writability( doms_out_file_path_fasta_file )
190 rescue ArgumentError => e
191 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
196 ids_map_writer = File.open( out_file_path_ids_map, 'a' )
197 rescue Exception => e
198 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
204 new_msa_normalized_ids = Msa.new
205 new_msa_domains = Msa.new
206 per_species_counter = 0
210 File.open( input_file ) do | file |
211 while line = file.gets
213 if ( !Util.is_string_empty?( line ) && !(line =~ /\s*#/ ) )
214 values = line.split( PROTEINS_LIST_FILE_SEPARATOR )
216 if ( values.length < 2 )
217 Util.fatal_error( PRG_NAME, "unexpected format: " + line )
219 species = values[ 0 ]
220 if species == "BRADI" || species == "ASPNG" || species == "SCLSC" || species == "PTEVA" || species == "EIMTE"
223 seq_name = values[ 1 ]
225 if ( values.length > 3 )
226 domain_ranges_block = values[ 3 ]
227 domain_ranges = domain_ranges_block.split( "/" )
229 if ( species != current_species )
230 current_species = species
231 my_file = input_dir + Constants::FILE_SEPARATOR + current_species
233 if ( !File.exist?( my_file ) )
234 if species_codes_to_paths == nil
235 Util.fatal_error( PRG_NAME, "error: [#{my_file}] not found and no mapping file provided" )
236 elsif ( !species_codes_to_paths.has_key?( current_species ) )
237 Util.fatal_error( PRG_NAME, "error: species [#{current_species}] not found in mapping file [#{mapping_file}]" )
239 my_file = species_codes_to_paths[ current_species ]
241 my_path = File.expand_path( my_file )
242 my_readlink = my_path
243 if ( File.symlink?( my_path ) )
244 my_readlink = File.readlink( my_path )
246 current_msa = read_fasta_file( my_file )
248 if ( per_species_counter > 0 )
249 print_counts( per_species_counter, log, Constants::LINE_DELIMITER )
250 per_species_counter = 0
252 puts " " + current_species + " [" + my_readlink + "]"
253 log << current_species + " [" + my_readlink + "]" + Constants::LINE_DELIMITER
256 log << " " + seq_name + Constants::LINE_DELIMITER
257 per_species_counter = per_species_counter + 1
260 if current_msa.find_by_name_start( seq_name, true ).size > 0
262 seq = current_msa.get_by_name_start( seq_name, true ).copy
263 rescue ArgumentError => e
264 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
267 # Not found, try finding by partial match.
269 seq = current_msa.get_by_name( seq_name, true, true )
270 rescue ArgumentError => e
271 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
275 normalized_id = per_species_counter.to_s( 16 ).upcase +
276 "_" + current_species
278 per_species_counter.to_i
280 ids_map_writer.write( normalized_id + ": " + seq.get_name + Constants::LINE_DELIMITER )
284 orig_name = seq.get_name
285 seq.set_name( seq.get_name + " [" + current_species + "]" )
286 new_msa.add_sequence( seq )
288 Util.fatal_error( PRG_NAME, "unexected error: seq is nil" )
291 if domain_ranges != nil
292 domain_ranges.each { |range|
293 if range != nil && range.length > 0
297 new_msa_domains.add_sequence( Sequence.new( orig_name + "/" + from + "-" + to + " [" + basename + "] [" + current_species + "]", seq.get_sequence_as_string[from.to_i..to.to_i] ) )
302 new_msa_normalized_ids.add_sequence( Sequence.new( normalized_id, seq.get_sequence_as_string ) )
311 if ( per_species_counter > 0 )
312 print_counts( per_species_counter, log, Constants::LINE_DELIMITER )
317 fasta_writer = FastaWriter.new()
318 fasta_writer.remove_gap_chars
322 io.write_to_file( new_msa, out_file_path_fasta_file, fasta_writer )
323 rescue Exception => e
324 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
327 if new_msa_domains != nil
329 io.write_to_file( new_msa_domains, doms_out_file_path_fasta_file, fasta_writer )
330 rescue Exception => e
331 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
336 io.write_to_file( new_msa_normalized_ids, out_file_path_normalized_ids_fasta_file, fasta_writer )
337 rescue Exception => e
338 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
342 f = File.open( out_dir + Constants::FILE_SEPARATOR + basename + LOG_SUFFIX , 'a' )
345 rescue Exception => e
346 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
351 def obtain_inputfiles( input_dir, seq_names_files_suffix )
352 input_files = Array.new()
353 Dir.foreach( input_dir ) { |file_name|
354 if file_name.index( seq_names_files_suffix ) == ( file_name.size - seq_names_files_suffix.size )
355 input_files.push( input_dir + Constants::FILE_SEPARATOR + file_name )
361 def extract_mappings( mapping_file )
362 species_code_to_path = Hash.new()
363 File.open( mapping_file ) do | file |
364 while line = file.gets
365 if ( !Util.is_string_empty?( line ) && !(line =~ /\s*#/ ) )
366 if ( line =~ /(\S+)\s+(\S+)/ )
369 if ( species_code_to_path.has_key?( species ) )
370 Util.fatal_error( PRG_NAME, "error: species code [#{species}] is not unique" )
372 if ( species_code_to_path.has_value?( path ) )
373 Util.fatal_error( PRG_NAME, "error: path [#{path}] is not unique" )
375 if ( !File.exist?( path ) )
376 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not exist" )
378 if ( !File.file?( path ) )
379 Util.fatal_error( PRG_NAME, "error: [#{path}] is not a regular file" )
381 if ( !File.readable?( path ) )
382 Util.fatal_error( PRG_NAME, "error: file [#{path}] is not readable" )
384 if ( File.size( path ) < 10000 )
385 Util.fatal_error( PRG_NAME, "error: file [#{path}] appears too small" )
387 if ( !Util.looks_like_fasta?( path ) )
388 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not appear to be a fasta file" )
390 species_code_to_path[ species ] = path
391 puts species + " -> " + path
399 def print_counts( per_species_counter, log, ld )
400 puts " [sum: " + per_species_counter.to_s + "]"
401 log << " [sum: " + per_species_counter.to_s + "]" + ld
404 def read_fasta_file( input )
408 msa = f.create_msa_from_file( input, FastaParser.new() )
409 rescue Exception => e
410 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
418 puts( " " + PRG_NAME + ".rb <sequence names file suffix> <input dir containing sequence names files " +
419 "and possibly genome multiple-sequence ('fasta') files> <output directory for sequences> <output directory for domains> [mapping file for " +
420 "genome multiple-sequence ('fasta') files not in input dir]" )
422 puts( " " + "Example: \"mse.rb .prot . seqs doms ../genome_locations.txt\"" )
426 end # class MultiSequenceExtractor