Esempio n. 1
0
def login():
	"""Checks the user credentials
	and logs him in
	"""
	form = LogInForm()
	if form.validate_on_submit():
		import hashlib
		user = models.User.query.get(request.form["username"])
		if user != None and \
				user.password == hashlib.sha512(request.form["password"]).hexdigest():
			login_user(user)
			log_entry = models.Log()
			import time
			log_entry.timestamp = int(time.time())
			log_entry.user_id = g.user.user_name
			log_entry.action = "Logged in"
			db.session.add(log_entry)
			db.session.commit()


			return redirect(url_for('index'))
		else:
			flash("Invalid login", "danger")
			return redirect(url_for('login'))
	return render_template('login.html', form=form)
Esempio n. 2
0
def delete_project(id):
	"""Delete a project and
	all associated data.
	"""
	project = models.Project.query.filter_by(project_id=id).one()
	if project.owner == g.user.user_name:
		memship = models.Membership.query.filter_by(project=id).delete()
		db.session.commit()
		genotypes = models.Genotype.query.filter_by(project_id=id).delete()
		db.session.commit()
		phenotypes = models.Phenotype.query.filter_by(project_id=id).delete()
		db.session.commit()
		individuals = models.Individual.query.filter_by(project_id=id).delete()
		db.session.commit()
		groups = models.Group.query.filter_by(project_id=id).delete()
		db.session.commit()
		db.session.delete(project)
		db.session.commit()

		"""
		Create log entry
		"""
		log_entry = models.Log()
		import time
		log_entry.timestamp = int(time.time())
		log_entry.user_id = g.user.user_name
		log_entry.action = "Deleted project " + project.name
		db.session.add(log_entry)
		db.session.commit()

		flash('Project deleted successfuly.', 'success')
	else:
		flash('You do not have the premision to do that.', 'danger')

	return redirect(url_for('projects'))
Esempio n. 3
0
def add_user():
	"""Add a new user to the system.
	Only the master administrator can do that.
	"""

	# Check if this is the master administrator
	if g.user.user_name != gen_app.config['ADMIN_USER']:
		flash("You should not be here", "danger")
		return redirect(url_for('index'))

	user = models.User()
	if request.method == 'POST':
		import hashlib
		user.password = hashlib.sha512(request.form["password"]).hexdigest()
		user.user_name = request.form['username']
		user.email = request.form['email']
		db.session.add(user)
		db.session.commit()

		"""
		Create log entry
		"""
		log_entry = models.Log()
		import time
		log_entry.timestamp = int(time.time())
		log_entry.user_id = g.user.user_name
		log_entry.action = "Created user " + request.form['username']
		db.session.add(log_entry)
		db.session.commit()


	return redirect(url_for('users'))
Esempio n. 4
0
def add_project():
	"""Create a new project.
	No duplicate names are allowed.
	"""
	form = AddProject()
	project = models.Project()
	memship = models.Membership()

	nm = models.Project.query.filter_by(name=form.project_name.data).all()
	if form.validate_on_submit():
		if nm:
			flash("A project with that name already exists", "danger")
			return redirect(url_for('projects'))
		project.name = form.project_name.data
		project.description = form.project_description.data
		project.owner = g.user.user_name

		"""
		Create log entry
		"""
		log_entry = models.Log()
		import time
		log_entry.timestamp = int(time.time())
		log_entry.user_id = g.user.user_name
		log_entry.action = "Created project " + form.project_name.data
		db.session.add(log_entry)
		db.session.add(project)
		db.session.commit()

		memship.user_name = g.user.user_name
		memship.project = project.project_id
		db.session.add(memship)
		db.session.commit()

		return redirect(url_for('projects'))

	return render_template('add_project.html',
			title="New Project",
			ap=form)
Esempio n. 5
0
def download_dat(filename,project_id):
	"""Generate and serve
	the DAT file
	"""
	geno = models.Genotype.query.filter_by(project_id=project_id).all()
	pheno = db.engine.execute('SELECT name FROM phenotype where project_id=' + \
			str(project_id) +' group by name;')

	gen_list = {}
	phn_list = list()

	for row in geno:
		gen_list[row.snp] = row.snp
	for row in pheno:
		phn_list.append(row.name)

	with open(gen_app.config['UPLOAD_FOLDER'] + filename, 'w+') as f:
		writer = csv.writer(f, delimiter=',')

		for row in gen_list:
			r = [] + ["M"] + [gen_list[row]]
			writer.writerow(r)
		for row in phn_list:
			r = [] + ["T"] + [row]
			writer.writerow(r)

	"""
	Create log entry
	"""
	log_entry = models.Log()
	import time
	log_entry.timestamp = int(time.time())
	log_entry.user_id = g.user.user_name
	log_entry.action = "Downloaded DAT file."
	db.session.add(log_entry)
	db.session.commit()


	return send_from_directory(gen_app.config['UPLOAD_FOLDER'], filename=filename)
