annotate microsatbed/find_str.py @ 0:50a1636fde68 draft default tip

Uploaded
author fubar
date Sun, 14 Jul 2024 02:32:13 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
50a1636fde68 Uploaded
fubar
parents:
diff changeset
1 import argparse
50a1636fde68 Uploaded
fubar
parents:
diff changeset
2
50a1636fde68 Uploaded
fubar
parents:
diff changeset
3 import pytrf # 1.3.0
50a1636fde68 Uploaded
fubar
parents:
diff changeset
4 from pyfastx import Fastx # 0.5.2
50a1636fde68 Uploaded
fubar
parents:
diff changeset
5
50a1636fde68 Uploaded
fubar
parents:
diff changeset
6 """
50a1636fde68 Uploaded
fubar
parents:
diff changeset
7 Allows all STR or those for a subset of motifs to be written to a bed file
50a1636fde68 Uploaded
fubar
parents:
diff changeset
8 Designed to build some of the microsatellite tracks from https://github.com/arangrhie/T2T-Polish/tree/master/pattern for the VGP.
50a1636fde68 Uploaded
fubar
parents:
diff changeset
9 """
50a1636fde68 Uploaded
fubar
parents:
diff changeset
10
50a1636fde68 Uploaded
fubar
parents:
diff changeset
11
50a1636fde68 Uploaded
fubar
parents:
diff changeset
12 def write_ssrs(args):
50a1636fde68 Uploaded
fubar
parents:
diff changeset
13 """
50a1636fde68 Uploaded
fubar
parents:
diff changeset
14 The integers in the call change the minimum repeats for mono-, di-, tri-, tetra-, penta-, hexa-nucleotide repeats
50a1636fde68 Uploaded
fubar
parents:
diff changeset
15 ssrs = pytrf.STRFinder(name, seq, 10, 6, 4, 3, 3, 3)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
16 NOTE: Dinucleotides GA and AG are reported separately by https://github.com/marbl/seqrequester.
50a1636fde68 Uploaded
fubar
parents:
diff changeset
17 The reversed pair STRs are about as common in the documentation sample.
50a1636fde68 Uploaded
fubar
parents:
diff changeset
18 Sequence read bias might be influenced by GC density or some other specific motif.
50a1636fde68 Uploaded
fubar
parents:
diff changeset
19 """
50a1636fde68 Uploaded
fubar
parents:
diff changeset
20 bed = []
50a1636fde68 Uploaded
fubar
parents:
diff changeset
21 specific = None
50a1636fde68 Uploaded
fubar
parents:
diff changeset
22 if args.specific:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
23 specific = args.specific.upper().split(",")
50a1636fde68 Uploaded
fubar
parents:
diff changeset
24 fa = Fastx(args.fasta, uppercase=True)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
25 for name, seq in fa:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
26 if args.specific:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
27 ssrs = pytrf.STRFinder(
50a1636fde68 Uploaded
fubar
parents:
diff changeset
28 name,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
29 seq,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
30 args.minreps,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
31 args.minreps,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
32 args.minreps,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
33 args.minreps,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
34 args.minreps,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
35 args.minreps,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
36 )
50a1636fde68 Uploaded
fubar
parents:
diff changeset
37 else:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
38 ssrs = pytrf.STRFinder(
50a1636fde68 Uploaded
fubar
parents:
diff changeset
39 name,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
40 seq,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
41 args.monomin,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
42 args.dimin,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
43 args.trimin,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
44 args.tetramin,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
45 args.pentamin,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
46 args.hexamin,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
47 )
50a1636fde68 Uploaded
fubar
parents:
diff changeset
48 for ssr in ssrs:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
49 row = (
50a1636fde68 Uploaded
fubar
parents:
diff changeset
50 ssr.chrom,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
51 ssr.start - 1,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
52 ssr.end,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
53 ssr.motif,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
54 ssr.repeat,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
55 ssr.length,
50a1636fde68 Uploaded
fubar
parents:
diff changeset
56 )
50a1636fde68 Uploaded
fubar
parents:
diff changeset
57 # pytrf reports a 1 based start position so start-1 fixes the bed interval lengths
50a1636fde68 Uploaded
fubar
parents:
diff changeset
58 if args.specific and ssr.motif in specific:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
59 bed.append(row)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
60 elif args.mono and len(ssr.motif) == 1:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
61 bed.append(row)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
62 elif args.di and len(ssr.motif) == 2:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
63 bed.append(row)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
64 elif args.tri and len(ssr.motif) == 3:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
65 bed.append(row)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
66 elif args.tetra and len(ssr.motif) == 4:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
67 bed.append(row)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
68 elif args.penta and len(ssr.motif) == 5:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
69 bed.append(row)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
70 elif args.hexa and len(ssr.motif) == 6:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
71 bed.append(row)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
72 bed.sort()
50a1636fde68 Uploaded
fubar
parents:
diff changeset
73 obed = ["%s\t%d\t%d\t%s_%d\t%d" % x for x in bed]
50a1636fde68 Uploaded
fubar
parents:
diff changeset
74 with open(args.bed, "w") as outbed:
50a1636fde68 Uploaded
fubar
parents:
diff changeset
75 outbed.write("\n".join(obed))
50a1636fde68 Uploaded
fubar
parents:
diff changeset
76 outbed.write("\n")
50a1636fde68 Uploaded
fubar
parents:
diff changeset
77
50a1636fde68 Uploaded
fubar
parents:
diff changeset
78
50a1636fde68 Uploaded
fubar
parents:
diff changeset
79 if __name__ == "__main__":
50a1636fde68 Uploaded
fubar
parents:
diff changeset
80 parser = argparse.ArgumentParser()
50a1636fde68 Uploaded
fubar
parents:
diff changeset
81 a = parser.add_argument
50a1636fde68 Uploaded
fubar
parents:
diff changeset
82 a("--di", action="store_true")
50a1636fde68 Uploaded
fubar
parents:
diff changeset
83 a("--tri", action="store_true")
50a1636fde68 Uploaded
fubar
parents:
diff changeset
84 a("--tetra", action="store_true")
50a1636fde68 Uploaded
fubar
parents:
diff changeset
85 a("--penta", action="store_true")
50a1636fde68 Uploaded
fubar
parents:
diff changeset
86 a("--hexa", action="store_true")
50a1636fde68 Uploaded
fubar
parents:
diff changeset
87 a("--mono", action="store_true")
50a1636fde68 Uploaded
fubar
parents:
diff changeset
88 a("--dimin", default=2, type=int)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
89 a("--trimin", default=2, type=int)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
90 a("--tetramin", default=2, type=int)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
91 a("--pentamin", default=2, type=int)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
92 a("--hexamin", default=2, type=int)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
93 a("--monomin", default=2, type=int)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
94 a("-f", "--fasta", default="humsamp.fa")
50a1636fde68 Uploaded
fubar
parents:
diff changeset
95 a("-b", "--bed", default="humsamp.bed")
50a1636fde68 Uploaded
fubar
parents:
diff changeset
96 a("--specific", default=None)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
97 a("--minreps", default=2, type=int)
50a1636fde68 Uploaded
fubar
parents:
diff changeset
98 args = parser.parse_args()
50a1636fde68 Uploaded
fubar
parents:
diff changeset
99 write_ssrs(args)