Seqanswers Leaderboard Ad

Collapse

Announcement

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

  • Add 'missing' lines of data by using python code

    So I am a beginner when it comes to programming and python and such. But I think I have a very simple question.

    I have large tab-delimited files that for example contain lines like this:

    10000 7
    20000 1
    30000 2
    60000 3

    What I want to have, is a file that also contains the 'missing' lines, such as this:

    10000 7
    20000 1
    30000 2
    40000 0
    50000 0
    60000 3

    The files are rather large as I am working with whole genome sequence data. The first column is basically a position in the genome and the second column is the number of SNPs I find within that 10kb window. However, I don't think this information is even relevant, I just want to write a simple python code that will add these lines to the file by using if else statements.

    So if the position does not match the position of the previous line + 10000, the 'missing line' is written, otherwise the normal occurring line is written.

    I just foresee one problem in this, namely when several lines in a row are missing (as in my example). Does anyone have a smart solution for this simple problem?

    Many thanks!

  • #2
    An easy solution would be to loop over the file and have a variable 'previous':

    !Untested sample code generated by tired coffee deprived me:

    Code:
    previous = 0
    for line in file:
        now = line.split('\t')[0]
        if  now != previous + 10000:
            for n in range(previous + 10000, now, step=10000):
                print(n + "\t0")
        print(line)
        previous = now

    Comment


    • #3
      I will try this soon, definitely!. It always looks so simple in the end but writing it yourself is still a struggle when you've only just started figuring out coding. Thank you so much I might come back to it!

      Comment


      • #4
        If I do this though I get an error that the range function does not take keywords as arguments. Not sure how to solve this yet

        Comment


        • #5
          I won't write out the code since I have to go to a meeting, but you could also take advantage of the power of Pandas data frame objects. If you are new to Python, learn Pandas as soon as possible.

          But you could create a data frame of one column that contain the values:
          10000
          20000
          30000
          ...
          max_value

          Then create a data frame object of your actual values. Then you simply do a "join" on the two tables and it will fill in the missing values by virtue of joining the 2 tables.

          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...
            Today, 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, 06-03-2024, 06:55 AM
          0 responses
          13 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 05-30-2024, 03:16 PM
          0 responses
          27 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 05-29-2024, 01:32 PM
          0 responses
          29 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 05-24-2024, 07:15 AM
          0 responses
          216 views
          0 likes
          Last Post seqadmin  
          Working...
          X