Class: Bio::DB::Polymarker

Inherits:
Object
  • Object
show all
Defined in:
lib/bio-polymarker_db_batch/polymarker_db_batch.rb

Instance Method Summary collapse

Constructor Details

#initialize(props) ⇒ Polymarker

Returns a new instance of Polymarker.



5
6
7
8
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 5

def initialize( props)
  @properties =Hash[*File.read(props).split(/[=\n]+/)]
  #puts @properties.inspect
end

Instance Method Details

#each_runningObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 30

def each_running
  query="SELECT snp_file_id, filename FROM snp_file WHERE status IN ('SUBMITTED', 'RUNNING');"
  ret = 0
  if block_given?
    ret = execute_query(query){|row| yield row }
  else
    ret = execute_query(query)
  end
  ret
end

#each_snp_in_file(file_id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 54

def each_snp_in_file(file_id)
  query="SELECT name, chromosome, sequence FROM snp, snp_file_snp WHERE snp_file_snp.snpList_snpId = snp.snpId AND snp_file_snp.snp_file_snp_file_id = '#{file_id}' AND snp.process = 1;"
  ret = 0
  puts query
  if block_given?
    ret = execute_query(query){|row| yield row }
  else
    ret = execute_query(query)
  end
  ret
end

#each_timeoutObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 41

def each_timeout
  #TODO: validate the timeouts. 
  #SELECT * FROM snp_file WHERE datediff(NOW(), lastChange) > 3 and status != 'DONE';
  query="SELECT snp_file_id, filename FROM snp_file WHERE datediff(NOW(), submitted) > 3 AND status IN ('NEW', 'SUBMITTED', 'RUNNING');"
  ret = 0
  if block_given?
     ret = execute_query(query){|row| yield row }
  else
    ret = execute_query(query)
  end
  ret
end

#each_to_runObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 19

def each_to_run
  query="SELECT snp_file_id, filename FROM snp_file WHERE status = 'NEW';"
  ret = 0
  if block_given?
    ret = execute_query(query){|row| yield row }
  else
    ret = execute_query(query)
  end
  ret
end

#execute_polymarker(snp_file) ⇒ Object



80
81
82
83
84
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 80

def execute_polymarker(snp_file)
  cmd="#{@properties['wrapper_prefix'] } polymarker.rb -m #{snp_file} -o #{snp_file}_out -c #{@properties['path_to_chromosomes']} #{@properties['wrapper_suffix'] }"
  #polymarker.rb -m 1_GWAS_SNPs.csv -o 1_test -c /Users/ramirezr/Documents/TGAC/references/Triticum_aestivum.IWGSP1.21.dna_rm.genome.fa
  execute_command(cmd)
end

#get_snp_file(id) ⇒ Object



14
15
16
17
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 14

def get_snp_file(id)
 results = con.query "SELECT * FROM snp_file WHERE snp_file_id=#{id}"
 results.fetch_hash
end

#mysql_versionObject



10
11
12
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 10

def mysql_version
  con.get_server_info
end

#review_running_status(file_id, filename) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 145

def review_running_status(file_id, filename)
  out_folder=@properties["execution_path"]+"/#{file_id}_#{filename}_out"
  started=File.exist?(out_folder)
  done=false
  

  error_message = ""
  error = false

  if started
    lines = IO.readlines("#{out_folder}/status.txt")
    puts lines.inspect

  lines.each do |l| 
    done = l.split(",").include?("DONE\n")
  end
    
    lines.each do |l|  
      error = l.include?("ERROR") unless error
      error_message << l if error
    end 
  end


  if done 
    exons_filename="#{out_folder}/exons_genes_and_contigs.fa"
    output_primers="#{out_folder}/primers.csv"
    read_file_to_snp_file("mask_fasta", file_id, exons_filename )
    read_file_to_snp_file("polymarker_output", file_id, output_primers )
    update_status(file_id, "DONE")
  elsif error
     update_error_status(file_id, error_message)
  elsif started
    update_status(file_id, "RUNNING")
  end
end

#send_email(to, id, status) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 125

def send_email(to,id, status)
  options = @properties

  msg = <<END_OF_MESSAGE
From: #{options['email_from_alias']} <#{options['email_from']}>
To: <#{to}>
Subject: Polymarker #{id} #{status}

The current status of your request (#{id}) is #{status}
The latest status and results (when done) are available in: #{options['web_domain']}/status?id=#{id}


END_OF_MESSAGE
  smtp = Net::SMTP.new options["email_server"], 587
  smtp.enable_starttls
  smtp.start( options["email_domain"], options["email_user"], options["email_pwd"], :login) do
    smtp.send_message(msg, options["email_from"], to)
  end
end

#update_error_status(snp_file_id, error_message) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 101

def update_error_status(snp_file_id, error_message)
  snp_file = get_snp_file(snp_file_id)
  return if snp_file['status'] == "ERROR"


  pst = con.prepare "UPDATE snp_file SET status = 'ERROR', error=? WHERE snp_file_id = ?"
  puts "update_error_status: #{pst}"
  pst.execute error_message, snp_file_id
  con.commit
  new_status = "ERROR: #{error_message}"
    hashed_id = "#{snp_file_id}:#{snp_file['hash']}"
  begin
    send_email(snp_file['email'], hashed_id,  new_status)
  rescue Exception => e  
    puts "Error sending email to #{snp_file['email']}: #{e.message}"
  end
    
  begin
    send_email(@properties['email_from'], hashed_id, new_status)
  rescue Exception => e  
    puts "Error sending email to #{@properties['email_from']}: #{e.message}"
  end
end

#update_status(snp_file_id, new_status) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 86

def update_status(snp_file_id, new_status)
  raise "Invalid status #{new_status}" unless ["NEW", "SUBMITTED", "RUNNING", "DONE", "ERROR"].include?(new_status)
  snp_file = get_snp_file(snp_file_id)
  return if snp_file['status'] == new_status
  pst = con.prepare "UPDATE snp_file SET status = ? WHERE snp_file_id = ?"
  pst.execute new_status, snp_file_id
  con.commit
  begin
    hashed_id = "#{snp_file_id}:#{snp_file['hash']}"
    send_email(snp_file['email'],hashed_id, new_status)
  rescue
    puts "Error sending email. "
  end
end

#write_output_file_and_execute(file_id, filename) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bio-polymarker_db_batch/polymarker_db_batch.rb', line 66

def write_output_file_and_execute(file_id, filename)
  puts "Writting: #{file_id}_#{filename}"
  path =@properties["execution_path"]+"/#{file_id}_#{filename}"
  puts "Writting: #{path}"
  f=File.open(path, "w")

  each_snp_in_file(file_id) do |row|
    f.puts(row.join(","))
  end
  execute_polymarker(path)
  update_status(file_id, "SUBMITTED")
  f.close
end