annotate syncpairs.py @ 0:79682a423af7 draft

Uploaded
author brenninc
date Mon, 09 May 2016 05:15:01 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
79682a423af7 Uploaded
brenninc
parents:
diff changeset
1 """
79682a423af7 Uploaded
brenninc
parents:
diff changeset
2 Source: https://raw.githubusercontent.com/mmendez12/sync_paired_end_reads/master/sync_paired_end_reads/syncpairs.py
79682a423af7 Uploaded
brenninc
parents:
diff changeset
3 """
79682a423af7 Uploaded
brenninc
parents:
diff changeset
4 __author__ = 'mickael'
79682a423af7 Uploaded
brenninc
parents:
diff changeset
5 __author__ = 'mickael'
79682a423af7 Uploaded
brenninc
parents:
diff changeset
6
79682a423af7 Uploaded
brenninc
parents:
diff changeset
7 from Bio import SeqIO
79682a423af7 Uploaded
brenninc
parents:
diff changeset
8 from itertools import izip
79682a423af7 Uploaded
brenninc
parents:
diff changeset
9 import argparse
79682a423af7 Uploaded
brenninc
parents:
diff changeset
10
79682a423af7 Uploaded
brenninc
parents:
diff changeset
11
79682a423af7 Uploaded
brenninc
parents:
diff changeset
12 def adjust_name(reads1, reads2):
79682a423af7 Uploaded
brenninc
parents:
diff changeset
13 for r1, r2 in izip(reads1, reads2):
79682a423af7 Uploaded
brenninc
parents:
diff changeset
14 r2.name = r1.description
79682a423af7 Uploaded
brenninc
parents:
diff changeset
15 r2.description = r1.description
79682a423af7 Uploaded
brenninc
parents:
diff changeset
16 r2.id = r1.description
79682a423af7 Uploaded
brenninc
parents:
diff changeset
17 yield r2
79682a423af7 Uploaded
brenninc
parents:
diff changeset
18
79682a423af7 Uploaded
brenninc
parents:
diff changeset
19
79682a423af7 Uploaded
brenninc
parents:
diff changeset
20 def remove_space_from_sequence_header(read):
79682a423af7 Uploaded
brenninc
parents:
diff changeset
21 """ replaces spaces in a read's name by three underscores.
79682a423af7 Uploaded
brenninc
parents:
diff changeset
22 Args:
79682a423af7 Uploaded
brenninc
parents:
diff changeset
23 read: A SeqRecord object (see Biopython)
79682a423af7 Uploaded
brenninc
parents:
diff changeset
24
79682a423af7 Uploaded
brenninc
parents:
diff changeset
25 >>> from Bio.Seq import Seq
79682a423af7 Uploaded
brenninc
parents:
diff changeset
26 >>> from Bio.SeqRecord import SeqRecord
79682a423af7 Uploaded
brenninc
parents:
diff changeset
27 >>> from Bio.Alphabet import SingleLetterAlphabet
79682a423af7 Uploaded
brenninc
parents:
diff changeset
28
79682a423af7 Uploaded
brenninc
parents:
diff changeset
29 >>> read = SeqRecord(Seq("AAAAA",SingleLetterAlphabet),\
79682a423af7 Uploaded
brenninc
parents:
diff changeset
30 id="read A", name="read A", description="read A")
79682a423af7 Uploaded
brenninc
parents:
diff changeset
31
79682a423af7 Uploaded
brenninc
parents:
diff changeset
32 >>> print remove_space_from_sequence_header(read).name
79682a423af7 Uploaded
brenninc
parents:
diff changeset
33 read___A
79682a423af7 Uploaded
brenninc
parents:
diff changeset
34 """
79682a423af7 Uploaded
brenninc
parents:
diff changeset
35
79682a423af7 Uploaded
brenninc
parents:
diff changeset
36 read.description = read.description.replace(' ', '___')
79682a423af7 Uploaded
brenninc
parents:
diff changeset
37 read.name = read.description
79682a423af7 Uploaded
brenninc
parents:
diff changeset
38 read.id = read.description
79682a423af7 Uploaded
brenninc
parents:
diff changeset
39 return read
79682a423af7 Uploaded
brenninc
parents:
diff changeset
40
79682a423af7 Uploaded
brenninc
parents:
diff changeset
41
79682a423af7 Uploaded
brenninc
parents:
diff changeset
42 def next_matching_read(reads1, reads2):
79682a423af7 Uploaded
brenninc
parents:
diff changeset
43 """ return next read2 that matches read2
79682a423af7 Uploaded
brenninc
parents:
diff changeset
44 Args:
79682a423af7 Uploaded
brenninc
parents:
diff changeset
45 reads1: A generator that contains a SeqRecord (see Biopython)
79682a423af7 Uploaded
brenninc
parents:
diff changeset
46
79682a423af7 Uploaded
brenninc
parents:
diff changeset
47 reads2: A generator that contains a SeqRecord (see Biopython)
79682a423af7 Uploaded
brenninc
parents:
diff changeset
48
79682a423af7 Uploaded
brenninc
parents:
diff changeset
49
79682a423af7 Uploaded
brenninc
parents:
diff changeset
50 >>> from Bio.Seq import Seq
79682a423af7 Uploaded
brenninc
parents:
diff changeset
51 >>> from Bio.SeqRecord import SeqRecord
79682a423af7 Uploaded
brenninc
parents:
diff changeset
52 >>> from Bio.Alphabet import SingleLetterAlphabet
79682a423af7 Uploaded
brenninc
parents:
diff changeset
53
79682a423af7 Uploaded
brenninc
parents:
diff changeset
54 >>> reads1 = []
79682a423af7 Uploaded
brenninc
parents:
diff changeset
55 >>> reads2 = []
79682a423af7 Uploaded
brenninc
parents:
diff changeset
56
79682a423af7 Uploaded
brenninc
parents:
diff changeset
57 >>> reads1.append(SeqRecord(Seq("AAAAA",SingleLetterAlphabet),\
79682a423af7 Uploaded
brenninc
parents:
diff changeset
58 id="read A", name="read A", description="read A"))
79682a423af7 Uploaded
brenninc
parents:
diff changeset
59 >>> reads2.append(SeqRecord(Seq("TTTTT",SingleLetterAlphabet),\
79682a423af7 Uploaded
brenninc
parents:
diff changeset
60 id="read A", name="read A", description="read A"))
79682a423af7 Uploaded
brenninc
parents:
diff changeset
61
79682a423af7 Uploaded
brenninc
parents:
diff changeset
62 >>> reads1.append(SeqRecord(Seq("AAAAA",SingleLetterAlphabet),\
79682a423af7 Uploaded
brenninc
parents:
diff changeset
63 id="read B", name="read B", description="read B"))
79682a423af7 Uploaded
brenninc
parents:
diff changeset
64
79682a423af7 Uploaded
brenninc
parents:
diff changeset
65 >>> reads1.append(SeqRecord(Seq("AAAAA",SingleLetterAlphabet),\
79682a423af7 Uploaded
brenninc
parents:
diff changeset
66 id="read C", name="read C", description="read C"))
79682a423af7 Uploaded
brenninc
parents:
diff changeset
67 >>> reads2.append(SeqRecord(Seq("TTTTT",SingleLetterAlphabet),\
79682a423af7 Uploaded
brenninc
parents:
diff changeset
68 id="read C", name="read C", description="read C"))
79682a423af7 Uploaded
brenninc
parents:
diff changeset
69
79682a423af7 Uploaded
brenninc
parents:
diff changeset
70 >>> match = [read2 for read2 in next_matching_read(reads1, reads2)]
79682a423af7 Uploaded
brenninc
parents:
diff changeset
71 >>> print match[0].name
79682a423af7 Uploaded
brenninc
parents:
diff changeset
72 read A
79682a423af7 Uploaded
brenninc
parents:
diff changeset
73 >>> print match[1].name
79682a423af7 Uploaded
brenninc
parents:
diff changeset
74 read C
79682a423af7 Uploaded
brenninc
parents:
diff changeset
75 """
79682a423af7 Uploaded
brenninc
parents:
diff changeset
76
79682a423af7 Uploaded
brenninc
parents:
diff changeset
77 for read1 in reads1:
79682a423af7 Uploaded
brenninc
parents:
diff changeset
78 for read2 in reads2:
79682a423af7 Uploaded
brenninc
parents:
diff changeset
79 if read1.name == read2.name:
79682a423af7 Uploaded
brenninc
parents:
diff changeset
80 yield read2
79682a423af7 Uploaded
brenninc
parents:
diff changeset
81 break
79682a423af7 Uploaded
brenninc
parents:
diff changeset
82
79682a423af7 Uploaded
brenninc
parents:
diff changeset
83
79682a423af7 Uploaded
brenninc
parents:
diff changeset
84 def main():
79682a423af7 Uploaded
brenninc
parents:
diff changeset
85
79682a423af7 Uploaded
brenninc
parents:
diff changeset
86 parser = argparse.ArgumentParser()
79682a423af7 Uploaded
brenninc
parents:
diff changeset
87
79682a423af7 Uploaded
brenninc
parents:
diff changeset
88 parser.add_argument("reads1",help='modified reads')
79682a423af7 Uploaded
brenninc
parents:
diff changeset
89 parser.add_argument("reads2", help='reads to adjust')
79682a423af7 Uploaded
brenninc
parents:
diff changeset
90
79682a423af7 Uploaded
brenninc
parents:
diff changeset
91 parser.add_argument('reads1_output', help='output folder and filename. Note that the folder should already exist')
79682a423af7 Uploaded
brenninc
parents:
diff changeset
92 parser.add_argument('reads2_output', help='output folder and filename. Note that the folder should already exist')
79682a423af7 Uploaded
brenninc
parents:
diff changeset
93
79682a423af7 Uploaded
brenninc
parents:
diff changeset
94 args = parser.parse_args()
79682a423af7 Uploaded
brenninc
parents:
diff changeset
95
79682a423af7 Uploaded
brenninc
parents:
diff changeset
96 #we'll need to go through the reads1 multiple time and it can be a large file
79682a423af7 Uploaded
brenninc
parents:
diff changeset
97 #so it's better to use inline func that return a generator
79682a423af7 Uploaded
brenninc
parents:
diff changeset
98 _reads1 = lambda: (rec for rec in SeqIO.parse(args.reads1, 'fastq'))
79682a423af7 Uploaded
brenninc
parents:
diff changeset
99 _reads2 = (rec for rec in SeqIO.parse(args.reads2, 'fastq'))
79682a423af7 Uploaded
brenninc
parents:
diff changeset
100
79682a423af7 Uploaded
brenninc
parents:
diff changeset
101 matching_reads2 = (read2 for read2 in next_matching_read(_reads1(), _reads2))
79682a423af7 Uploaded
brenninc
parents:
diff changeset
102 synced_reads2_names = (read2 for read2 in adjust_name(_reads1(), matching_reads2))
79682a423af7 Uploaded
brenninc
parents:
diff changeset
103
79682a423af7 Uploaded
brenninc
parents:
diff changeset
104 final_reads1 = (remove_space_from_sequence_header(r1) for r1 in _reads1())
79682a423af7 Uploaded
brenninc
parents:
diff changeset
105 final_reads2 = (remove_space_from_sequence_header(r2) for r2 in synced_reads2_names)
79682a423af7 Uploaded
brenninc
parents:
diff changeset
106
79682a423af7 Uploaded
brenninc
parents:
diff changeset
107 SeqIO.write(final_reads1, args.reads1_output, "fastq")
79682a423af7 Uploaded
brenninc
parents:
diff changeset
108 SeqIO.write(final_reads2, args.reads2_output, "fastq")
79682a423af7 Uploaded
brenninc
parents:
diff changeset
109
79682a423af7 Uploaded
brenninc
parents:
diff changeset
110 if __name__ == '__main__':
79682a423af7 Uploaded
brenninc
parents:
diff changeset
111 main()