Extracts data criteria from the HQMF document.
501616
def extract_criteria
      # Grab each data criteria entry from the HQMF
      extracted_data_criteria = []
      @doc.xpath('cda:QualityMeasureDocument/cda:component/cda:dataCriteriaSection/cda:entry', NAMESPACES).each do |entry|
        extracted_data_criteria << entry
        dc = HQMF2CQL::DataCriteria.new(entry) # Create new data criteria
        sdc = dc.clone # Clone data criteria
        sdc.id += '_source' # Make it a source

        @data_criteria << dc
        @source_data_criteria << sdc
      end
      make_positive_entry
    end
44891
def atom_criteria(*params):
    """"""
    result = {}
    for index, param in enumerate(params):
        if param is None:
            continue
        elif isinstance(param, int):
            result[index] = HasAtomNumber(param)
        else:
            result[index] = param
    return result
275433
def extract_specific_occurrences_and_source_data_criteria
      specific_def = @entry.at_xpath('./*/cda:outboundRelationship[@typeCode="OCCR"]', HQMF2::Document::NAMESPACES)
      source_def = @entry.at_xpath('./*/cda:outboundRelationship[cda:subsetCode/@code="SOURCE"]',
                                   HQMF2::Document::NAMESPACES)
      if specific_def
        source_data_criteria_extension = HQMF2::Utilities.attr_val(specific_def,
                                                                   './cda:criteriaReference/cda:id/@extension')
        source_data_criteria_root = HQMF2::Utilities.attr_val(specific_def, './cda:criteriaReference/cda:id/@root')

        occurrence_criteria = @data_criteria_references[strip_tokens("#{source_data_criteria_extension}_#{source_data_criteria_root}")]

        return if occurrence_criteria.nil?
        specific_occurrence_const = HQMF2::Utilities.attr_val(specific_def,
                                                              './cda:localVariableName/@controlInformationRoot')
        specific_occurrence = HQMF2::Utilities.attr_val(specific_def,
                                                        './cda:localVariableName/@controlInformationExtension')

        # FIXME: Remove debug statements after cleaning up occurrence handling
        # build regex for extracting alpha-index of specific occurrences
        occurrence_identifier = obtain_occurrence_identifier(strip_tokens(@id),
                                                             strip_tokens(@local_variable_name) || '',
                                                             strip_tokens(source_data_criteria_extension),
                                                             @is_variable)

        handle_specific_and_source(occurrence_identifier, source_data_criteria_extension, source_data_criteria_root,
                                   specific_occurrence_const, specific_occurrence)

      elsif source_def
        extension = HQMF2::Utilities.attr_val(source_def, './cda:criteriaReference/cda:id/@extension')
        root = HQMF2::Utilities.attr_val(source_def, './cda:criteriaReference/cda:id/@root')
        ["#{extension}_#{root}_source", root, extension] # return the soruce data criteria itself, the rest will be blank
      end
    end
1006036
def parse_parts_to_dc(parts)
      case parts.length
      when 1
        # If there is only one part, it is a reference to an existing data criteria's value
        @doc.find_criteria_by_lvn(parts.first.strip.split('.')[0])
      when 2
        # If there are two parts, there is a computation performed, specifically time difference, on the two criteria
        children = parts.collect { |p| @doc.find_criteria_by_lvn(p.strip.split('.')[0]).id }
        id = "GROUP_TIMEDIFF_#{@id_generator.next_id}"
        HQMF2::DataCriteriaWrapper.new(id: id,
                                       title: id,
                                       subset_operators: [HQMF::SubsetOperator.new('DATETIMEDIFF', nil)],
                                       children_criteria: children,
                                       derivation_operator: HQMF::DataCriteria::XPRODUCT,
                                       type: 'derived',
                                       definition: 'derived',
                                       negation: false,
                                       source_data_criteria: id
                                      )
      else
        # If there are neither one or 2 parts, the code should fail
        fail "No defined extraction method to handle #{parts.length} parts"
      end
    end
