-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sayers_Python_Assignment1_Problem5.py
70 lines (57 loc) · 1.72 KB
/
Sayers_Python_Assignment1_Problem5.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
#!python2
# Author: Kevin Sayers
# Assignment 5 implement a fasta parser and calculate the GC
# content of the sequences, determining the sequence with the
# highest GC content. The ID for the highest GC content is then
# output along with the GC content
# parse_fasta_file:
# input: a fasta file
# return: a dictionary containing the ID as the key and the sequence
# as the associated value.
import sys #This import is just for command line argument parsing
def parse_fasta_file(fastafile):
filelist = []
sequences = {}
with open(fastafile) as f:
filelist = [line.strip() for line in f]
i = 0
# each time a > char is reached it becomes the current id
# it is assumed the following lines are valid nucleotide
# sequences
currentid = ''
while i <= len(filelist)-1:
if ">" in filelist[i]:
sequences[filelist[i]] = ''
currentid = filelist[i]
i+=1
else:
sequences[currentid] = sequences[currentid] + filelist[i]
i+=1
return sequences
# get_gc_content:
# input: a string of a DNA nucleotide sequence
# return: the percentage of G/C in the sequence
def get_gc_content(sequence):
gccount = 0.0 #a float so the percentage works later
for nt in sequence:
if nt == "G" or nt == "C":
gccount += 1
return gccount/len(sequence)*100
def main():
# these if/else determines if the file was input as a
# command line argument or whether it should prompt
if len(sys.argv) > 1:
fastadata = parse_fasta_file(sys.argv[1])
else:
filein = raw_input("FASTA filename: ")
fastadata = parse_fasta_file(filein)
maxid = None
maxgc = 0.0
for i in fastadata:
if get_gc_content(fastadata[i]) > maxgc:
maxgc = get_gc_content(fastadata[i])
maxid = i
print maxid
print maxgc
if __name__ == '__main__':
main()