Hello I have a df1 file such as :
Acc_number
ACC1.1_CP_Sp1_1
ACC2.1_CP_Sp1_1
ACC3.1_CP_Sp1_1
ACC4.1_CP_Sp1_1
and another df2 such as :
Cluster_nb SeqName
Cluster1 YP_009216714
Cluster1 YP_002051918
Cluster1 JZSA01005235.1:37071-37973(-):Sp1_1
Cluster1 NW_014464344.1:68901-69716(-):Sp2_3
Cluster1 YP_001956729
Cluster1 ACC1.1_CP_Sp1_1
Cluster1 YP_009213712
Cluster2 ACC2.1_CP_Sp1_1
Cluster2 NR_014464231.1:35866-36717(-):Sp1_1
Cluster2 NR_014464232.1:35889-36788(-):Sp1_1
Cluster2 YP_009213728
Cluster3 ACC3.1_CP_Sp1_1
Cluster3 NK_014464231.1:35772-38898(-):Sp1_2
Cluster3 NZ_014464232.1:3533-78787(+):Sp1_2
Cluster3 YP_009213723
Cluster3 YP_009213739
I want to check for each Acc_number in df1 if a groupby Cluster_nb that contains Acc_number[i] also contains another sequence with the same extension (the part after _CP_ in the Acc_number) in its (+ or -):... part.
For example
for ACC1.1_CP_Sp1_1 as i
I see by doing a :
df=df2.loc[df2['SeqName']==i]
Cluster_number=df['Cluster_nb'].iloc[0]
df3=df2.loc[df2['Cluster_nb']==Cluster_number]
print(df3)
Cluster_nb SeqName
Cluster1 YP_009216714
Cluster1 YP_002051918
Cluster1 JZSA01005235.1:37071-37973(-):Sp1_1
Cluster1 NW_014464344.1:68901-69716(-):Sp2_3
Cluster1 YP_001956729
that the sequence JZSA01005235.1:37071-37973(-):Sp1_1 in line number 3 has the same Sp1_1 pattern at its end.
So here the answer is yes, ACC1.1_CP_Sp1_1 is in the same cluster as another sequence with the same ending (but with (-or +): in its name)
for ACC3.1_CP_Sp1_1 as i
I see by doing a :
df=df2.loc[df2['SeqName']==i]
Cluster_number=df['Cluster_nb'].iloc[0]
df3=df2.loc[df2['Cluster_nb']==Cluster_number]
print(df3)
Cluster3 ACC3.1_CP_Sp1_1
Cluster3 NK_014464231.1:35772-38898(-):Sp1_2
Cluster3 NZ_014464232.1:3533-78787(+):Sp1_2
Cluster3 YP_009213723
Cluster3 YP_009213739
I see that in the cluster no other sequence has the same ending as ACC3.1_CP_Sp1_1, so the answer is no.
The results should be summarized in df3:
Acc_number present cluster
ACC1.1_CP_Sp1_1 Yes Cluster1
ACC2.1_CP_Sp1_1 Yes Cluster2
ACC3.1_CP_Sp1_1 No NaN
ACC4.1_CP_Sp1_1 No NaN
Thank you a lot for you help
I tried :
for CP in df1['Acc_number']:
df=df2.loc[df2['SeqName']==CP]
try:
Cluster_number=df['Cluster_nb'].iloc[0]
df3=df2.loc[df2['Cluster_nb']==Cluster_number]
for a in df3['SeqName']:
if '(+)' in a or '(-)' in a:
if re.sub('.*_CP_','',CP) in a:
new_df=new_df.append({"Cluster":Cluster_number,"Acc_nb":CP,"present":'yes'}, ignore_index=True)
print(CP,'yes')
except:
continue