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
299 log << " " + seq_name + Constants::LINE_DELIMITER
300 per_species_counter = per_species_counter + 1
303 indices = current_msa.find_by_name_start( seq_name, true )
305 seq = current_msa.get_sequence( indices[ 0 ] )
306 elsif indices.size == 0
307 # Not found, try finding by partial match.
309 seq = current_msa.get_by_name( seq_name, true, true )
310 rescue ArgumentError => e
311 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
314 Util.fatal_error( PRG_NAME, "error: seq name \"" + seq_name + "\" not unique" )
317 # if current_msa.find_by_name_start( seq_name, true ).size > 0
319 # seq = current_msa.get_by_name_start( seq_name, true ).copy
320 # rescue ArgumentError => e
321 # Util.fatal_error( PRG_NAME, "error: " + e.to_s )
324 # # Not found, try finding by partial match.
326 # seq = current_msa.get_by_name( seq_name, true, true )
327 # rescue ArgumentError => e
328 # Util.fatal_error( PRG_NAME, "error: " + e.to_s )
332 normalized_id = per_species_counter.to_s( 16 ).upcase +
333 "_" + current_species
335 per_species_counter.to_i
337 ids_map_writer.write( normalized_id + "\t" + seq.get_name + Constants::LINE_DELIMITER )
341 orig_name = seq.get_name
342 seq.set_name( seq.get_name + " [" + current_species + "]" )
343 new_msa.add_sequence( seq )
345 Util.fatal_error( PRG_NAME, "unexected error: seq is nil" )
348 if domain_ranges != nil
352 domain_ranges.each { |range|
353 if range != nil && range.length > 0
357 new_msa_domains.add_sequence( Sequence.new( orig_name +
360 "] [" + basename + "] [" +
361 current_species + "]",
362 seq.get_sequence_as_string[from..to] ) )
364 from_e = from - extension
368 to_e = to + extension
369 if to_e > seq.get_sequence_as_string.length - 1
370 to_e = seq.get_sequence_as_string.length - 1
372 new_msa_domains_extended.add_sequence( Sequence.new( orig_name +
377 "] [" + basename + "] [" +
378 current_species + "]",
379 seq.get_sequence_as_string[ from_e..to_e ] ) )
389 mod_line = line + "\t[" + get_linker_sequence( f, t, seq ) + "|"
391 mod_line += get_linker_sequence( prev_to + 1, from - 1, seq ) + "|"
395 end # range != nil && range.length > 0
397 if extract_linkers && prev_to > 0
399 t = seq.get_sequence_as_string.length - 1
401 t = prev_to + extension
403 mod_line += get_linker_sequence( f, t, seq ) + "]"
407 new_msa_normalized_ids.add_sequence( Sequence.new( normalized_id, seq.get_sequence_as_string ) )
409 linkers << mod_line + Constants::LINE_DELIMITER
411 end # !Util.is_string_empty?( line ) && !(line =~ /\s*#/ )
412 end # while line = file.gets
418 if ( per_species_counter > 0 )
419 print_counts( per_species_counter, log, Constants::LINE_DELIMITER )
424 fasta_writer = FastaWriter.new()
425 fasta_writer.remove_gap_chars
429 io.write_to_file( new_msa, out_file_path_fasta_file, fasta_writer )
430 rescue Exception => e
431 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
434 if new_msa_domains != nil
436 io.write_to_file( new_msa_domains, doms_out_file_path_fasta_file, fasta_writer )
437 rescue Exception => e
438 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
442 if extension > 0 && new_msa_domains_extended != nil
444 io.write_to_file( new_msa_domains_extended, doms_ext_out_file_path_fasta_file, fasta_writer )
445 rescue Exception => e
446 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
451 io.write_to_file( new_msa_normalized_ids, out_file_path_normalized_ids_fasta_file, fasta_writer )
452 rescue Exception => e
453 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
457 f = File.open( out_dir + Constants::FILE_SEPARATOR + basename + LOG_SUFFIX , 'a' )
460 rescue Exception => e
461 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
464 if extract_linkers && linkers != nil
466 f = File.open( out_dir + Constants::FILE_SEPARATOR + basename + LINKERS_SUFFIX , 'a' )
469 rescue Exception => e
470 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
476 def get_linker_sequence( from, to, seq )
480 if to > seq.get_sequence_as_string.length - 1
481 to = seq.get_sequence_as_string.length - 1
486 return from.to_s + "-" + to.to_s + ":" + seq.get_sequence_as_string[ from..to ]
490 def obtain_inputfiles( input_dir, seq_names_files_suffix )
491 input_files = Array.new()
492 Dir.foreach( input_dir ) { |file_name|
493 if file_name.index( seq_names_files_suffix ) == ( file_name.size - seq_names_files_suffix.size )
494 input_files.push( input_dir + Constants::FILE_SEPARATOR + file_name )
500 def extract_mappings( mapping_file )
501 species_code_to_path = Hash.new()
502 File.open( mapping_file ) do | file |
503 while line = file.gets
504 if ( !Util.is_string_empty?( line ) && !(line =~ /\s*#/ ) )
505 if ( line =~ /(\S+)\s+(\S+)/ )
508 if ( species_code_to_path.has_key?( species ) )
509 Util.fatal_error( PRG_NAME, "error: species code [#{species}] is not unique" )
511 if ( species_code_to_path.has_value?( path ) )
512 Util.fatal_error( PRG_NAME, "error: path [#{path}] is not unique" )
514 if ( !File.exist?( path ) )
515 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not exist" )
517 if ( !File.file?( path ) )
518 Util.fatal_error( PRG_NAME, "error: [#{path}] is not a regular file" )
520 if ( !File.readable?( path ) )
521 Util.fatal_error( PRG_NAME, "error: file [#{path}] is not readable" )
523 if ( File.size( path ) < 10000 )
524 Util.fatal_error( PRG_NAME, "error: file [#{path}] appears too small" )
526 if ( !Util.looks_like_fasta?( path ) )
527 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not appear to be a fasta file" )
529 species_code_to_path[ species ] = path
530 puts species + " -> " + path
538 def print_counts( per_species_counter, log, ld )
539 puts " [sum: " + per_species_counter.to_s + "]"
540 log << " [sum: " + per_species_counter.to_s + "]" + ld
543 def read_fasta_file( input )
544 if @file_to_msa.has_key?( input )
545 return @file_to_msa[ input ]
551 msa = f.create_msa_from_file( input, FastaParser.new() )
552 rescue Exception => e
553 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
555 if @seqs <= 100000000
556 @file_to_msa[ input ] = msa
557 @seqs += msa.get_number_of_seqs
558 puts " total seqs in memory: " + @seqs.to_s
566 puts( " " + PRG_NAME + ".rb <sequence names file suffix> <input dir containing sequence names files " +
567 "and possibly genome multiple-sequence ('fasta') files> <output directory for sequences> <output directory for domains> [mapping file for " +
568 "genome multiple-sequence ('fasta') files not in input dir]" )
570 puts( " option: -" + EXT_OPTION + "=<int>: to extend extracted domains" )
571 puts( " -" + EXTRACT_LINKERS_OPTION + " : to extract linkers" )
573 puts( " " + "Example: \"mse.rb .prot . protein_seqs domain_seqs ../genome_locations.txt\"" )
577 end # class MultiSequenceExtractor