954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to join 2 lines to make a big line ??

Hello everyone,

I am writting a code where result is not coming correct because I am not able to join 2 lines to make a single line.

Please help me

e.g my input file is:

AATTCCGGTTT
CCTTAACCCCC

I want my code to first join them together as
AATTCCGGTTTCCTTAACCCCC

and then do what my rest code is doing

so please tell me what python code can do this...!!

It will be a great help please suggest..

Thanks in advance

joe82
Light Poster
27 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Hi see if this help you.

>>> str_input = '''AATTCCGGTTT
CCTTAACCCCC'''
>>> str_input
'AATTCCGGTTT\nCCTTAACCCCC'
>>> x = str_input.replace ('\n', '')
>>> x
'AATTCCGGTTTCCTTAACCCCC'
>>>
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

Thanks for the reply but
I am reading lines in a file so i guess i ned to
do something while reading lines..
eg:

with open('filename') as fil:
	f = fil.readlines()
	for line in f:

I do not know what to do from here...

Please suggest

Many Thanks

joe82
Light Poster
27 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Somthing like this.
readlines() return a list,so we just use join().
Something with a for loop should also be possibly.
See if this solve it.

>>> my_file = open('test.txt', 'r')
>>> x = my_file.readlines()
>>> x
['AATTCCGGTTT\n', 'CCTTAACCCCC']
>>> y = ''.join(x)
>>> y
'AATTCCGGTTT\nCCTTAACCCCC'
>>> finish = y.replace('\n', '')
>>> finish
'AATTCCGGTTTCCTTAACCCCC'
>>>
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

If you want to concatenate (join) every two lines in your file, then you can use something simple like this ...

names = """\
Valerie
Vanessa
Velvet
Venice
Venus
Verena
"""

# create a test file
filename = "mynames.txt"
fout = open(filename, "w")
fout.write(names)
fout.close()

mod_list = []
count = 1
for line in file(filename):
    # strip ending whitespaces including newline char
    line = line.rstrip()
    # concatenate every two lines
    if count % 2 == 0:
        mod_list.append(old_line+line)
    else:
        old_line = line
    count += 1

print(mod_list)

"""
my result -->
['ValerieVanessa', 'VelvetVenice', 'VenusVerena']
"""
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Hello,

Thanks for replying to the post but this is getting very complicated for me to use this in my code:

here is what I am trying to do:

My input file is:

>test100-1
TTGACGAGCAATGACAC
CGGAGTGGGATC
>test100-2
TGGCAATATTCTGT
CAAGTGGGTA
>test100-3
ttccgg
what i am trying to do is:
making complements of the sequence and save in a file with the same format.

result file should be:
>test100-1
AACTGCTCGTTACTGTG
GCCTCACCCTAG
>test100-2
ACCGTTATAAGACA
GTTCACCCAT
>test100-3
AAGGC

PROBLEM IN MY CODE:

In the file where i have 2 lines I am not able to join them so that i get the results correct.

here is my code:

from __future__ import with_statement

def lukup(comp):
	complement= {'A':'T','T':'A','C':'G','G':'C','a':'T','t':'A','c':'G','g':'C'}
	return complement[comp]
	
 #defining forward complement and saving in a file.

with open ('C:\\Documents and Settingsdna.txt','r') as fil:
    f = fil.readlines()
    result = []
    listheader = []
    for lin in f:
        if lin[0]=='>':
            listheader = lin
            #print listheader
        if ">" not in lin:
                line1 = ""
                line1 = lin[0:-1]
                end = len(line1)
                dna = [lukup(line1[i:i+1])for i in range(0,end)]
                # calling above defined directory
                dna = (''.join(dna))# converting in string
                dna = listheader + dna
                result.append(dna)
                print result

                   
with open ('C:\\Documents and Settings\\dna1.txt','w') as resultfil:
    resultfil.write('\n'.join(result))

What I am getting from above code is:

>test100-1
AACTGCTCGTTACTGTG
>test100-1
GCCTCACCCTAG
>test100-2
ACCGTTATAAGACA
>test100-2
GTTCACCCAT
>test100-3
AAGGC

PLEASE HELP ME I NEED TO FIX THIS CODE ...!!!

MANY THANKS..!!

joe82
Light Poster
27 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You