-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_mpra_sequences.py
213 lines (191 loc) · 10.6 KB
/
get_mpra_sequences.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
import pandas as pd
import argparse
import py2bit
def getUpdatedPaddings(allele,left,right):
alleleLength = len(allele)
deductable = "right"
for i in range(alleleLength-1):
if deductable=="right":
right-=1
deductable="left"
elif deductable=="left":
left-=1
deductable="right"
return left,right
def getSequencesAndPrint(dataFile, genome_object, mpraSeqLength, exptName, summitColumnPresent):
if summitColumnNotPresent:
data = pd.read_csv(dataFile,
sep='\t',
header=None,
names=["CHROM",
"SNPSTART",
"SNPEND",
"RSID",
"REF",
"ALT",
"PEAKCHROM",
"PEAKSTART",
"PEAKEND",
"NAME",
"SCORE",
"_",
]
)
data["SUMMIT"] = data["PEAKEND"]-data["PEAKSTART"]
data["SUMMIT"] = data["SUMMIT"]//2
else:
data = pd.read_csv(dataFile,
sep='\t',
header=None,
names=["CHROM",
"SNPSTART",
"SNPEND",
"RSID",
"REF",
"ALT",
"PEAKCHROM",
"PEAKSTART",
"PEAKEND",
"NAME",
"SCORE",
"STRAND",
"SIGNAL",
"P",
"Q",
"SUMMIT"
]
)
print("CHROM",
"RSID",
"SNPSTART",
"SNPEND",
"REF",
"ALT",
"EXPERIMENT",
"PEAKSTART",
"PEAKEND",
"SUMMIT",
"SUMMITZEROBASED",
"SUMMITREFSEQ",
"SUMMITALTSEQ",
"SNPREFSEQ",
"SNPALTSEQ",
sep='\t'
)
for index,row in data.iterrows():
chrom = row["CHROM"]
ref = row["REF"]
alt = row["ALT"]
summitLocation = int(row["PEAKSTART"]+ row["SUMMIT"])
mpraSeqStart = summitLocation - mpraSeqLength//2
mpraSeqEnd = mpraSeqStart + mpraSeqLength
snpStart = row["SNPSTART"]
snpEnd = row["SNPEND"]
if mpraSeqStart <= snpStart and snpEnd <= mpraSeqEnd and (snpStart+len(alt)) <= mpraSeqEnd:
#making sure both the ref and alt alleles are within 227bp of summit
(snpCenteredRefSequenceLeftWindow,
snpCenteredRefSequenceRightWindow) = getUpdatedPaddings(ref,
mpraSeqLength//2,
mpraSeqLength//2
)
(snpCenteredAltSequenceLeftWindow,
snpCenteredAltSequenceRightWindow) = getUpdatedPaddings(alt,
mpraSeqLength//2,
mpraSeqLength//2
)
snpCenteredRefSequenceLeft = genome_object.sequence(chrom,
snpStart-snpCenteredRefSequenceLeftWindow,
snpStart)
snpCenteredRefSequenceRight = genome_object.sequence(chrom,
snpStart+len(ref),
snpStart+len(ref)+snpCenteredRefSequenceRightWindow
)
snpCenteredRefSequence = snpCenteredRefSequenceLeft.lower()+ref.lower()+snpCenteredRefSequenceRight.lower()
snpCenteredAltSequenceLeft = genome_object.sequence(chrom,
snpStart-snpCenteredAltSequenceLeftWindow,
snpStart
)
snpCenteredAltSequenceRight = genome_object.sequence(chrom,
snpStart+len(ref),
snpStart+len(ref)+snpCenteredAltSequenceRightWindow
)
snpCenteredAltSequence = snpCenteredAltSequenceLeft.lower()+alt.lower()+snpCenteredAltSequenceRight.lower()
if snpStart < summitLocation:
# case when snp is to the left of summit
summitCenteredRefSequence = genome_object.sequence(chrom, snpEnd, mpraSeqEnd).lower()
summitCenteredRefSequence = ref.lower() + summitCenteredRefSequence
curRefSeqLen = len(summitCenteredRefSequence)
if not curRefSeqLen == mpraSeqLength:
#edge case when the SNP is at the edge of the sequence
summitCenteredRefSequence = genome_object.sequence(chrom,
snpStart+curRefSeqLen-mpraSeqLength,
snpStart).lower() + summitCenteredRefSequence
summitCenteredAltSequence = genome_object.sequence(chrom, snpEnd, mpraSeqEnd).lower()
summitCenteredAltSequence = alt.lower()+summitCenteredAltSequence
curAltSeqLen = len(summitCenteredAltSequence)
if not curAltSeqLen == mpraSeqLength:
#edge case when the SNP is at the edge of the sequence
summitCenteredAltSequence = genome_object.sequence(chrom,
snpStart+curAltSeqLen-mpraSeqLength,
snpStart).lower() + summitCenteredAltSequence
elif snpStart >= summitLocation:
# case when snp is to the right of summit
summitCenteredRefSequence = genome_object.sequence(chrom,mpraSeqStart,snpStart).lower()
summitCenteredRefSequence = summitCenteredRefSequence + ref.lower()
curRefSeqLen = len(summitCenteredRefSequence)
if not curRefSeqLen == mpraSeqLength:
#edge case when the SNP is at the edge of the sequence
summitCenteredRefSequence = (summitCenteredRefSequence
+genome_object.sequence(chrom,
snpEnd,
snpEnd-curRefSeqLen+mpraSeqLength)).lower()
summitCenteredAltSequence = genome_object.sequence(chrom,mpraSeqStart,snpStart).lower()
summitCenteredAltSequence = summitCenteredAltSequence + alt.lower()
curAltSeqLen = len(summitCenteredAltSequence)
if not curAltSeqLen >= mpraSeqLength:
#edge case when the SNP is at the edge of the sequence
summitCenteredAltSequence = (summitCenteredAltSequence
+genome_object.sequence(chrom,
snpEnd,
snpEnd-curAltSeqLen+mpraSeqLength)).lower()
assert(len(summitCenteredRefSequence)==mpraSeqLength)
assert(len(summitCenteredAltSequence)==mpraSeqLength)
assert(summitCenteredRefSequence.lower()==genome_object.sequence(chrom,
mpraSeqStart,
mpraSeqEnd).lower())
print(row["CHROM"],
row["RSID"],
row["SNPSTART"],
row["SNPEND"],
row["REF"],
row["ALT"],
exptName,
row["PEAKSTART"],
row["PEAKEND"],
row["SUMMIT"],
summitLocation,
summitCenteredRefSequence,
summitCenteredAltSequence,
snpCenteredRefSequence,
snpCenteredAltSequence,
sep='\t'
)
if __name__=="__main__":
parser = argparse.ArgumentParser(description='get sequences for MPRA array', fromfile_prefix_chars='@')
parser.add_argument('-n', '--name', help='name of cell type or experiment', required=True)
parser.add_argument('-i', '--input', help='input text file (bedtools intersection of SNP file and peak file with -wa -wb)',
required=True)
parser.add_argument('-s',
'--summit-column-not-present',
action='store_true',
help='include flag if summit column is not present in the intersection file. If true, middle of peak is treated as summit. False by default',
default=False)
parser.add_argument('-l', '--seq-length', help='length of MPRA sequence', type=int, default=227)
parser.add_argument('-g', '--genome', help='path to genome 2bit file', default="/home/eramamur/resources/genomes/hg19/hg19.2bit")
args = parser.parse_args()
exptName = args.name
dataFile = args.input
genome_object = py2bit.open(args.genome)
mpraSeqLength = args.seq_length
summitColumnNotPresent = args.summit_column_not_present
getSequencesAndPrint(dataFile, genome_object, mpraSeqLength, exptName, summitColumnNotPresent)