1285414
def _get_db_refs(term):
    """"""
    db_refs = {}
    # Here we extract the text name of the Agent
    # There are two relevant tags to consider here.
    # The <text> tag typically contains a larger phrase surrounding the
    # term but it contains the term in a raw, non-canonicalized form.
    # The <name> tag only contains the name of the entity but it is
    # canonicalized. For instance, MAP2K1 appears as MAP-2-K-1.
    agent_text_tag = term.find('name')
    if agent_text_tag is not None:
        db_refs['TEXT'] = agent_text_tag.text
        # If we have some drum-terms, the matched-name of the first
        # drum-term (e.g. "MAP2K1") is a better value for TEXT than
        # the name of the TERM (e.g. "MAP-2-K-1") so we put that in there
        drum_terms = term.findall('drum-terms/drum-term')
        if drum_terms:
            matched_name = drum_terms[0].attrib.get('matched-name')
            if matched_name:
                db_refs['TEXT'] = matched_name

    # We make a list of scored grounding terms from the DRUM terms
    grounding_terms = _get_grounding_terms(term)
    if not grounding_terms:
        # This is for backwards compatibility with EKBs without drum-term
        # scored entries. It is important to keep for Bioagents
        # compatibility.
        dbid = term.attrib.get('dbid')
        if dbid:
            dbids = dbid.split('|')
            for dbname, dbid in [d.split(':') for d in dbids]:
                if not db_refs.get(dbname):
                    db_refs[dbname] = dbid
        return db_refs, None, []


    # This is the INDRA prioritization of grounding name spaces. Lower score
    # takes precedence.
    ns_priority = {
        'HGNC': 1,
        'UP': 1,
        'FPLX': 2,
        'CHEBI': 3,
        'PC': 3,
        'GO': 4,
        'FA': 5,
        'XFAM': 5,
        'NCIT': 5
    }
    # We get the top priority entry from each score group
    score_groups = itertools.groupby(grounding_terms, lambda x: x['score'])
    top_per_score_group = []
    ambiguities = []
    for score, group in score_groups:
        entries = list(group)
        for entry in entries:
            priority = 100
            for ref_ns, ref_id in entry['refs'].items():
                # Skip etc UP entries
                if ref_ns == 'UP' and ref_id == 'etc':
                    continue
                try:
                    priority = min(priority, ns_priority[ref_ns])
                except KeyError:
                    pass
                if ref_ns == 'UP':
                    if not up_client.is_human(ref_id):
                        priority = 4
            entry['priority'] = priority
        if len(entries) > 1:
            top_entry = entries[0]
            top_idx = 0
            for i, entry in enumerate(entries):
                # We take the lowest priority entry within the score group
                # as the top entry
                if entry['priority'] < top_entry['priority']:
                    # This is a corner case in which a protein family
                    # should be prioritized over a specific protein,
                    # specifically when HGNC was mapped from NCIT but
                    # FPLX was not mapped from NCIT, the HGNC shouldn't
                    # take precedence.
                    if entry.get('comment') == 'HGNC_FROM_NCIT' and \
                        'FPLX' in top_entry['refs'] and \
                        top_entry.get('comment') != 'FPLX_FROM_NCIT':
                        continue
                    top_entry = entry
                    top_idx = i
            for i, entry in enumerate(entries):
                if i == top_idx:
                    continue
                if (entry['priority'] - top_entry['priority']) <= 1:
                    ambiguities.append((top_entry, entry))
        else:
            top_entry = entries[0]
        top_per_score_group.append(top_entry)
    # Get the top priority for each score group
    priorities = [entry['priority'] for entry in top_per_score_group]

    # By default, we coose the top priority entry from the highest score group
    top_grounding = top_per_score_group[0]
    # Sometimes the top grounding has much lower priority and not much higher
    # score than the second grounding. Typically 1.0 vs 0.82857 and 5 vs 2.
    # In this case we take the second entry. A special case is handled where
    # a FPLX entry was mapped from FA, in which case priority difference of < 2
    # is also accepted.
    if len(top_per_score_group) > 1:
        score_diff = top_per_score_group[0]['score'] - \
                     top_per_score_group[1]['score']
        priority_diff = top_per_score_group[0]['priority'] - \
                        top_per_score_group[1]['priority']
        if score_diff < 0.2 and (priority_diff >= 2 or \
            top_per_score_group[0].get('comment') == 'FPLX_FROM_FA'):
            top_grounding = top_per_score_group[1]
    relevant_ambiguities = []
    for amb in ambiguities:
        if top_grounding not in amb:
            continue
        if top_grounding == amb[0]:
            relevant_ambiguities.append({'preferred': amb[0],
                                         'alternative': amb[1]})
        else:
            relevant_ambiguities.append({'preferred': amb[1],
                                         'alternative': amb[0]})

    for k, v in top_grounding['refs'].items():
        db_refs[k] = v

    # Now standardize db_refs to the INDRA standards
    # We need to add a prefix for CHEBI
    chebi_id = db_refs.get('CHEBI')
    if chebi_id and not chebi_id.startswith('CHEBI:'):
        db_refs['CHEBI'] = 'CHEBI:%s' % chebi_id
    # We need to strip the trailing version number for XFAM and rename to PF
    pfam_id = db_refs.get('XFAM')
    if pfam_id:
        pfam_id = pfam_id.split('.')[0]
        db_refs.pop('XFAM', None)
        db_refs['PF'] = pfam_id
    # We need to add GO prefix if it is missing
    go_id = db_refs.get('GO')
    if go_id:
        if not go_id.startswith('GO:'):
            db_refs['GO'] = 'GO:%s' % go_id
    # We need to deal with Nextprot families
    nxp_id = db_refs.get('FA')
    if nxp_id:
        db_refs.pop('FA', None)
        db_refs['NXPFA'] = nxp_id
    # We need to rename PC to PUBCHEM
    pc_id = db_refs.get('PC')
    if pc_id:
        db_refs.pop('PC', None)
        db_refs['PUBCHEM'] = pc_id

    # Here we also get and return the type, which is a TRIPS
    # ontology type. This is to be used in the context of
    # Bioagents.
    ont_type = top_grounding['type']

    return db_refs, ont_type, relevant_ambiguities
