I am currently developing an application which leverages genomic count data. This data is loaded into a pandas dataframe. An example of this data would look like this:
| chrom | chromStart | chromEnd | height |
|---|---|---|---|
| chr3 | 93504854 | 93504940 | 0 |
| chr3 | 93504940 | 93504977 | 1 |
| chr3 | 93504977 | 93505016 | 2 |
| chr3 | 93505016 | 93505018 | 1 |
| chr3 | 93505018 | 93505053 | 2 |
| chr3 | 93505053 | 93505093 | 1 |
| chr3 | 93505093 | 93505151 | 0 |
| chr3 | 93505151 | 93505227 | 1 |
| chr3 | 93505227 | 93505252 | 0 |
| chr3 | 93505252 | 93505328 | 1 |
I currently have an implementation of the title which accounts for quite a bit of my time taken. Here is the implementation:
def checkInBounds(row, chrom, chromStart, chromEnd):
try:
if not chrom == row['chrom']:
return False
except KeyError:
print('row count', row.count)
print("CheckInBoundsKeyError\nRow\n", row, '\n chrom', chrom, 'start', chromStart, 'end', chromEnd)
return False
if chromStart <= row['chromStart'] <= chromEnd:
return True
elif chromStart <= row['chromEnd'] <= chromEnd:
return True
else:
return (row['chromStart'] < chromEnd) and (row['chromEnd'] > chromEnd)
This is ran using an apply statement which looks like this:
df.apply(checkInBounds, axis=1, args=(chrom, start, end))
Is there a faster way to accomplish this same task?