Add standard PED file attributes into metadata if not present.

    http://pngu.mgh.harvard.edu/~purcell/plink/data.shtml#ped
961842
def _add_ped_metadata(name, metadata):
    
    ignore = set(["-9", "undefined", "unknown", "."])
    def _ped_mapping(x, valmap):
        try:
            x = int(x)
        except ValueError:
            x = -1
        for k, v in valmap.items():
            if k == x:
                return v
        return None
    def _ped_to_gender(x):
        return _ped_mapping(x, {1: "male", 2: "female"})
    def _ped_to_phenotype(x):
        known_phenotypes = set(["unaffected", "affected", "tumor", "normal"])
        if x in known_phenotypes:
            return x
        else:
            return _ped_mapping(x, {1: "unaffected", 2: "affected"})
    def _ped_to_batch(x):
        if x not in ignore and x != "0":
            return x
    with open(metadata["ped"]) as in_handle:
        for line in in_handle:
            parts = line.split("\t")[:6]
            if parts[1] == str(name):
                for index, key, convert_fn in [(4, "sex", _ped_to_gender), (0, "batch", _ped_to_batch),
                                               (5, "phenotype", _ped_to_phenotype)]:
                    val = convert_fn(parts[index])
                    if val is not None and key not in metadata:
                        metadata[key] = val
                break
    return metadata
1056270
def add_item_metadata(self, handle, key, value):
        
        if not os.path.isdir(self._metadata_fragments_abspath):
            os.mkdir(self._metadata_fragments_abspath)

        prefix = self._handle_to_fragment_absprefixpath(handle)
        fpath = prefix + '.{}.json'.format(key)

        with open(fpath, 'w') as fh:
            json.dump(value, fh)
1265395
def write_metadata(self):
        """"""
        import mutagen
        from mutagen import id3

        id3 = id3.ID3(self.filename)
        for tag in self.metadata.keys():
            value = self.metadata[tag]
            frame = mutagen.id3.Frames[tag](3, value)
            try:
                id3.add(frame)
            except:
                raise IOError('EncoderError: cannot tag "' + tag + '"')
        try:
            id3.save()
        except:
            raise IOError('EncoderError: cannot write tags')
1199641
def update_asset_attributes
      if !self.external_service? and self.present? and self.changed?
        self.content_type = self.filename.file.content_type
        self.file_size = self.filename.file.size
        self.width, self.height = `identify -format "%wx%h" #{self.filename.file.path}`.split(/x/) unless self.document? or self.width? or self.height?
        self.ratio = self.width.to_f / self.height.to_f if self.width.present? and self.height.present?
      end
    end
788719
def addRnaQuantMetadata(self, fields):
        
        self._featureSetIds = fields["feature_set_ids"].split(',')
        self._description = fields["description"]
        self._name = fields["name"]
        self._biosampleId = fields.get("biosample_id", "")
        if fields["read_group_ids"] == "":
            self._readGroupIds = []
        else:
            self._readGroupIds = fields["read_group_ids"].split(',')
        if fields["programs"] == "":
            self._programs = []
        else:
            # Need to use program Id's here to generate a list of Programs
            # for now set to empty
            self._programs = []
653946
protected function wizardAddFile()
    {
        $this->validatePrompt('path', $this->text('Path'), 'file');
        $this->validatePrompt('entity', $this->text('Entity'), 'file');
        $this->validatePrompt('entity_id', $this->text('Entity ID'), 'file', 0);
        $this->validatePrompt('title', $this->text('Title'), 'file', '');
        $this->validatePrompt('description', $this->text('Description'), 'file', '');
        $this->validatePrompt('weight', $this->text('Weight'), 'file', 0);

        $this->validateComponent('file');
        $this->addFile();
    }
888037
def _collate_metadata(self):
        
        import pandas as pd

        DEFAULT_FIELDS = None
        metadata = []

        for c in self._classifications:
            m = c.sample.metadata

            if DEFAULT_FIELDS is None:
                DEFAULT_FIELDS = list(m._resource._schema["properties"].keys())
                DEFAULT_FIELDS.remove("$uri")
                DEFAULT_FIELDS.remove("sample")

            metadatum = {f: getattr(m, f) for f in DEFAULT_FIELDS}
            metadatum["classification_id"] = c.id
            metadatum["sample_id"] = m.sample.id
            metadatum["metadata_id"] = m.id
            metadatum["created_at"] = m.sample.created_at
            metadatum["filename"] = c.sample.filename

            metadatum.update(m.custom)
            metadata.append(metadatum)

        if metadata:
            metadata = pd.DataFrame(metadata).set_index("classification_id")
        else:
            metadata = pd.DataFrame(
                columns=["classification_id", "sample_id", "metadata_id", "created_at"]
            )

        self._cached["metadata"] = metadata
