-
Notifications
You must be signed in to change notification settings - Fork 55
/
seqproperties.py
2203 lines (1905 loc) · 90.6 KB
/
seqproperties.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''!
Nucleotide and Amino Acid Sequence Analysis Tool
Date created: 3rd May 2018
License: GNU General Public License version 3 for academic or
not-for-profit use only
Reference: Ling, MHT. 2020. SeqProperties: A Python Command-Line Tool
for Basic Sequence Analysis. Acta Scientific Microbiology 3(6): 103-106.
Bactome package 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import random
import subprocess
import sys
try:
from Bio import Align
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqUtils.ProtParam import ProteinAnalysis
except ImportError:
subprocess.check_call([sys.executable, '-m', 'pip',
'install', 'biopython',
'--trusted-host', 'pypi.org',
'--trusted-host', 'files.pythonhosted.org'])
from Bio import Align
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqUtils.ProtParam import ProteinAnalysis
import Bio
biopython_version = float(Bio.__version__)
if biopython_version < 1.78:
from Bio.Alphabet import generic_dna
from Bio.Alphabet import generic_rna
try:
import fire
except ImportError:
subprocess.check_call([sys.executable, '-m', 'pip',
'install', 'fire',
'--trusted-host', 'pypi.org',
'--trusted-host', 'files.pythonhosted.org'])
import fire
class CodonUsageBias(object):
'''!
Class to hold the core methods for codon usage bias analysis.
'''
def __init__(self):
'''!
Constructor method.
'''
self.seqNN = {}
self.codonCount = {}
self.aalist = ['A', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'K', 'L',
'M', 'N', 'P', 'Q', 'R',
'S', 'T', 'V', 'W', 'Y',
'*']
def addSequencesFromFasta(self, fastafile):
'''!
Method to add sequences from FASTA file into data structure.
This allows for sequences to be added from multiple FASTA
files.
@param fastafile String: Path to the FASTA file to add.
'''
for r in SeqIO.parse(fastafile, 'fasta'):
self.seqNN[r.id] = [r.seq, r.description]
def generateCodonCount(self, seq, genetic_code=1):
'''!
Method to generate codon counts from sequence. This method
will take the nucleotide sequence to translate into amino
acid sequence before codon count generation for each amino
acid, resulting in codon counts per amino acid.
@param seq String: Nucleotide sequence string
@param genetic_code Integer: Genetic code number to be used
for translation. Default = 1 (Standard Code). For more
information, see <https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi>
@return A nested dictionary of {'amino acid': {'codon':
count}}
'''
nnseq = str(seq)
nnseq = [nnseq[i:3+i] for i in range(0, len(nnseq), 3)]
aaseq = str(seq.translate(genetic_code))
aaseq = [aaseq[i:1+i] for i in range(len(aaseq))]
table = {'G': {}, 'P': {}, 'A': {}, 'V': {}, 'L': {},
'I': {}, 'M': {}, 'C': {}, 'F': {}, 'Y': {},
'W': {}, 'H': {}, 'K': {}, 'R': {}, 'Q': {},
'N': {}, 'E': {}, 'D': {}, 'S': {}, 'T': {},
'*': {}}
for index in range(len(nnseq)):
codon = nnseq[index]
aa = aaseq[index]
if codon in table[aa]:
table[aa][codon] = table[aa][codon] + 1
else:
table[aa][codon] = 1
return table
def generateCodonCounts(self, genetic_code=1):
'''!
Method to iterate through all sequences to generate codon
counts.
@param genetic_code Integer: Genetic code number to be used
for translation. Default = 1 (Standard Code). For more
information, see <https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi>
'''
for k in self.seqNN:
codonCount = self.generateCodonCount(self.seqNN[k][0],
genetic_code)
self.codonCount[k] = codonCount
def sequenceIDs(fastafile):
'''!
Function to print out the sequence IDs of all the FASTA records
in the FASTA file.
Usage:
python seqproperties.py showIDs --fastafile=<FASTA file path>
The output will be in the format of
<count> : <sequence ID>
where
- count is the numeric running order
- sequence ID is the sequence ID of the FASTA record.
@param fastafile String: Path to the FASTA file to be processed.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
count = 1
for k in o.seqNN:
print('%i : %s' % (count, k))
count = count + 1
def sequenceDescriptions(fastafile):
'''!
Function to print out the sequence IDs and descriptions of all the
FASTA records in the FASTA file.
Usage:
python seqproperties.py showDesc --fastafile=<FASTA file path>
The output will be in the format of
<count> : <sequence ID> : <description>
where
- count is the numeric running order
- sequence ID is the sequence ID of the FASTA record
- description is the description of the FASTA record
@param fastafile String: Path to the FASTA file to be processed.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
count = 1
for k in o.seqNN:
print('%i : %s : %s' % (count, k, o.seqNN[k][1]))
count = count + 1
def translate(fastafile, genetic_code=1):
'''!
Function to translate all the FASTA records from nucleotide
sequence(s) to amino acid sequence(s).
Usage:
python seqproperties.py translate --fastafile=<FASTA file path> --genetic_code=<genetic code number>
The output will be in the format of
<sequence ID> : <amino acid sequence>
where
- sequence ID is the sequence ID of the FASTA record
- amino acid sequence is the translated amino acid sequence
of the FASTA record
@param fastafile String: Path to the FASTA file to be processed.
@param genetic_code Integer: Genetic code number to be used for
translation. Default = 1 (Standard Code). For more information,
see <https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi>
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
aaseq = o.seqNN[k][0].translate(genetic_code)
print('%s : %s' % (k, str(aaseq)))
def aminoacidCount(fastafile, molecule, genetic_code=1, to_stop=True):
'''!
Function to translate each nucleotide sequence (by FASTA record)
and generate a frequency table of the amino acids.
Usage:
python seqproperties.py aacount --molecule=<molecule type> --genetic_code=<genetic code number> --to_stop=<Boolean flag> --fastafile=<FASTA file path>
The output will be in the format of
<sequence ID> : <A count> : <C count> : <D count> : <E count> : <F count> : <G count> : <H count> : <I count> : <K count> : <L count> : <M count> : <N count> : <P count> : <Q count> : <R count> : <S count> : <T count> : <V count> : <W count> : <Y count>
where
- sequence ID is the sequence ID of the FASTA record
- the counts are the number of the respective amino acid; for
example, A count is the number of alanine in the peptide
@param fastafile String: Path to the FASTA file to be processed.
@param molecule String: Defines the type of molecule. Three
options are allowed: 'peptide' for amino acid sequences, 'DNA' for
DNA sequences (requires transcription and translation), and 'RNA'
for RNA sequence (requires translation).
@param genetic_code Integer: Genetic code number to be used for
translation. Default = 1 (Standard Code). For more information,
see <https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi>
@param to_stop Boolean: Flag to stop translation when first stop
codon is encountered. Default = True.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
header = ' : '.join(['SequenceID', 'A', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V',
'W', 'Y'])
print(header)
for k in o.seqNN:
sequence = _toPeptide(str(o.seqNN[k][0]), molecule,
genetic_code, to_stop)
aacount = sequence.count_amino_acids()
try:
data = [k]
for aa in ['A', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'K', 'L',
'M', 'N', 'P', 'Q', 'R',
'S', 'T', 'V', 'W', 'Y']:
data.append(aacount[aa])
data = ' : '.join([str(x) for x in data])
print(data)
except ZeroDivisionError:
data = ' : '.join([str(k), 'undefined'])
print(data)
except KeyError:
data = ' : '.join([str(k), 'KeyError'])
print(data)
except IndexError:
data = ' : '.join([str(k), 'IndexError'])
print(data)
except IOError:
data = ' : '.join([str(k), 'Error'])
print(data)
def genericCount(fastafile):
'''!
Function to count the frequency of each character in each
nucleotide sequence (by FASTA record) and generate a frequency
table.
Usage:
python seqproperties.py count --fastafile=<FASTA file path>
The output will be in the format of
<sequence ID> : <length of sequence> : [list of counts delimited by " : "]
where
- sequence ID is the sequence ID of the FASTA record
- the counts are the number of the respective characters
@param fastafile String: Path to the FASTA file to be processed.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
char_set = set()
for k in o.seqNN:
sequence = [x for x in str(o.seqNN[k][0])]
char_set.update(sequence)
char_set = list(char_set)
char_set.sort()
header = ['SequenceID', 'Length'] + [c.upper() for c in char_set]
header = ' : '.join(header)
print(header)
for k in o.seqNN:
sequence = str(o.seqNN[k][0])
data = [k, len(sequence)] + \
[sequence.count(c) for c in char_set]
data = ' : '.join([str(x) for x in data])
print(data)
def peptideLength(fastafile):
'''!
Function to count the number of amino acids (peptide length) by
peptide FASTA record.
Usage:
python seqproperties.py plength --fastafile=<FASTA file path>
The output will be in the format of
<sequence ID> : <peptide length>
where
- sequence ID is the sequence ID of the FASTA record
- peptide length is the number of amino acids in the peptide
@param fastafile String: Path to the FASTA file to be processed.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
aaseq = o.seqNN[k][0]
print('%s : %s' % (k, str(len(str(aaseq)))))
def nucleotideLength(fastafile):
'''!
Function to count the number of nucleotides (number of bases) in
each FASTA record.
Usage:
python seqproperties.py nlength --fastafile=<FASTA file path>
The output will be in the format of
<sequence ID> : <nucleotide length>
where
- sequence ID is the sequence ID of the FASTA record
- nucleotide length is the number of bases in the sequence
@param fastafile String: Path to the FASTA file to be processed.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
nnseq = o.seqNN[k][0]
print('%s : %s' % (k, str(len(str(nnseq)))))
def complement(fastafile):
'''!
Function to generate the complement sequence of each FASTA record.
This is done using reverse_complement function in Biopython - each
FASTA sequence is assumed to be in 5'-->3', this function will
generate the complementary sequence in 5'-->3' orientation rather
than 3'<--5' orientation.
Usage:
python seqproperties.py complement --fastafile=<FASTA file path>
The output will be in FASTA format.
@param fastafile String: Path to the FASTA file to be processed.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
nnseq = o.seqNN[k][0]
print("> %s" % k)
print(str(nnseq.reverse_complement()))
def flattenCodonCount(CC):
'''!
Function to flatten the codon usage / frequency table by
removing the amino acid identifiers.
@param CC: Codon usage table as a nested dictionary
of {'amino acid': {'codon': count}}.
@return Dictionary of {'codon': count}.
'''
table = {'AAA': 0, 'AAT': 0, 'AAG': 0, 'AAC': 0,
'ATA': 0, 'ATT': 0, 'ATG': 0, 'ATC': 0,
'AGA': 0, 'AGT': 0, 'AGG': 0, 'AGC': 0,
'ACA': 0, 'ACT': 0, 'ACG': 0, 'ACC': 0,
'TAA': 0, 'TAT': 0, 'TAG': 0, 'TAC': 0,
'TTA': 0, 'TTT': 0, 'TTG': 0, 'TTC': 0,
'TGA': 0, 'TGT': 0, 'TGG': 0, 'TGC': 0,
'TCA': 0, 'TCT': 0, 'TCG': 0, 'TCC': 0,
'GAA': 0, 'GAT': 0, 'GAG': 0, 'GAC': 0,
'GTA': 0, 'GTT': 0, 'GTG': 0, 'GTC': 0,
'GGA': 0, 'GGT': 0, 'GGG': 0, 'GGC': 0,
'GCA': 0, 'GCT': 0, 'GCG': 0, 'GCC': 0,
'CAA': 0, 'CAT': 0, 'CAG': 0, 'CAC': 0,
'CTA': 0, 'CTT': 0, 'CTG': 0, 'CTC': 0,
'CGA': 0, 'CGT': 0, 'CGG': 0, 'CGC': 0,
'CCA': 0, 'CCT': 0, 'CCG': 0, 'CCC': 0}
for aa in CC:
for codon in CC[aa]:
table[codon] = CC[aa][codon]
return table
def codonCount(fastafile, genetic_code=1):
'''!
Function to generate the codon usage frequency table by each
FASTA record.
Usage:
python seqproperties.py codoncount --fastafile=<FASTA file path> --genetic_code=<genetic code number>
The output will be in the format of
<sequence ID> : <AAA count> : <AAC count> : <AAG count> : <AAT count> : <ACA count> : <ACC count> : <ACG count> : <ACT count> : <AGA count> : <AGC count> : <AGG count> : <AGT count> : <ATA count> : <ATC count> : <ATG count> : <ATT count> : <CAA count> : <CAC count> : <CAG count> : <CAT count> : <CCA count> : <CCC count> : <CCG count> : <CCT count> : <CGA count> : <CGC count> : <CGG count> : <CGT count> : <CTA count> : <CTC count> : <CTG count> : <CTT count> : <GAA count> : <GAC count> : <GAG count> : <GAT count> : <GCA count> : <GCC count> : <GCG count> : <GCT count> : <GGA count> : <GGC count> : <GGG count> : <GGT count> : <GTA count> : <GTC count> : <GTG count> : <GTT count> : <TAA count> : <TAC count> : <TAG count> : <TAT count> : <TCA count> : <TCC count> : <TCG count> : <TCT count> : <TGA count> : <TGC count> : <TGG count> : <TGT count> : <TTA count> : <TTC count> : <TTG count> : <TTT count>
where
- sequence ID is the sequence ID of the FASTA record
- the counts are the number of each codon; for example,
AAA count is the number of AAA codon in the sequence
@param fastafile String: Path to the FASTA file to be processed.
@param genetic_code Integer: Genetic code number to be used for
translation. Default = 1 (Standard Code). For more information,
see <https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi>
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
header = ' : '.join(['SequenceID', 'AAA', 'AAC', 'AAG', 'AAT',
'ACA', 'ACC', 'ACG', 'ACT', 'AGA', 'AGC', 'AGG', 'AGT', 'ATA',
'ATC', 'ATG', 'ATT', 'CAA', 'CAC', 'CAG', 'CAT', 'CCA', 'CCC',
'CCG', 'CCT', 'CGA', 'CGC', 'CGG', 'CGT', 'CTA', 'CTC', 'CTG',
'CTT', 'GAA', 'GAC', 'GAG', 'GAT', 'GCA', 'GCC', 'GCG', 'GCT',
'GGA', 'GGC', 'GGG', 'GGT', 'GTA', 'GTC', 'GTG', 'GTT', 'TAA',
'TAC', 'TAG', 'TAT', 'TCA', 'TCC', 'TCG', 'TCT', 'TGA', 'TGC',
'TGG', 'TGT', 'TTA', 'TTC', 'TTG', 'TTT'])
print(header)
for k in o.seqNN:
try:
codonCount = o.generateCodonCount(o.seqNN[k][0],
genetic_code)
fCCount = flattenCodonCount(codonCount)
data = [k, fCCount['AAA'], fCCount['AAC'], fCCount['AAG'],
fCCount['AAT'], fCCount['ACA'], fCCount['ACC'],
fCCount['ACG'], fCCount['ACT'], fCCount['AGA'],
fCCount['AGC'], fCCount['AGG'], fCCount['AGT'],
fCCount['ATA'], fCCount['ATC'], fCCount['ATG'],
fCCount['ATT'], fCCount['CAA'], fCCount['CAC'],
fCCount['CAG'], fCCount['CAT'], fCCount['CCA'],
fCCount['CCC'], fCCount['CCG'], fCCount['CCT'],
fCCount['CGA'], fCCount['CGC'], fCCount['CGG'],
fCCount['CGT'], fCCount['CTA'], fCCount['CTC'],
fCCount['CTG'], fCCount['CTT'], fCCount['GAA'],
fCCount['GAC'], fCCount['GAG'], fCCount['GAT'],
fCCount['GCA'], fCCount['GCC'], fCCount['GCG'],
fCCount['GCT'], fCCount['GGA'], fCCount['GGC'],
fCCount['GGG'], fCCount['GGT'], fCCount['GTA'],
fCCount['GTC'], fCCount['GTG'], fCCount['GTT'],
fCCount['TAA'], fCCount['TAC'], fCCount['TAG'],
fCCount['TAT'], fCCount['TCA'], fCCount['TCC'],
fCCount['TCG'], fCCount['TCT'], fCCount['TGA'],
fCCount['TGC'], fCCount['TGG'], fCCount['TGT'],
fCCount['TTA'], fCCount['TTC'], fCCount['TTG'],
fCCount['TTT']]
data = ' : '.join([str(x) for x in data])
print(data)
except: pass
def percentGC(fastafile):
'''!
Function to generate the %GC by each FASTA record.
Usage:
python seqproperties.py gc --fastafile=<FASTA file path>
The output will be in the format of
<sequence ID> : <%GC>
@param fastafile String: Path to the FASTA file to be processed.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
sequence = str(o.seqNN[k][0])
percent = sequence.count('G') + sequence.count('C') + \
sequence.count('g') + sequence.count('c')
percent = percent / len(sequence)
data = [k, percent]
data = ' : '.join([str(x) for x in data])
print(data)
def percentG(fastafile):
'''!
Function to generate the %G by each FASTA record.
Usage:
python seqproperties.py g --fastafile=<FASTA file path>
The output will be in the format of
<sequence ID> : <%G>
@param fastafile String: Path to the FASTA file to be processed.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
sequence = str(o.seqNN[k][0])
percent = sequence.count('G') + sequence.count('g')
percent = percent / len(sequence)
data = [k, percent]
data = ' : '.join([str(x) for x in data])
print(data)
def percentA(fastafile):
'''!
Function to generate the %A by each FASTA record.
Usage:
python seqproperties.py a --fastafile=<FASTA file path>
The output will be in the format of
<sequence ID> : <%A>
@param fastafile String: Path to the FASTA file to be processed.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
sequence = str(o.seqNN[k][0])
percent = sequence.count('A') + sequence.count('a')
percent = percent / len(sequence)
data = [k, percent]
data = ' : '.join([str(x) for x in data])
print(data)
def percentGCi(fastafile, i, j=3):
'''!
Function to generate the %GC of the i-th base in each codon
(of j length) by each FASTA record.
Usage:
python seqproperties.py gci --i=1 --j=3 --fastafile=<FASTA file path>
The output will be in the format of
<sequence ID> : <%GC of i-th base in codon of j bases>
@param fastafile String: Path to the FASTA file to be processed.
@param i Integer: Position of the base in the codon.
@param j Integer: Size (length) of each codon. Default = 3.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
i = int(i)
j = int(j)
for k in o.seqNN:
sequence = str(o.seqNN[k][0])
sequence = [sequence[i:i+j]
for i in range(0, len(sequence), j)]
temp = []
for x in sequence:
try: temp.append(x[i-1])
except IndexError: pass
sequence = temp
percent = sequence.count('G') + sequence.count('C') + \
sequence.count('g') + sequence.count('c')
percent = percent / len(sequence)
data = ' : '.join([str(k), str(percent)])
print(data)
def percentGi(fastafile, i, j=3):
'''!
Function to generate the %G of the i-th base in each codon
(of j length) by each FASTA record.
Usage:
python seqproperties.py gi --i=1 --j=3 --fastafile=<FASTA file path>
The output will be in the format of
<sequence ID> : <%G of i-th base in codon of j bases>
@param fastafile String: Path to the FASTA file to be processed.
@param i Integer: Position of the base in the codon.
@param j Integer: Size (length) of each codon. Default = 3.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
i = int(i)
j = int(j)
for k in o.seqNN:
sequence = str(o.seqNN[k][0])
sequence = [sequence[i:i+j]
for i in range(0, len(sequence), j)]
temp = []
for x in sequence:
try: temp.append(x[i-1])
except IndexError: pass
sequence = temp
percent = sequence.count('G') + sequence.count('g')
percent = percent / len(sequence)
data = ' : '.join([str(k), str(percent)])
print(data)
def percentAi(fastafile, i, j=3):
'''!
Function to generate the %A of the i-th base in each codon
(of j length) by each FASTA record.
Usage:
python seqproperties.py ai --i=1 --j=3 --fastafile=<FASTA file path>
The output will be in the format of
<sequence ID> : <%A of i-th base in codon of j bases>
@param fastafile String: Path to the FASTA file to be processed.
@param i Integer: Position of the base in the codon.
@param j Integer: Size (length) of each codon. Default = 3.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
i = int(i)
j = int(j)
for k in o.seqNN:
sequence = str(o.seqNN[k][0])
sequence = [sequence[i:i+j]
for i in range(0, len(sequence), j)]
temp = []
for x in sequence:
try: temp.append(x[i-1])
except IndexError: pass
sequence = temp
percent = sequence.count('A') + sequence.count('a')
percent = percent / len(sequence)
data = ' : '.join([str(k), str(percent)])
print(data)
def _toPeptide(sequence, molecule, genetic_code=1, to_stop=True):
'''
Private function - Takes a sequence (DNA/RNA/amino acid) and
process it according to return a ProteinAnalysis object.
@param sequence String: Nucleotide (DNA/RNA) or amino acid
sequence.
@param molecule String: Defines the type of molecule. Three
options are allowed: 'peptide' for amino acid sequences, 'DNA' for
DNA sequences (requires transcription and translation), and 'RNA'
for RNA sequence (requires translation).
@param genetic_code Integer: Genetic code number to be used for
translation. Default = 1 (Standard Code). For more information,
see <https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi>
@param to_stop Boolean: Flag to stop translation when first stop
codon is encountered. Default = True.
@return: Bio.SeqUtils.ProtParam.ProteinAnalysis object
'''
if molecule.lower() == 'peptide':
peptide = ProteinAnalysis(sequence)
elif molecule.lower() == 'rna':
rna = str(sequence)
rna = Seq(rna, generic_rna)
peptide = rna.translate(genetic_code, to_stop=to_stop)
peptide = ProteinAnalysis(str(peptide))
elif molecule.lower() == 'dna':
dna = str(sequence)
dna = Seq(dna, generic_dna)
rna = dna.transcribe()
peptide = rna.translate(genetic_code, to_stop=to_stop)
peptide = ProteinAnalysis(str(peptide))
return peptide
def molecularWeight(fastafile, molecule, genetic_code=1, to_stop=True):
'''!
Function to calculate the molecular weight, using Biopython, by
each FASTA record.
Usage:
python seqproperties.py mw --molecule=<molecule type> --genetic_code=<genetic code number> --to_stop=<Boolean flag> --fastafile=<FASTA file path>
Options for genetic_code and to_stop are needed if molecule type
is not peptide, as these options are needed for translation. The
output will be in the format of
<sequence ID> : <molecular weight>
@param fastafile String: Path to the FASTA file to be processed.
@param molecule String: Defines the type of molecule. Three
options are allowed: 'peptide' for amino acid sequences, 'DNA' for
DNA sequences (requires transcription and translation), and 'RNA'
for RNA sequence (requires translation).
@param genetic_code Integer: Genetic code number to be used for
translation. Default = 1 (Standard Code). For more information,
see <https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi>
@param to_stop Boolean: Flag to stop translation when first stop
codon is encountered. Default = True.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
try:
sequence = _toPeptide(str(o.seqNN[k][0]), molecule,
genetic_code, to_stop)
result = '%0.2f' % sequence.molecular_weight()
data = [k, result]
data = ' : '.join([str(x) for x in data])
print(data)
except ZeroDivisionError:
data = ' : '.join([str(k), 'undefined'])
print(data)
except KeyError:
data = ' : '.join([str(k), 'KeyError'])
print(data)
except IndexError:
data = ' : '.join([str(k), 'IndexError'])
print(data)
except:
data = ' : '.join([str(k), 'Error'])
print(data)
def aromaticity(fastafile, molecule, genetic_code=1, to_stop=True):
'''!
Function to calculate the aromaticity index by each FASTA record.
This is calculated by BioPython library using method described in
Lobry JR, Gautier C. 1994. Hydrophobicity, expressivity and
aromaticity are the major trends of amino-acid usage in 999
Escherichia coli chromosome-encoded genes, Nucleic Acids Research
22(15):3174-3180. https://doi.org/10.1093/nar/22.15.3174
Usage:
python seqproperties.py aromaticity --molecule=<molecule type> --genetic_code=<genetic code number> --to_stop=<Boolean flag> --fastafile=<FASTA file path>
Options for genetic_code and to_stop are needed if molecule type
is not peptide, as these options are needed for translation. The
output will be in the format of
<sequence ID> : <aromaticity index>
@param fastafile String: Path to the FASTA file to be processed.
@param molecule String: Defines the type of molecule. Three
options are allowed: 'peptide' for amino acid sequences, 'DNA' for
DNA sequences (requires transcription and translation), and 'RNA'
for RNA sequence (requires translation).
@param genetic_code Integer: Genetic code number to be used for
translation. Default = 1 (Standard Code). For more information,
see <https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi>
@param to_stop Boolean: Flag to stop translation when first stop
codon is encountered. Default = True.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
sequence = _toPeptide(str(o.seqNN[k][0]), molecule,
genetic_code, to_stop)
try:
result = '%0.6f' % sequence.aromaticity()
data = [k, result]
data = ' : '.join([str(x) for x in data])
print(data)
except ZeroDivisionError:
data = ' : '.join([str(k), 'undefined'])
print(data)
except KeyError:
data = ' : '.join([str(k), 'KeyError'])
print(data)
except IndexError:
data = ' : '.join([str(k), 'IndexError'])
print(data)
except:
data = ' : '.join([str(k), 'Error'])
print(data)
def instability(fastafile, molecule, genetic_code=1, to_stop=True):
'''!
Function to calculate the instability index by each FASTA record.
This is calculated by BioPython library using method described in
Guruprasad et al. 1990. Correlation between stability of a protein
and its dipeptide composition: a novel approach for predicting in
vivo stability of a protein from its primary sequence, Protein
Engineering, Design and Selection 4(2):155-161.
Usage:
python seqproperties.py instability --molecule=<molecule type> --genetic_code=<genetic code number> --to_stop=<Boolean flag> --fastafile=<FASTA file path>
Options for genetic_code and to_stop are needed if molecule type
is not peptide, as these options are needed for translation. The
output will be in the format of
<sequence ID> : <instability index>
@param fastafile String: Path to the FASTA file to be processed.
@param molecule String: Defines the type of molecule. Three
options are allowed: 'peptide' for amino acid sequences, 'DNA' for
DNA sequences (requires transcription and translation), and 'RNA'
for RNA sequence (requires translation).
@param genetic_code Integer: Genetic code number to be used for
translation. Default = 1 (Standard Code). For more information,
see <https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi>
@param to_stop Boolean: Flag to stop translation when first stop
codon is encountered. Default = True.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
sequence = _toPeptide(str(o.seqNN[k][0]), molecule,
genetic_code, to_stop)
try:
result = '%0.3f' % sequence.instability_index()
data = [k, result]
data = ' : '.join([str(x) for x in data])
print(data)
except ZeroDivisionError:
data = ' : '.join([str(k), 'undefined'])
print(data)
except KeyError:
data = ' : '.join([str(k), 'KeyError'])
print(data)
except IndexError:
data = ' : '.join([str(k), 'IndexError'])
print(data)
except:
data = ' : '.join([str(k), 'Error'])
print(data)
def isoelectric(fastafile, molecule, genetic_code=1, to_stop=True):
'''!
Function to calculate the isoelectric point (pI) by each FASTA
record. This is calculated by BioPython library.
Usage:
python seqproperties.py isoelectric --molecule=<molecule type> --genetic_code=<genetic code number> --to_stop=<Boolean flag> --fastafile=<FASTA file path>
Options for genetic_code and to_stop are needed if molecule type
is not peptide, as these options are needed for translation. The
output will be in the format of
<sequence ID> : <isoelectric point>
@param fastafile String: Path to the FASTA file to be processed.
@param molecule String: Defines the type of molecule. Three
options are allowed: 'peptide' for amino acid sequences, 'DNA' for
DNA sequences (requires transcription and translation), and 'RNA'
for RNA sequence (requires translation).
@param genetic_code Integer: Genetic code number to be used for
translation. Default = 1 (Standard Code). For more information,
see <https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi>
@param to_stop Boolean: Flag to stop translation when first stop
codon is encountered. Default = True.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
sequence = _toPeptide(str(o.seqNN[k][0]), molecule,
genetic_code, to_stop)
try:
result = '%0.2f' % sequence.isoelectric_point()
data = [k, result]
data = ' : '.join([str(x) for x in data])
print(data)
except ZeroDivisionError:
data = ' : '.join([str(k), 'undefined'])
print(data)
except KeyError:
data = ' : '.join([str(k), 'KeyError'])
print(data)
except IndexError:
data = ' : '.join([str(k), 'IndexError'])
print(data)
except:
data = ' : '.join([str(k), 'Error'])
print(data)
def secondaryStructure(fastafile, molecule, genetic_code=1,
to_stop=True):
'''!
Function to calculate the secondary structure fractions by each
FASTA record. This is calculated by BioPython library.
Usage:
python seqproperties.py secstruct --molecule=<molecule type> --genetic_code=<genetic code number> --to_stop=<Boolean flag> --fastafile=<FASTA file path>
Options for genetic_code and to_stop are needed if molecule type
is not peptide, as these options are needed for translation. The
output will be in the format of
<sequence ID> : <helix fraction> : <turn fraction> : <sheet fraction>
@param fastafile String: Path to the FASTA file to be processed.
@param molecule String: Defines the type of molecule. Three
options are allowed: 'peptide' for amino acid sequences, 'DNA' for
DNA sequences (requires transcription and translation), and 'RNA'
for RNA sequence (requires translation).
@param genetic_code Integer: Genetic code number to be used for
translation. Default = 1 (Standard Code). For more information,
see <https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi>
@param to_stop Boolean: Flag to stop translation when first stop
codon is encountered. Default = True.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
sequence = _toPeptide(str(o.seqNN[k][0]), molecule,
genetic_code, to_stop)
result = sequence.secondary_structure_fraction()
helix = '%0.4f' % result[0]
turn = '%0.4f' % result[1]
sheet = '%0.4f' % result[2]
data = [k, helix, turn, sheet]
data = ' : '.join([str(x) for x in data])
print(data)
def gravy(fastafile, molecule, genetic_code=1, to_stop=True):
'''!
Function to calculate the hydropathy, also known as GRAVY (Grand
Average of Hydropathy), by each FASTA record. This is calculated
by BioPython library using method described in Kyte J and
Doolittle RF. 1982. A simple method for displaying the hydropathic
character of a protein. J. Mol. Biol. 157, 105-132.
Usage:
python seqproperties.py gravy --molecule=<molecule type> --genetic_code=<genetic code number> --to_stop=<Boolean flag> --fastafile=<FASTA file path>
Options for genetic_code and to_stop are needed if molecule type
is not peptide, as these options are needed for translation. The
output will be in the format of
<sequence ID> : <GRAVY value>
@param fastafile String: Path to the FASTA file to be processed.
@param molecule String: Defines the type of molecule. Three
options are allowed: 'peptide' for amino acid sequences, 'DNA' for
DNA sequences (requires transcription and translation), and 'RNA'
for RNA sequence (requires translation).
@param genetic_code Integer: Genetic code number to be used for
translation. Default = 1 (Standard Code). For more information,
see <https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi>
@param to_stop Boolean: Flag to stop translation when first stop
codon is encountered. Default = True.
'''
o = CodonUsageBias()
o.addSequencesFromFasta(fastafile)
for k in o.seqNN:
sequence = _toPeptide(str(o.seqNN[k][0]), molecule,
genetic_code, to_stop)
try:
result = '%0.6f' % sequence.gravy()
data = [k, result]
data = ' : '.join([str(x) for x in data])
print(data)
except ZeroDivisionError:
data = ' : '.join([str(k), 'undefined'])
print(data)
except KeyError:
data = ' : '.join([str(k), 'KeyError'])
print(data)
except IndexError:
data = ' : '.join([str(k), 'IndexError'])
print(data)
except:
data = ' : '.join([str(k), 'Error'])
print(data)
def flexibility(fastafile, molecule, genetic_code=1, to_stop=True):
'''!
Function to calculate the flexibility by each FASTA record. This
is calculated by BioPython library using method described in
Vihinen, et al. 1994. Accuracy of protein flexibility predictions.
Proteins, 19: 141-149.
Usage: