text: user16923: I have a big fasta.dataset file containing half a million proteins (1.0 GB). I have four lines for each protein code:
  
  line 1:the protein code
  line 2: protein length in amino acids
  line 3: amino acid sequence
  line 4: secondary structure
  
  Now, I am trying to open and read it in python (Biopython), and it does not work:
  filename = 'pdb.fasta_qual.dataset'
  sequences = SeqIO.parse ( filename,'fasta')
  for record in sequences:
      example = record
      break
  print(example)
  
  How can I read it in python and loop through the file to look at line 3  for each protein to count the sequence length and distribution?
  here is the first 5 lines of my file: which my file contains 500,000 proteins for each one has a 4 lines (name ,len of protein in amino acid,the seq represents by letters which what I would to calculate,the secondary structure)
  4LGTD
  247
  M       S       E       K       L       Q       K       V       L       A       R       A       G       H       G      T
  .       .       E       E       H       H       H       H       H       H       H       T       T       S       S       .
  
  I want to open and read the file and loop through line 3 for each protein to calculate the length of the sequences and plot a histogram ! to check the distribution.
  ,,,
  The output i am expecting is :
  The len for the first seq is  =
  The len for the second seq is  =
  Until
  the len of the last sequence  which is number (500.000)=
  And then i can plot a histogram for the len of the sequences
  ,,,
  NOTE: I have opened and read the file's info by Linux, but I could not by python.
  
  user298: It sounds like all you really want is to print every 2nd line of a text file. If so, you don't need Python, let alone BioPython, you can do it with basic *nix tools:
  $ awk 'NR%4==2' pdb.fasta_qual.dataset 
  247
  
  NR is the current line number, and % is the modulo operator. Therefore, NR % 4 will equal 2 on the second line of each group of 4 lines in the file. In awk, the default action when something evaluates to true is to print the current line, so this will print out all length lines from your input file (assuming you don't have blank lines anywhere, which we don't know since you only showed the first entry).
  If you want to add some sort of message to be printed, you could do:
  $ awk 'NR%4==2{ print "The length of protein",++c,"is:",$0}' pdb.fasta_qual.dataset 
  The length of protein 1 is: 247
  
  If you really must do this in Python, you could do something like this:
  #!/usr/bin/env python3
  
  import sys
  
  #my_file= sys.argv[1]
  line_number = 0
  with open(sys.argv[1]) as f:
      for line in f:
          line_number += 1
          if (line_number % 4 == 2):
              print("The length of protein %d is: %d" % (line_number,int(line.strip())))
  
  
  And then you would run it like this (assuming you named the script foo.py):
  $ python3 foo.py pdb.fasta_qual.dataset 
  The length of protein 2 is: 247
  
  
  Out of curiosity, I created a test file by repeating your example 10000000 times, which left me with a 2.4G file:
  $ perl -e 'open(A,"pdb.fasta_qual.dataset"); @lines=<A>; print "@lines" x 10000000' > testFile
  $ ls -lh testFile 
  -rw-r--r-- 1 terdon terdon 2.4G Feb  7 17:06 testFile
  
  I then tested both approaches for speed. Unsurprisingly, awk was  much faster:
  $ time awk 'NR%4==2' testFile > /dev/null 
  
  real    0m6.447s
  user    0m5.917s
  sys     0m0.492s
  
  $ time foo.py testFile > /dev/null 
  
  real    0m10.856s
  user    0m10.111s
  sys     0m0.523s
  
  
