Skip to content

Instantly share code, notes, and snippets.

@eskutkaan
Created November 8, 2023 18:43
Show Gist options
  • Save eskutkaan/04ef1c1e863073c18e006067ba31e4bf to your computer and use it in GitHub Desktop.
Save eskutkaan/04ef1c1e863073c18e006067ba31e4bf to your computer and use it in GitHub Desktop.
# Define a function to extract the value of a specified attribute
def extract_attribute_value(line, attribute):
start = line.find(attribute) + len(attribute) + 2 # +2 to skip the attribute name and the initial space
end = line.find('";', start)
return line[start:end]
# Read the input file
input_filename = "trial.gtf"
output_filename = "output_trial.gtf"
with open(input_filename, "r") as input_file, open(output_filename, "w") as output_file:
for line in input_file:
if "gene_name" in line:
gene_name = extract_attribute_value(line, "gene_name")
gene_id = extract_attribute_value(line, "gene_id")
line = line.replace(f'gene_name "{gene_name}"', f'gene_name "{gene_id} | {gene_name}"')
output_file.write(line)
print(f"Modified content saved to {output_filename}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment