def do_save(user, **kwargs): can_approve = user.role.Type == 'super-admin' id = kwargs.get("ID", None) was_approved = None uid = None if not id or can_approve:# Only super-admins can edit a molecule for now if id: mole = Molecule.query.filter_by(ID=kwargs['ID']).first() already_approved = mole.Approved mole.merge_fields(**kwargs) was_approved = mole.Approved and not already_approved mole.validate_required_fields() else: mole = Molecule() mole.merge_fields(**kwargs) mole.UserID = user.UserID mole.Approved = can_approve and kwargs['Approved'] mole.validate_required_fields() mole.save() uid = mole.ID if was_approved: message = "%s was approved" % mole.Name comment = Comment(UserID=user.UserID, Type='Molecules',ParentID=mole.ID, Message=message, RenderType='text') comment.save() if uid: mole = Molecule.query.filter_by(ID=uid).first() return mole return None
def create_molecules(user_ids): molecule_sofie_id = {} for x in xrange(1, 30): res = requests.get('http://www.sofienetwork.com/probe/%i/' % x, \ headers={'Accept': 'application/json', 'Authorization': authorization}) try: molecule_json = res.json() spn_id = molecule_json["ID"] del molecule_json['ID'] user_id = molecule_json['UserID'] molecule_json['UserID'] = user_ids[user_id] if user_id in user_ids else None molecule = Molecule(**molecule_json) res = requests.get('http://www.sofienetwork.com/probe/%i/image/' % x, headers={'Authorization': authorization}) molecule._Image = res.content molecule.save() print molecule.ID molecule_sofie_id[spn_id] = molecule.ID res = requests.get('http://www.sofienetwork.com/keyword/Molecules/%i/' % x, headers={'Authorization': authorization}) keywords = res.json() for keyword in keywords: del keyword['CreationDate'] del keyword['KeywordID'] key = Keyword(**keyword) key.ParentID = molecule.ID key.save() except Exception as e: print str(e) print 'molecule insertion error' db.session.rollback() return molecule_sofie_id
def create(): data = request.json try: mole = do_save(g.user, **data) if request.headers.get("Accept") == "application/json": return Response(json.dumps(mole.to_hash()), headers={"Content-Type": "application/json"}) return redirect("/") except Exception as e: db.session.rollback() msg = str(e) if e.__class__ == ValidationException().__class__: msg = e.errors() if request.headers.get("Accept") == "application/json": return Response(json.dumps({"molecule": Molecule.to_hash(), "error_details": msg}), status=400, headers={"Content-Type": "application/json"}) flash(msg) return render_template("molecule/new.html", molecule=Molecule().to_hash()) """
def construct_ingredient(json: Dict, type: str) -> Ingredient: """ Returns an Ingredient object from a given JSON file """ ingredient_type = IngredientType(type) molecules: List[Molecule] = [ Molecule(m["pubchem_id"], m["common_name"], tuple(m["fooddb_flavor_profile"].split("@"))) for m in json["molecules"] ] return Ingredient(json["entity_alias_readable"], json["category_readable"], json["entity_id"], molecules, ingredient_type)
def autofill(cid): mole = Molecule(CID=cid) mole.autofill(get_image=False) keywords = mole.get_keywords() keyword_resp = [keyword.to_hash() for keyword in keywords] resp = {"molecule": mole.to_hash(), "keywords": keyword_resp} return Response(json.dumps(resp), content_type="application/json")
def new(): mole = Molecule() resp = json.dumps(mole.to_hash()) return render_template("molecule/edit.html", molecule=resp, runninguser=json.dumps(g.user.to_hash()))
for my_cids in chunks(objs.keys(), 10): local_cids = [str(cid) for cid in my_cids] url = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/%s/property/MolecularFormula/JSON" % ",".join(local_cids) res = requests.get(url) res = res.json() for prop in res['PropertyTable']['Properties']: cid = prop["CID"] objs[cid]["Formula"] = prop["MolecularFormula"] time.sleep(1) for key in objs.keys(): obj = objs[key] try: mole = Molecule(**obj) mole.save() except: db.session.rollback() print 'failed to save' ### Get Molecule Images moles = Molecule.query.all() """ for mole in moles: mole.save_image_from_pubchem() print 'saving image' time.sleep(1) """
def seed_data(): molecule = Molecule(CID=3232583, DisplayFormat="<p>[<sup>18</sup>F]-<sub>D</sub>-FEAU</p>", \ Description="FEAU is an nucleoside analog and a highly specific substrate for herpes simplex " \ "virus 1 thymidine kinase (HSV1-tk). FEAU is phosphorylated by HSV1-tk " \ "and subsequently retained intracellularly; hence it can annotate HSV1-tk " \ "expression in vivo. The gene encoding HSV1-tk is a commonly used reporter gene " \ "in cell-based therapy studies.", Name="<p>[<sup>18</sup>F]-<sub>D</sub>-FEAU</p>") molecule.save() molecule1 = Molecule(CID=3232584, DisplayFormat="<p>[<sup>18</sup>F]-<sub>D</sub>-FEAU</p>", \ Description="FEAU is an nucleoside analog and a highly specific substrate for herpes simplex " \ "virus 1 thymidine kinase (HSV1-tk). FEAU is phosphorylated by HSV1-tk " \ "and subsequently retained intracellularly; hence it can annotate HSV1-tk " \ "expression in vivo. The gene encoding HSV1-tk is a commonly used reporter gene " \ "in cell-based therapy studies.", Name="FEAU") molecule1.save() molecule2 = Molecule(CID=3232585, DisplayFormat="<p>[<sup>18</sup>F]-<sub>D</sub>-FEAU</p>", \ Description="FEAU is an nucleoside analog and a highly specific substrate for herpes simplex " \ "virus 1 thymidine kinase (HSV1-tk). FEAU is phosphorylated by HSV1-tk " \ "and subsequently retained intracellularly; hence it can annotate HSV1-tk " \ "expression in vivo. The gene encoding HSV1-tk is a commonly used reporter gene " \ "in cell-based therapy studies.", Name="FEAU") molecule2.save() molecule3 = Molecule(CID=3232586, DisplayFormat="<p>[<sup>18</sup>F]-<sub>D</sub>-FEAU</p>", \ Description="FEAU is an nucleoside analog and a highly specific substrate for herpes simplex " \ "virus 1 thymidine kinase (HSV1-tk). FEAU is phosphorylated by HSV1-tk " \ "and subsequently retained intracellularly; hence it can annotate HSV1-tk " \ "expression in vivo. The gene encoding HSV1-tk is a commonly used reporter gene " \ "in cell-based therapy studies.", Name="FEAU") molecule3.save() molecule.save_synonyms_from_pubchem() accounts = generate_accounts() main_account = accounts[0] generate_forums(main_account.id) alternative_account = accounts[1] chemist, admin, super_admin = generate_roles() main_user = generate_user(main_account.id, "test", super_admin.RoleID) create_approval_following() main_account_users = generate_users(main_account.id, 5, chemist.RoleID) alternative_account_users = generate_users(alternative_account.id, 5, chemist.RoleID) competitor_user = alternative_account_users[0] sequence_id = generate_zipped_sequence(main_user, molecule.ID) data = open(os.path.join("seeds", "molecule.jpeg"), 'r') data = data.read() reaction_scheme = SequenceAttachment(SequenceID=sequence_id, FileName='molecules.jpg', Type="ReactionScheme", Attachment=data) reaction_scheme.save() competitor_sequence = generate_zipped_sequence(competitor_user, molecule.ID) follow = Follower(UserID=main_user, Type="Sequences", ParentID=competitor_sequence) follow.save() follow = Follower(UserID=main_user, Type="Molecules", ParentID=molecule.ID) follow.save() generate_comment(main_user, sequence_id) generate_comment(competitor_user, sequence_id, "My #sequence produces a better yield") generate_private_sequence(main_user, molecule.ID) generate_private_sequence(main_user, molecule.ID, False) for x in xrange(PAGINATION_SIZE+2): sequence_id = generate_sequence(main_user, molecule.ID) sequence = Sequence.query.filter_by(SequenceID=sequence_id).first() sequence.StartingActivity = 21.33 sequence.Yield = 12.55 sequence.SpecificActivity = 20.3 sequence.NumberOfSteps = 20 sequence.SynthesisTime = 400 sequence.save() generate_comment(main_user, competitor_sequence, "Testing Pagination size %i" % x) image_comment = Comment(ParentID=1,Type='Sequences',RenderType='image',Message='',UserID=main_user) image_comment.save() data = open(os.path.join("seeds", "molecule.jpeg"), 'r') data = data.read() image = SequenceAttachment(ParentID=image_comment.CommentID,Type='comments',Attachment=data) image.save() generate_unique_downloads() generate_issues() time.sleep(15) generate_issues()