annotate assign_clades.py @ 0:2e262bbd193f draft

planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
author dfornika
date Wed, 25 Oct 2017 20:54:06 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
1 '''Accepts fasta files containing amino acid sequence, reading them in as
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
2 amino acid sequence objects. Reads influenza clade defintions (i.e. amino
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
3 acids at certain positions) from .json file into dictionary structure. Searches
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
4 each of the amino acid sequence objects for the more general clades (i.e. 3C.2a or
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
5 3C.3a and then 3c.2a1 variants if applicable) and appends and underscore to the
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
6 Sequence name, followed by either the name of the clade or "undetermined". '''
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
7
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
8 '''Author: Diane Eisler, Molecular Microbiology & Genomics, BCCDC Public Health Laboratory, Oct 2017'''
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
9
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
10 import sys,string,os, time, Bio, json
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
11 from Bio import Seq, SeqIO, SeqUtils, Alphabet, SeqRecord
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
12 from Bio.SeqRecord import SeqRecord
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
13 from Bio.Alphabet import IUPAC
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
14 from Bio.Seq import Seq
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
15
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
16 localtime = time.asctime(time.localtime(time.time())) #date and time of analysis
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
17 inFileHandle1 = sys.argv[1] #batch fasta file with sequences to be parsed
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
18 inFileHandle2 = sys.argv[2] # .csv file containing clade definitions and "depth"
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
19 outFileHandle = sys.argv[3] #user-specified name for output file of aa seq's with clade suffixes
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
20 outFile= open(outFileHandle,'w') #open a writable, appendable output file
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
21 seqList = [] #list of aa sequence objects to parse for clade definitions
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
22 cladeList = [] #empty list to hold clade tuples i.e. ("3C.3a", 1 ,{"3":"I", "9":"V"..})
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
23
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
24 '''Searches record for required amino acids at defined positions. If found, assigns
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
25 clade name to sequence name by appending underscore and clade name to record id.'''
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
26 def call_clade(record):
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
27 print "---------------------------------------------------------------------"
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
28 print "Parsing %s for matching flu clade definitions..." % (record.id)
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
29 matchList = [] #empty list to hold clades that match 100%
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
30 #iterate over each tuple in the clade list
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
31 for clade in cladeList:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
32 cladeName = clade[0] #temp variable for name
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
33 depth = clade[1] #temp variable for depth
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
34 sites = clade[2] #temp variable for aa def dictionary
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
35 shouldFind = len(sites) #number of sites that should match
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
36 found = 0 #a counter to hold matches to antigenic sites
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
37 #iterate over each position in sites dictionary
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
38 for pos, aa in sites.iteritems():
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
39 #translate pos to corresponding index in target sequence
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
40 index = int(pos) - 1
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
41 #if record at index has same amino acid as 'aa', increment 'found'
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
42 if record[index] == aa:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
43 found += 1
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
44 if (found == shouldFind):
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
45 #print "\n%s contains %i of %i clade \"%s\" antigenic sites!" % (record.id,found, shouldFind, cladeName)
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
46 #add the matching clade name to a list
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
47 #matchList.append(cladeName)
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
48 #add the matching clade tuple to the list of matches
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
49 matchList.append(clade)
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
50 return matchList
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
51
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
52 '''Compares depth level of clades in a list and returns the most granular one'''
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
53 def decide_clade_by_depth(matchList):
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
54 #empty variable for maximum depth encountered
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
55 max_depth = 0
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
56 best_match_name = '' #variable to hold most granular clade
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
57 #for each matching clade, check depth of the corresponding tuple
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
58 for clade in matchList:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
59 #if the current clade is 'deeper' than the one before it
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
60 if clade[1] > max_depth:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
61 #store this depth
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
62 max_depth = clade[1]
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
63 #store name of the clade
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
64 best_match_name = clade[0]
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
65 return best_match_name
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
66
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
67 '''opens the .csv file of clade definitions and clade "depth" '''
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
68 with open (inFileHandle2, 'r') as clade_file:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
69 #remove whitespace from the end of each line and split elements at commas
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
70 for line in clade_file:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
71 #print "Current Line in File:" + line
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
72 sites={} #initialize a dictionary for clade
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
73 elementList = line.rstrip().split(',')
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
74 new_list = [] #start a new list to put non-empty strings into
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
75 #remove empty stings in list
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
76 for item in elementList:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
77 if item != '':
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
78 new_list.append(item)
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
79 name = new_list.pop(0) #move 1st element to name field
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
80 depth = int(new_list.pop(0)) #move 2nd element to depth field
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
81 #read remaining pairs of non-null elements into clade def dictionary
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
82 for i in range(0, len(new_list), 2):
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
83 #move next 2 items from the list into the dict
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
84 pos = new_list[i]
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
85 aa = new_list[i + 1]
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
86 sites[pos] = aa
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
87 #add the clade info as a tuple to the cladeList[]
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
88 oneClade =(name, depth, sites)
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
89 cladeList.append(oneClade)
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
90 print "The List of Clades:"
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
91 for clade in cladeList:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
92 print "Clade Name: %s Depth: %i Antigenic Sites: %i" % (clade[0], clade[1], len(clade[2]))
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
93 for pos, aa in clade[2].iteritems():
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
94 print "Pos: %s\tAA: %s" % (pos,aa)
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
95
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
96 '''opens readable input file of sequences to parse using filename from cmd line,
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
97 instantiates as AA Sequence objects, with ppercase sequences'''
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
98 with open(inFileHandle1,'r') as inFile:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
99 #read in Sequences from fasta file, uppercase and add to seqList
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
100 for record in SeqIO.parse(inFile, "fasta", alphabet=IUPAC.protein):
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
101 record = record.upper()
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
102 seqList.append(record) #add Seq to list of Sequences
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
103 print "\n%i flu HA sequences will be compared to current clade definitions..." % len(seqList)
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
104 #parse each target sequence object
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
105 for record in seqList:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
106 clade_call = '' #empty variale for final clade call on sequence
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
107 matchingCladeList = call_clade(record) #holds matching clade tuples
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
108 #if there is more than one clade match
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
109 if len(matchingCladeList) > 1:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
110 #choose the most granular clade based on depth
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
111 clade_call = decide_clade_by_depth(matchingCladeList)
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
112 #if there is only one clade call
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
113 elif len(matchingCladeList) > 0:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
114 clade = matchingCladeList[0] #take the first tuple in the list
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
115 clade_call = clade[0] #clade name is the first item in the tuple
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
116 #empty list return, no matches
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
117 else:
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
118 clade_call = "No_Match"
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
119 print clade_call
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
120 seq_name = record.id
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
121 mod_name = seq_name + "_" + clade_call
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
122 print "New Sequence Name: " + mod_name
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
123 record.id = mod_name
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
124
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
125
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
126 #output fasta file with clade calls appended to sequence names
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
127 SeqIO.write(seqList,outFile,"fasta")
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
128
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
129 #print("\n%i Sequences Extracted to Output file: %s" % ((len(extractedSeqList),outFileHandle)))
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
130 inFile.close()
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
131 clade_file.close()
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
132 outFile.close()
2e262bbd193f planemo upload for repository https://github.com/dfornika/clade_assignment commit 03a6fa631a506f446f52a7d9bfba649edac8a212
dfornika
parents:
diff changeset
133