def test_prebuild_index(self): test_dir = tempfile.mkdtemp() work_path = os.path.join(test_dir, self.path) with open(work_path, 'w') as dest, open(self.path) as source: dest.write(source.read()) assert dest.closed with mgf.IndexedMGF(work_path) as inst: offsets_exist = os.path.exists(inst._byte_offset_filename) self.assertEqual(offsets_exist, inst._check_has_byte_offset_file()) self.assertTrue(isinstance(inst._offset_index, aux.OffsetIndex)) self.assertTrue(inst._source.closed) mgf.IndexedMGF.prebuild_byte_offset_file(work_path) with mgf.IndexedMGF(work_path) as inst: offsets_exist = os.path.exists(inst._byte_offset_filename) self.assertTrue(offsets_exist) self.assertEqual(offsets_exist, inst._check_has_byte_offset_file()) self.assertTrue(isinstance(inst._offset_index, aux.OffsetIndex)) self.assertTrue(inst._source.closed) os.remove(inst._byte_offset_filename) with mgf.IndexedMGF(work_path) as inst: offsets_exist = os.path.exists(inst._byte_offset_filename) self.assertEqual(offsets_exist, inst._check_has_byte_offset_file()) self.assertTrue(isinstance(inst._offset_index, aux.OffsetIndex)) self.assertTrue(inst._source.closed) shutil.rmtree(test_dir, True)
def _create_parser(self): if self._use_index: return mgf.IndexedMGF(self.source_file, read_charges=False, convert_arrays=1, encoding=self.encoding) else: return mgf.MGF(self.source_file, read_charges=False, convert_arrays=1, encoding=self.encoding)
def process_maracluster_mgf(fname, get_cluster=get_cluster_id, get_pepmass=naive_average_mass_and_charge, get_rt=median_rt, **kwargs): outputs = [] with mgf.IndexedMGF(fname, read_charges=False) as fin: titles = fin.index.keys() for cluster_id, cluster_group in groupby(titles, get_cluster): ids = list(cluster_group) spectra = fin[ids] mz, c = get_pepmass(spectra) rt = get_rt(spectra) outputs.append(average_spectrum( spectra, cluster_id, pepmass=mz, charge=c, rtinseconds=rt, **kwargs)) return outputs
def test_write_index_keys(self): test_dir = tempfile.mkdtemp() work_path = os.path.join(test_dir, self.path) with open(work_path, 'wb') as dest, open(self.path, 'rb') as source: dest.write(source.read()) assert dest.closed mgf.IndexedMGF.prebuild_byte_offset_file(work_path) with mgf.IndexedMGF(work_path) as inst: ipath = inst._byte_offset_filename with open(ipath) as ifp: container = json.load(ifp, object_hook=OrderedDict) tag_key = mgf.IndexedMGF._index_class._schema_version_tag_key self.assertEqual(set(container.keys()), {tag_key, 'index'}) self.assertEqual(tuple(container[tag_key]), mgf.IndexedMGF._index_class.schema_version) self.assertEqual( container['index'], [['Spectrum 1', [217, 343]], ['Spectrum 2', [343, 506]]])
def test_map(self): with mgf.IndexedMGF(self.path) as reader: spectra = sorted(list(reader.map()), key=lambda s: s['params']['title']) self.assertEqual(data.mgf_spectra_long, spectra)
def test_indexedmgf_picklable(self): with mgf.IndexedMGF(self.path) as reader: spec = pickle.dumps(reader) with pickle.loads(spec) as reader: self.assertEqual(data.mgf_spectra_long[0], next(reader))
def test_read_list(self): key = ['Spectrum 2', 'Spectrum 1'] with mgf.IndexedMGF(self.path) as f: self.assertEqual(data.mgf_spectra_long[::-1], f[key])
def read_mgf(file_path): return mgf.IndexedMGF(file_path)
def test_key_access_ions(self): with mgf.IndexedMGF(self.path_annotated, read_ions=True) as f: np.testing.assert_array_equal( f['RAEYWENYPPAH||3']['ion array'], self.spectra_annotated[1]['ion array'])