# Identify duplicated sample groups

**URL:** https://discuss.hail.is/t/identify-duplicated-sample-groups/3960
**Category:** Hail Query & hailctl
**Created:** [October 9, 2024, 12:33pm UTC](https://discuss.hail.is/t/identify-duplicated-sample-groups/3960 "2024-10-09T12:33:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![DBScan](https://avatars.discourse-cdn.com/v4/letter/d/bc79bd/32.png) [@DBScan](https://discuss.hail.is/u/DBScan)
#### Post date: [October 9, 2024, 12:33pm UTC](https://discuss.hail.is/t/identify-duplicated-sample-groups/3960/1 "2024-10-09T12:33:02Z")

</div>

How can I group duplicated samples after running `pc_relate`?

I am running `pc_relate` with the following command:

```python
rel = hl.pc_relate(mt.GT,
        min_individual_maf = 0.01,
        k = 20,
        statistics = "kin",
        min_kinship = (1/(2**1.5)))

```

The table after running `pc_relate` looks like this:

| i.s | j.s | Column 3 | Column 4 |
| --- | --- | --- | --- |
| Sample1\_Rep1 | Sample1\_Rep2 | | |
| Sample1\_Rep2 | Sample1\_Rep3 | | |
| Sample2\_Rep1 | Sample2\_Rep2 | | |
| Sample3\_Rep1 | Sample3\_Rep2 | | |
| Sample3\_Rep2 | Sample3\_Rep3 | | |
| Sample3\_Rep3 | Sample3\_Rep4 | | |
| Sample3\_Rep2 | Sample3\_Rep3 | | |
| Sample3\_Rep2 | Sample3\_Rep4 | | |
| Sample3\_Rep3 | Sample3\_Rep4 | | |

Sample1 would have been sequenced 3 times, Sample2 2 times, and Sample3 4 times.  
I would like to have a table in the following format:

| Sample | Group | Column 3 | Column 4 |
| --- | --- | --- | --- |
| Sample1\_Rep1 | 1 | | |
| Sample1\_Rep2 | 1 | | |
| Sample1\_Rep3 | 1 | | |
| Sample2\_Rep1 | 2 | | |
| Sample2\_Rep2 | 2 | | |
| Sample3\_Rep1 | 3 | | |
| Sample3\_Rep2 | 3 | | |
| Sample3\_Rep3 | 3 | | |
| Sample3\_Rep4 | 3 | | |

I have tried it with pandas, but so far I was not successful; I always end up with too many groups. For instance, Sample3\_Rep1 and Sample3\_Rep2 is a single group, Sample3\_Rep1 and Sample3\_Rep3 is another group.

---

<div class="post-metadata">

### Author: ![kasittig](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/kasittig/32/1184_2.png) [@kasittig](https://discuss.hail.is/u/kasittig)
#### Post date: [October 9, 2024, 8:38pm UTC](https://discuss.hail.is/t/identify-duplicated-sample-groups/3960/2 "2024-10-09T20:38:16Z")

</div>

Would you be able to post the code snippet where you’re doing the grouping? My guess is that something wonky is going on with your key selection. I’m remembering that the Python / pandas syntax here can be a little tricky so hopefully it’s an easy fix!

---

<div class="post-metadata">

### Author: ![DBScan](https://avatars.discourse-cdn.com/v4/letter/d/bc79bd/32.png) [@DBScan](https://discuss.hail.is/u/DBScan)
#### Post date: [October 11, 2024, 11:49am UTC](https://discuss.hail.is/t/identify-duplicated-sample-groups/3960/3 "2024-10-11T11:49:49Z")

</div>

I gave up with pandas, and instead used igraph for this task.

```python
# Identify duplicates samples
rel = hl.pc_relate(mt.GT, 
        min_individual_maf = 0.01, 
        k = 20, 
        statistics = "kin", 
        min_kinship = (1/(2**1.5)))

# Convert to pandas df with three 3 columns:
# i.s, j.s and kinship
rel_df = rel.to_pandas()

# Create a graph
g = ig.Graph.DataFrame(rel_df, use_vids = False)

# Create network, because we only know A->B and B->C, but not A->C
components = g.connected_components(mode = "weak")

# Sample names
sample_names = g.vs["name"]

# Group membership
group = components.membership

# Create pandas dataframe
rel_df = pd.DataFrame(data = {"Sample": sample_names, "group": [x + 1 for x in group]})

```