90797
def save_hdf_metadata(filename, metadata, groupname="data", mode="a"):
    """""""
    with _h5py.File(filename, mode) as f:
        for key, val in metadata.items():
            f[groupname].attrs[key] = val
439198
def _add_embedding_config(file_path, data_dir, has_metadata=False, label_img_shape=None):
    """"""
    with open(os.path.join(file_path, 'projector_config.pbtxt'), 'a') as f:
        s = 'embeddings {\n'
        s += 'tensor_name: "{}"\n'.format(data_dir)
        s += 'tensor_path: "{}"\n'.format(os.path.join(data_dir, 'tensors.tsv'))
        if has_metadata:
            s += 'metadata_path: "{}"\n'.format(os.path.join(data_dir, 'metadata.tsv'))
        if label_img_shape is not None:
            if len(label_img_shape) != 4:
                logging.warning('expected 4D sprite image in the format NCHW, while received image'
                                ' ndim=%d, skipping saving sprite'
                                ' image info', len(label_img_shape))
            else:
                s += 'sprite {\n'
                s += 'image_path: "{}"\n'.format(os.path.join(data_dir, 'sprite.png'))
                s += 'single_image_dim: {}\n'.format(label_img_shape[3])
                s += 'single_image_dim: {}\n'.format(label_img_shape[2])
                s += '}\n'
        s += '}\n'
        f.write(s)
870731
def add_resource(mt_file, ref, cache):
    """"""

    if isinstance(mt_file, MetapackDoc):
        doc = mt_file
    else:
        doc = MetapackDoc(mt_file)

    if not 'Resources' in doc:
        doc.new_section('Resources')

    doc['Resources'].args = [e for e in set(doc['Resources'].args + ['Name', 'StartLine', 'HeaderLines', 'Encoding']) if
                             e]

    seen_names = set()

    u = parse_app_url(ref)

    # The web and file URLs don't list the same.

    if u.proto == 'file':
        entries = u.list()
    else:
        entries = [ssu for su in u.list() for ssu in su.list()]

    errors = []

    for e in entries:
        if not add_single_resource(doc, e, cache=cache, seen_names=seen_names):
            errors.append(e)

    if errors:
        prt()
        warn("Found, but failed to add these urls:")
        for e in errors:
            print('    ', e)

    write_doc(doc, mt_file)
300851
def add_metadata_comments(self, d, metadata):
        

        if len(metadata) > 2:
            string_pairs = metadata[1:-1]  # get all metadata pairs
            for sp in string_pairs:
                # get the raw metadata key

                if isinstance(sp.children[0], Token):
                    token = sp.children[0]
                    assert token.type == "UNQUOTED_STRING"
                    key = token.value
                else:
                    # quoted string (double or single)
                    token = sp.children[0].children[0]
                    key = token.value

                # clean it to match the dict key
                key = self._mapfile_todict.clean_string(key).lower()
                assert key in d.keys()
                key_comments = self.get_comments(sp.meta)
                d["__comments__"][key] = key_comments

        return d
738888
def _GenerateApiFileDetails(path_infos):
  """"""

  type_attrs = []
  hash_attrs = []
  size_attrs = []
  stat_attrs = []
  pathspec_attrs = []

  def _Value(age, value):
    """Generate ApiAff4ObjectAttributeValue from an age and a value."""

    v = ApiAff4ObjectAttributeValue()
    # TODO(user): get rid of RDF builtin "age" property.
    v.Set("age", age)
    # With dynamic values we first have to set the type and
    # then the value itself.
    # TODO(user): refactor dynamic values logic so that it's not needed,
    # possibly just removing the "type" attribute completely.
    v.Set("type", compatibility.GetName(value.__class__))
    v.value = value
    return v

  for pi in path_infos:
    if pi.directory:
      object_type = "VFSDirectory"
    else:
      object_type = "VFSFile"

    type_attrs.append(_Value(pi.timestamp, rdfvalue.RDFString(object_type)))

    if pi.hash_entry:
      hash_attrs.append(_Value(pi.timestamp, pi.hash_entry))
      size_attrs.append(
          _Value(pi.timestamp, rdfvalue.RDFInteger(pi.hash_entry.num_bytes)))
    if pi.stat_entry:
      stat_attrs.append(_Value(pi.timestamp, pi.stat_entry))

      if pi.stat_entry.pathspec:
        pathspec_attrs.append(_Value(pi.timestamp, pi.stat_entry.pathspec))

  return ApiAff4ObjectRepresentation(types=[
      ApiAff4ObjectType(
          name="AFF4Object",
          attributes=[
              ApiAff4ObjectAttribute(
                  name="TYPE",
                  values=type_attrs,
              ),
          ],
      ),
      ApiAff4ObjectType(
          name="AFF4Stream",
          attributes=[
              ApiAff4ObjectAttribute(
                  name="HASH",
                  values=hash_attrs,
              ),
              ApiAff4ObjectAttribute(
                  name="SIZE",
                  values=size_attrs,
              ),
          ],
      ),
      ApiAff4ObjectType(
          name="VFSFile",
          attributes=[
              ApiAff4ObjectAttribute(
                  name="PATHSPEC",
                  values=pathspec_attrs,
              ),
              ApiAff4ObjectAttribute(
                  name="STAT",
                  values=stat_attrs,
              ),
          ],
      ),
  ])
