forked from UMCUGenetics/pymc4c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc4c.py
279 lines (236 loc) · 8.98 KB
/
mc4c.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
import argparse
import sys
import log
import numpy as np
import mc4ctools as mc
import pandas as pd
def makePrimerFasta(args):
""" Turn primer sequences into a fasta file.
"""
settings = mc.loadIni(args.inifile)
primerSeqs = mc.getPrimerSeqs(settings)
mc.writePrimerFasta(primerSeqs, args.outfile)
def cleaveReads(args):
""" Cleave the reads by primer sequences. Requires BowTie2 information. """
settings = mc.loadIni(args.inifile)
primerLens = [len(x) for x in settings['prm_seq']]
primers = ['']
primers.extend(settings['prm_seq'])
#print primers
prmCuts = mc.combinePrimers(args.bamfile,primerLens)
#print prmCuts[:10]
mc.applyCuts(args.fastqfile,args.outfile,prmCuts,primers)
def splitReads(args):
""" Split the reads by restriction site information based on the reference genome. """
settings = mc.loadIni(args.inifile)
restSeqs = settings['re_seq']
# TODO: Substitute reference genome with reads (?)
mc.findRestrictionSeqs(args.fastqfile,args.outfile,restSeqs)
def findRefRestSites(args):
""" Determine the location of restriction sites on the reference genome. Takes a fasta file
and stores results as a list per chromosome in a dictionary, which is saved as an npz.
"""
settings = mc.loadIni(args.inifile)
restSeqs = settings['re_seq']
restDict = mc.findReferenceRestSites(args.fastafile,restSeqs,lineLen=args.linelen)
np.savez_compressed(args.restfile,restrsites=restDict)
def getRefResPositions(args):
""" Extract a subset of restriction site positions from the reference genome. """
settings = mc.loadIni(args.inifile)
print [settings['vp_chr']],[settings['vp_start'], settings['vp_end']]
print 'Loading restrsites, this takes a while...'
restrefs=np.load(args.restfile)['restrsites'].item()
print 'Finished loading, moving on'
result = mc.mapToRefSite(restrefs[settings['vp_chr'][0]],[settings['vp_start'][0], settings['vp_end'][0]])
refPosList = []
for i in range(result[0],result[1]+1):
#print i,restrefs[settings['vp_chr'][0]][i]
refPosList.append(restrefs[settings['vp_chr'][0]][i])
pdFrame = pd.DataFrame(refPosList, index=range(result[0],result[1]+1), columns=['start','stop'])
np.savez_compressed(args.outfile,
pdframe=pdFrame,
pdcolumns=pdFrame.columns,
pdindex=pdFrame.index)
def exportToPlot(args):
""" Originally written to easily import the data into interactive plotting tools.
Converts the mapped data to a pandas dataframe and adds restriction site information.
Additionally it creates 2 files that link between restrition sites and read ids for
interaction down the line.
"""
settings = mc.loadIni(args.inifile)
print 'Loading restrsites, this takes a while...'
restrefs=np.load(args.restfile)['restrsites'].item()
print 'Finished loading, moving on'
byRegion,byRead,pdFrame = mc.exportToPlot(settings,restrefs,args.bamfile)
#dupSet = mc.findDuplicates(settings,byRead,byRegion)
#pdFrame['Duplicate'] = np.where(pdFrame['CircleId'].isin(dupSet), True, False)
#print pdFrame
np.savez_compressed(args.plotfile,
pdframe=pdFrame,
pdcolumns=pdFrame.columns,
pdindex=pdFrame.index)
np.savez_compressed(args.plotfile+'_extra',
byregion=byRegion,
byread=byRead)
def markDuplicates(args):
""" This function aims to identify reads that are most likely PCR duplicates.
Identification is based on having overlap with eachother that is not in the viewport.
It takes a pandas dataframe and adds a new column to the end of it.
"""
settings = mc.loadIni(args.inifile)
exFile = np.load(args.extra)
try:
byRead = exFile['byread'].item()
except KeyError:
byRead = exFile['byreads'].item()
byRegion = exFile['byregion'].item()
pdFile = np.load(args.pdframe)
pdFrame = pd.DataFrame(pdFile['pdframe'],columns=pdFile['pdcolumns'],index=pdFile['pdindex'])
dupSet = mc.findDuplicates(settings,byRead,byRegion)
#df['dup']=np.where(pd.Series(df.index).isin([1,5]),True,False)
#pdFrame['Duplicate'] = np.where(pdFrame['CircleId'].isin(dupSet), True, False)
pdFrame['Duplicate'] = np.where(pd.Series(pdFrame.index).isin(dupSet), True, False)
np.savez_compressed(args.outfile,
pdframe=pdFrame,
pdcolumns=pdFrame.columns,
pdindex=pdFrame.index)
def flattenFragments(args):
""" This function aims to identify parts of reads that are repeats of themselves, overlapping
the same regions multiple times.
It takes a pandas dataframe and adds a new column to the end of it.
"""
pdFile = np.load(args.pdframe)
pdFrame = pd.DataFrame(pdFile['pdframe'],columns=pdFile['pdcolumns'],index=pdFile['pdindex'])
mc.findRepeats(pdFrame)
print pdFrame.iloc[:10].T
np.savez_compressed(args.outfile,
pdframe=pdFrame,
pdcolumns=pdFrame.columns,
pdindex=pdFrame.index)
# Huge wall of argparse text starts here
def main():
""" Everything in here is to interpret calls from a command line.
Anything being run will just call a similarly named function
above.
"""
descIniFile = 'File containing experiment specific details'
descFqFile = 'Fastq file containing actual data from sequencing'
parser = argparse.ArgumentParser(
description="MC4C pipeline for processing multi-contact data")
subparsers = parser.add_subparsers()
#
parser_mkprfa = subparsers.add_parser('makeprimerfa',
description='Make a fasta file of primer sequences')
parser_mkprfa.add_argument('inifile',
type=str,
help=descIniFile)
parser_mkprfa.add_argument('outfile',
type=str,
help='Fasta file with primer sequences')
parser_mkprfa.set_defaults(func=makePrimerFasta)
#
parser_clvprm = subparsers.add_parser('cleavereads',
description='Cleave reads by primer sequences')
parser_clvprm.add_argument('inifile',
type=str,
help=descIniFile)
parser_clvprm.add_argument('bamfile',
type=str,
help='Bam file that is created after makeprimerfa results were mapped by bowtie2')
parser_clvprm.add_argument('fastqfile',
type=str,
help=descFqFile)
parser_clvprm.add_argument('outfile',
type=str,
help='Fastq file to dump primer cleaved sequences into')
parser_clvprm.set_defaults(func=cleaveReads)
#
parser_splrest = subparsers.add_parser('splitreads',
description='Split reads by restriction site sequences')
parser_splrest.add_argument('inifile',
type=str,
help=descIniFile)
parser_splrest.add_argument('fastqfile',
type=str,
help=descFqFile)
parser_splrest.add_argument('outfile',
type=str,
help='Fasta file to dump restriction site split sequences into')
parser_splrest.set_defaults(func=splitReads)
#
parser_refrest = subparsers.add_parser('refrestr',
description='Determine restriction site coordinates on reference by their sequences')
parser_refrest.add_argument('inifile',
type=str,
help=descIniFile)
parser_refrest.add_argument('fastafile',
type=str,
help='Fasta file of reference genome containing all chromosomes (eg hg19)')
parser_refrest.add_argument('restfile',
type=str,
help='Numpy compressed file containing restriction site coordinates')
parser_refrest.add_argument('-linelen',
type=int,
default=50,
help='Number of basepairs per line in reference sequence')
parser_refrest.set_defaults(func=findRefRestSites)
#
parser_refregion = subparsers.add_parser('refregion',
description='TODO')
parser_refregion.add_argument('inifile',
type=str,
help=descIniFile)
parser_refregion.add_argument('restfile',
type=str,
help='Numpy compressed file containing restriction site coordinates')
parser_refregion.add_argument('outfile',
type=str,
help='File to dump relevant restriction site locations into')
parser_refregion.set_defaults(func=getRefResPositions)
#
parser_export = subparsers.add_parser('export',
description='Combine and export results for interactive plotting')
parser_export.add_argument('inifile',
type=str,
help=descIniFile)
parser_export.add_argument('bamfile',
type=str,
help='Bam file after mapping previously split fastq data to reference genome (eg hg19) using BWA')
parser_export.add_argument('restfile',
type=str,
help='Numpy compressed file containing restriction site coordinates')
parser_export.add_argument('plotfile',
type=str,
help='Numpy compressed file containing restriction site coordinates')
parser_export.set_defaults(func=exportToPlot)
#
parser_export = subparsers.add_parser('markdup',
description='Add duplicate info for exported results')
parser_export.add_argument('inifile',
type=str,
help=descIniFile)
parser_export.add_argument('pdframe',
type=str,
help='The main file previously exported')
parser_export.add_argument('extra',
type=str,
help='The _extra file created during previous export')
parser_export.add_argument('outfile',
type=str,
help='New export file')
parser_export.set_defaults(func=markDuplicates)
#
parser_flatten = subparsers.add_parser('flatten',
description='Mark within-circle-repeated-fragments in exported results')
parser_flatten.add_argument('pdframe',
type=str,
help='The main file previously exported')
parser_flatten.add_argument('outfile',
type=str,
help='New export file')
parser_flatten.set_defaults(func=flattenFragments)
args = parser.parse_args(sys.argv[1:])
log.printArgs(args)
args.func(args)
if __name__ == '__main__':
main()