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
              Best Practices for Single-Cell Sequencing Analysis
              by seqadmin



              While isolating and preparing single cells for sequencing was historically the bottleneck, recent technological advancements have shifted the challenge to data analysis. This highlights the rapidly evolving nature of single-cell sequencing. The inherent complexity of single-cell analysis has intensified with the surge in data volume and the incorporation of diverse and more complex datasets. This article explores the challenges in analysis, examines common pitfalls, offers...
              06-06-2024, 07:15 AM
            • seqadmin
              Latest Developments in Precision Medicine
              by seqadmin



              Technological advances have led to drastic improvements in the field of precision medicine, enabling more personalized approaches to treatment. This article explores four leading groups that are overcoming many of the challenges of genomic profiling and precision medicine through their innovative platforms and technologies.

              Somatic Genomics
              “We have such a tremendous amount of genetic diversity that exists within each of us, and not just between us as individuals,”...
              05-24-2024, 01:16 PM

            ad_right_rmr

            Collapse

            News

            Collapse

            Topics Statistics Last Post
            Started by seqadmin, Yesterday, 06:58 AM
            0 responses
            13 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 06-06-2024, 08:18 AM
            0 responses
            20 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 06-06-2024, 08:04 AM
            0 responses
            18 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 06-03-2024, 06:55 AM
            0 responses
            13 views
            0 likes
            Last Post seqadmin  
            Working...
            X