1078146
public static String getEnclosingEmbeddedFieldName(EntityMetadata m, String criteria, boolean viaColumnName,
            final KunderaMetadata kunderaMetadata)
    {
        String enclosingEmbeddedFieldName = null;

        StringTokenizer strToken = new StringTokenizer(criteria, ".");
        String embeddableAttributeName = null;
        String embeddedFieldName = null;
        String nestedEmbeddedFieldName = null;

        if (strToken.countTokens() > 0)
        {
            embeddableAttributeName = strToken.nextToken();
        }
        if (strToken.countTokens() > 0)
        {
            embeddedFieldName = strToken.nextToken();
        }
        if (strToken.countTokens() > 0)
        {
            nestedEmbeddedFieldName = strToken.nextToken();
        }

        Metamodel metaModel = kunderaMetadata.getApplicationMetadata().getMetamodel(m.getPersistenceUnit());
        EntityType entity = metaModel.entity(m.getEntityClazz());

        try
        {
            Attribute attribute = entity.getAttribute(embeddableAttributeName);

            if (((MetamodelImpl) metaModel).isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType()))
            {
                EmbeddableType embeddable = metaModel.embeddable(((AbstractAttribute) attribute).getBindableJavaType());
                Iterator<Attribute> attributeIter = embeddable.getAttributes().iterator();
                while (attributeIter.hasNext())
                {
                    AbstractAttribute attrib = (AbstractAttribute) attributeIter.next();

                    if (viaColumnName && attrib.getName().equals(embeddedFieldName))
                    {
                        if (nestedEmbeddedFieldName != null
                                && ((MetamodelImpl) metaModel).isEmbeddable(((AbstractAttribute) attrib)
                                        .getBindableJavaType()))
                        {
                            EmbeddableType nestedEmbeddable = metaModel.embeddable(((AbstractAttribute) attrib)
                                    .getBindableJavaType());
                            Iterator<Attribute> iter = embeddable.getAttributes().iterator();
                            while (iter.hasNext())
                            {
                                AbstractAttribute nestedAttribute = (AbstractAttribute) iter.next();

                                if (viaColumnName && nestedAttribute.getName().equals(embeddedFieldName))
                                {
                                    return nestedAttribute.getName();
                                }

                                if (!viaColumnName && nestedAttribute.getJPAColumnName().equals(embeddedFieldName))
                                {
                                    return nestedAttribute.getName();
                                }
                            }
                        }
                        else if (nestedEmbeddedFieldName != null
                                && !((MetamodelImpl) metaModel).isEmbeddable(((AbstractAttribute) attrib)
                                        .getBindableJavaType()))
                        {
                            return null;
                        }
                        else
                        {
                            return attribute.getName();
                        }
                    }

                    if (!viaColumnName && attrib.getJPAColumnName().equals(embeddedFieldName))
                    {
                        return attribute.getName();
                    }
                }
            }

        }
        catch (IllegalArgumentException iax)
        {
            return null;
        }
        return enclosingEmbeddedFieldName;
    }
