Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • fasta file manipulation- combining sequences by gene rather than species

    I've got 1000 gene sequences, each in a separate fasta file, for 4 different species. So, each species has its own sequence for 1000 different genes. Each species is it's own directory, and all the sequence files are in the same order for each species, so:

    Directory1(species1): geneA.fa, geneB.fa, geneC.fa, ...
    Directory2(species2): geneA.fa, geneB.fa, geneC.fa, ...
    etc...

    I want to concatenate all the sequences for geneA into a single file to end up for 1000 fasta files with 4 sequences (1 from each species) in every file.

    Is there an easy way to automate this? I could just use cat and go one gene at a time, but I'd like to do it more quickly.

  • #2
    Hi- This python script should do what you need. There is no error checking. *All* the files in each input directory are concatenated, and it is assumed they are all in the same order (as you mention above).

    Assuming your input dirs are species1, species2..., this will dump the concatenated files in directory OUTDIR:

    Code:
    python -c "
    import os
    
    INPUTDIRS= ['./species1', './species2', './species3', './species4']
    OUTDIR= './'
    PREFIX= 'gene.'
    
    geneDict= {}
    for d in INPUTDIRS:
        fileNames= sorted(os.listdir(d))
        geneDict[d]= fileNames
    
    for i in range(0, len(fileNames)):
        with open(os.path.join(OUTDIR, PREFIX + fileNames[i]), 'w') as fout:
            for d in INPUTDIRS:
                with open(os.path.join(d, geneDict[d][i])) as fin:
                    for line in fin:
                        fout.write(line)
    "
    (There must be an easier way of doing it!)

    Dario

    Comment


    • #3
      Originally posted by gevielr View Post
      I've got 1000 gene sequences, each in a separate fasta file, for 4 different species. So, each species has its own sequence for 1000 different genes. Each species is it's own directory, and all the sequence files are in the same order for each species, so:

      Directory1(species1): geneA.fa, geneB.fa, geneC.fa, ...
      Directory2(species2): geneA.fa, geneB.fa, geneC.fa, ...
      etc...

      I want to concatenate all the sequences for geneA into a single file to end up for 1000 fasta files with 4 sequences (1 from each species) in every file.

      Is there an easy way to automate this? I could just use cat and go one gene at a time, but I'd like to do it more quickly.
      File globbing in the shell makes this easy:

      Code:
      mkdir -p combined_files
      for x in $(ls Directory1 | grep '\.fa$'); do echo "Creating combined_files/${x}"
        cat Directory*/${x} > combined_files/${x}
      done
      For a more complicated situation, I might use find and exec.

      Comment

      Latest Articles

      Collapse

      • seqadmin
        Essential Discoveries and Tools in Epitranscriptomics
        by seqadmin




        The field of epigenetics has traditionally concentrated more on DNA and how changes like methylation and phosphorylation of histones impact gene expression and regulation. However, our increased understanding of RNA modifications and their importance in cellular processes has led to a rise in epitranscriptomics research. “Epitranscriptomics brings together the concepts of epigenetics and gene expression,” explained Adrien Leger, PhD, Principal Research Scientist...
        04-22-2024, 07:01 AM
      • seqadmin
        Current Approaches to Protein Sequencing
        by seqadmin


        Proteins are often described as the workhorses of the cell, and identifying their sequences is key to understanding their role in biological processes and disease. Currently, the most common technique used to determine protein sequences is mass spectrometry. While still a valuable tool, mass spectrometry faces several limitations and requires a highly experienced scientist familiar with the equipment to operate it. Additionally, other proteomic methods, like affinity assays, are constrained...
        04-04-2024, 04:25 PM

      ad_right_rmr

      Collapse

      News

      Collapse

      Topics Statistics Last Post
      Started by seqadmin, 04-25-2024, 11:49 AM
      0 responses
      19 views
      0 likes
      Last Post seqadmin  
      Started by seqadmin, 04-24-2024, 08:47 AM
      0 responses
      17 views
      0 likes
      Last Post seqadmin  
      Started by seqadmin, 04-11-2024, 12:08 PM
      0 responses
      62 views
      0 likes
      Last Post seqadmin  
      Started by seqadmin, 04-10-2024, 10:19 PM
      0 responses
      60 views
      0 likes
      Last Post seqadmin  
      Working...
      X