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"
29 COPYRIGHT = "2008-2013 Christian M Zmasek"
30 CONTACT = "phylosoft@gmail.com"
31 WWW = "https://sites.google.com/site/cmzmasek/home/software/forester"
32 HELP_OPTION_1 = 'help'
36 EXTRACT_LINKERS_OPTION = 'l'
37 LOG_SUFFIX = ".mse_log"
38 LINKERS_SUFFIX = ".linkers"
39 FASTA_SUFFIX = ".fasta"
40 FASTA_WITH_NORMALIZED_IDS_SUFFIX = ".ni.fasta"
41 NORMALIZED_IDS_MAP_SUFFIX = ".nim"
42 PROTEINS_LIST_FILE_SEPARATOR = "\t"
45 @file_to_msa = Hash.new
51 Util.print_program_information( PRG_NAME,
60 ld = Constants::LINE_DELIMITER
63 cla = CommandLineArguments.new( ARGV )
64 rescue ArgumentError => e
65 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
68 if ( cla.is_option_set?( HELP_OPTION_1 ) ||
69 cla.is_option_set?( HELP_OPTION_2 ) )
74 if ( cla.get_number_of_files != 4 && cla.get_number_of_files != 5 )
79 allowed_opts = Array.new
80 allowed_opts.push(EXT_OPTION)
81 allowed_opts.push(EXTRACT_LINKERS_OPTION)
83 disallowed = cla.validate_allowed_options_as_str( allowed_opts )
84 if ( disallowed.length > 0 )
85 Util.fatal_error( PRG_NAME,
86 "unknown option(s): " + disallowed,
90 seq_names_files_suffix = cla.get_file_name( 0 )
91 input_dir = cla.get_file_name( 1 )
92 out_dir = cla.get_file_name( 2 )
93 out_dir_doms = cla.get_file_name( 3 )
96 if ( cla.get_number_of_files == 5 )
97 mapping_file = cla.get_file_name( 4 )
99 Util.check_file_for_readability( mapping_file )
100 rescue ArgumentError => e
101 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
106 if cla.is_option_set?(EXT_OPTION)
107 extension = cla.get_option_value_as_int(EXT_OPTION)
113 extract_linkers = false
114 if cla.is_option_set?(EXTRACT_LINKERS_OPTION)
115 extract_linkers = true
118 if !File.exist?( input_dir )
119 Util.fatal_error( PRG_NAME, "error: input directory [#{input_dir}] does not exist" )
121 if !File.exist?( out_dir )
122 Util.fatal_error( PRG_NAME, "error: output directory [#{out_dir}] does not exist" )
124 if !File.exist?( out_dir_doms )
125 Util.fatal_error( PRG_NAME, "error: output directory [#{out_dir_doms}] does not exist" )
127 if !File.directory?( input_dir )
128 Util.fatal_error( PRG_NAME, "error: [#{input_dir}] is not a directory" )
130 if !File.directory?( out_dir )
131 Util.fatal_error( PRG_NAME, "error: [#{out_dir}] is not a directory" )
133 if !File.directory?( out_dir_doms )
134 Util.fatal_error( PRG_NAME, "error: [#{out_dir_doms}] is not a directory" )
140 log << "Program : " + PRG_NAME + ld
141 log << "Version : " + PRG_VERSION + ld
142 log << "Program date : " + PRG_DATE + ld
145 puts( "Sequence names files suffix: " + seq_names_files_suffix )
146 log << "Sequence names files suffix: " + seq_names_files_suffix + ld
147 puts( "Input dir : " + input_dir )
148 log << "Input dir : " + input_dir + ld
149 puts( "Output dir : " + out_dir )
150 log << "Output dir : " + out_dir + ld
151 puts( "Output dir domains : " + out_dir_doms )
152 log << "Output dir domains : " + out_dir_doms + ld
153 if ( mapping_file != nil )
154 puts( "Mapping file : " + mapping_file )
155 log << "Mapping file : " + mapping_file + ld
158 puts( "Extension : " + extension.to_s )
159 log << "Extension : " + extension.to_s + ld
162 puts( "Extract linkers : true" )
163 log << "Extract linkers : true" + ld
165 log << "Date : " + Time.now.to_s + ld
168 if ( mapping_file != nil )
169 species_codes_to_paths = extract_mappings( mapping_file )
172 input_files = obtain_inputfiles( input_dir, seq_names_files_suffix )
176 input_files.each { |input_file|
180 puts counter.to_s + "/" + input_files.size.to_s
181 read_seq_family_file( input_file,
182 seq_names_files_suffix,
184 species_codes_to_paths,
193 Util.print_message( PRG_NAME, "OK" )
199 def read_seq_family_file( input_file,
200 seq_names_files_suffix,
202 species_codes_to_paths,
211 Util.check_file_for_readability( input_file )
212 rescue ArgumentError => e
213 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
215 basename = File.basename( input_file, seq_names_files_suffix )
216 out_file_path_fasta_file = out_dir + Constants::FILE_SEPARATOR + basename + FASTA_SUFFIX
217 out_file_path_normalized_ids_fasta_file = out_dir + Constants::FILE_SEPARATOR + basename + FASTA_WITH_NORMALIZED_IDS_SUFFIX
218 out_file_path_ids_map = out_dir + Constants::FILE_SEPARATOR + basename + NORMALIZED_IDS_MAP_SUFFIX
219 doms_out_file_path_fasta_file = out_dir_doms + Constants::FILE_SEPARATOR + basename + "_domains" + FASTA_SUFFIX
220 doms_ext_out_file_path_fasta_file = nil
222 doms_ext_out_file_path_fasta_file = out_dir_doms + Constants::FILE_SEPARATOR + basename + "_domains_ext_" + extension.to_s + FASTA_SUFFIX
225 Util.check_file_for_writability( out_file_path_fasta_file )
226 Util.check_file_for_writability( out_file_path_normalized_ids_fasta_file )
227 Util.check_file_for_writability( out_file_path_ids_map )
228 Util.check_file_for_writability( doms_out_file_path_fasta_file )
229 rescue ArgumentError => e
230 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
235 ids_map_writer = File.open( out_file_path_ids_map, 'a' )
236 rescue Exception => e
237 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
243 new_msa_normalized_ids = Msa.new
244 new_msa_domains = Msa.new
245 new_msa_domains_extended = Msa.new
246 per_species_counter = 0
251 File.open( input_file ) do | file |
253 while line = file.gets
255 if !Util.is_string_empty?( line ) && !(line =~ /\s*#/ )
256 values = line.split( PROTEINS_LIST_FILE_SEPARATOR )
258 if ( values.length < 2 )
259 Util.fatal_error( PRG_NAME, "unexpected format: " + line )
261 species = values[ 0 ]
262 #if species == "BRADI" || species == "ASPNG" || species == "SCLSC" || species == "PTEVA" || species == "EIMTE"
265 seq_name = values[ 1 ]
267 if ( values.length > 3 )
268 domain_ranges_block = values[ 3 ]
269 domain_ranges = domain_ranges_block.split( "/" )
271 if ( species != current_species )
272 current_species = species
273 my_file = input_dir + Constants::FILE_SEPARATOR + current_species
275 if ( !File.exist?( my_file ) )
276 if species_codes_to_paths == nil
277 Util.fatal_error( PRG_NAME, "error: [#{my_file}] not found and no mapping file provided" )
278 elsif ( !species_codes_to_paths.has_key?( current_species ) )
279 Util.fatal_error( PRG_NAME, "error: species [#{current_species}] not found in mapping file [#{mapping_file}]" )
281 my_file = species_codes_to_paths[ current_species ]
283 my_path = File.expand_path( my_file )
284 my_readlink = my_path
285 if ( File.symlink?( my_path ) )
286 my_readlink = File.readlink( my_path )
288 current_msa = read_fasta_file( my_file )
290 if ( per_species_counter > 0 )
291 print_counts( per_species_counter, log, Constants::LINE_DELIMITER )
292 per_species_counter = 0
294 puts " " + species_counter.to_s + ":" + current_species + " [" + my_readlink + "]"
295 log << species_counter.to_s << ": " << current_species << " [" + my_readlink + "]" << Constants::LINE_DELIMITER
298 log << " " << seq_name << Constants::LINE_DELIMITER
299 per_species_counter = per_species_counter + 1
302 indices = current_msa.find_by_name_start( seq_name, true )
304 seq = current_msa.get_sequence( indices[ 0 ] )
305 elsif indices.size == 0
306 # Not found, try finding by partial match.
308 seq = current_msa.get_by_name( seq_name, true, true )
309 rescue ArgumentError => e
310 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
313 Util.fatal_error( PRG_NAME, "error: seq name \"" + seq_name + "\" not unique" )
316 normalized_id = per_species_counter.to_s( 16 ).upcase +
317 "_" + current_species
319 per_species_counter.to_i
321 ids_map_writer.write( normalized_id + "\t" + seq.get_name + Constants::LINE_DELIMITER )
325 orig_name = seq.get_name
326 seq.set_name( seq.get_name + " [" + current_species + "]" )
327 new_msa.add_sequence( seq )
329 Util.fatal_error( PRG_NAME, "unexected error: seq is nil" )
332 if domain_ranges != nil
336 domain_ranges.each { |range|
337 if range != nil && range.length > 0
341 new_msa_domains.add_sequence( Sequence.new( orig_name +
344 "] [" + basename + "] [" +
345 current_species + "]",
346 seq.get_sequence_as_string[from..to] ) )
348 from_e = from - extension
352 to_e = to + extension
353 if to_e > seq.get_sequence_as_string.length - 1
354 to_e = seq.get_sequence_as_string.length - 1
356 new_msa_domains_extended.add_sequence( Sequence.new( orig_name +
361 "] [" + basename + "] [" +
362 current_species + "]",
363 seq.get_sequence_as_string[ from_e..to_e ] ) )
373 mod_line = line + "\t[" + get_linker_sequence( f, t, seq ) + "|"
375 mod_line += get_linker_sequence( prev_to + 1, from - 1, seq ) + "|"
379 end # range != nil && range.length > 0
381 if extract_linkers && prev_to > 0
383 t = seq.get_sequence_as_string.length - 1
385 t = prev_to + extension
387 mod_line += get_linker_sequence( f, t, seq ) + "]"
391 new_msa_normalized_ids.add_sequence( Sequence.new( normalized_id, seq.get_sequence_as_string ) )
393 linkers << mod_line + Constants::LINE_DELIMITER
395 end # !Util.is_string_empty?( line ) && !(line =~ /\s*#/ )
396 end # while line = file.gets
402 if ( per_species_counter > 0 )
403 print_counts( per_species_counter, log, Constants::LINE_DELIMITER )
408 fasta_writer = FastaWriter.new()
409 fasta_writer.remove_gap_chars
413 io.write_to_file( new_msa, out_file_path_fasta_file, fasta_writer )
414 rescue Exception => e
415 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
418 if new_msa_domains != nil
420 io.write_to_file( new_msa_domains, doms_out_file_path_fasta_file, fasta_writer )
421 rescue Exception => e
422 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
426 if extension > 0 && new_msa_domains_extended != nil
428 io.write_to_file( new_msa_domains_extended, doms_ext_out_file_path_fasta_file, fasta_writer )
429 rescue Exception => e
430 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
435 io.write_to_file( new_msa_normalized_ids, out_file_path_normalized_ids_fasta_file, fasta_writer )
436 rescue Exception => e
437 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
441 f = File.open( out_dir + Constants::FILE_SEPARATOR + basename + LOG_SUFFIX , 'a' )
444 rescue Exception => e
445 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
448 if extract_linkers && linkers != nil
450 f = File.open( out_dir + Constants::FILE_SEPARATOR + basename + LINKERS_SUFFIX , 'a' )
453 rescue Exception => e
454 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
460 def get_linker_sequence( from, to, seq )
464 if to > seq.get_sequence_as_string.length - 1
465 to = seq.get_sequence_as_string.length - 1
470 return from.to_s + "-" + to.to_s + ":" + seq.get_sequence_as_string[ from..to ]
474 def obtain_inputfiles( input_dir, seq_names_files_suffix )
475 input_files = Array.new()
476 Dir.foreach( input_dir ) { |file_name|
477 if file_name.index( seq_names_files_suffix ) == ( file_name.size - seq_names_files_suffix.size )
478 input_files.push( input_dir + Constants::FILE_SEPARATOR + file_name )
484 def extract_mappings( mapping_file )
485 species_code_to_path = Hash.new()
486 File.open( mapping_file ) do | file |
487 while line = file.gets
488 if ( !Util.is_string_empty?( line ) && !(line =~ /\s*#/ ) )
489 if ( line =~ /(\S+)\s+(\S+)/ )
492 if ( species_code_to_path.has_key?( species ) )
493 Util.fatal_error( PRG_NAME, "error: species code [#{species}] is not unique" )
495 if ( species_code_to_path.has_value?( path ) )
496 Util.fatal_error( PRG_NAME, "error: path [#{path}] is not unique" )
498 if ( !File.exist?( path ) )
499 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not exist" )
501 if ( !File.file?( path ) )
502 Util.fatal_error( PRG_NAME, "error: [#{path}] is not a regular file" )
504 if ( !File.readable?( path ) )
505 Util.fatal_error( PRG_NAME, "error: file [#{path}] is not readable" )
507 if ( File.size( path ) < 10000 )
508 Util.fatal_error( PRG_NAME, "error: file [#{path}] appears too small" )
510 if ( !Util.looks_like_fasta?( path ) )
511 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not appear to be a fasta file" )
513 species_code_to_path[ species ] = path
514 puts species + " -> " + path
522 def print_counts( per_species_counter, log, ld )
523 puts " sum: " + per_species_counter.to_s
524 log << " sum: " + per_species_counter.to_s + ld
527 def read_fasta_file( input )
528 if @file_to_msa.has_key?( input )
529 return @file_to_msa[ input ]
535 msa = f.create_msa_from_file( input, FastaParser.new() )
536 rescue Exception => e
537 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
540 @file_to_msa[ input ] = msa
541 @seqs += msa.get_number_of_seqs
542 puts " total seqs in memory: " + @seqs.to_s
550 puts( " " + PRG_NAME + ".rb <sequence names file suffix> <input dir containing sequence names files " +
551 "and possibly genome multiple-sequence ('fasta') files> <output directory for sequences> <output directory for domains> [mapping file for " +
552 "genome multiple-sequence ('fasta') files not in input dir]" )
554 puts( " option: -" + EXT_OPTION + "=<int>: to extend extracted domains" )
555 puts( " -" + EXTRACT_LINKERS_OPTION + " : to extract linkers" )
557 puts( " " + "Example: \"mse.rb .prot . protein_seqs domain_seqs ../genome_locations.txt\"" )
561 end # class MultiSequenceExtractor