Last active
December 15, 2015 14:42
-
-
Save Alex-Rosenberg/244991be3d41aeed6c21 to your computer and use it in GitHub Desktop.
Combines sequences with less than N hamming distance using counts of each sequence.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hamdist(str1, str2): | |
"""Count the # of differences between equal length strings str1 and str2""" | |
diffs = 0 | |
for ch1, ch2 in zip(str1, str2): | |
if ch1 != ch2: | |
diffs += 1 | |
return diffs | |
def remove_duplicates_round(df,hamm_thres=4,merge_counts=False): | |
seqs = list(df.Seq.values) | |
counts = list(df.Counts.values) | |
c = 0 | |
while c<(len(counts)-1): | |
if(hamdist(seqs[c],seqs[c+1]))<hamm_thres: | |
if(counts[c]>counts[c+1]): | |
if(merge_counts): | |
counts[c]+=counts[c+1] | |
del counts[c+1],seqs[c+1] | |
else: | |
if(merge_counts): | |
counts[c+1]+=counts[c] | |
del counts[c],seqs[c] | |
else: | |
c+=1 | |
return pd.DataFrame({'Seq':seqs,'Counts':counts}) | |
def remove_all_duplicates(sequences,counts,hamming_thresh=5,merge_counts=False): | |
""" Removes potential sequence duplicates with < hamming_thesh separation. | |
If merge counts is selected, the counts from the duplicate sequences with less | |
counts are added to the counts of the corresponding sequence. Otherwise, the | |
sequence with less counts is discarded with its counts. | |
Inputs: | |
sequnces - array or list of DNA sequences | |
counts - array or list of counts of each sequence | |
Outputs: | |
df - pandas dataframe with a column of all unique seqs and counts. | |
""" | |
df = pd.DataFrame({'Seq':sequences,'Counts':counts}) | |
seq_len = sequences[0] | |
for i in range(seq_len): | |
df = df.ix[(df.Seq.str.slice(seq_len-i)+df.Seq.str.slice(i)).order().index] | |
df = remove_duplicates_round(df,hamm_thres=hamming_thresh,merge_counts=merge_counts) | |
print i, | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment