1 | #!/usr/bin/env python
|
---|
2 | #
|
---|
3 | # Parse 2 FASTA files and print statistics
|
---|
4 | # BSDLicence
|
---|
5 | # Rick van der Zwet - 0433373 - <info@rickvaderzwet.nl>
|
---|
6 | from Bio import SeqIO,Seq
|
---|
7 | from Bio import Alphabet
|
---|
8 | from Bio.Alphabet.IUPAC import ambiguous_dna,unambiguous_dna
|
---|
9 | import Bio.Data.CodonTable
|
---|
10 | from MultiReplace import MultiReplace
|
---|
11 | from reading_frames import reading_frames
|
---|
12 |
|
---|
13 | fasta_translate = {
|
---|
14 | 'r' : 'ga', # purine
|
---|
15 | 'y' : 'tc', # pyrimide
|
---|
16 | 'k' : 'gt', # keto
|
---|
17 | 'm' : 'ac', # amino
|
---|
18 | 's' : 'gc', # strong
|
---|
19 | 'w' : 'at', # weak
|
---|
20 | 'b' : 'gtc',
|
---|
21 | 'd' : 'gat',
|
---|
22 | 'h' : 'act',
|
---|
23 | 'v' : 'gca',
|
---|
24 | }
|
---|
25 |
|
---|
26 | fasta = MultiReplace(fasta_translate)
|
---|
27 |
|
---|
28 | def parse_file(file):
|
---|
29 | handle = open(file, "rU")
|
---|
30 | for seq_record in SeqIO.parse(handle, "fasta",ambiguous_dna):
|
---|
31 | # How to translate damm thing into plain nucleic acid codes
|
---|
32 | # http://en.wikipedia.org/wiki/FASTA_format
|
---|
33 | retval = seq_record.seq.__str__()
|
---|
34 |
|
---|
35 | handle.close()
|
---|
36 | return(retval)
|
---|
37 |
|
---|
38 | def stats(seq):
|
---|
39 | pdict = {}
|
---|
40 | for n in range(1, len(seq)):
|
---|
41 | protein = seq[n]
|
---|
42 | if not pdict.has_key(protein):
|
---|
43 | pdict[protein] = 1
|
---|
44 | else:
|
---|
45 | pdict[protein] += 1
|
---|
46 |
|
---|
47 | print pdict
|
---|
48 |
|
---|
49 |
|
---|
50 | def concat(head,tail,max_length=1000):
|
---|
51 | "Concat two strings together removing common parts"
|
---|
52 | l_head = len(head)
|
---|
53 | l_tail = len(tail)
|
---|
54 |
|
---|
55 | # Start/Stop at the right time
|
---|
56 | start = 1
|
---|
57 | if (l_head < l_tail):
|
---|
58 | stop = l_head + 1
|
---|
59 | else:
|
---|
60 | stop = l_tail + 1
|
---|
61 |
|
---|
62 | # Make sure not to run for-ever
|
---|
63 | if (stop > max_length):
|
---|
64 | stop = max_length
|
---|
65 |
|
---|
66 | # Find largest common part
|
---|
67 | # XXX: Not very effient (on very large overlap sets
|
---|
68 | for i in reversed(range(start,stop)):
|
---|
69 | #print "tail[0:%i] '%s' == '%s'" % (i, head[-i:], tail[0:i])
|
---|
70 | if head[-i:] == tail[0:i]:
|
---|
71 | return((i,(tail[0:i]),head + tail[i:]))
|
---|
72 |
|
---|
73 | # None found return full part
|
---|
74 | return(-1,'',head + tail)
|
---|
75 |
|
---|
76 | # File names
|
---|
77 | file1 = "data/AE005174v2-1.fas"
|
---|
78 | file2 = "data/AE005174v2-2.fas"
|
---|
79 |
|
---|
80 | # Get data
|
---|
81 | seq1 = parse_file(file1)
|
---|
82 | seq2 = parse_file(file1)
|
---|
83 |
|
---|
84 | seq1 = fasta.replace(seq1)
|
---|
85 | seq2 = fasta.replace(seq2)
|
---|
86 |
|
---|
87 | # Find overlap
|
---|
88 | (retval, common, result) = concat(seq2,seq1)
|
---|
89 | print retval, common
|
---|
90 |
|
---|
91 | print file1
|
---|
92 | stats(seq1)
|
---|
93 | reading_frames(seq1)
|
---|
94 | print file2
|
---|
95 | stats(seq2)
|
---|
96 | reading_frames(seq2)
|
---|
97 |
|
---|
98 |
|
---|
99 | # Strictly speaking there is a gap of about 4 kbs (4000 bs) between seq1 and
|
---|
100 | # seq2, so lets' put that into the the statistics as well. Due to circular
|
---|
101 | # nature, does not matter wether we add it in the beginning or in the end
|
---|
102 | print "Total (inc 4 kbs gap (n))"
|
---|
103 | result = result + "n" * 4000;
|
---|
104 | stats(result)
|
---|
105 | reading_frames(result)
|
---|
106 |
|
---|
107 | # Write to file for later further processing
|
---|
108 | out = open("full_contig.raw","w")
|
---|
109 | out.write(result)
|
---|
110 | out.close()
|
---|