704786
private void parseAttributesSheet(
      Repository<Entity> attributesRepo, IntermediateParseResults intermediateResults) {
    for (Attribute attr : attributesRepo.getEntityType().getAtomicAttributes()) {
      checkAttrSheetHeaders(attr);
    }

    Map<String, Map<String, EmxAttribute>> attributesMap = newLinkedHashMap();

    // 1st pass: create attribute stubs
    int rowIndex = 1; // Header
    createAttributeStubs(attributesRepo, attributesMap, rowIndex);

    // 2nd pass: set all properties on attribute stubs except for attribute relations
    rowIndex = 1; // Header
    for (Entity emxAttrEntity : attributesRepo) {
      rowIndex++;
      parseSingleAttribute(intermediateResults, attributesMap, rowIndex, emxAttrEntity);
    }

    // 3rd pass
    Map<String, Set<String>> rootAttributes =
        validateAndCreateAttributeRelationships(attributesRepo, attributesMap);

    // store attributes with entities
    storeAttributes(intermediateResults, attributesMap, rootAttributes);
  }
1335107
def read_criteria_file(self, criteria_file):
        '''
        
        '''
        if self.data_model == 3:
            self.acceptance_criteria = pmag.initialize_acceptance_criteria(
                data_model=self.data_model)
            self.add_thellier_gui_criteria()
            fnames = {'criteria': criteria_file}
            contribution = cb.Contribution(
                self.WD, custom_filenames=fnames, read_tables=['criteria'])
            if 'criteria' in contribution.tables:
                crit_container = contribution.tables['criteria']
                crit_data = crit_container.df
                crit_data['definition'] = 'acceptance criteria for study'
                # convert to list of dictionaries
                self.crit_data = crit_data.to_dict('records')
                for crit in self.crit_data:  # step through and rename every f-ing one
                    # magic2[magic3.index(crit['table_column'])] # find data
                    # model 2.5 name
                    m2_name = map_magic.convert_intensity_criteria(
                        'magic2', crit['table_column'])
                    if not m2_name:
                        pass
                    elif m2_name not in self.acceptance_criteria:
                        print('-W- Your criteria file contains {}, which is not currently supported in Thellier GUI.'.format(m2_name))
                        print('    This record will be skipped:\n    {}'.format(crit))
                    else:
                        if m2_name != crit['table_column'] and 'scat' not in m2_name != "":
                            self.acceptance_criteria[m2_name]['value'] = float(
                                crit['criterion_value'])
                            self.acceptance_criteria[m2_name]['pmag_criteria_code'] = crit['criterion']
                        if m2_name != crit['table_column'] and 'scat' in m2_name != "":
                            if crit['criterion_value'] == 'True':
                                self.acceptance_criteria[m2_name]['value'] = 1
                            else:
                                self.acceptance_criteria[m2_name]['value'] = 0
            else:
                print("-E- Can't read criteria file")

        else:  # Do it the data model 2.5 way:
            self.crit_data = {}
            try:
                self.acceptance_criteria = pmag.read_criteria_from_file(
                    criteria_file, self.acceptance_criteria)
            except:
                print("-E- Can't read pmag criteria file")
        # guesss if average by site or sample:
        by_sample = True
        flag = False
        for crit in ['sample_int_n', 'sample_int_sigma_perc', 'sample_int_sigma']:
            if self.acceptance_criteria[crit]['value'] == -999:
                flag = True
        if flag:
            for crit in ['site_int_n', 'site_int_sigma_perc', 'site_int_sigma']:
                if self.acceptance_criteria[crit]['value'] != -999:
                    by_sample = False
        if not by_sample:
            self.acceptance_criteria['average_by_sample_or_site']['value'] = 'site'
691242
def get_docs_from_split(session, candidate_classes, split):
    """"""
    # Only grab the docs containing candidates from the given split.
    sub_query = session.query(Candidate.id).filter(Candidate.split == split).subquery()
    split_docs = set()
    for candidate_class in candidate_classes:
        split_docs.update(
            cand.document
            for cand in session.query(candidate_class)
            .filter(candidate_class.id.in_(sub_query))
            .all()
        )
    return split_docs
963358
static List<UberData> extractLinksAndContent(EntityModel<?> resource) {

		List<UberData> data = extractLinks(resource);

		extractContent(resource.getContent()).ifPresent(data::add);

		return data;
	}
470196
def build_population_criteria(criteria_def, criteria_id, population)
      criteria = PopulationCriteria.new(criteria_def, @document, @id_generator)

      # check to see if we have an identical population criteria.
      # this can happen since the hqmf 2.0 will export a DENOM, NUMER, etc for each population, even if identical.
      # if we have identical, just re-use it rather than creating DENOM_1, NUMER_1, etc.
      identical = @population_criteria.select { |pc| pc.to_model.hqmf_id == criteria.to_model.hqmf_id }

      @reference_ids.concat(criteria.to_model.referenced_data_criteria)

      if identical.empty?
        # this section constructs a human readable id.  The first IPP will be IPP, the second will be IPP_1, etc.
        # This allows the populations to be more readable.  The alternative would be to have the hqmf ids in the
        # populations, which would work, but is difficult to read the populations.
        if @ids_by_hqmf_id["#{criteria.hqmf_id}-#{population['stratification']}"]
          criteria.create_human_readable_id(@ids_by_hqmf_id["#{criteria.hqmf_id}-#{population['stratification']}"])
        else
          criteria.create_human_readable_id(population_id_with_counter(criteria_id))
          @ids_by_hqmf_id["#{criteria.hqmf_id}-#{population['stratification']}"] = criteria.id
        end

        @population_criteria << criteria
        population[criteria_id] = criteria.id
      else
        population[criteria_id] = identical.first.id
      end
    end
732897
private String extractHibernateJpaVendorDatabase(Document doc, Element bean)
    {
        for (Element jpaVendorAdapterProperty : $(bean).children("property").filter(attr("name", "jpaVendorAdapter")).get())
        {
            String propertyRef = $(jpaVendorAdapterProperty).attr("ref");
            if (StringUtils.isNotBlank(propertyRef))
            {
                // look for the properties referenced by a local bean..
                for (Element jpaVendorAdapter : findLocalBeanById(doc, propertyRef))
                {
                    // check attribute on element
                    String propAttrValue = $(jpaVendorAdapter).attr("database");
                    if (StringUtils.isNotBlank(propAttrValue))
                    {
                        return propAttrValue;
                    }

                    // now look for the property "dataSource" off of that bean.
                    for (Element p : $(jpaVendorAdapter).children("property").filter(attr("name", "database")).get())
                    {
                        String value = $(p).attr("value");
                        if (StringUtils.isNotBlank(value))
                        {
                            return value;
                        }
                    }
                }
            }
        }

        return null;
    }
290315
def _ExtractRequestSummaryFields(document):
  
  headers = document.childAtPath('Header/RequestHeader')
  body = document.childAtPath('Body')

  summary_fields = {
      'methodName': body.getChildren()[0].name
  }

  # Extract AdWords-specific fields if they exist.
  # Note: We need to check if None because this will always evaluate False.
  client_customer_id = headers.getChild('clientCustomerId')
  if client_customer_id is not None:
    summary_fields['clientCustomerId'] = client_customer_id.text

  # Extract Ad Manager-specific fields if they exist.
  # Note: We need to check if None because this will always evaluate False.
  network_code = headers.getChild('networkCode')
  if network_code is not None:
    summary_fields['networkCode'] = network_code.text

  return summary_fields
944471
def definition_for_nil_entry
      reference = @entry.at_xpath('./*/cda:outboundRelationship/cda:criteriaReference', HQMF2::Document::NAMESPACES)
      ref_id = nil
      unless reference.nil?
        ref_id = "#{HQMF2::Utilities.attr_val(reference, 'cda:id/@extension')}_#{HQMF2::Utilities.attr_val(reference, 'cda:id/@root')}"
      end
      reference_criteria = @data_criteria_references[strip_tokens(ref_id)] unless ref_id.nil?
      if reference_criteria
        # we only want to copy the reference criteria definition, status, and code_list_id if this is this is not a grouping criteria (i.e., there are no children)
        if @children_criteria.blank?
          @definition = reference_criteria.definition
          @status = reference_criteria.status
          if @specific_occurrence
            @title = reference_criteria.title
            @description = reference_criteria.description
            @code_list_id = reference_criteria.code_list_id
          end
        else
          # if this is a grouping data criteria (has children) mark it as derived and only pull title and description from the reference criteria
          @definition = 'derived'
          if @specific_occurrence
            @title = reference_criteria.title
            @description = reference_criteria.description
          end
        end
      else
        puts "MISSING_DC_REF: #{ref_id}" unless @variable
        @definition = 'variable'
      end
    end
170903
def getanymentions(idf, anidfobject):
    """"""
    name = anidfobject.obj[1]
    foundobjs = []
    keys = idfobjectkeys(idf)
    idfkeyobjects = [idf.idfobjects[key.upper()] for key in keys]
    for idfobjects in idfkeyobjects:
        for idfobject in idfobjects:
            if name.upper() in [item.upper() 
                                    for item in idfobject.obj 
                                        if isinstance(item, basestring)]:
                foundobjs.append(idfobject)
    return foundobjs
358181
private Action extractDocumentationAnnotations(
			ExtDirectMethodDocumentation documentation) {
		if (!documentation.value().isEmpty()) {
			ActionDoc actionDoc = new ActionDoc(getAction(), documentation.value(),
					documentation.author(), documentation.version(),
					documentation.deprecated());
			ExtDirectDocParameters docParameters = documentation.parameters();
			if (null != docParameters) {
				String[] params = docParameters.params();
				String[] descriptions = docParameters.descriptions() == null
						? new String[params.length]
						: docParameters.descriptions();
				if (params.length == descriptions.length) {
					for (int i = 0; i < params.length; i++) {
						actionDoc.getParameters().put(params[i],
								descriptions[i] == null ? "No description"
										: descriptions[i]);
					}
				}
				else {
					LogFactory.getLog(MethodInfo.class).info(
							"Documentation: skip generation of parameters, params size is different from descriptions size");
				}
			}
			ExtDirectDocReturn docReturn = documentation.returnMethod();
			if (null != docReturn) {
				String[] properties = docReturn.properties();
				String[] descriptions = docReturn.descriptions() == null
						? new String[properties.length]
						: docReturn.descriptions();
				if (properties.length == descriptions.length) {
					for (int i = 0; i < properties.length; i++) {
						actionDoc.getReturnMethod().put(properties[i],
								descriptions[i] == null ? "No description"
										: descriptions[i]);
					}
				}
				else {
					LogFactory.getLog(MethodInfo.class).info(
							"Documentation: skip generation of return method properties, properties size is different from descriptions size");
				}
			}
			return actionDoc;
		}
		return this.action;
	}
563437
def extract_main_library
      population_criteria_sections = @doc.xpath("//cda:populationCriteriaSection/cda:component[@typeCode='COMP']", HQMF2::Document::NAMESPACES)
      criteria_section = population_criteria_sections.at_xpath("cda:initialPopulationCriteria", HQMF2::Document::NAMESPACES)
      if criteria_section
        # Example: the full name for the population criteria section is "BonnieNesting01.\"Initial Population\""
        # The regex returns everything before the "."  (BonnieNesting01), which is the file name of the cql measure
        cql_main_library_name = criteria_section.at_xpath("*/*/cda:id", HQMF2::Document::NAMESPACES).attribute('extension').to_s.match(/[^.]*/).to_s
      else
        nil
      end
    end