Beispiel #1
0
    def _old_savepm():
        def _saveattr(attr):
            with unmask_all_peakmatrix(pm) as m:
                dm = m.attr_matrix(attr, flagged_only = False)

            dt = np.float64 if dm.dtype.kind == 'f' else \
                 np.int64 if dm.dtype.kind in ('i', 'u') else \
                 ('S%d' % np.max([list(map(len,l)) for l in dm]))

            ds = f.create_dataset(attr, dm.shape, dtype=dt)
            ds[...] = dm.astype(dt)
            ds.attrs['dtype'] = dm.dtype.str

        for a in pm.attributes: _saveattr(a)

        dset = f['mz']  # must exists in pm
        dset.attrs['class'] = 'PeakMatrix'
        dset.attrs['attributes'] = np.string_(pm.attributes)
        dset.attrs['mask'] = pm.mask

        with unmask_all_peakmatrix(pm):
            dset.attrs['peaklist_ids'] = np.string_(pm.peaklist_ids)
            for i, tags in enumerate(pm.peaklist_tags):
                dset.attrs['peaklist_tags_%d' % i] = np.string_([(t or 'None', v) for v,t in tags.to_list()])

            dset.attrs['flag_names'] = np.string_(pm.flag_names)
            for fn in pm.flag_names:
                dset.attrs[fn] = pm.flag_values(fn)
Beispiel #2
0
        def _saveattr(attr):
            with unmask_all_peakmatrix(pm) as m:
                dm = m.attr_matrix(attr, flagged_only = False)

            dt = np.float64 if dm.dtype.kind == 'f' else \
                 np.int64 if dm.dtype.kind in ('i', 'u') else \
                 ('S%d' % np.max([list(map(len,l)) for l in dm]))

            ds = f.create_dataset(attr, dm.shape, dtype=dt)
            ds[...] = dm.astype(dt)
            ds.attrs['dtype'] = dm.dtype.str
Beispiel #3
0
    def _saveattr(attr):
        if attr in f.keys():
            raise IOError('attribute [%s] already exists' % attr)

        with unmask_all_peakmatrix(pm) as m:
            dm = m.attr_matrix(attr, flagged_only=False)

        dt = np.float64 if dm.dtype.kind == 'f' else \
             np.int64 if dm.dtype.kind in ('i', 'u') else \
             ('S%d' % np.max(map(lambda l: map(len,l), dm)))

        ds = f.create_dataset(attr, dm.shape, dtype=dt)
        ds[...] = dm.astype(dt)
        ds.attrs['dtype'] = dm.dtype.str
Beispiel #4
0
def save_peak_matrix_as_txt(pm: PeakMatrix, filename: str, *args, **kwargs):
    """
    Saves a peak matrix in plain text file.

    :param pm: the target peak matrix object
    :param filename: path to a new text file
    :param args: arguments to be passed to PeakMatrix.to_str
    :param kwargs: keyword arguments to be passed to PeakMatrix.to_str

    """
    if os.path.isfile(filename):
        logging.warning('plain text file [%s] already exists, override' % filename)
    with open(filename, 'w') as f:
        with unmask_all_peakmatrix(pm) as m: f.write(m.to_str(*args, **kwargs))
Beispiel #5
0
def save_peak_matrix_as_hdf5(pm, filename):
    """
    Saves a peak matrix object to a HDF5 file.

    :param pm: the target peak matrix object
    :param filename: path to a new HDF5 file

    The order of the attributes and flags will be retained.

    """
    if os.path.isfile(filename):
        logging.warning('HDF5 database [%s] already exists and override' %
                        filename)
    f = h5py.File(filename, 'w')

    def _saveattr(attr):
        if attr in f.keys():
            raise IOError('attribute [%s] already exists' % attr)

        with unmask_all_peakmatrix(pm) as m:
            dm = m.attr_matrix(attr, flagged_only=False)

        dt = np.float64 if dm.dtype.kind == 'f' else \
             np.int64 if dm.dtype.kind in ('i', 'u') else \
             ('S%d' % np.max(map(lambda l: map(len,l), dm)))

        ds = f.create_dataset(attr, dm.shape, dtype=dt)
        ds[...] = dm.astype(dt)
        ds.attrs['dtype'] = dm.dtype.str

    map(_saveattr, pm.attributes)

    dset = f['mz']  # must exists in pm
    dset.attrs['class'] = 'PeakMatrix'
    dset.attrs['attributes'] = pm.attributes
    dset.attrs['mask'] = pm.mask

    with unmask_all_peakmatrix(pm):
        dset.attrs['peaklist_ids'] = pm.peaklist_ids
        for i, tags in enumerate(pm.peaklist_tags):
            dset.attrs['peaklist_tags_%d' % i] = [(t or 'None', v)
                                                  for v, t in tags.to_list()]

        dset.attrs['flag_names'] = pm.flag_names
        for fn in pm.flag_names:
            fvals = pm.flag_values(fn)
            dset.attrs[fn] = fvals if fvals.nbytes < 64000 else _packBool(
                fvals) if fvals.dtype.kind == 'b' else _packMeta(fvals)
