Skip to main content

Return the range of a dataframe not within a range of another dataframe

I have the following main dataframe:

first.seqnames  first.start first.end   first.width first.strand    second.seqnames second.start    second.end  second.width    second.strand
0   chr1    346212  346975  7   *   chr1    10882   10888   7   *
1   chr1    3135476 3136100 2   *   chr1    10890   10891   2   *
2   chr1    11473   11484   12  *   chr1    10893   10904   12  *
3   chr1    5388140 5388730 2   *   chr1    11096   11097   2   *
4   chr1    346213  346984  68  *   chr1    11202   11269   68  *

I want to return the rows of above dataframe that don't exist within the range of the following dataframe:

first.seqnames  first.start first.end   3   4   5
3503    chr1    346213  346984  .   0   .
3504    chr1    3135466 3136202 .   0   .
3505    chr1    3190760 3191377 .   0   .
3506    chr1    3354604 3355258 .   0   .
3507    chr1    5388136 5388749 .   0   .

Here, the first dataframe's 'first.start' and 'first.end' should not exist within the range(346213, 346984),.......

I've tried the following code which creates memory and time complexity. Even the result isn't accurate. Here, some of the df1 ranges exist exactly between the df2 ranges and some overlaps. In case of overlaps, the ranges can be ignored.

def range_subset(range1, range2):
    """Whether range1 is a subset of range2."""
    if not range1:
        return True  # empty range is subset of anything
    if not range2:
        return False  # non-empty range can't be subset of empty range
    if len(range1) > 1 and range1.step % range2.step:
        return False  # must have a single value or integer multiple step
    return range1.start in range2 and range1[-1] in range2

for a,b in zip(df1['first.start'], df1['first.end']):
    for i,j in zip(df2['first.start'], df2['first.end']):
        if(range_subset(range(a, b), range(i, j)) == True):
            print(a,b)

Output:

first.seqnames  first.start first.end   first.width first.strand    second.seqnames second.start    second.end  second.width    second.strand
0   chr1    346212  346975  7   *   chr1    10882   10888   7   *
2   chr1    11473   11484   12  *   chr1    10893   10904   12  *


source https://stackoverflow.com/questions/71227508/return-the-range-of-a-dataframe-not-within-a-range-of-another-dataframe

Comments

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data