Hi, it is a good idea but it actually does not avoid sequences paired with the same ID:
For exemple I get:
Cluster : 25720.0
g10322.t1_0042 vs g10322.t1_0042 (they are the same specie)
g25505.t1_0042_0035 vs g25505.t1_0042_0035
Cluster : 14187.0
g10321.t1_0035_0042 vs g10321.t1_0035_0042 (they are the same specie)
g10321.t1_0035_0042 vs g17887.t1_0035
g1675.t1_0035 vs g1675.t1_0035
g17887.t1_0035 vs g10321.t1_0035_0042
g17887.t1_0035 vs g17887.t1_0035
and so on I would need to only desplay the sequence with the best ID within each cluster and this sequence has to be a pair of 2 different species.
So two species are identical when they have the same ID and (new thing) also sequences which begins with the same patern after the first "_"? For exemple: if the 2 sequences compared are: g1.t1_0035_0042 vs g1.t1_0035 or g23602.t1_0035_0042 vs g44900.t1_0035. In fact the first number correspond at the specie number so when the two number are equal, it means that we are comparing the same specie. Do you think it is possible to also removed those sequences with the same n°? cf the number after the first "_"?
I actually got one code and it allows to do it but only for sequences with the same ID, and I do not really know how to add this part (remove also when the first number is equal):
import pandas as pd
blast=pd.read_table("matches.m8",header=None)
blast.columns = ["qseqid", "sseqid", "pident", "length", "mismatch", "gapopen","qstart", "qend", "sstart", "send", "evalue", "bitscore"]
cluster=pd.read_table("seq.fnodes2",header=None)
cluster.columns = ["cluster_name", "seq_names"]
data = cluster.merge(blast, left_on='seq_names', right_on='qseqid')
data=data.drop(columns=["length" ,"mismatch" , "gapopen" ,"qstart" ,"qend","sstart", "send","evalue" , "bitscore"])
data = data[data['qseqid']!=data["sseqid"]]
data2=data.groupby('cluster_name').max()
for index, row in data2.iterrows():
print(row['qseqid']+" vs "+row['sseqid'])
print("\n")
your import is incorrect. You lost the data in headers.
First things first, you need to set column names and indexes for both
blastandclusterdataframes. Because in your example your first row line is your header columnOk, I changed it thanks you for the comment.
I think that if you join your dataframes (linked
seq_namesfor cluster andqseqidfor blast) you will be good. You will have a huge dataframe with all the information you need, then filter the dataframe as your convenience.Ok thansk, here is the resulte, do you have an idea of a function to get only the best paired sequences between each cluster?
group by cluster and print max value in each group
I did:
and now I do not know how to print max values on dataframe "grouped" but only in the row pident.
Have you an idea?
something like this:
df.groupby(['cluster_name'], sort=False)['pident'].max()Ok, thanks, but it actually gives me :
ABut I actually do not know which paires have the best similarity score (It does not display their seq ID) and I have to remove or at list not take into account the sequences paired by themselves, here it is always 100 but I want those wo are differents but display the best similarity score.
See if following works:
if you don't have a tie within a cluster, then you can try this as well:
I personally don't appreciate the
groupbymethod, it's powerfull and cleaner but I don't like to use it, you can try this otherwise :After merging your dataframes in
dfIn fact I thing something is going wrong when I merge my two dataframes: When typing:
I'm actually getting for the first cluster for exemple:
But as you can see the cluster 1 is forming by these 2 sequences:
And the blast file is :
So, what I want is to display the best score between two differents sequences, here only g1.t1_0035_0042 vs g1.t1_0035
The problem is that the data.frame marged should not be like that:
but like that:
and the same here:
for this cluster:
This is the only one who exists, so this sequence is alone inside this cluster but your code desplays:
but I should actually get nothing thing this sequence is alone.
And indeed something is wrong with my marging again because it gives me that:
Which is wrong...