Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bigWig_visu
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LBMC
ReGArDS
Projects_Analyzes
bigWig_visu
Commits
1144c187
Commit
1144c187
authored
4 years ago
by
nfontrod
Browse files
Options
Downloads
Patches
Plain Diff
src/bed_handler/bed_resize.py: allow to resize the bed file
parent
a5e8bfbb
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/bed_handler/bed_resize.py
+102
-0
102 additions, 0 deletions
src/bed_handler/bed_resize.py
with
102 additions
and
0 deletions
src/bed_handler/bed_resize.py
0 → 100644
+
102
−
0
View file @
1144c187
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
Description: The goal of this script is to resize a bed
"""
import
pandas
as
pd
import
lazyparser
as
lp
from
doctest
import
testmod
from
.config
import
OutputBed
def
resize_row_from_start
(
rowo
:
pd
.
Series
,
size
:
int
,
resize_from
:
str
)
->
pd
.
Series
:
"""
Resize the bed feature from start inside a bed row.
:param rowo: A bed row
:param size: The maximum size the feature must have
:param resize_from: The coordinate for which we want to resize (default
\
"
start
"
)
:return: The row resized
>>>
crow
=
pd
.
Series
({
"
#ref
"
:
10
,
"
start
"
:
10
,
"
end
"
:
20
,
"
id
"
:
1
,
...
"
score
"
:
1
,
"
strand
"
:
"
+
"
})
>>>
resize_row_from_start
(
crow
,
5
,
"
start
"
).
to_dict
()
{
'
#ref
'
:
10
,
'
start
'
:
10
,
'
end
'
:
15
,
'
id
'
:
1
,
'
score
'
:
1
,
'
strand
'
:
'
+
'
}
>>>
resize_row_from_start
(
crow
,
5
,
"
end
"
).
to_dict
()
{
'
#ref
'
:
10
,
'
start
'
:
15
,
'
end
'
:
20
,
'
id
'
:
1
,
'
score
'
:
1
,
'
strand
'
:
'
+
'
}
>>>
resize_row_from_start
(
crow
,
20
,
"
start
"
).
to_dict
()
{
'
#ref
'
:
10
,
'
start
'
:
10
,
'
end
'
:
20
,
'
id
'
:
1
,
'
score
'
:
1
,
'
strand
'
:
'
+
'
}
>>>
crow
=
pd
.
Series
({
"
#ref
"
:
10
,
"
start
"
:
50
,
"
end
"
:
60
,
"
id
"
:
1
,
...
"
score
"
:
1
,
"
strand
"
:
"
-
"
})
>>>
resize_row_from_start
(
crow
,
5
,
"
start
"
).
to_dict
()
{
'
#ref
'
:
10
,
'
start
'
:
55
,
'
end
'
:
60
,
'
id
'
:
1
,
'
score
'
:
1
,
'
strand
'
:
'
-
'
}
>>>
resize_row_from_start
(
crow
,
5
,
"
end
"
).
to_dict
()
{
'
#ref
'
:
10
,
'
start
'
:
50
,
'
end
'
:
55
,
'
id
'
:
1
,
'
score
'
:
1
,
'
strand
'
:
'
-
'
}
"""
row
=
rowo
.
copy
()
row_strand
=
row
[
"
strand
"
]
d
=
{
"
+
"
:
"
-
"
,
"
-
"
:
"
+
"
}
row_strand
=
d
[
row_strand
]
if
resize_from
==
"
end
"
else
row_strand
if
row
[
"
end
"
]
-
row
[
"
start
"
]
<=
size
:
return
row
if
row_strand
==
"
+
"
:
row
[
'
end
'
]
=
row
[
'
start
'
]
+
size
else
:
row
[
'
start
'
]
=
row
[
'
end
'
]
-
size
return
row
def
update_bed
(
df_bed
:
pd
.
DataFrame
,
size
:
int
,
resize_from
:
str
)
->
pd
.
DataFrame
:
"""
Resize each feature in a bed dataframe.
:param df_bed: The dataframe corresponding to a bed to resize
:param size: The maximum size the feature must have
:param resize_from: The coordinate for which we want to resize (default
\
"
start
"
)
:return: The dataframe resized
>>>
cdf
=
pd
.
DataFrame
({
"
#ref
"
:
[
1
,
1
],
"
start
"
:
[
10
,
50
],
...
"
end
"
:
[
20
,
60
],
"
id
"
:
[
1
,
2
],
"
strand
"
:
[
"
+
"
,
"
-
"
]})
>>>
update_bed
(
cdf
,
5
,
"
start
"
)
#ref start end id strand
0
1
10
15
1
+
1
1
55
60
2
-
>>>
update_bed
(
cdf
,
5
,
"
end
"
)
#ref start end id strand
0
1
15
20
1
+
1
1
50
55
2
-
"""
list_s
=
[
resize_row_from_start
(
df_bed
.
iloc
[
i
,
:],
size
,
resize_from
)
for
i
in
range
(
df_bed
.
shape
[
0
])
]
return
pd
.
DataFrame
(
list_s
)
@lp.parse
(
bed
=
"
file
"
,
size
=
"
size > 0
"
,
resize_from
=
[
"
start
"
,
"
end
"
])
def
bed_resizer
(
bed
:
str
,
size
:
int
,
outfile
:
str
,
resize_from
:
str
=
"
start
"
)
->
None
:
"""
Resize bed features inside a bed file from their start or stop
\
position (according to their strand).
:param bed: A bed file with the features to resize
:param size: The maximum size the feature must have
:param outfile: The output bed name
:param resize_from: The coordinate for which we want to resize (default
\
"
start
"
)
"""
df
=
pd
.
read_csv
(
bed
,
sep
=
"
\t
"
)
ndf
=
update_bed
(
df
,
size
,
resize_from
)
ndf
.
to_csv
(
OutputBed
.
output
/
outfile
,
sep
=
"
\t
"
,
index
=
False
)
if
__name__
==
"
__main__
"
:
bed_resizer
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment