def new_track(): user_id = g.current_user_id # Get json json = request.get_json() # Try and get everything track_name = json.get('track_name') data_type = json.get('data_type').lower() data_id = json.get('data_id') file_name = json.get('file_name') # Ensure everything was passed for field in [track_name,user_id,data_type,data_id,file_name]: if not field: return jsonify(response="Could not create track, {} field is required".format(track)),404 new_track = Track( track_name = track_name, user_id = user_id, data_type = data_type, data_id = data_id, file_name = file_name, ) session.add(new_track) session.commit() # Last check to make sure it was commited properly if not new_track.id: return jsonify(response="Could not create track"),404 # Return the user id on success return jsonify(track_id=new_track.id)
def new_fasta(fasta_file): position = 0 basepairs = [] header = fasta_file.readline() if not validate_fasta_header(header): return jsonify(response="Cannot upload fasta file {} is not a valid header".format(header)),404 fasta = Fasta(header=header) session.add(fasta) session.commit() fasta_id = fasta.id for line in fasta_file: line.strip("\n") for base in line: bp = BasePair(nucleotide=base,position=position,fasta_id=fasta_id) position+=1 basepairs.append(bp) session.add_all(basepairs) session.commit() return fasta_id
def new_fasta(fasta_file): position = 0 basepairs = [] header = fasta_file.readline() if not validate_fasta_header(header): return jsonify( response="Cannot upload fasta file {} is not a valid header". format(header)), 404 fasta = Fasta(header=header) session.add(fasta) session.commit() fasta_id = fasta.id for line in fasta_file: line.strip("\n") for base in line: bp = BasePair(nucleotide=base, position=position, fasta_id=fasta_id) position += 1 basepairs.append(bp) session.add_all(basepairs) session.commit() return fasta_id
def update_data_view(view_id): user_id = g.current_user_id view = session.query(View).get(view_id) # make sure the view was found if not view: return jsonify(response="Cannot update data for view {0} from user {1} - view not found".format(view_id,user_id)),404 # make sure the view belongs to the userid if not view.user_id == int(user_id): return jsonify(response="Cannot update data view {0} it does not belong to user {1}".format(view_id,user_id)),404 view_json = request.get_json() # we are only checking the view_track display params # other updates need to happen through the other end points view_tracks = view_json.get('view_tracks') for view_track in view_tracks: view_track_obj = session.query(ViewTrack).get(view_track.get('view_track_id')) if not view_track_obj: return jsonify(response="Cannot update display parameters for view_track {0} it does not exist".format(view_track_id)),404 param_array = view_track.get('display_params') for param in ['sticky','hidden','y_max']: val = param_array.get(param) if val: setattr(view_track_obj,param,val) session.add(view_track_obj) session.commit() return jsonify(view.to_data())
def new_view(): user_id = g.current_user_id json = request.get_json() track_ids = json.get('track_ids') view_name = json.get('view_name') for field in [track_ids, view_name]: if not field: return jsonify(response="Could not create view, {} is required".format(field)),404 new_view = View(view_name = view_name, user_id = user_id) session.add(new_view) session.commit() view_tracks = [] for track_id in track_ids: track = session.query(Track).get(track_id) if not track: return jsonify(response="Could not create view, {} is not a valid track id".format(track_id)),404 if not track.user_id == int(user_id): return jsonify(response="Could not create view with track {} is not a owned by user {}".format(track_id,user_id)),404 view_track = ViewTrack(track_id = track_id, view_id = new_view.id) view_tracks.append(view_track) session.add_all(view_tracks) session.commit() return jsonify(view_id=new_view.id),200
def new_user(): # Parse the json json = request.get_json() u_name = json.get('username') email = json.get('email') password = json.get('password') # Ensure username and email were passed if not u_name: return jsonify(response="Could not create user, username field is required"), 404 elif u_name == "default": return jsonify(response="Invalid username."), 400 if not email: return jsonify(response="Could not create user. email field is required"),404 # Create user and commit new_user = User(username=json.get('username'),email=json.get('email'),password=json.get('password')) session.add(new_user) session.commit() # Last check to make sure it was commited properly if not new_user.id: return jsonify(response="Could not create user"),404 # Return the user id on success return jsonify(user_id=new_user.id)
def update_view(view_id): user_id = g.current_user_id json = request.get_json() view = session.query(View).get(view_id) if not view: return jsonify(response="Can't fetch view with id: {}".format(view_id)),404 if not view.user_id == int(user_id): return jsonify(response="Cant update view {0} for user {1} they do not own it".format(view.id,user.id)),404 # update the view_name if given view_name = json.get('view_name') if view_name: view.view_name = view_name # update the viewtracks if given track_ids = json.get('track_ids') if track_ids is not None: track_ids = set(track_ids) existing_ids = set(view_track.track_id for view_track in view.view_tracks) for new_track_id in track_ids.difference(existing_ids): app.logger.warning('Adding track {}'.format(new_track_id)) session.add(ViewTrack(track_id=new_track_id, view_id=view.id)) for stale_track_id in existing_ids.difference(track_ids): app.logger.warning('Removing track {}'.format(stale_track_id)) session.delete(session.query(ViewTrack).filter_by(track_id=stale_track_id,view_id=view.id).first()) session.commit() return jsonify(view.to_json())
def new_bed(bed_file): # create the base record bed = Bed() session.add(bed) session.commit() bed_vals = [] for line in bed_file: # skip the header if line.startswith("#"): continue line_vals = line.split("\t") if len(line_vals) != 12: app.logger.warning('Invalid bed line - skipping') continue [ chrom, start, stop, name, score, strand, thick_start, thick_end, item_rgb, block_count, bed_block_sizes, bed_block_starts ] = line_vals bed_val = BedValue(chrom, int(start), int(stop), name, int(score), strand, int(thick_start), int(thick_end), int(item_rgb), int(block_count), bed.id) session.add(bed_val) session.commit() index = 0 bed_block_sizes = bed_block_sizes.rstrip(",").split(",") for size in bed_block_sizes: bed_block_size = BedBlockSize(index, size, bed_val.id) index += 1 bed_vals.append(bed_block_size) index = 0 bed_block_starts = bed_block_starts.rstrip("\n").split(",") for start in bed_block_starts: bed_block_start = BedBlockStart(index, start, bed_val.id) index += 1 bed_vals.append(bed_block_start) session.add_all(bed_vals) session.commit() return bed.id
def new_bed(bed_file): # create the base record bed = Bed() session.add(bed) session.commit() bed_vals = [] for line in bed_file: # skip the header if line.startswith("#"): continue line_vals = line.split("\t") if len(line_vals) != 12: app.logger.warning('Invalid bed line - skipping') continue [chrom, start, stop, name, score, strand, thick_start, thick_end, item_rgb, block_count, bed_block_sizes, bed_block_starts] = line_vals bed_val = BedValue(chrom, int(start), int(stop), name, int(score), strand, int(thick_start), int(thick_end), int(item_rgb), int(block_count), bed.id) session.add(bed_val) session.commit() index = 0 bed_block_sizes = bed_block_sizes.rstrip(",").split(",") for size in bed_block_sizes: bed_block_size = BedBlockSize(index, size, bed_val.id) index += 1 bed_vals.append(bed_block_size) index = 0 bed_block_starts = bed_block_starts.rstrip("\n").split(",") for start in bed_block_starts: bed_block_start = BedBlockStart(index, start, bed_val.id) index += 1 bed_vals.append(bed_block_start) session.add_all(bed_vals) session.commit() return bed.id
def new_gtf(gtf_file): gtf_fields = [ 'seqname', 'source', 'feature', 'start', 'end', 'score', 'strand', 'frame', 'attribute' ] # create the base record gtf = Gtf() session.add(gtf) session.commit() gtf_values = [] for line in gtf_file: gtf_dict = dict(zip(gtf_fields, line.split("\t"))) gtf_dict['gtf_id'] = gtf.id # handle the '.' in score and frame if gtf_dict['score'] == '.': gtf_dict['score'] = 0.0 if gtf_dict['frame'] == '.': gtf_dict['frame'] = 0 # create each value record gtf_val = GtfValue(seqname=gtf_dict['seqname'], source=gtf_dict['source'], feature=gtf_dict['feature'], start=int(gtf_dict['start']), end=int(gtf_dict['end']), score=gtf_dict['score'], strand=gtf_dict['strand'], frame=int(gtf_dict['frame']), attribute=gtf_dict['attribute'], gtf_id=gtf_dict['gtf_id']) gtf_values.append(gtf_val) session.add_all(gtf_values) session.commit() return gtf.id
def new_gtf(gtf_file): gtf_fields = ['seqname','source','feature','start','end','score','strand','frame','attribute'] # create the base record gtf = Gtf() session.add(gtf) session.commit() gtf_values = [] for line in gtf_file: gtf_dict = dict(zip(gtf_fields, line.split("\t"))) gtf_dict['gtf_id'] = gtf.id # handle the '.' in score and frame if gtf_dict['score'] == '.': gtf_dict['score'] = 0.0 if gtf_dict['frame'] == '.': gtf_dict['frame'] = 0 # create each value record gtf_val = GtfValue( seqname = gtf_dict['seqname'], source = gtf_dict['source'], feature = gtf_dict['feature'], start = int(gtf_dict['start']), end = int(gtf_dict['end']), score = gtf_dict['score'], strand = gtf_dict['strand'], frame = int(gtf_dict['frame']), attribute = gtf_dict['attribute'], gtf_id = gtf_dict['gtf_id'] ) gtf_values.append(gtf_val) session.add_all(gtf_values) session.commit() return gtf.id
def new_track(): user_id = g.current_user_id # Get json json = request.get_json() # Try and get everything track_name = json.get('track_name') data_type = json.get('data_type').lower() data_id = json.get('data_id') file_name = json.get('file_name') # Ensure everything was passed for field in [track_name, user_id, data_type, data_id, file_name]: if not field: return jsonify( response="Could not create track, {} field is required".format( track)), 404 new_track = Track( track_name=track_name, user_id=user_id, data_type=data_type, data_id=data_id, file_name=file_name, ) session.add(new_track) session.commit() # Last check to make sure it was commited properly if not new_track.id: return jsonify(response="Could not create track"), 404 # Return the user id on success return jsonify(track_id=new_track.id)
def seed_db(): # Setup default user # default_user = User(username="******", email="*****@*****.**", password="******") session.add(default_user) session.commit() # add the users user_ids = [default_user.id] users = ["kyle","karl","coda","max","goof"] for name in users: new_user = User(username=name,email="{}@email.com".format(name),password="******") session.add(new_user) session.commit() user_ids.append(new_user.id) ## Give each a track ## for u_id in user_ids: view = View(view_name = "Test View", user_id = u_id) session.add(view) session.commit() # Create a fasta fasta = Fasta(header=">EBV1") session.add(fasta) session.commit() i = 0 for base in "ATTATTAGCATGCATGATCAGTAGCTAGGGGATGCATGCAACTGATCGATCGATGCATGCAT": bp = BasePair(nucleotide=base,position=i,fasta_id=fasta.id) i+=1 session.add(bp) session.commit() # Add a fasta track fasta_track = Track( track_name = "Fasta Test Track", user_id = u_id, data_type = "fasta", data_id = fasta.id, file_name = "testFasta.fasta" ) session.add(fasta_track) session.commit() session.add(ViewTrack(fasta_track.id,view.id)) # Create a wig track wig = Wig(chrom='EBV1') session.add(wig) session.commit() for pos in range(1,10): wig_val = WigValue( position = pos, value = 1, wig_id = wig.id ) session.add(wig_val) # add the track wig_track = Track( track_name = "Wig Test Track", user_id = u_id, data_type = "wig", data_id = wig.id, file_name = "testWig.wig" ) session.add(wig_track) session.commit() session.add(ViewTrack(wig_track.id,view.id)) # Create a GTF track gtf = Gtf() session.add(gtf) session.commit() for pos in range(1,10): gtf_val = GtfValue( seqname = "EBV1", source = "mRNA-Seq", feature = "exon", start = pos, end = pos + 10, score = 0, strand = "+", frame = 0, attribute = "spanning-juncs=false;host=hg19", gtf_id = gtf.id ) session.add(gtf_val) gtf_track = Track( track_name = "Gtf Test Track", user_id = u_id, data_type = "gtf", data_id = gtf.id, file_name = "testGtf.gtf" ) session.add(gtf_track) session.commit() session.add(ViewTrack(gtf_track.id,view.id)) # last make a bed bed = Bed() session.add(bed) session.commit() for pos in range(1,10): bed_val = BedValue( chrom = "EBV1", start = pos, end = pos+9, name = "BED-THING", score = 0, strand = "+", thick_start = pos, thick_end = pos+9, item_rgb = 10, block_count = 1, bed_id = bed.id ) session.add(bed_val) session.commit() bb_size = BedBlockSize( index = pos, value = pos+9, bed_value_id = bed_val.id ) bb_start = BedBlockStart( index = pos, value = pos, bed_value_id = bed_val.id ) session.add_all([bb_size, bb_start]) session.commit() bed_track = Track( track_name = "Bed Test Track", user_id = u_id, data_type = "bed", data_id = bed.id, file_name = "testBED.bed" ) session.add(bed_track) session.commit() session.add(ViewTrack(bed_track.id,view.id)) session.commit()
def new_wigs(wig_file): current_id = -1 current_dict = None current_data = [] wig_ids = [] for line in wig_file: if line.startswith("track"): continue # this line starts a wig block elif line.startswith("variableStep") or line.startswith("fixedStep"): # validate the wig header is valid if not valid_wig_header(line): return jsonify(response="Wig format incorrect"), 404 # get the values from the header header_dict = parse_header(line) # commit the bases from the the previous wig if current_data: session.add_all(current_data) session.commit() # create the new wig wig = Wig(chrom=header_dict['chrom']) session.add(wig) session.commit() current_id = wig.id wig_ids.append(current_id) current_dict = header_dict current_data = [] # we have a data line else: # parse a var step line if current_dict['stepType'] == "variableStep": (position, score) = (int(val) for val in line.split()) # if there is no span it is easy if 'span' not in current_dict: wig_val = WigValue(position=position, value=score, wig_id=current_id) current_data.append(wig_val) # handle the span else: span = int(current_dict['span']) for pos in range(position, position + span): wig_val = WigValue(position=pos, value=score, wig_id=current_id) current_data.append(wig_val) # parse a fix step line else: score = int(line.strip()) # if there is no span it is easy if 'span' not in current_dict: wig_val = WigValue(position=current_dict['start'], value=score, wig_id=current_id) current_data.append(wig_val) current_dict['start'] += current_dict['step'] # handle the span else: span = current_dict['span'] for pos in range(current_dict['start'], current_dict['start'] + span): wig_val = WigValue(position=pos, value=score, wig_id=current_id) current_data.append(wig_val) current_dict['start'] += current_dict['step'] session.add_all(current_data) session.commit() return wig_ids
def new_file(): user_id = g.current_user_id file = request.files['file'] type = request.form['type'] track_name = request.form['track_name'] if request.form.has_key('track_name') else file.filename if not file: return jsonify(response="Can't create upload file! No file found in form data"),404 if not type in ['wig','bed','gtf','fasta']: return jsonify(response="Can't create upload file! {} is not a valid file type".format(type)),404 if type == 'fasta': fasta_id = new_fasta(file) new_track = Track( track_name = track_name, user_id = user_id, data_type = type, data_id = fasta_id, file_name = file.filename, ) session.add(new_track) session.commit() return jsonify(track_id = new_track.id) elif type == 'wig': wig_ids = new_wigs(file) new_tracks = [] count = 1 for wig_id in wig_ids: curr_name = "{}-{}".format(track_name, count) count += 1 new_track = Track( track_name = curr_name, user_id = user_id, data_type = type, data_id = wig_id, file_name = file.filename, ) new_tracks.append(new_track) session.add_all(new_tracks) session.commit() return jsonify(track_ids = [new_track.id for new_track in new_tracks]) elif type == 'gtf': gtf_id = new_gtf(file) new_track = Track( track_name = track_name, user_id = user_id, data_type = type, data_id = gtf_id, file_name = file.filename, ) session.add(new_track) session.commit() return jsonify(track_id = new_track.id) elif type == 'bed': bed_id = new_bed(file) new_track = Track( track_name = track_name, user_id = user_id, data_type = type, data_id = bed_id, file_name = file.filename, ) session.add(new_track) session.commit() return jsonify(track_id = new_track.id)
def new_wigs(wig_file): current_id = -1 current_dict = None current_data = [] wig_ids = [] for line in wig_file: if line.startswith("track"): continue # this line starts a wig block elif line.startswith("variableStep") or line.startswith("fixedStep"): # validate the wig header is valid if not valid_wig_header(line): return jsonify(response="Wig format incorrect"),404 # get the values from the header header_dict = parse_header(line) # commit the bases from the the previous wig if current_data: session.add_all(current_data) session.commit() # create the new wig wig = Wig(chrom=header_dict['chrom']) session.add(wig) session.commit() current_id = wig.id wig_ids.append(current_id) current_dict = header_dict current_data = [] # we have a data line else: # parse a var step line if current_dict['stepType'] == "variableStep": (position, score) = (int(val) for val in line.split()) # if there is no span it is easy if 'span' not in current_dict: wig_val = WigValue( position = position, value = score, wig_id = current_id ) current_data.append(wig_val) # handle the span else: span = int(current_dict['span']) for pos in range(position, position+span): wig_val = WigValue( position = pos, value = score, wig_id = current_id ) current_data.append(wig_val) # parse a fix step line else: score = int(line.strip()) # if there is no span it is easy if 'span' not in current_dict: wig_val = WigValue( position = current_dict['start'], value = score, wig_id = current_id ) current_data.append(wig_val) current_dict['start'] += current_dict['step'] # handle the span else: span = current_dict['span'] for pos in range(current_dict['start'], current_dict['start']+span): wig_val = WigValue( position = pos, value = score, wig_id = current_id ) current_data.append(wig_val) current_dict['start'] += current_dict['step'] session.add_all(current_data) session.commit() return wig_ids
def new_file(): user_id = g.current_user_id file = request.files['file'] type = request.form['type'] track_name = request.form['track_name'] if request.form.has_key( 'track_name') else file.filename if not file: return jsonify( response="Can't create upload file! No file found in form data" ), 404 if not type in ['wig', 'bed', 'gtf', 'fasta']: return jsonify( response="Can't create upload file! {} is not a valid file type". format(type)), 404 if type == 'fasta': fasta_id = new_fasta(file) new_track = Track( track_name=track_name, user_id=user_id, data_type=type, data_id=fasta_id, file_name=file.filename, ) session.add(new_track) session.commit() return jsonify(track_id=new_track.id) elif type == 'wig': wig_ids = new_wigs(file) new_tracks = [] count = 1 for wig_id in wig_ids: curr_name = "{}-{}".format(track_name, count) count += 1 new_track = Track( track_name=curr_name, user_id=user_id, data_type=type, data_id=wig_id, file_name=file.filename, ) new_tracks.append(new_track) session.add_all(new_tracks) session.commit() return jsonify(track_ids=[new_track.id for new_track in new_tracks]) elif type == 'gtf': gtf_id = new_gtf(file) new_track = Track( track_name=track_name, user_id=user_id, data_type=type, data_id=gtf_id, file_name=file.filename, ) session.add(new_track) session.commit() return jsonify(track_id=new_track.id) elif type == 'bed': bed_id = new_bed(file) new_track = Track( track_name=track_name, user_id=user_id, data_type=type, data_id=bed_id, file_name=file.filename, ) session.add(new_track) session.commit() return jsonify(track_id=new_track.id)
def seed_db(): # Setup default user # default_user = User(username="******", email="*****@*****.**", password="******") session.add(default_user) session.commit() # add the users user_ids = [default_user.id] users = ["kyle", "karl", "coda", "max", "goof"] for name in users: new_user = User(username=name, email="{}@email.com".format(name), password="******") session.add(new_user) session.commit() user_ids.append(new_user.id) ## Give each a track ## for u_id in user_ids: view = View(view_name="Test View", user_id=u_id) session.add(view) session.commit() # Create a fasta fasta = Fasta(header=">EBV1") session.add(fasta) session.commit() i = 0 for base in "ATTATTAGCATGCATGATCAGTAGCTAGGGGATGCATGCAACTGATCGATCGATGCATGCAT": bp = BasePair(nucleotide=base, position=i, fasta_id=fasta.id) i += 1 session.add(bp) session.commit() # Add a fasta track fasta_track = Track(track_name="Fasta Test Track", user_id=u_id, data_type="fasta", data_id=fasta.id, file_name="testFasta.fasta") session.add(fasta_track) session.commit() session.add(ViewTrack(fasta_track.id, view.id)) # Create a wig track wig = Wig(chrom='EBV1') session.add(wig) session.commit() for pos in range(1, 10): wig_val = WigValue(position=pos, value=1, wig_id=wig.id) session.add(wig_val) # add the track wig_track = Track(track_name="Wig Test Track", user_id=u_id, data_type="wig", data_id=wig.id, file_name="testWig.wig") session.add(wig_track) session.commit() session.add(ViewTrack(wig_track.id, view.id)) # Create a GTF track gtf = Gtf() session.add(gtf) session.commit() for pos in range(1, 10): gtf_val = GtfValue(seqname="EBV1", source="mRNA-Seq", feature="exon", start=pos, end=pos + 10, score=0, strand="+", frame=0, attribute="spanning-juncs=false;host=hg19", gtf_id=gtf.id) session.add(gtf_val) gtf_track = Track(track_name="Gtf Test Track", user_id=u_id, data_type="gtf", data_id=gtf.id, file_name="testGtf.gtf") session.add(gtf_track) session.commit() session.add(ViewTrack(gtf_track.id, view.id)) # last make a bed bed = Bed() session.add(bed) session.commit() for pos in range(1, 10): bed_val = BedValue(chrom="EBV1", start=pos, end=pos + 9, name="BED-THING", score=0, strand="+", thick_start=pos, thick_end=pos + 9, item_rgb=10, block_count=1, bed_id=bed.id) session.add(bed_val) session.commit() bb_size = BedBlockSize(index=pos, value=pos + 9, bed_value_id=bed_val.id) bb_start = BedBlockStart(index=pos, value=pos, bed_value_id=bed_val.id) session.add_all([bb_size, bb_start]) session.commit() bed_track = Track(track_name="Bed Test Track", user_id=u_id, data_type="bed", data_id=bed.id, file_name="testBED.bed") session.add(bed_track) session.commit() session.add(ViewTrack(bed_track.id, view.id)) session.commit()