forked from amarallab/NullSeq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnullseq.py
166 lines (149 loc) · 6.09 KB
/
nullseq.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'''
File: NullSeq.py
Author: Sophia Liu
Date: May 16, 2016
Description: This script will generate random nucleotide sequences with a
given GC content either based on a nucleotide or amino acid
sequence or amino acid usage probabitliy
'''
from Bio.Seq import Seq
import NullSeq_Functions as NS
import argparse
import os.path
def main(outfile, number=None, TT=None, AAfile=None,
seqfile=None, l=None, gc=None, ES=False):
#Intro
print()
print('*************************')
print('* Welcome to NullSeq *')
print('*************************')
N = TT
of = outfile
if AAfile is not None:
GC = gc
if AAfile.split('.')[-1] == 'csv':
AAUsage = NS.df_to_dict(NS.AAUsage_from_csv(AAfile))
length = l
AASequence = None
operatingmode = 'AA Usage Frequency'
else:
AASequence = NS.parse_fastafile(AAfile)
AAUsage = NS.get_AA_Freq(AASequence, N, nucleotide=False)
operatingmode = 'Exisitng Sequence - AA'
if l == None:
length = len(AASequence)-1
else:
length = l
if ES:
pass
else:
AASequence = None
elif seqfile is not None:
NucleotideSeq = NS.parse_fastafile(seqfile)
AASequence = str(Seq(NucleotideSeq).translate(table=N)[0:-1])
AAUsage = NS.get_AA_Freq(AASequence, N, nucleotide=False)
operatingmode = 'Exisitng Sequence - Nucleotide'
if gc is None:
gc = NS.get_GC_Freq(NucleotideSeq)*100
else:
pass
if l is None:
length = len(AASequence)-1
else:
length = l
if ES:
pass
else:
AASequence = None
print('\n***********************************************************************')
print('Translation Table:\t%s' %N)
print('Operating Mode:\t\t%s' %operatingmode)
print('AAFreq :')
for AA in AAUsage:
print('\t\t' + AA, '\t', round(AAUsage[AA], 4))
if AASequence is None:
pass
else:
splitAA = [AASequence[i:i+50] for i in range(0, len(AASequence), 50)]
print('AAseq :\t\t', splitAA[0])
for j in range(len(splitAA)):
if j != 0:
print('\t\t', splitAA[j])
else:
pass
print('GC content :\t\t%s' % gc)
print('Length :\t\t%s' % length)
print('Number of sequence :\t%s' % number)
print('***********************************************************************')
if NS.evaluate_possibility(AAUsage, gc, length, N):
seqlist = NS.get_Random_Nuc_Seq(AAfreq=AAUsage, AAseq=AASequence,
GC=gc/100, length=length, n=N, nseq=number)
with open(of, 'w') as f:
for i in range(number):
f.write('>%s\n' %str(i+1))
f.write(seqlist[i])
f.write('\n')
else:
print('The GC content selected is not possible for this AA composition.\n \
Please select a new GC content and retry!')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Make Random Sequences')
parser.add_argument('-m', action='store_true',
help='use exact AA mode')
parser.add_argument('-n', type=int, metavar='number', default=100,
help='number of random sequences generated')
parser.add_argument('-l', type=int, metavar='length', default = 200,
help='Length of random sequence')
parser.add_argument('--TT', type=int, metavar='TransTable', default=11,
choices=[1, 2, 3, 4, 5,
6, 9, 10, 11, 12,
13, 14, 16, 21, 22,
23, 24, 25],
help='NCBI translation Table')
parser.add_argument('--seq', help='location of sample sequence')
parser.add_argument('--AA', help='location of csv file specifiying Amino acid usage frequency')
parser.add_argument('--GC', type=float,
help='GC content of desired sequence [0, 100]. \
If none is given, takes GC content from seq')
parser.add_argument('-o', default='NullSeq_Output.fasta',
help='Output file (fasta format)')
args = parser.parse_args()
if args.n < 1:
raise ValueError('Number of generated random sequences must be greater than 0')
else:
pass
if args.AA is not None:
if os.path.isfile(args.AA):
if args.AA.split('.')[-1] == 'csv':
if args.GC is None or args.l is None:
raise ValueError('Missing Parameters')
else:
main(args.o, AAfile=args.AA, number=args.n,
TT=args.TT, gc=args.GC, l=args.l)
elif args.AA.split('.')[-1] == 'fasta':
if args.GC is None:
raise ValueError('Missing Parameters')
else:
if args.m:
main(args.o, AAfile=args.AA, number=args.n,
TT=args.TT, gc=args.GC, l=args.l, ES=args.m)
else:
main(args.o, AAfile=args.AA, number=args.n,
TT=args.TT, gc=args.GC, l=args.l, ES=args.m)
else:
raise ValueError('Improper File')
else:
raise ValueError('Improper File')
else:
if args.seq is None:
raise ValueError('Input Proper AA usage or nucleotide sequence file')
else:
if os.path.isfile(args.seq) and args.seq.split('.')[-1] == 'fasta':
if args.m:
main(args.o, number=args.n, seqfile=args.seq,
l=args.l, gc=args.GC, TT=args.TT, ES=args.m)
else:
main(args.o, number=args.n, seqfile=args.seq,
l=args.l, gc=args.GC, TT=args.TT, ES=args.m)
else:
raise ValueError('Improper Input File')