Permalink
Browse files

Query datasets now identified by accession numbers (not IDs)

  • Loading branch information...
lmrodriguezr committed Jan 12, 2018
1 parent fa175c4 commit e23b93f2ea16a4a9d48f84c0ca94220f9d5372c0
@@ -2,7 +2,7 @@ source 'https://rubygems.org'


# Use MiGA base code to communicate with the projects
gem 'miga-base', '~> 0.3'
gem 'miga-base', '~> 0.3.1.5'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
# Make it safe
@@ -1,7 +1,8 @@
class QueryDatasetsController < ApplicationController
before_action :logged_in_user, only: [:index, :destroy]

before_action :correct_user_or_admin, only: [:show, :destroy, :result]
before_action :set_query_dataset, only: [:show, :destroy, :result, :run_mytaxa_scan, :run_distances]
before_action :correct_user_or_admin, only: [:show, :destroy, :result, :run_mytaxa_scan, :run_distances]

def index
# Find query datasets
@@ -27,7 +28,6 @@ def new
end

def show
@query_dataset = QueryDataset.find(params[:id])
@query_dataset.complete_seen!
end

@@ -64,7 +64,7 @@ def create
end

def destroy
qd = QueryDataset.find(params[:id])
qd = @query_dataset
p = qd.project
p.miga.unlink_dataset qd.miga.name
qd.miga.remove!
@@ -73,7 +73,7 @@ def destroy
end

def result
qd = QueryDataset.find(params[:id])
qd = @query_dataset
m = qd.miga
res = m.result(params[:result])
unless res.nil?
@@ -102,14 +102,12 @@ def result

# Execute the MyTaxa Scan step upon request.
def run_mytaxa_scan
@query_dataset = QueryDataset.find(params[:id])
@query_dataset.run_mytaxa_scan!
redirect_to(@query_dataset)
end

# Re-calculate the Distances step upon request.
def run_distances
@query_dataset = QueryDataset.find(params[:id])
@query_dataset.run_distances!
redirect_to(@query_dataset)
end
@@ -149,10 +147,21 @@ def list_query_datasets
end
qd
end

# Sets the query dataset by ID or accession
def set_query_dataset
if params[:id] =~ /\AM:/
@query_dataset = QueryDataset.find_by(acc: params[:id])
else
@query_dataset = QueryDataset.find(params[:id])
flash.now[:warning]= "Entry IDs are being phased out, please update your links"
# redirect_to root_url and return
end
end

# Confirms the correct user
def correct_user_or_admin
@user = QueryDataset.find(params[:id]).user
@user = @query_dataset.user
return true if @user.nil?
redirect_to(root_url) if current_user.nil? or
not( current_user?(@user) or current_user.admin? )
@@ -25,6 +25,16 @@ def self.unknown_ready_by_user(user)
QueryDataset.where(["user_id=? and ready=?", user.nil? ? 0 : user.id, false])
end

# Generate a random new accession number
def self.new_acc
"M:" + SecureRandom.urlsafe_base64(5).upcase.tap{ |i| i[3]="_" }
end

def to_param
self.update(acc:QueryDataset.new_acc) if self.acc.nil?
self.acc
end

# Returns the MiGA name of the dataset
def miga_name
"qG_#{name}_u#{user_id}"
@@ -95,44 +105,47 @@ def job_log(task)

def create_miga_dataset
f = { 1=>input_file.path, 2=>input_file_2.path }
t = input_type.to_sym
return if MiGA::Dataset.exist? project.miga, miga_name
MiGA::Dataset.new(project.miga, miga_name, false,
{user: user_id, type: :genome})
project.miga.add_dataset(miga.name)
return unless MiGA::Dataset.RESULT_DIRS.keys.include? input_type.to_sym
project.miga.add_dataset(miga_name)
return unless MiGA::Dataset.RESULT_DIRS.keys.include? t
r_base = File.join(project.miga.path, "data",
MiGA::Dataset.RESULT_DIRS[input_type.to_sym], miga_name)

# ToDo: Add support for .gz!
case input_type.to_sym
case t
when :raw_reads
FileUtils.copy(f[1], "#{r_base}.1.fastq")
FileUtils.copy(f[2], "#{r_base}.2.fastq") unless f[2].nil?
copy_to_miga(f[1], "#{r_base}.1.fastq")
copy_to_miga(f[2], "#{r_base}.2.fastq") unless f[2].nil?
when :trimmed_fasta
if f[2].nil?
FileUtils.copy(f[1], "#{r_base}.SingleReads.fa")
copy_to_miga(f[1], "#{r_base}.SingleReads.fa")
else
FileUtils.copy(f[1], "#{r_base}.1.fasta")
FileUtils.copy(f[2], "#{r_base}.2.fasta")
copy_to_miga(f[1], "#{r_base}.1.fasta")
copy_to_miga(f[2], "#{r_base}.2.fasta")
end
when :assembly
FileUtils.copy(f[1], "#{r_base}.LargeContigs.fna")
copy_to_miga(f[1], "#{r_base}.LargeContigs.fna")
else
return
end
File.open("#{r_base}.done", "w") { |fh| fh.puts Time.now.to_s }
miga.add_result input_type.to_sym
miga.add_result t
ensure
# Empty input files
if f
File.open(f[1], "r"){ |fh| fh.print "" } unless f[1].nil?
File.open(f[2], "r"){ |fh| fh.print "" } unless f[2].nil?
end
f ||= {}
f.each{ |_,i| File.open(i, "r"){ |fh| fh.print "" } unless i.nil? }
end

def load_miga_dataset
self.miga_obj = project.miga.dataset(miga_name) if
miga_obj.nil? and MiGA::Dataset.exist?(project.miga, miga_name)
end

def copy_to_miga(input, output)
output += ".gz" if input =~ /\.gz\z/
FileUtils.copy(input, output)
end

end
@@ -10,10 +10,8 @@
<% if current_user.nil? %>
<div class="alert alert-warning">
You're currently not logged in. Datasets created without an associated
user are openly accessible and can be purged without warning. Please
be adviced that your data will be publicly available and can be
deleted at any point. If you find this undesirable, please
<%= link_to "log in", login_url %>.
user can be purged by administrators without warning. If you find this
undesirable, please <%= link_to "log in", login_url %>.
</div>
<%= f.hidden_field :user_id, value: 0 %>
<% else %>
@@ -29,9 +27,12 @@
<%= label_tag :type, "Type of dataset" %>
<%= select_tag(:type,
options_for_select(
MiGA::Dataset.KNOWN_TYPES.
map{ |k,t| [k.to_s.capitalize + ": " + t[:description], k]}),
class: "form-control") %>
MiGA::Dataset.KNOWN_TYPES.
map{ |k,t|
[
k.to_s.capitalize + ": " + t[:description], k
] if @project.miga.is_multi? or !t[:multi]
}.compact ), class: "form-control") %>

<%= f.label :input_type, "Type of input" %>
<%= f.collection_select(:input_type,
@@ -62,13 +62,11 @@
:mytaxa_scan, :report) :
query_dataset_result_path(obj.id, :mytaxa_scan, :report)
f_path = nil if res.file_path(:report).nil?
regions = res.data[:files][:region_ids].nil? ? 0 :
File.readlines(res.file_path :region_ids).count
%>
<p>
MyTaxa Scan detected <%= pluralize regions, "region" %> with unusual
taxonomic distribution (<%= f_path.nil? ? "No report available" :
link_to("download PDF", f_path) %>).
MyTaxa Scan detected regions with unusual taxonomic distribution
(<%= f_path.nil? ? "No report available" :
link_to("download PDF", f_path) %>).
</p>
<% when :mytaxa %>
<%
@@ -0,0 +1,7 @@
class AddAccToQueryDatasets < ActiveRecord::Migration
def change
add_column :query_datasets, :acc, :string
add_column :query_datasets, :acc_seen, :boolean, default: false
add_index :query_datasets, :acc, unique: true
end
end
@@ -0,0 +1,5 @@
class RemoveAccSeenFromQueryDatasets < ActiveRecord::Migration
def change
remove_column :query_datasets, :acc_seen, :boolean
end
end
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160527051252) do
ActiveRecord::Schema.define(version: 20171231232106) do

create_table "projects", force: :cascade do |t|
t.text "path"
@@ -36,8 +36,10 @@
t.string "input_file_2"
t.boolean "notified", default: false
t.boolean "complete_new", default: false
t.string "acc"
end

add_index "query_datasets", ["acc"], name: "index_query_datasets_on_acc", unique: true
add_index "query_datasets", ["project_id", "created_at"], name: "index_query_datasets_on_project_id_and_created_at"
add_index "query_datasets", ["project_id", "user_id", "name"], name: "index_query_datasets_on_project_id_and_user_id_and_name", unique: true
add_index "query_datasets", ["project_id"], name: "index_query_datasets_on_project_id"

0 comments on commit e23b93f

Please sign in to comment.