Esempio n. 6
0
def single_geno():
	"""Upload a file containing information
	for a single genotype.
	The file format is the following:
			ID - column with genotype IDs
			SNP - SNP name column
			Call - call column

	No header is required.
	"""
	abort_commit = False
	csv.field_size_limit(sys.maxsize)
	num_rows = 0
	current_geno_q = models.Genotype.query.\
			filter_by(project_id=request.form['id']).all()
	current_geno = set()
	duplicate = set()



	for row in current_geno_q:
		current_geno.add(row.snp)

	# Upload file and parse it
	if request.method == 'POST':
		single_geno = request.files['single_geno']

		# Check if this is the correct file type
		if single_geno and allowed_file(single_geno.filename):
			filename = secure_filename(single_geno.filename)
			single_geno.save(os.path.join(gen_app.config['UPLOAD_FOLDER'], filename))
			flash("Genotype file uploaded successfully", "success")

			try:
				with open(gen_app.config['UPLOAD_FOLDER'] + filename, 'rb') as csvfile:
					header=csvfile.readline()

					# Determine delimiter
					dell = ","
					if header.find(" ") !=- 1:
						dell = " "
					elif header.find("\t") != -1:
						dell = "\t"
					elif header.find(",") != -1:
						dell = ","
					sr = csv.reader(csvfile, delimiter=dell)

					csvfile.seek(0)
					# List of IDs for when it crashes
					future = []
					for row in sr:
						future.append(row[0])

					# Perform checks
					csvfile.seek(0)
					for row in sr:
						num_rows = num_rows+1
						if len(row) > 4:
							flash("Ivalid file format", "danger")
							return redirect(url_for('project_page', id=request.form['id']))
						gen = models.Genotype()
						gen.individual_id = row[0]
						if row[1] in current_geno:
							duplicate.add(row[1])
							abort_commit = True
						gen.snp = row[1]

						if len(row) == 4:
							gen.call = row[2] + "/" + row[3]
						else:
							gen.call = row[2]

						gen.project_id = request.form['id']
						db.session.add(gen)

					if abort_commit:
						flash("Duplicate genotypes detected", "danger")
						flash(list(duplicate), "info")
						return redirect(url_for('project_page', id=request.form['id']))

					# Generate a list of missing IDs
					inds = models.Individual.query.filter_by(project_id=request.form['id']).all()
					current = []
					missing = []
					for row in inds:
						current.append(row.new_id)
					for row in future:
						if row not in current:
							missing.append(row)
					if len(missing) != 0:

						flash("Error: missing or misspelled IDs.", "danger")
						flash(missing, "info")
						db.session.rollback()
						try:
							os.remove(gen_app.config['UPLOAD_FOLDER'] + '/' +filename)
						except OSError,e :
							print str(e)

						return redirect(url_for('project_page', id=request.form['id']))


					"""
					Create log entry
					"""
					log_entry = models.Log()
					import time
					log_entry.timestamp = int(time.time())
					log_entry.user_id = g.user.user_name
					log_entry.action = "Upload Genotype file for project "\
							+ request.form['proname']
					db.session.add(log_entry)
					"""
					Commit changes to DB
					"""
					db.session.commit()
			except exc.IntegrityError, e:
				db.session.rollback()
				try:
					os.remove(gen_app.config['UPLOAD_FOLDER'] + '/' +filename)
				except OSError,e :
					print str(e)


			try:
				os.remove(gen_app.config['UPLOAD_FOLDER'] + '/' +filename)
			except OSError,e :
				print str(e)
