-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplement_and_split_sites.py
93 lines (73 loc) · 2.52 KB
/
complement_and_split_sites.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'''
Copyright © 2018 Anton Tsukanov. Contacts: [email protected]
License: http://www.gnu.org/licenses/gpl.txt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
'''
import linecache
import argparse
import sys
import pandas as pd
import numpy as np
def read_fasta(path):
fasta = list()
with open(path, 'r') as file:
for line in file:
if not line.startswith('>'):
fasta.append(line.strip().upper())
return(fasta)
def complement(seq):
'''
Make reverse and compelent
'''
output = str()
for letter in seq:
if letter == 'A':
output += 'T'
elif letter == 'C':
output += 'G'
elif letter == 'G':
output += 'C'
elif letter == 'T':
output += 'A'
output = output[::-1]
return(output)
def write_results(path, data):
with open(path, 'w') as file:
for line in data:
file.write(line + '\n')
file.close()
pass
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('input', action='store',
help='path to file with motifs in format: site\nsite\n...')
parser.add_argument('output', action='store',
help='path to write results')
parser.add_argument('-l', '--left', action='store', dest='left',
required=False, type=int, default=0, help='start position of motif in sites (def first = 0)')
parser.add_argument('-r', '--right', action='store', dest='right',
required=False, type=int, default=-1, help='end position of motif in sites (def last = -1)')
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
return(parser.parse_args())
def main():
args = parse_args()
input_path = args.input
output_path = args.output
left = args.left
right = args.right
container = list()
sites = read_fasta(input_path)
for site in sites:
container.append(complement(site[left:right]))
write_results(output_path, container)
if __name__=='__main__':
main()