Created
March 19, 2019 15:05
-
-
Save Tillsa/c64fd490cacfbaea0a76c135ba23553b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# old code" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with open(\"new_fileB.txt\") as new_headers_fh:\n", | |
" new_headers = new_headers_fh.readlines()\n", | |
" \n", | |
"with open(\"file1.fasta\") as genome_fasta:\n", | |
" gf_lines = genome_fasta.readlines()\n", | |
" \n", | |
"counter = 0\n", | |
"new_lines_for_new_fasta = []\n", | |
" \n", | |
"for gf_line in gf_lines:\n", | |
" if gf_line.startswith(\">\"):\n", | |
" old_header = gf_line\n", | |
" new_header_line = gf_line.replace(old_header, new_headers[counter])\n", | |
" counter += 1\n", | |
" new_lines_for_new_fasta.append(new_header_line)\n", | |
" else:\n", | |
" new_lines_for_new_fasta.append(gf_line)\n", | |
" \n", | |
"with open(\"fasta_replaced_headers.fa\", 'a') as replaced_headers_fh:\n", | |
" for new_line in new_lines_for_new_fasta:\n", | |
" replaced_headers_fh.write(new_line)\n", | |
" \n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# improved code" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with open(\"new_fileB.txt\") as new_headers_fh:\n", | |
" new_headers = new_headers_fh.readlines()\n", | |
" \n", | |
"with open(\"file1.fasta\") as genome_fasta:\n", | |
" gf_lines = genome_fasta.readlines()\n", | |
" \n", | |
"# remove the counter\n", | |
"new_lines_for_new_fasta = []\n", | |
" \n", | |
"for gf_line in gf_lines:\n", | |
" if gf_line.startswith(\">\"):\n", | |
" new_lines_for_new_fasta.append(new_headers[0])\n", | |
" new_headers.remove(new_headers[0])\n", | |
" else:\n", | |
" new_lines_for_new_fasta.append(gf_line)\n", | |
" \n", | |
"with open(\"fasta_replaced_headers.fa\", 'wt') as replaced_headers_fh: # changed a to wt\n", | |
" for new_line in new_lines_for_new_fasta:\n", | |
" replaced_headers_fh.write(new_line)\n", | |
" \n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# enumerate" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment