Querying for Nodes Not in Any Group

Hi all,

I’m looking to query for nodes that are not in any group, but am having trouble figuring out the correct way to query for that. In my query so far, I find SinglefileData nodes that are from CalcJobNode, where the CalcJobNodes are in a given group. However, I want to limit it to only find SinglefileData nodes that are not in any group. Here’s the query I have so far:

qb = orm.QueryBuilder()
qb.append(Group,
        filters={
        'label':'gaussian_sp',
        },
        tag='sp_group')
qb.append(orm.CalcJobNode,
          with_group='sp_group',
          tag='sp_calc')
qb.append(orm.SinglefileData,
          with_incoming='sp_calc',
          )

Can anyone help me figure out the syntax to do this?

Thanks

Unfortunately there is no syntax to express “not in Group” in the querybuilder. A typical workaround is to first get the ids that are in the target group, and then perform a second query that excludes those ids:

qb = orm.QueryBuilder()
qb.append(Group, filters={'label': 'gaussian_sp'}, tag='sp_group')
qb.append(orm.CalcJobNode, with_group='sp_group', tag='sp_calc')
qb.append(orm.SinglefileData, with_incoming='sp_calc', project='pk')
pks_in_gaussian_sp = qb.all(flat=True)

qb = orm.QueryBuilder()
qb.append(orm.CalcJobNode, tag='sp_calc')
qb.append(orm.SinglefileData, with_incoming='sp_calc', filters={'pk': {'!in': pks_in_gaussian_sp}})
orphan_single_files = qb.all(flat=True)

Note that this will get all SinglfileData nodes that are created by a CalcJobNode that is not in the group gaussian_sp. It will include nodes that may be part of another group. It was not clear from your question if those should also be excluded, in which case you would have to repeat the first query for each group to be excluded.

1 Like

Thanks for the workaround! It works. For my purposes, the orphan files that are the output of the CalcJobNode should only ever be in one group, so querying for one group works.

However, I adjusted the query for all groups for future proofing

group_list = QueryBuilder().append(Group, filters={'type_string': 'core'},project='label').all(flat=True)
qb = orm.QueryBuilder()
qb.append(Group, filters={'label': {'in':group_list}}, tag='all_group')
qb.append(orm.SinglefileData, with_group='all_group',project='pk')
sfd_in_group = qb.all(flat=True)

qb = orm.QueryBuilder()
qb.append(Group,filters={'label':'gaussian_sp'},tag='sp_group')
qb.append(orm.CalcJobNode, tag='sp_calc',with_group='sp_group')
qb.append(orm.SinglefileData,  filters={'pk': {'!in': sfd_in_group}},with_incoming='sp_calc',)
orphan_single_files = qb.all(flat=True)

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.