lib/bio-publisci/dataset/dataset_for.rb in bio-publisci-0.0.6 vs lib/bio-publisci/dataset/dataset_for.rb in bio-publisci-0.0.7

- old
+ new

@@ -1,8 +1,9 @@ -module R2RDF +require 'open-uri' +module PubliSci class Dataset - extend R2RDF::Interactive + extend PubliSci::Interactive def self.for(object, options={}, ask_on_ambiguous=true) if options == false || options == true ask_on_ambiguous = options @@ -21,22 +22,31 @@ case extension when ".RData" r_object(object, options, ask_on_ambiguous) when /.csv/i - R2RDF::Reader::CSV.new.automatic(object,nil,options,ask_on_ambiguous) + PubliSci::Reader::CSV.new.automatic(object,nil,options,ask_on_ambiguous) end + elsif object =~ %r{http[s]://.+} + self.for(download(object).path, options, ask_on_ambiguous) else - raise "Unable to find reader for File or String" + raise "Unable to find reader for File or String #{object}" end elsif object.is_a? Rserve::REXP r_object(object, options, ask_on_ambiguous) else raise "not recognize Ruby objects of this type yet (#{object})" end end + def self.download(uri) + out = Tempfile.new(uri.split('/').last) + out.write open(uri).read + out.close + out + end + def self.r_object(object, options={}, ask_on_ambiguous=true) if object.is_a? String con = Rserve::Connection.new vars = con.eval("load('#{File.absolute_path object}')") if vars.to_ruby.size > 1 && ask_on_ambiguous @@ -47,11 +57,11 @@ end r_classes = con.eval("class(#{var})").to_ruby if r_classes.include? "data.frame" - df = R2RDF::Reader::Dataframe.new + df = PubliSci::Reader::Dataframe.new unless options[:dimensions] || !ask_on_ambiguous dims = con.eval("names(#{var})").to_ruby puts "Which dimensions? #{dims}" selection = gets.chomp if selection.size > 0 @@ -68,11 +78,11 @@ end df.generate_n3(con.eval(var),var,options) elsif r_classes.include? "cross" - bc = R2RDF::Reader::RCross.new + bc = PubliSci::Reader::RCross.new unless options[:measures] || !ask_on_ambiguous pheno_names = con.eval("names(#{var}$pheno)").to_ruby puts "Which phenotype traits? #{pheno_names}" selection = gets.chomp @@ -89,11 +99,11 @@ end bc.generate_n3(con, var, base, options) elsif r_classes.include? "matrix" - mat = R2RDF::Reader::RMatrix.new + mat = PubliSci::Reader::RMatrix.new unless options[:measures] || !ask_on_ambiguous puts "Row label" rows = gets.chomp rows = "row" unless rows.size > 0 @@ -116,16 +126,16 @@ base = var unless base.size > 0 end mat.generate_n3(con, var, base, options) else - raise "no R2RDF::Reader found for #{r_classes}" + raise "no PubliSci::Reader found for #{r_classes}" end elsif object.is_a? Rserve::REXP if object.attr.payload["class"].payload.first - df = R2RDF::Reader::Dataframe.new + df = PubliSci::Reader::Dataframe.new var = nil if ask_on_ambiguous var = interact("Dataset name?",nil) \ No newline at end of file