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
              Strategies for Sequencing Challenging Samples
              by seqadmin


              Despite advancements in sequencing platforms and related sample preparation technologies, certain sample types continue to present significant challenges that can compromise sequencing results. Pedro Echave, Senior Manager of the Global Business Segment at Revvity, explained that the success of a sequencing experiment ultimately depends on the amount and integrity of the nucleic acid template (RNA or DNA) obtained from a sample. “The better the quality of the nucleic acid isolated...
              03-22-2024, 06:39 AM
            • seqadmin
              Techniques and Challenges in Conservation Genomics
              by seqadmin



              The field of conservation genomics centers on applying genomics technologies in support of conservation efforts and the preservation of biodiversity. This article features interviews with two researchers who showcase their innovative work and highlight the current state and future of conservation genomics.

              Avian Conservation
              Matthew DeSaix, a recent doctoral graduate from Kristen Ruegg’s lab at The University of Colorado, shared that most of his research...
              03-08-2024, 10:41 AM

            ad_right_rmr

            Collapse

            News

            Collapse

            Topics Statistics Last Post
            Started by seqadmin, Yesterday, 06:37 PM
            0 responses
            10 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, Yesterday, 06:07 PM
            0 responses
            9 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 03-22-2024, 10:03 AM
            0 responses
            51 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 03-21-2024, 07:32 AM
            0 responses
            67 views
            0 likes
            Last Post seqadmin  
            Working...
            X