#!/usr/bin/env python # # Parse 2 FASTA files and print statistics # BSDLicence # Rick van der Zwet - 0433373 - from Bio import SeqIO,Seq from Bio import Alphabet from Bio.Alphabet.IUPAC import ambiguous_dna,unambiguous_dna import Bio.Data.CodonTable from MultiReplace import MultiReplace fasta_translate = { 'r' : 'ga', # purine 'y' : 'tc', # pyrimide 'k' : 'gt', # keto 'm' : 'ac', # amino 's' : 'gc', # strong 'w' : 'at', # weak 'b' : 'gtc', 'd' : 'gat', 'h' : 'act', 'v' : 'gca', } fasta = MultiReplace(fasta_translate) def parse_file(file): handle = open(file, "rU") for seq_record in SeqIO.parse(handle, "fasta",ambiguous_dna): # How to translate damm thing into plain nucleic acid codes # http://en.wikipedia.org/wiki/FASTA_format retval = seq_record.seq.__str__() handle.close() return(retval) def stats(seq): pdict = {} for n in range(1, len(seq)): protein = seq[n] if not pdict.has_key(protein): pdict[protein] = 1 else: pdict[protein] += 1 print pdict def concat(head,tail,max_length=1000): "Concat two strings together removing common parts" l_head = len(head) l_tail = len(tail) # Start/Stop at the right time start = 1 if (l_head < l_tail): stop = l_head + 1 else: stop = l_tail + 1 # Make sure not to run for-ever if (stop > max_length): stop = max_length # Find largest common part # XXX: Not very effient (on very large overlap sets for i in reversed(range(start,stop)): #print "tail[0:%i] '%s' == '%s'" % (i, head[-i:], tail[0:i]) if head[-i:] == tail[0:i]: return((i,(tail[0:i]),head + tail[i:])) # None found return full part return(-1,'',head + tail) # Get data file1 = parse_file("data/AE005174v2-1.fas") file2 = parse_file("data/AE005174v2-2.fas") file1 = fasta.replace(file1) file2 = fasta.replace(file2) # Find overlap (retval, common, result) = concat(file2,file1) print retval, common # Strictly speaking there is a gap of about 4 kbs (4000 bs) between file1 and # file2, so lets' put that into the the statistics as well. Due to circular # nature, does not matter wether we add it in the beginning or in the end result = result + "n" * 4000; stats(result) # Write to file for later further processing out = open("full_contig.raw","w") out.write(result) out.close()