Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54759

Re: Extracting lines from text files - script with a couple of 'side effects'

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'newbie': 0.05; 'subject:text': 0.05; 'subject:files': 0.09; 'subject:script': 0.09; 'def': 0.12; 'question.': 0.14; "'w')": 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'match:': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'skip:> 20': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; 'file,': 0.19; 'first.': 0.19; 'thoughts': 0.19; 'work,': 0.20; 'written': 0.21; 'seems': 0.21; 'import': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'headers': 0.24; 'header': 0.24; 'file.': 0.24; 'script': 0.25; 'second': 0.26; 'skip:_ 20': 0.27; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; 'lines': 0.31; 'extract': 0.31; 'file:': 0.31; 'skip:r 60': 0.31; 'file': 0.32; 'class': 0.32; 'another': 0.32; 'text': 0.33; 'third': 0.33; 'skip:_ 10': 0.34; 'subject:from': 0.34; 'subject:with': 0.35; 'one,': 0.35; 'but': 0.35; 'false': 0.36; 'version:': 0.36; 'done': 0.36; 'thanks': 0.36; 'similar': 0.36; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'skip:- 60': 0.39; 'how': 0.40; 'first': 0.61; 'more': 0.64; 'dear': 0.65; 'here': 0.66; 'side': 0.67; 'header:Reply-To:1': 0.67; 'containing': 0.69; 'reply-to:no real name:2**0': 0.71; 'found!': 0.84; 'reply-to:addr:python.org': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=MqNrtQqe c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=0kkAYlmtguIA:10 a=6OqlJPfbv6oA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=5M_2A6aDnKsA:10 a=D2x73GFbzq0QGny551UA:9 a=qYFRow6uSqODLQlp:21 a=J3zuL3vRSqxitjD9:21 a=wPNLvfGTeEIA:10
X-AUTH mrabarnett:2500
Date Wed, 25 Sep 2013 22:53:52 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130801 Thunderbird/17.0.8
MIME-Version 1.0
To python-list@python.org
Subject Re: Extracting lines from text files - script with a couple of 'side effects'
References <cca8071a-c33e-47d6-b4dc-83dba2c28b74@googlegroups.com>
In-Reply-To <cca8071a-c33e-47d6-b4dc-83dba2c28b74@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.324.1380146037.18130.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1380146037 news.xs4all.nl 15900 [2001:888:2000:d::a6]:58558
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54759

Show key headers only | View raw


On 25/09/2013 21:06, mstagliamonte wrote:
> Dear All,
>
> Here I am, with another newbie question. I am trying to extract some lines from a fasta (text) file which match the headers in another file. i.e:
> Fasta file:
>>header1|info1:info2_info3
> general text
>>header2|info1:info2_info3
> general text
>
> headers file:
> header1|info1:info2_info3
> header2|info1:info2_info3
>
> I want to create a third file, similar to the first one, but only containing headers and text of what is listed in the second file. Also, I want to print out how many headers were actually found from the second file to match the first.
>
> I have done a script which seems to work, but with a couple of 'side effects'
> Here is my script:
> -------------------------------------------------------------------
> import re
> class Extractor():
>
>      def __init__(self,headers_file, fasta_file,output_file):
>          with open(headers_file,'r') as inp0:
>              counter0=0
>              container=''
>              inp0_bis=inp0.read().split('\n')
>              for x in inp0_bis:
>                  container+=x.replace(':','_').replace('|','_')
>              with open(fasta_file,'r') as inp1:
>                  inp1_bis=inp1.read().split('>')
>                  for i in inp1_bis:
>                      i_bis= i.split('\n')
>                      match = re.search(i_bis[0].replace(':','_').replace('|','_'),container)
>                      if match:
>                          counter0+=1
>                          with open(output_file,'at') as out0:
>                              out0.write('>'+i)
>               print '{} sequences were found'.format(counter0)
>
> -------------------------------------------------------------------
> Side effects:
> 1) The very first header is written as >>header1 rather than >header1
> 2) the number of sequences found is 1 more than the ones actually found!
>
> Have you got any thoughts about causes/solutions?
>
> Thanks for your time!
>
Here's my version:

class Extractor():
     def __init__(self, headers_file, fasta_file, output_file):
         with open(headers_file) as inp:
             headers = set('>' + line for line in inp)

         counter = 0
         accept = False

         with open(fasta_file) as inp, open(output_file, 'w') as out:
             for line in inp:
                 if line.startswith('>'):
                     accept = line in headers
                     if accept:
                         counter += 1

                 if accept:
                     out.write(line)

         print '{} sequences were found'.format(counter)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Extracting lines from text files - script with a couple of 'side effects' mstagliamonte <madmaxthc@yahoo.it> - 2013-09-25 13:06 -0700
  Re: Extracting lines from text files - script with a couple of 'side effects' Dave Angel <davea@davea.name> - 2013-09-25 21:02 +0000
  Re: Extracting lines from text files - script with a couple of 'side effects' MRAB <python@mrabarnett.plus.com> - 2013-09-25 22:53 +0100
  Re: Extracting lines from text files - script with a couple of 'side effects' mstagliamonte <madmaxthc@yahoo.it> - 2013-09-26 05:44 -0700

csiph-web