original_text: 16923: <p>I have a big <code>fasta.dataset</code> file containing half a million proteins (1.0 GB). I have four lines for each protein code:</p>
  <ul>
  <li>line 1:the protein code</li>
  <li>line 2: protein length in amino acids</li>
  <li>line 3: amino acid sequence</li>
  <li>line 4: secondary structure</li>
  </ul>
  <p>Now, I am trying to open and read it in python (Biopython), and it does not work:</p>
  <pre><code>filename = 'pdb.fasta_qual.dataset'
  sequences = SeqIO.parse ( filename,'fasta')
  for record in sequences:
      example = record
      break
  print(example)
  </code></pre>
  <p>How can I read it in python and loop through the file to look at line 3  for each protein to count the sequence length and distribution?</p>
  <p>here is the first 5 lines of my file: which my file contains 500,000 proteins for each one has a 4 lines (name ,len of protein in amino acid,the seq represents by letters which what I would to calculate,the secondary structure)</p>
  <pre><code>4LGTD
  247
  M       S       E       K       L       Q       K       V       L       A       R       A       G       H       G      T
  .       .       E       E       H       H       H       H       H       H       H       T       T       S       S       .
  </code></pre>
  <p>I want to open and read the file and loop through line 3 for each protein to calculate the length of the sequences and plot a histogram ! to check the distribution.
  ,,,</p>
  <p>The output i am expecting is :</p>
  <p>The len for the first seq is  =</p>
  <p>The len for the second seq is  =</p>
  <p>Until</p>
  <p>the len of the last sequence  which is number (500.000)=</p>
  <p>And then i can plot a histogram for the len of the sequences
  ,,,</p>
  <p>NOTE: I have opened and read the file's info by Linux, but I could not by python.</p>
   298: <p>It sounds like all you really want is to print every 2nd line of a text file. If so, you don't need Python, let alone BioPython, you can do it with basic *nix tools:</p>
  <pre><code>$ awk 'NR%4==2' pdb.fasta_qual.dataset 
  247
  </code></pre>
  <p><code>NR</code> is the current line number, and <code>%</code> is the <a href="https://en.wikipedia.org/wiki/Modulo" rel="nofollow noreferrer">modulo</a> operator. Therefore, <code>NR % 4</code> will equal <code>2</code> on the second line of each group of 4 lines in the file. In <code>awk</code>, the default action when something evaluates to true is to print the current line, so this will print out all length lines from your input file (assuming you don't have blank lines anywhere, which we don't know since you only showed the first entry).</p>
  <p>If you want to add some sort of message to be printed, you could do:</p>
  <pre><code>$ awk 'NR%4==2{ print &quot;The length of protein&quot;,++c,&quot;is:&quot;,$0}' pdb.fasta_qual.dataset 
  The length of protein 1 is: 247
  </code></pre>
  <p>If you really must do this in Python, you could do something like this:</p>
  <pre class="lang-py prettyprint-override"><code>#!/usr/bin/env python3
  
  import sys
  
  #my_file= sys.argv[1]
  line_number = 0
  with open(sys.argv[1]) as f:
      for line in f:
          line_number += 1
          if (line_number % 4 == 2):
              print(&quot;The length of protein %d is: %d&quot; % (line_number,int(line.strip())))
  
  </code></pre>
  <p>And then you would run it like this (assuming you named the script <code>foo.py</code>):</p>
  <pre><code>$ python3 foo.py pdb.fasta_qual.dataset 
  The length of protein 2 is: 247
  </code></pre>
  <hr />
  <p>Out of curiosity, I created a test file by repeating your example 10000000 times, which left me with a 2.4G file:</p>
  <pre><code>$ perl -e 'open(A,&quot;pdb.fasta_qual.dataset&quot;); @lines=&lt;A&gt;; print &quot;@lines&quot; x 10000000' &gt; testFile
  $ ls -lh testFile 
  -rw-r--r-- 1 terdon terdon 2.4G Feb  7 17:06 testFile
  </code></pre>
  <p>I then tested both approaches for speed. Unsurprisingly, <code>awk</code> was  much faster:</p>
  <pre class="lang-none prettyprint-override"><code>$ time awk 'NR%4==2' testFile &gt; /dev/null 
  
  real    0m6.447s
  user    0m5.917s
  sys     0m0.492s
  
  $ time foo.py testFile &gt; /dev/null 
  
  real    0m10.856s
  user    0m10.111s
  sys     0m0.523s
  </code></pre>
  