1126896
private void addMetaData() {
		document.addTitle(getDocumentTitle());
		document.addAuthor(ReleaseInfoAdapter.getCompany());
		document.addCreator("NextReports " + ReleaseInfoAdapter.getVersionNumber());
		document.addSubject("Created by NextReports Designer" + ReleaseInfoAdapter.getVersionNumber());
		document.addCreationDate();
		document.addKeywords(ReleaseInfoAdapter.getHome());
	}
52244
def add_raw_data(self, data, attrs):
        
        self.assert_writeable()
        if "Raw" not in self.handle:
            self.handle.create_group("Raw")
        if "Signal" in self.handle['Raw']:
            msg = "Fast5 file already has raw data for read '{}' in: {}"
            raise KeyError(msg.format(self.read_id, self.filename))
        self.handle['Raw'].create_dataset('Signal', data=data, compression='gzip', shuffle=True, dtype='i2')
        self._add_attributes("Raw", attrs, clear=True)
441996
def create_image_metadata(data):
    """"""

    # Default metadata
    meta = {'protected': data.get('protected', False),
            'disk_format': data.get('disk_format', 'raw'),
            'container_format': data.get('container_format', 'bare'),
            'min_disk': data.get('min_disk') or 0,
            'min_ram': data.get('min_ram') or 0,
            'name': data.get('name', '')}

    # Glance does not really do anything with container_format at the
    # moment. It requires it is set to the same disk_format for the three
    # Amazon image types, otherwise it just treats them as 'bare.' As such
    # we will just set that to be that here instead of bothering the user
    # with asking them for information we can already determine.
    if meta['disk_format'] in ('ami', 'aki', 'ari',):
        meta['container_format'] = meta['disk_format']
    elif meta['disk_format'] == 'docker':
        # To support docker containers we allow the user to specify
        # 'docker' as the format. In that case we really want to use
        # 'raw' as the disk format and 'docker' as the container format.
        meta['disk_format'] = 'raw'
        meta['container_format'] = 'docker'
    elif meta['disk_format'] == 'ova':
        # If the user wishes to upload an OVA using Horizon, then
        # 'ova' must be the container format and 'vmdk' must be the disk
        # format.
        meta['container_format'] = 'ova'
        meta['disk_format'] = 'vmdk'

    properties = {}

    for prop, key in [('description', 'description'),
                      ('kernel_id', 'kernel'),
                      ('ramdisk_id', 'ramdisk'),
                      ('architecture', 'architecture')]:
        if data.get(key):
            properties[prop] = data[key]

    _handle_unknown_properties(data, properties)

    if ('visibility' in data and
            data['visibility'] not in ['public', 'private', 'community',
                                       'shared']):
        raise KeyError('invalid visibility option: %s' % data['visibility'])
    _normalize_is_public_filter(data)

    if VERSIONS.active < 2:
        meta['properties'] = properties
        meta['is_public'] = data.get('is_public', False)
    else:
        meta['visibility'] = data.get('visibility', 'private')
        meta.update(properties)

    return meta
1079881
def _add_to_package_contents(self, pkgroot, node_path, hashes, target,
                                 source_path, transform, user_meta_hash):
        
        assert isinstance(node_path, list)
        assert user_meta_hash is None or isinstance(user_meta_hash, str)

        contents = pkgroot

        if not node_path:
            # Allow setting metadata on the root node, but that's it.
            assert target is TargetType.GROUP
            contents.metadata_hash = user_meta_hash
            return

        ptr = contents
        for node in node_path[:-1]:
            ptr = ptr.children[node]

        if target is TargetType.GROUP:
            node = GroupNode(
                children=dict(),
                metadata_hash=user_meta_hash
            )
        else:
            node = FileNode( # pylint:disable=R0204
                hashes=hashes,
                metadata=dict(
                    q_ext=transform,
                    q_path=source_path,
                    q_target=target.value
                ),
                metadata_hash=user_meta_hash
            )

        ptr.children[node_path[-1]] = node
434020
def AddFiles(self, hash_id_metadatas):
    
    for hash_id, metadata in iteritems(hash_id_metadatas):
      self.AddFile(hash_id, metadata)