<commit_before>#!/usr/bin/env python
"""Module for the select taxa step."""

from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from divergence import parse_options, create_archive_of_files
import logging as log
import os
import sys
import tempfile

__author__ = "<NAME>"
__contact__ = "<EMAIL>"
__copyright__ = "Copyright 2011, Netherlands Bioinformatics Centre"
__license__ = "MIT"


def format_fasta_genome_headers(label, nucl_fasta_file):
    """Format an individual nucleotide fasta file containing coding regions so the headers match expected patterns."""
    label = label.replace(' ', '_')

    #Determine output file name
    filename = os.path.split(nucl_fasta_file)[1]
    formatted_fasta_file = tempfile.mkstemp(suffix='.ffn', prefix='{0}.{1}.formatted_'.format(label, filename))[1]
    with open(formatted_fasta_file, mode='w') as write_handle:
        for index, nucl_seqrecord in enumerate(SeqIO.parse(nucl_fasta_file, 'fasta'), 1):
            #Try to retain user specified protein identifiers, hoping they stuck to this guideline:
            #http://www.ncbi.nlm.nih.gov/books/NBK7183/?rendertype=table&id=ch_demo.T5
            #But do throw in a little counter to make sure the generated IDs are actually unique
            protein_id = '{0:06}_{1}'.format(index, nucl_seqrecord.id.replace('|', '_'))

            #OrthoMCL has the nasty habit of truncating label|protein_id to 60 characters, which leads to mismatches
            #when we try to map OrthoMCL groups.txt IDs back to the original DNA sequences. Therefore pretruncate here:
            if len(label) + 1 + len(protein_id) > 60:
                protein_id = protein_id[:60 - len(label) - 1]

            #Alternatively create a new unique protein id using the enumerated number, first twelve bases and length
            #protein_id = 'protein_{0}:{1}...({2})'.format(index, str(nucl_sequence)[:12], len(nucl_sequence))

            #Remove gap codons from input nucleotide fasta file in complete codons only
            nucl_sequence_str = str(nucl_seqrecord.seq).replace('---', '').upper()
            nucl_sequence = Seq(nucl_sequence_str)

            #Write out fasta. Header format as requested: >project_id|genbank_ac|protein_id|cog|source
            header = '{0}|{1}|{2}|{3}|{4}'.format(label, filename, protein_id, None, 'upload')

            #Create protein sequence record and write it to file
            formatted_seqrecord = SeqRecord(nucl_sequence, id=header, description='')
            SeqIO.write(formatted_seqrecord, write_handle, 'fasta')
    return formatted_fasta_file

<commit_msg>Replace static string 'upload' in converted user files with the user specified ID line contents<commit_after>#!/usr/bin/env python
"""Module for the select taxa step."""

from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from divergence import parse_options, create_archive_of_files
import logging as log
import os
import sys
import tempfile

__author__ = "<NAME>"
__contact__ = "<EMAIL>"
__copyright__ = "Copyright 2011, Netherlands Bioinformatics Centre"
__license__ = "MIT"


def format_fasta_genome_headers(label, nucl_fasta_file):
    """Format an individual nucleotide fasta file containing coding regions so the headers match expected patterns."""
    label = label.replace(' ', '_')

    #Determine output file name
    filename = os.path.split(nucl_fasta_file)[1]
    formatted_fasta_file = tempfile.mkstemp(suffix='.ffn', prefix='{0}.{1}.formatted_'.format(label, filename))[1]
    with open(formatted_fasta_file, mode='w') as write_handle:
        for index, nucl_seqrecord in enumerate(SeqIO.parse(nucl_fasta_file, 'fasta'), 1):
            #Try to retain user specified protein identifiers, hoping they stuck to this guideline:
            #http://www.ncbi.nlm.nih.gov/books/NBK7183/?rendertype=table&id=ch_demo.T5
            #But do throw in a little counter to make sure the generated IDs are actually unique
            user_record_id_line = nucl_seqrecord.id.replace('|', '_').split(' ', 1)
            original_id = user_record_id_line[0]
            original_extra = user_record_id_line[-1]
            protein_id = '{0:06}_{1}'.format(index, original_id)

            #OrthoMCL has the nasty habit of truncating label|protein_id to 60 characters, which leads to mismatches
            #when we try to map OrthoMCL groups.txt IDs back to the original DNA sequences. Therefore pretruncate here:
            if len(label) + 1 + len(protein_id) > 60:
                protein_id = protein_id[:60 - len(label) - 1]

            #Alternatively create a new unique protein id using the enumerated number, first twelve bases and length
            #protein_id = 'protein_{0}:{1}...({2})'.format(index, str(nucl_sequence)[:12], len(nucl_sequence))

            #Remove gap codons from input nucleotide fasta file in complete codons only
            nucl_sequence_str = str(nucl_seqrecord.seq).replace('---', '').upper()
            nucl_sequence = Seq(nucl_sequence_str)

            #Write out fasta. Header format as requested: >project_id|genbank_ac|protein_id|cog|source
            header = '{0}|{1}|{2}|{3}|{4}'.format(label, filename, protein_id, None, original_extra)

            #Create protein sequence record and write it to file
            formatted_seqrecord = SeqRecord(nucl_sequence, id=header, description='')
            SeqIO.write(formatted_seqrecord, write_handle, 'fasta')
    return formatted_fasta_file


{'fraction_non_alphanumeric': 0.10192937750273025, 'fraction_numerical': 0.01365125591554423, 'mean_word_length': 3.5868113522537564, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3553810', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1579, 'n_words': 486}