-
Notifications
You must be signed in to change notification settings - Fork 2
/
seqlite_mod.py
executable file
·233 lines (189 loc) · 6.52 KB
/
seqlite_mod.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
#!/usr/bin/env python
# version 1.07
#originally written by Steve Haddock, modified by S. Hird
#original full script seqlite.py available http://www.mbari.org/staff/haddock/scripts/
def SNPnumber(fileFromProgram):
"""Reads in FASTA or NBRF files, with Mac or unix CRs and
outputs (or #prints) files with only unique sequences and sites included
v. 1.07 -- started to add support for sys.arg and skipping duplicate names
"""
import re
import sys
from sets import Set as set # useful for version 2.3?
### Change settings here
keepall=False # don't chop out conserved sites? just redundant seqs
printtoscreen=False # false means it will save to the outfile
printhtml=False
savetofile=False
sortthem=True
appendseqs=False
skipseq=False
skipcount=0
fractionseqs=2.0 # proportion below which to label (make 4.0 for 25%)
#other definitinos
firstseq = False
seqname= ''
dataset={}
skipnext=False
nbrf=False
backseqs={}
# First, load in the seqs into a dictionary
# if len(sys.argv)>1:
inputfilename = fileFromProgram
# else:
# print usage
try:
inFile = open(inputfilename, 'r')
alllines = inFile.readlines() # trying a different approach
# this is easier for the \n detection, but for very large files,
# it is probably better to use the original formulation
# plus, does readlines() work in 2.4??
except IOError:
print"\nCan't find the file %s. Are you in the right directory?" % inputfilename
print
print
printtoscreen=True # false means it will save to the outfile
printhtml=False
savetofile=False
alllines=[]
exit(1)
if alllines[0].find('\r')>0:
print
print "MacFormat (use unix file when possible)."
lines=alllines[0].split('\r')
# print "Found %d lines" % len(lines)
else:
lines=alllines
# print len(lines)
for inline in lines:
line=inline.strip('\r').strip('\n')
if skipnext: # needed for NBRF format
skipnext=False
else:
if line and line[0]=='>':
if firstseq:
if dataset.get(seqname) in backseqs.keys(): # gets the sequence from the previous record n the loop
pass #print "Same sequence in",backseqs[dataset[seqname]],"and",seqname
else:
backseqs[dataset[seqname]]=seqname
skipseq=False # if we are skipping a repeat, need to reset now
if line.startswith('>DL;') or line.startswith('>P1;'):
nbrf=True
# print 'NBRF format'
seqname=line.split()[1] # defaults to space
skipnext=True
else:
seqname=line[1:]
# print seqname
if seqname in dataset.keys() and (not appendseqs):
#print "Duplicate sequence name skipped" , seqname
skipcount+=1
skipseq=True # seqname already exists -- skip to next >
firstseq=True
elif skipseq:
continue
elif firstseq: # we know it's not the first line
# print 'we have sequence'
try:
dataset[seqname] += line
except:
dataset[seqname] = line
# this should be modified to append
inFile.close()
if nbrf: print "NBRF format"
dataname=[]
# removes identical sequences -- make another way
# to pupulate catacol_list if you want to keep these
# ->it would just be dataset.items()
# modified to do this above
# for key, value in dataset.items():
# backseqs[value]=key
datarows = backseqs.keys()
dataname = backseqs.values()
numseqs = len(dataname) # after redundant taxon removal
# print dataname
# print datarows
# now get rid of invariable columns
datacols=[]
# don't ask me how this works!!
# transpose the matrix and stuff with - if variable
datacol_list = map(lambda*datarows: [elem or '-' for elem in datarows], *datarows)
# remove invariable columns by testing for the length of set()
# this function leaves unique values
regsub = re.compile(r'[-?xX]')
labelindex = {}
colnum=0
for ri in range(len(datacol_list)):
tempcol=(''.join(datacol_list[ri]))
# Ignore dashes in making consensus
try:
testset=set(regsub.sub('',tempcol))
except NameError:
#print "failed to find set command"
#print 'update to python 2.5 or add the line "from sets import Set as set"'
printtoscreen=False # false means it will save to the outfile
printhtml=False
savetofile=False
break
if len(testset)>1 or keepall: # keepall will be the column saver
datacols.append(tempcol)
starts=[]
currentindex=[]
for lett in testset:
# should define this as a fraction above
currentcount=tempcol.count(lett)
# print str(colnum) +':'+ lett + ':'+ str(currentcount)
# ** Can change this to MORE THAN **
if currentcount < (numseqs/fractionseqs):
starts = [match.start() for match in re.finditer(lett, tempcol)]
currentindex.extend(starts)
# print str(colnum) +':' +lett +':' + str(starts)
for eyes in set(currentindex):
try:
labelindex[eyes].extend([colnum])
except KeyError:
labelindex[eyes]=[colnum]
colnum+=1
## DEBUG ##
# print 'labelindex', labelindex
# return the sequences to their original orientation...
datarow_sub=[]
if len(datacols) > 0:
datarow_list= map(lambda *datacols: [elem or '-' for elem in datacols], *datacols)
# these are lists, so have to "pack" them back into strings
for ri in range(len(datarow_list)):
datarow_sub.append(''.join(datarow_list[ri]))
# print datarow_sub
# (create a numbered list that we can use to look up in the index order later)
indexholder=range(numseqs)
# pull names out of original list to add to new list...
indexdict = dict(zip(datarow_sub,indexholder))
# make a dictionary with d['a'][0] = index and d['a'][1] as name
## DEBUG ##
# print 'indexdict:',indexdict
# remove repeats now that the list has been shortened
sortkeys = list(set(datarow_sub)) # still unsorted
# sort keys (i.e., sequences) to group by similarity
if sortthem:
sortkeys.sort()
# # use this -- a backwards dictionary again
outdict=dict(zip(datarow_sub,dataname))
# needed???
datarow_sub.sort()
########### need to check that we aren't messing things up with all this sorting!
origname=dataset.keys()
origval=dataset[origname[0]];
# remove 1 from the length if NBRF format because of the asterisk
introout='Processing file '+inputfilename
#print introout
firstout= "From a list of %d sites in %d sequences," % (len(origval)-1*nbrf, len(origname))
#print firstout
secondout= "%d unique sites remain in %d sequences\n" % (len(datarow_sub[0]), len(sortkeys))
number_of_SNPS = (len(datarow_sub[0]))
#print'This is the SNP # ', SNPSnumber
#print secondout
return number_of_SNPS
else:
return 0 ;
if __name__ == "__main__":
print info.__doc__