import skbio

"""
Explanation: Forensics
End of explanation
"""


fp = "forensic-seqs.fna"

skbio.io.sniff(fp)

"""
Explanation: We have samples of bacterial DNA from three people's hands and three peoples keyboards. Can we figure out which individual belongs to which keyboard by seeing which keyboards and which hands have the most bacterial DNA sequences in common?
First, we'll download some gzipped sequence file. Let's find out what type of file it is...
End of explanation
"""


skbio.io.sniff(fp)


"""
Explanation: Ok, it's fasta. But there is a lot of metadata embedded in the sample ids. Let's be a little more sane, and propagate that to sequence metadata.
End of explanation
"""


for rec in skbio.io.read(fp, format='fasta'):
    print(rec, rec.id)    
    break

"""
Explanation: Do some fancy sequence exploration to show off the repr...
End of explanation
"""


from collections import defaultdict
data = defaultdict(list)
for rec in skbio.io.read(fp, format='fasta'):
    subject = rec.id.split('.')[0]
    data[subject].append(rec)

"""
Explanation: Hash the sequences on a per subject basis
End of explanation
"""


for k, v in data.items():
    print k, len(v), len(set(v))

for k, v in data.items():
    data[k] = set(v)
    print k, len(data[k])

"""
Explanation: Figure out how many sequences we have for each subject, and then how many unique sequences we have for each subject
End of explanation
"""


# maybe load these data into a distance matrix to show that repr?
for i in ['M2', 'M3', 'M9']:
    for j in ['K1', 'K2', 'K3']:
        print i, j, len(data[i] & data[j])
    print ''

"""
Explanation: The largest union of sequences always provides the best match of subject to keyboard.
End of explanation
"""


