def eval_import_file_annotation(self, file_format, sample_file_path, annotation_file_path, expected_res, user): #sample_file_path = 'fragmentation/tests/files/test_import_annotation.mgf' with open(sample_file_path, 'rb') as fss: fs = FragSample.import_sample(fss, user, 'name', 'file_name') fs.wait_import_done() #annotation_file_path = 'fragmentation/tests/files/para_annotation.csv' with open(annotation_file_path, 'rb') as fa: #fs.import_annotation_file(fa) fs.import_annotation_file(fa, file_format) for er in expected_res: m = Molecule.find_from_smiles(er[1]) self.assertTrue(m) fms_search = FragMolSample.objects.filter(frag_sample=fs, ion_id=er[0]) self.assertEqual(fms_search.count(), 1) fms = fms_search.first() fa_search = FragAnnotationDB.objects.filter( molecule=m, frag_mol_sample=fms, db_source=er[2], db_id=er[3], ) self.assertEqual(fa_search.count(), 1, fa_search.count())
def create_frag_sample(self, ion_charge="positive"): if not self.user: self.create_user() sample_file_path = "fragmentation/tests/files/example.mgf" with open(sample_file_path, "rb") as fss: fs = FragSample.import_sample(fss, self.user, "name", ion_charge=ion_charge) fs.wait_import_done() return fs
def test_ions_limit(self): sample_file_path, u = self.path_and_user() FragSample.IONS_LIMIT = 2 with self.assertRaises(IntegrityError): with open(sample_file_path, 'rb') as fss: fs = FragSample.import_sample(fss, u, 'name', 'file_name', energy=0) fs.wait_import_done() FragSample.IONS_LIMIT = 10000
def create_frag_sample(self, sample_file_path=None): if not sample_file_path: sample_file_path = self.sample_file_path self.get_user() with open(sample_file_path, "rb") as fss: fs = FragSample.import_sample(fss, self.user, "name", "file_name", task=False) fs.wait_import_done() return fs
def uploadfile(self, request): req_data = request.data try: fs = FragSample.import_sample(file_object=req_data['file_data'], user=request.user, file_name=req_data['file_name'], name=req_data['name'], description=req_data['description'], task=True) return Response({'status': 'ok'}) except IntegrityError as e: return Response({'error': e.message})
def test_cosine_2_mgf(self): # user = get_user_model().objects.create() fc = self.new_frag_compare() # import fragsamples fm = [] for fn in ["cosine_1.mgf", "cosine_2.mgf"]: with open(self.TEST_FILES_PATH + fn, "rb") as fr: fs = FragSample.import_sample( fr, FragCompareModelTests.user, "name", fn ) fs.wait_import_done() fm.append(fs.fragmolsample_set.first()) fmc = fc.compare_frag_mols(fm) self.assertNotEqual(fmc, None)
def test_import_sample(self): sample_file_path, u = self.path_and_user() with open(sample_file_path, 'r') as fss: exp_res = ''.join(fss.readlines()) with open(sample_file_path, 'rb') as fss: fs = FragSample.import_sample(fss, u, 'name', 'file_name', energy=0, task=False) fs.wait_import_done() self.maxDiff = None self.assertEqual(fs.gen_mgf(energy=0), exp_res)
def uploadfile(self, request): req_data = request.data try: fs = FragSample.import_sample( file_object=req_data["file_data"], user=request.user, file_name=req_data["file_name"], name=req_data["name"], description=req_data["description"], ion_charge=req_data["ion_charge"], task=True, ) return Response({"status": "ok"}) except IntegrityError as e: return Response({"error": str(e)})
def test_cosine_mgf_sim(self): # user = get_user_model().objects.create() fc = self.new_frag_compare() with open(self.TEST_FILES_PATH + "cosine_4.mgf", "rb") as fr: fsample = FragSample.import_sample( fr, FragCompareModelTests.user, "name", "file_name" ) fsample.wait_import_done() sm = "N=C(N)NCC/C=C1/N=C(O)N(/C=C/c2ccc(O)cc2)C1=O" m = Molecule.load_from_smiles(sm) fsim = FragCommonTests.new_frag_sim() fms = fsim.frag_molecule(m) fmc = fc.compare_frag_mols([fsample.fragmolsample_set.first(), fms]) # print(fmc.cosine)#, fmc.num_frag_match self.assertTrue(fmc.cosine >= 0.2, fmc.cosine)
def import_annotation_file( self, file_format, sample_file_path, annotation_file_path, user, ): with open(sample_file_path, "rb") as fss: frag_sample = FragSample.import_sample(fss, user, "name", "file_name") frag_sample.wait_import_done() with open(annotation_file_path, "rb") as fa: frag_sample.import_annotation_file(fa, file_format) return frag_sample
def test_mass_delta(self): u = get_user_model().objects.create() sample_folder = 'fragmentation/tests/files/test_annotation_project/' sample_file_path = sample_folder + 'test_annotation_project.mgf' with open(sample_file_path, 'rb') as fss: fs = FragSample.import_sample(fss, u, 'name', 'file_name', energy=0, task=False) fs.wait_import_done() self.assertIsNotNone(fs.mass_delta_single) self.assertIsNotNone(fs.mass_delta_double) self.assertIn(14.01565, fs.mass_delta_single.value) self.assertNotIn(77.074168, fs.mass_delta_double.value) fs.reaction_mass_max = 100.0 fs.save() fs.gen_mass_delta(update_reaction_mass_max=False) self.assertIn(77.074168, fs.mass_delta_double.value)
def test_mass_selection(self): u = get_user_model().objects.create(email='*****@*****.**') methylation = Reaction.objects.create(user=u, name='methylation', smarts='[N,O:1]>>[*:1]-[#6]') methylation.run_reaction([Molecule.load_from_smiles('CCO')]) methylation_modified = Reaction.objects.create( user=u, name='methylation_modified', smarts='[N,O:1]>>IC(I)C[*:1]') methylation_modified.run_reaction([Molecule.load_from_smiles('CCO')]) diels_alder = Reaction.objects.create( user=u, name='diels_alder', smarts= '[#6:1]=,:[#6:2]-[#6:3]=,:[#6:4].[#6:5]=,:[#6:6]>>[#6:1]1-[#6:2]=,:[#6:3]-[#6:4]-[#6:6]-[#6:5]-1' ) diels_alder.run_reaction([ Molecule.load_from_smiles('C=Cc1c[nH]c(N)n1'), Molecule.load_from_smiles('C=C') ]) diels_alder_modified = Reaction.objects.create( user=u, name='diels_alder_modified', smarts= '[#6:1]=,:[#6:2]-[#6:3]=,:[#6:4]-[H].[#6:5]=,:[#6:6]>>[#6]-[#6:2]-1=,:[#6:3]-[#6:4]-[#6:6]-[#6:5]-[#6:1]-1' ) diels_alder_modified.run_reaction([ Molecule.load_from_smiles('C=Cc1c[nH]c(N)n1'), Molecule.load_from_smiles('C=C') ]) for r in [methylation, diels_alder, diels_alder_modified]: r.status_code = Reaction.status.ACTIVE r.save() sample_folder = 'fragmentation/tests/files/test_annotation_project/' sample_file_path = sample_folder + 'test_annotation_project.mgf' anno_file_path = sample_folder + 'anno_1.csv' with open(sample_file_path, 'rb') as fss: fs = FragSample.import_sample(fss, u, energy=2) # Import annotation with open(anno_file_path, 'rb') as f_annot: fs.import_annotation_file(f_annot) p = SampleAnnotationProject.objects.create(\ user = u, depth_total = 3, depth_last_match = 0) p.save() p.update_frag_sample(fs) p.select_reactions_by_mass() self.assertIn(methylation, p.reactions()) self.assertNotIn(methylation_modified, p.reactions()) self.assertIn(diels_alder, p.reactions()) self.assertNotIn(diels_alder_modified, p.reactions()) # test frag_sample.gen_mass_delta reevaluation # when new reaction added methylation_modified.status_code = Reaction.status.ACTIVE methylation_modified.save() self.assertNotIn(methylation_modified, p.reactions()) p.select_reactions_by_mass() self.assertIn(methylation_modified, p.reactions())