Seqanswers Leaderboard Ad

Collapse

Announcement

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

  • Re: New to programming need help for inserting text

    Hi all

    I am new to programming. I have two very big files

    File 1: (>80,000 lines)

    CCDS635.1_0 123 G S 255 33 1.00 63 60 G 255 C
    CCDS635.1_0 175 C S 255 51 0.94 63 62 C 243 G
    CCDS635.1_8 259 G R 90 254 1.00 63 62 G 255 A
    CCDS635.1_14 328 A T 39 4 0.00 63 36 N 158 N
    CCDS635.1_16 80 G K 139 22 1.00 63 62 G 255 T

    File 2:

    CCDS635.1_0 1 67162857
    CCDS635.1_1 1 67143396
    CCDS635.1_2 1 67131424
    CCDS635.1_3 1 67129349
    CCDS635.1_4 1 67112976

    I want to insert all three columns of File 2 to File one at the
    respective positions according to column 1 of File 1.

    e.g.

    File 3 should be

    CCDS635.1_0 1 67162857 123 G S 255 33 1.00 63 60 G 255 C
    CCDS635.1_0 1 67162857 175 C S 255 51 0.94 63 62 C 243 G

    Can anyone help me with this?

    Manav

  • #2
    join 2.txt 1.txt > 3.txt

    on mac/linux

    Comment


    • #3
      Thanks for replying.
      I tried to join but it gives error:

      join: file 1 is not in sorted order
      join: file 2 is not in sorted order

      There is another problem also..These both files have different number of lines.
      I don't want exactly merging of file, I want to create new file where
      the similar identifier get replaced by three columns of 2nd file. If
      similar identifier occurs in 4 rows of 1st file then the next 2
      columns of the same identifier in file 2 get inserted before 2nd
      column of file one for all 4 rows. It can be like find and replace
      where identifier "CCDS635.1_0" will be replaced by "CCDS635.1_0
      1 67162857" in all rows. I can do it with "sed" command of unix
      but then "sed" will do one identifier at one time and I have >1000.
      And everytime sed will create new file. So I was looking for solution
      where "identifier" replaced by new identifier with 3 columns.

      Manav

      Comment


      • #4
        Thanks...It worked I given the option join --nocheck-order

        Manav

        Comment


        • #5
          Maybe somthing like this would work:

          Code:
          #!/bin/env python
             
          def merge_files(fname1, fname2, out_fname):
            
               #read file 2 into memory
               file2 = {}
               for line in open(fname2):
                   line = line.strip()
                   if not line:
                       continue
                   line_name = line.split()[0]
                   file2[line_name] = line
           
               #now create the output
               output_fhand = open(out_fname, 'w')
               for line in open(fname1):
                   line = line.strip()
                   line_name, line = line.split(' ', 1)
                   line_name = file2[line_name]
                   output_fhand.write(' '.join((line_name, line, '\n')))
             
             if __name__ == '__main__':
                 merge_files('file1.txt', 'file2.txt', 'merged.txt')

          Comment


          • #6
            Thanks for the help.

            awk 'BEGIN{FS=OFS="\t"}FNR==NR{a[$1]=$2}FNR!=NR{print $1,a[$1],a[$2],$2,$3,$4,$5,$6,$7,$8,$9}' exonlegth.txt coor.txt > coor1.txt

            worked for me and I was able to join the two files without sorting.

            Thanks all for help

            Manav

            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 on Modified Bases...
              Yesterday, 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-11-2024, 12:08 PM
            0 responses
            39 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 04-10-2024, 10:19 PM
            0 responses
            41 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 04-10-2024, 09:21 AM
            0 responses
            35 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 04-04-2024, 09:00 AM
            0 responses
            55 views
            0 likes
            Last Post seqadmin  
            Working...
            X