Beispiel #6
0
    def _savepm():
        def _saveattr(attr):
            with unmask_all_peakmatrix(pm) as m:
                dm = m.attr_matrix(a, flagged_only = False)
                ds = f.create_array(f.root, a, dm)
                ds.attrs.dtype = dm.dtype.str
        for a in pm.attributes: _saveattr(a)

        dset = f.root.mz  # must exists in pm
        dset.attrs.data_class = 'PeakMatrix'
        dset.attrs.attributes = pm.attributes
        dset.attrs.mask = pm.mask

        with unmask_all_peakmatrix(pm):
            dset.attrs.peaklist_ids = pm.peaklist_ids
            for i, tags in enumerate(pm.peaklist_tags):
                dset.attrs['peaklist_tags_%d' % i] = np.array([(t or 'None', v) for v,t in tags.to_list()])

            dset.attrs.flag_names = pm.flag_names
            for fn in pm.flag_names:
                dset.attrs[fn] = pm.flag_values(fn)
Beispiel #7
0
    def test_pm_mask(self):
        pm = self._createPeakMatrix()

        self.assertEqual(set(map(lambda x: x.value, pm.tags_of('plate'))),
                         {1, 2})
        self.assertEqual(set(map(lambda x: x.value, pm.tags_of())),
                         {'sample', 'qc'})
        self.assertRaises(KeyError, lambda: pm.tags_of('treatment'))
        self.assertRaises(KeyError, lambda: pm.tags_of('not_exist'))

        pm.mask_tags(1)
        self.assertTupleEqual(pm.peaklist_ids,
                              ('sample_1_1', 'sample_1_2', 'QC_1',
                               'sample_2_1', 'sample_2_2', 'QC_2'))
        pm.mask_tags('qc', plate=1)  # mask samples with both of the two
        self.assertTupleEqual(
            pm.peaklist_ids,
            ('sample_1_1', 'sample_1_2', 'sample_2_1', 'sample_2_2', 'QC_2'))
        pm.mask = None
        pm.mask_tags('qc')
        self.assertTupleEqual(
            pm.peaklist_ids,
            ('sample_1_1', 'sample_1_2', 'sample_2_1', 'sample_2_2'))
        pm.mask = None
        pm.mask_tags('qc').mask_tags(plate=1)
        self.assertTupleEqual(pm.peaklist_ids, ('sample_2_1', 'sample_2_2'))
        pm.mask = None
        pm.mask_tags('not_exist')
        self.assertTupleEqual(pm.peaklist_ids,
                              ('sample_1_1', 'sample_1_2', 'QC_1',
                               'sample_2_1', 'sample_2_2', 'QC_2'))

        pm.mask = [True] * 6
        pm.unmask_tags('qc', plate=1)  # unmask samples with both of the two
        self.assertTupleEqual(pm.peaklist_ids, ('QC_1', ))
        pm.mask = [True] * 6
        pm.unmask_tags('qc')
        self.assertTupleEqual(pm.peaklist_ids, ('QC_1', 'QC_2'))
        pm.mask = [True] * 6
        pm.unmask_tags('qc').unmask_tags(plate=1)
        self.assertTupleEqual(pm.peaklist_ids,
                              ('sample_1_1', 'sample_1_2', 'QC_1', 'QC_2'))
        pm.mask = [True] * 6
        pm.unmask_tags('not_exist')
        self.assertTupleEqual(pm.peaklist_ids, ())

        pm.unmask_tags('qc', override=True)
        self.assertTupleEqual(pm.peaklist_ids, ('QC_1', 'QC_2'))
        with mask_all_peakmatrix(pm) as m:
            m.unmask_tags('sample')
            self.assertTupleEqual(
                pm.peaklist_ids,
                ('sample_1_1', 'sample_1_2', 'sample_2_1', 'sample_2_2'))

        pm.mask_tags('qc', override=True)
        self.assertTupleEqual(
            pm.peaklist_ids,
            ('sample_1_1', 'sample_1_2', 'sample_2_1', 'sample_2_2'))
        with unmask_all_peakmatrix(pm) as m:
            m.mask_tags('sample')
            self.assertTupleEqual(m.peaklist_ids, ('QC_1', 'QC_2'))
        self.assertTupleEqual(
            pm.peaklist_ids,
            ('sample_1_1', 'sample_1_2', 'sample_2_1', 'sample_2_2'))

        pm.mask = None
        with unmask_peakmatrix(pm, plate=1) as m:
            self.assertTupleEqual(m.peaklist_ids,
                                  ('sample_1_1', 'sample_1_2', 'QC_1'))
            self.assertTupleEqual(m.full_shape, (6, 10))
        self.assertEqual(len(pm.peaklist_ids), 6)

        with mask_peakmatrix(pm, plate=2) as m:
            self.assertTupleEqual(m.peaklist_ids,
                                  ('sample_1_1', 'sample_1_2', 'QC_1'))
            with unmask_all_peakmatrix(pm) as mm:
                self.assertTupleEqual(mm.peaklist_ids,
                                      ('sample_1_1', 'sample_1_2', 'QC_1',
                                       'sample_2_1', 'sample_2_2', 'QC_2'))

        with mask_peakmatrix(pm, 'qc') as m:
            m.remove_samples((1, 2))
        self.assertTupleEqual(pm.peaklist_ids,
                              ('sample_1_1', 'QC_1', 'sample_2_2', 'QC_2'))
Beispiel #8
0
 def _saveattr(attr):
     with unmask_all_peakmatrix(pm) as m:
         dm = m.attr_matrix(a, flagged_only = False)
         ds = f.create_array(f.root, a, dm)
         ds.attrs.dtype = dm.dtype.str