This is a test version of Biostars. For the public version, visit https://www.biostars.org.
how to modify one column in text file in python - solved

I have a text file in which there is column containing IDs. here is a small example of this column:

ENSG00000072803.13 
ENSG00000163002.8 
ENSG00000102221.9 
ENSG00000072121.11
ENSG00000149532.11
ENSG00000134419.11

I want to get rid of point and any number after that. so, for this example I need something like this:

ENSG00000072803
ENSG00000163002
ENSG00000102221
ENSG00000072121
ENSG00000149532
ENSG00000134419

do you guys know how to do that in python?

rna-seq

Hi Ashkan,

and a simple command line : cut -f1 -d '.' yourfile > yourfile.pointless

~ Best

As usual people don't ask clear questions so if there is only one column then this solution may be fine.

It is not clear from the original question if there is only one column in the file or other things as well.

Hi genomax2, yes you are right (as always).

By the way, does this one vs. several column situation has any impact on @Wouter python script or not?

and I guess the first thing that most of the people try to solve is the "example" have been offered in the question.

My piece of code should work fine regardless of one or multiple column files, but if there is only one column a tab will be appended to each line. Essentially my script will modify the first column of the file and leave the rest of the file as it, regardless of there is a rest.

Dear Wouter, I have used your code in a two column tab separated file (two columns are separate from each other with tab), but it seems that it has just remove the first column point and show the second column original lines!

Am I having any error in running your code?

ENSG00000072803 ENSG00000072803.18

ENSG00000163002 ENSG00000072803.13

ENSG00000163088 ENSG00000072803.33

ENSG00000163054

He mentioned that his script will modify only the first column. If you want to do it for all columns:

with open("inputfile.txt") as input:
        for line in input:
                linelist = [ l.split(".")[0] for l in line.strip().split("\t")]
                print "\t".join(linelist)

Thank you (and WouterDeCoster), that works!

Yes exactly, I assumed his IDs would be in the first column/field (but that can be adapted.) and that removing '.' in the rest of the file wasn't desirable. So the removing only from the first column is a feature, not a bug ;) But as Goutham Atla demonstrates it can be done easily.

It may.

That is why the mods are fighting this battle of making sure people realize that they need to ask clear questions (and/or do a minimal search for a solution before posting a new question).

1 answer

Ehm something trivial like the following, but this is actually not a real bioinformatics question... If I understood correctly there also other columns in the file which you want to keep.

with open("inputfile.txt") as input:
    for line in input:
        linelist = line.strip().split('\t') #Assuming tab separated file
        print("{}\t{}".format(linelist[0].split('.')[0], '\t'.join(linelist[1:])))

Log in to answer this question.