Esempio n. 7
0
def bulk_geno():
	"""Bulk upload of genotype information
	The file format is the following:
			IDs - column containing all the IDs
			Gen1 - first genotype
			Gen2 - second genotype
			Gen3 - etc...
			GenN - ...

	This file REQUIRES a header:
			ID - the first cell can be anything
			Gen Name 1 - coresponding genotype name
			Gen Name 2 - ...
			Gen Name 3 - ...
			Gen Name N - ...
	"""

	csv.field_size_limit(sys.maxsize)
	num_rows = 0

	if request.method == 'POST':
		bulk_geno = request.files['bulk_geno']

		if bulk_geno and allowed_file(bulk_geno.filename):
			filename = secure_filename(bulk_geno.filename)
			bulk_geno.save(os.path.join(gen_app.config['UPLOAD_FOLDER'], filename))
			flash("Genotype file uploaded successfuly", "success")

			try:
				with open(gen_app.config['UPLOAD_FOLDER'] + filename, 'rb') as csvfile:
					header=csvfile.readline()
					dell = ","
					if header.find(" ") !=- 1:
						dell = " "
					elif header.find("\t") != -1:
						dell = "\t"
					elif header.find(",") != -1:
						dell = ","
					# List of IDs for when it crashes
					csvfile.seek(0)
					future = []
					for row in sr:
						future.append(row[0])

					sr = csv.reader(csvfile, delimiter=dell)
					header = sr.next()

					csvfile.seek(0)
					for row in sr:
						num_rows = num_rows+1
						num_cols = len(row)
						for i in range(1,num_cols):
							gen = models.Genotype()
							gen.individual_id = row[0]
							gen.snp = header[i]
							gen.call = row[i]
							gen.project_id = request.form['id']
							db.session.add(gen)

					"""
					Create log entry
					"""
					log_entry = models.Log()
					import time
					log_entry.timestamp = int(time.time())
					log_entry.user_id = g.user.user_name
					log_entry.action = "Upload Genotype file for project " + request.form['proname']
					db.session.add(log_entry)
					"""
					Commit changes to DB
					"""
					db.session.commit()


			except exc.IntegrityError, e:
				db.session.rollback()
				try:
					os.remove(gen_app.config['UPLOAD_FOLDER'] + '/' +filename)
				except OSError,e :
					print str(e)
				# Generate a list of missing IDs
				inds = models.Individual.query.filter_by(project_id=request.form['id']).all()
				current = []
				missing = []
				for row in inds:
					current.append(row.new_id)
				for row in future:
					if row not in current:
						missing.append(row)

				flash("Error: missing or misspelled IDs", "danger")
				flash(missing, "info")
				return redirect(url_for('project_page', id=request.form['id']))

			try:
				os.remove(gen_app.config['UPLOAD_FOLDER'] + '/' +filename)
			except OSError,e :
				print str(e)
Esempio n. 8
0
def upload_individual():
	"""Upload a file containing a list of individuals.
	The file format is the following:

			ID - a column containing all the individual IDs
			Gender - a column containing gender information

	No header is required.
	"""
	csv.field_size_limit(sys.maxsize)
	num_rows = 0

	if request.method == 'POST':
		ind_file = request.files['individuals']

		if ind_file and allowed_file(ind_file.filename):
			filename = secure_filename(ind_file.filename)
			ind_file.save(os.path.join(gen_app.config['UPLOAD_FOLDER'], filename))
			flash("Individuals file uploaded successfully", "success")

			try:
				with open(gen_app.config['UPLOAD_FOLDER'] + '/' + filename, 'rb') as csvfile:
					sr = csv.reader(csvfile, delimiter=',')

					for row in sr:
						num_rows = num_rows+1
						if len(row) != 2:
							flash("Ivalid file format. Only two column with \
									IDs and gender are required", "danger")
							return redirect(url_for('project_page', id=request.form['id']))
						ind = models.Individual()
						ind.new_id = row[0]
						ind.gender = row[1]
						ind.project_id = request.form['id']
						db.session.add(ind)
					"""
					Create log entry
					"""
					log_entry = models.Log()
					import time
					log_entry.timestamp = int(time.time())
					log_entry.user_id = g.user.user_name
					log_entry.action = "Upload Individual file for project "\
							+ request.form['proname']
					db.session.add(log_entry)

					"""
					Commit changes to DB
					"""
					db.session.commit()

			# clean up
			except exc.IntegrityError, e:
				try:
					os.remove(gen_app.config['UPLOAD_FOLDER'] + '/' +filename)
				except OSError,e :
					print str(e)
					flash("Error: missing or misspelled IDs", "danger")
					return redirect(url_for('project_page', id=request.form['id']))

			try:
				os.remove(gen_app.config['UPLOAD_FOLDER'] + '/' +filename)
			except OSError,e :
				print str(e)