TransWikia.com

How can I add for several bed files the header : track type=narrowPeak name=“narrowPeak” preferably in python ,can handle with R

Bioinformatics Asked by Libby Kosolapov on January 25, 2021

I want to create custom tracks from theseenter image description here files

I can add the line : track type=narrowPeak name=“narrowPeak” manually by opening it with text editor:
track type=narrowPeak name=“narrowPeak”
enter image description here
but I wonder if there is no simple way to do it with code- to open all files that end with bed and add them this line.
Didnt find this option in bed tools, If it is possible to add the name extracting it from the file name it would be amazing.

Many thanks

One Answer

I don't think you need to go to R or Python to do that. In bash

set -euxo pipefail
for f in *.bed
do
  bn="$(basename ${f})"
  on="${bn%%.bed}_with_header.bed"
  echo 'track type=narrowPeak name="narrowPeak"' > "${on}"
  cat "${f}" >> "${on}"
done

EDIT: I just read you want to set the name based on the filename.

EDIT2: And also to add an arbitrary directory

set -euxo pipefail

INDIR="/path/to/bedfiles"

for f in $(find "${INDIR}" -name "*.bed")
do
  bn=$(basename "${f}")
  on="${bn%%.bed}_with_header.bed"
  dn=$(dirname "${f}")
  echo "track type=narrowPeak name="${bn}"" > "${dn}"/"${on}"
  cat "${f}" >> "${dn}"/"${on}"
done

Answered by Bastian Schiffthaler on January 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP