Class: Genfrag::App::IndexCommand::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/genfrag/app/index_command/db.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ops, input_filenames) ⇒ DB

Returns a new instance of DB.



12
13
14
15
16
17
18
# File 'lib/genfrag/app/index_command/db.rb', line 12

def initialize( ops, input_filenames )
  @normalized_fasta = nil
  @freq_lookup = nil
  @ops = ops
  @input_filenames = input_filenames
  
end

Instance Attribute Details

#freq_lookupObject

Returns the value of attribute freq_lookup.



10
11
12
# File 'lib/genfrag/app/index_command/db.rb', line 10

def freq_lookup
  @freq_lookup
end

#input_filenamesObject

Returns the value of attribute input_filenames.



8
9
10
# File 'lib/genfrag/app/index_command/db.rb', line 8

def input_filenames
  @input_filenames
end

#normalized_fastaObject

Returns the value of attribute normalized_fasta.



9
10
11
# File 'lib/genfrag/app/index_command/db.rb', line 9

def normalized_fasta
  @normalized_fasta
end

#opsObject

an OpenStruct of the options



7
8
9
# File 'lib/genfrag/app/index_command/db.rb', line 7

def ops
  @ops
end

Instance Method Details

#closeObject



88
89
90
# File 'lib/genfrag/app/index_command/db.rb', line 88

def close
  self.send("close_#{sc}")
end

#close_csvObject



95
96
97
98
# File 'lib/genfrag/app/index_command/db.rb', line 95

def close_csv
  @normalized_fasta.close
  @freq_lookup.close
end

#close_sqliteObject



92
93
# File 'lib/genfrag/app/index_command/db.rb', line 92

def close_sqlite
end

#scObject



20
21
22
# File 'lib/genfrag/app/index_command/db.rb', line 20

def sc
  @ops.sqlite ? 'sqlite' : 'csv'
end

#write_entry_to_fasta(normalized_fasta_id, seq, definitions) ⇒ Object



62
63
64
# File 'lib/genfrag/app/index_command/db.rb', line 62

def write_entry_to_fasta(normalized_fasta_id, seq, definitions)
  self.send("write_entry_to_fasta_#{sc}", normalized_fasta_id, seq, definitions)
end

#write_entry_to_fasta_csv(normalized_fasta_id, seq, definitions) ⇒ Object



70
71
72
# File 'lib/genfrag/app/index_command/db.rb', line 70

def write_entry_to_fasta_csv(normalized_fasta_id, seq, definitions)
  @normalized_fasta.puts [normalized_fasta_id,definitions.join('!!-genfrag-!!'),seq].join("\t")
end

#write_entry_to_fasta_sqlite(normalized_fasta_id, seq, definitions) ⇒ Object



66
67
68
# File 'lib/genfrag/app/index_command/db.rb', line 66

def write_entry_to_fasta_sqlite(normalized_fasta_id, seq, definitions)
  @normalized_fasta.execute( "insert into db_normalized_fasta values ( ?, ?, ? )", normalized_fasta_id, definitions.join('!!-genfrag-!!'), seq )
end

#write_entry_to_freq(i, size, str) ⇒ Object



75
76
77
# File 'lib/genfrag/app/index_command/db.rb', line 75

def write_entry_to_freq(i, size, str)
  self.send("write_entry_to_freq_#{sc}", i, size, str)
end

#write_entry_to_freq_csv(i, size, str) ⇒ Object



83
84
85
# File 'lib/genfrag/app/index_command/db.rb', line 83

def write_entry_to_freq_csv(i, size, str)
  @freq_lookup.puts [i,size,str].join("\t")
end

#write_entry_to_freq_sqlite(i, size, str) ⇒ Object



79
80
81
# File 'lib/genfrag/app/index_command/db.rb', line 79

def write_entry_to_freq_sqlite(i, size, str)
  @freq_lookup.execute( "insert into db_freq_lookup values ( ?, ?, ? )", i, size, str )
end

#write_headersObject



25
26
27
# File 'lib/genfrag/app/index_command/db.rb', line 25

def write_headers
  self.send("write_headers_#{sc}")
end

#write_headers_csvObject



54
55
56
57
58
59
# File 'lib/genfrag/app/index_command/db.rb', line 54

def write_headers_csv
  @normalized_fasta = File.new(File.join(@ops.outdir,Genfrag.name_normalized_fasta(@input_filenames,@ops.filefasta) + '.tdf'), 'w')
  @normalized_fasta.puts %w(id Definitions Sequence).join("\t")
  @freq_lookup = File.new( File.join(@ops.outdir,Genfrag.name_freq_lookup(@input_filenames,@ops.filefasta,@ops.filelookup,@ops.re5,@ops.re3) + '.tdf'), 'w')
  @freq_lookup.puts %w(id Size Positions).join("\t")
end

#write_headers_sqliteObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/genfrag/app/index_command/db.rb', line 29

def write_headers_sqlite
  @normalized_fasta = SQLite3::Database.new( File.join(@ops.outdir, Genfrag.name_normalized_fasta(@input_filenames,@ops.filefasta) + '.db') )
  sql = <<-SQL
    drop table if exists db_normalized_fasta;
    create table db_normalized_fasta (
      id integer,
      definitions text,
      sequence text
    );
    create unique index db_normalized_fasta_idx on db_normalized_fasta(id);
  SQL
  @normalized_fasta.execute_batch( sql )
  @freq_lookup = SQLite3::Database.new( File.join(@ops.outdir, Genfrag.name_freq_lookup(@input_filenames,@ops.filefasta,@ops.filelookup,@ops.re5,@ops.re3) + '.db') )
  sql = <<-SQL
    drop table if exists db_freq_lookup;
    create table db_freq_lookup (
    id integer,
    size integer,
    positions text
    );
    create unique index db_freq_lookup_idx on db_freq_lookup(id);
  SQL
  @freq_lookup.execute_batch( sql )
end