annotate GeneCodonSearch1_2.py @ 1:e8c8bf56aab0 draft default tip

Uploaded
author jamueller
date Thu, 23 May 2019 05:16:03 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
1 #1: Modules
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
2 import re
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
3 import argparse
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
4 import numpy as np
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
5 np.set_printoptions(threshold=np.inf)
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
6
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
7 parser = argparse.ArgumentParser()
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
8 parser.add_argument("-m", "--minimum",type=float, help="cutoff value")
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
9 parser.add_argument("-c", "--codon",type=str, help="codons of interest")
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
10 parser.add_argument("-i", "--input_sequences", type=str, help="")
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
11 parser.add_argument("-r", "--result", type=str)
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
12
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
13 args = parser.parse_args()
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
14
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
15 sequences = args.input_sequences
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
16 cutoff = args.minimum
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
17 codonlist=args.codon
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
18 resultfile=args.result
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
19
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
20 codonlist = codonlist.split(",")
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
21
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
22 i=0
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
23 codon_dictionary = {}
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
24 while i < len(codonlist):
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
25 codon_dictionary[codonlist[i]] = 0
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
26 i+=1
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
27 #2:input
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
28 dict1 = {}
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
29 dict2 = {}
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
30 seq_id =""
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
31
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
32 i=0
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
33 input_sequences = open(sequences,mode = 'r')
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
34 for line in input_sequences:
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
35 if line [0] == ">":
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
36 match = re.search(">(\S+)\s(.*)$",line)
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
37 seq_id = match.group(1)
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
38 dict1[seq_id] = ""
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
39 elif seq_id != "" and line.strip()!= "":
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
40 dict1[seq_id] += line.strip()
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
41
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
42 i=0
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
43 for name in dict1.keys():
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
44 dict2[i] = name
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
45 i+=1
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
46
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
47 testsequences=int(len(dict2))
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
48
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
49 #3: Codon-Scoring of the sequences
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
50 i=0
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
51 m=1
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
52 testseq=""
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
53
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
54 scoremat=np.zeros((len(dict2)+1,len(codon_dictionary)+2)).astype(object)
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
55
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
56 for i in range(len(dict2)):
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
57
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
58 l=0
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
59 scorecomp=0
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
60 codonscores=[]
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
61 testseq=dict1[dict2[i]]
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
62 codon_dictionary=dict.fromkeys(codon_dictionary,0)
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
63
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
64 for l in range(len(testseq)):
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
65 try:
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
66 codon_dictionary.get(testseq[l*3:l*3+3])
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
67 codon_dictionary[testseq[l*3:l*3+3]]+=1
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
68 except KeyError:
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
69 continue
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
70
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
71 scorecomp=sum(codon_dictionary.values())/(float(len(testseq)/3)*len(codon_dictionary))
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
72 for x in codon_dictionary.values():
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
73 codonscores.append(x)
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
74
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
75 if scorecomp > cutoff :
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
76 scoremat[m,0]=dict2[i]
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
77 scoremat[m,1]=round(scorecomp,7)
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
78 scoremat[m,2:]=codonscores
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
79 m+=1
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
80
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
81 scoremat_sort=scoremat[np.lexsort((scoremat[:, 1], ))]
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
82 scoremat_sort[0,0]="Gene_ID"
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
83 scoremat_sort[0,1]="Total Score"
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
84 scoremat_sort[0,2:]=codonlist
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
85 scoremat_cleaned = scoremat_sort[[i for i, x in enumerate(scoremat_sort) if x.any()]]
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
86
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
87 #4: output
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
88 with open(resultfile, 'w') as file_object:
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
89 file_object.write("DNA sequences with a higher total score than ")
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
90 file_object.write(str(cutoff))
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
91 file_object.write("\n")
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
92 file_object.write("\n")
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
93 file_object.write(str(scoremat_cleaned))
e8c8bf56aab0 Uploaded
jamueller
parents:
diff changeset
94 file_object.close()