require 'sampler'

def sh1 fromTube, sampleVolume, bubblePt, lysisVolume, diluent, toTube

 loadPuck fromTube, :collection
 collectSample sampleVolume, bubblePt
 lysing :lysis, lysisVolume, :waste1, 88, '5:00'
 if (diluent == :none) 
   recoverLysate
 else 
  diluteLysate diluent, lysisVolume 
end
 unloadPuck :collection, toTube
  
end  #end of sh1

def daExtract fromTube, sampleVolume, bubblePt, toTube

 loadPuck fromTube, :collection
 collectSample sampleVolume, bubblePt
 lysing :AqMeOH, 2, :waste2, 71, '10:00'
 unloadPuck :collection, toTube
  
end #end of daExtract

def collectSample sampleVolume, bubblePtOrSampler=Sampler.new, optionalParameters={}
#  Do an Sandwitch Hyrbridization "1" with specified 
#  sample volume and filter bubble point.  
#  optionlParameters are specified as a ruby hash.  See sampler.rb
#
#  Examples:
#    sh1 240, 35   #sample 240ml with a filter bubble point of 35psi
#    sh1 530, 40, :maxDelay=>10   #sample 530ml with 40psi bubble pt...
#                 #wait only 10 seconds for fuild to "catch up" with plunger

  collectionSampler = if bubblePtOrSampler.kind_of? Numeric  #assume its a bubble point in psi
    options = optionalParameters.dup
    options[:bubblePt] = bubblePtOrSampler
    Sampler.new options
  else #it must have been an already initialized sampler object
    bubblePtOrSampler.dup.with optionalParameters
  end
  
  Log.record "collectSample #{sampleVolume}ml w/bubble point of #{collectionSampler.bubblePt}psi"

  collectionSampler.prime.sample sampleVolume
  collectionSampler.puckEvac

  Log.record "collectSample: Complete"
  
end

#heatProfile should be a seperate routine which does the ramp that you want
def lysing lysisSolution, lysisVolume, wastePlace, heatProfile, time
  
  Log.record "Lysing: Adding #{lysisSolution}"
  CBV.dialBetween :CSV, :waste1
  CTV.to :CSV
  CSV.to :CSR
  CSR.to lysisSolution
  CS.to 1
  delay 5
  CSR.to :air
  CS.pull 5
  delay 2
  CSV.to wastePlace
  CS.empty CSfast
  
  CSV.to :CSR
  CSR.to lysisSolution
  CS.to lysisVolume
  delay 5
  CSR.to :air
  CS.pull 3
  delay 2
  
  CSV.to :CTV
  CS.to 2.1-(.55*lysisVolume)

  heatProfile

  CS.to 0
  CSV.to :air
  delay 1

end

def recoverLysate
#  CS.empty CSfast
  CSV.to :CBV  #do not "connect", order is important
  CBV.to :CSV
  CS.pull 5
  CTV.to :air
  delay 5
  CS.pull 2
  delay 5
  CSV.to :air2
  delay 3
  CSV.to :air, :down
  if (CS.volume + 1 > 10)
   CS.to 10
  else 
   CS.pull 1
  end
  
  Log.record "Lysing: Complete"
end

def diluteLysate diluent, lysisVolume
  CSV.to :CSR
  CSR.to diluent
  CS.to lysisVolume 
  CSR.to :air
  CS.pull 1  #need to be adjusted based on where in manifold
  recoverLysate
  CBV.dialBetween :CSV, :waste1
  CTV.to :CSV
  CSV.to :CTV
  CS.to 3
  CSV.to :air
  CS.to 0
  delay 1
  recoverLysate    
end

def extracting lysisSolution, lysisVolume, wastePlace, temperature, time
  
  Log.record "Extracting: Adding #{lysisSolution}"
  CBV.dialBetween :CSV, :waste1
  CTV.to :CSV
  CSV.to :CSR
  CSR.to lysisSolution
  CS.to 1
  delay 5
  CSR.to :air
  CS.pull 5
  delay 2
  CSV.to wastePlace
  CS.empty CSfast
  
  CSV.to :CSR
  CSR.to lysisSolution
  CS.to lysisVolume
  delay 5
  CSR.to :air
  CS.pull 3
  delay 2
  
  CSV.to :CTV
  CS.to 1

  CH.seek 50,180
  CH.hold(30,55).finish
  CH.hold('10:00',65).finish

  CS.to 0
  CSV.to :air
  delay 1
#  CS.empty CSfast
  CSV.to :CBV  #do not "connect", order is important
  CBV.to :CSV
  CS.to 5
  CTV.to :air
  delay 5
  CS.to 7
  delay 5
  CSV.to :air2
  delay 3
  CSV.to :air, :down
  CS.to 8
  
  Log.record "Extracting: Complete"
end
